From 003638a66da61e2269e4ef398aa294c6ee02b171 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Thu, 10 Jul 2014 15:16:53 -0500 Subject: [PATCH] Added/fixed editor, added play level functionality. --- src/editorwidget.cpp | 57 ++++++++++++++++++++++++++++++++++++++------ src/editorwidget.h | 3 ++- src/editorwindow.cpp | 9 +++++++ src/editorwindow.h | 7 +++++- src/gameboard.cpp | 37 ++++++++++++++++++++++++++++ src/gameboard.h | 3 +++ src/gamewidget.cpp | 10 ++++++++ src/gamewidget.h | 4 ++++ src/mainwin.cpp | 5 ++++ 9 files changed, 126 insertions(+), 9 deletions(-) diff --git a/src/editorwidget.cpp b/src/editorwidget.cpp index 1431edc..4059632 100644 --- a/src/editorwidget.cpp +++ b/src/editorwidget.cpp @@ -130,6 +130,21 @@ void EditorWidget::paintEvent(QPaintEvent * /*event*/) painter.setBrush(Qt::transparent); painter.setPen(Qt::darkGreen); painter.drawEllipse((x * GRIDSIZE) + 4, (y * GRIDSIZE) + 4, GRIDSIZE - 8, GRIDSIZE - 8); + + char s[256]; + int boxes, spots; + CountBoxesAndSpots(boxes, spots); + + if (boxes == spots) + sprintf(s, "Boxes = Spots"); + else if (boxes < spots) + sprintf(s, "Need %i more box%s", spots - boxes, (spots - boxes == 1 ? "" : "es")); + else if (spots < boxes) + sprintf(s, "Need %i more spot%s", boxes - spots, (boxes - spots == 1 ? "" : "s")); + + painter.setPen(Qt::blue); + painter.drawText(0, 0, clientArea.width(), clientArea.height(), + Qt::AlignRight | Qt::AlignBottom, QString(s)); } @@ -236,20 +251,25 @@ void EditorWidget::keyPressEvent(QKeyEvent * event) } else if (key == Qt::Key_Period) { - level.board[cursor.x][cursor.y] ^= GTBoxSpot; + if (!(level.board[cursor.x][cursor.y] & (GTWall | GTNull))) + level.board[cursor.x][cursor.y] ^= GTBoxSpot; } else if (key == Qt::Key_B) { - level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox); - level.board[cursor.x][cursor.y] |= GTBox; + level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTNull); + level.board[cursor.x][cursor.y] ^= GTBox; } else if (key == Qt::Key_W) { - level.board[cursor.x][cursor.y] = GTWall; + // If it's not a wall, just stick one in; otherwise, toggle it off + if (level.board[cursor.x][cursor.y] != GTWall) + level.board[cursor.x][cursor.y] = GTWall; + else + level.board[cursor.x][cursor.y] = GTSpace; } else if (key == Qt::Key_Space) { - level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox); + level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox | GTNull); level.board[cursor.x][cursor.y] |= GTSpace; } else if (key == Qt::Key_O) @@ -265,7 +285,11 @@ void EditorWidget::keyPressEvent(QKeyEvent * event) } else if (key == Qt::Key_V) { - level.board[cursor.x][cursor.y] = GTNull; + // If it's not a void, just stick one in; otherwise, toggle it off + if (level.board[cursor.x][cursor.y] != GTNull) + level.board[cursor.x][cursor.y] = GTNull; + else + level.board[cursor.x][cursor.y] = GTSpace; } else return; // Only update screen if keypress was recognized @@ -451,6 +475,25 @@ void EditorWidget::AddNewLevelAtCurrentPosition(void) } +void EditorWidget::CountBoxesAndSpots(int & boxes, int & spots) +{ + Level & level = levelStorage[currentLevel]; + boxes = spots = 0; + + for(int x=0; x"); editor = new EditorWidget(parent); list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); @@ -44,6 +45,7 @@ EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window) buttonBox->addWidget(down); buttonBox->addWidget(add); buttonBox->addWidget(remove); + buttonBox->addWidget(play); listBox->addWidget(list); listBox->addLayout(buttonBox); @@ -61,6 +63,7 @@ EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window) connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown())); connect(add, SIGNAL(clicked()), this, SLOT(AddLevel())); connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel())); + connect(play, SIGNAL(clicked()), this, SLOT(PlayLevel())); } @@ -160,3 +163,9 @@ void EditorWindow::DeleteLevel(void) SetEditorLevel(currentRow); } + +void EditorWindow::PlayLevel(void) +{ + emit SetupLevel(&(editor->levelStorage[editor->currentLevel])); +} + diff --git a/src/editorwindow.h b/src/editorwindow.h index d758cf0..c17c7b0 100644 --- a/src/editorwindow.h +++ b/src/editorwindow.h @@ -4,6 +4,7 @@ #include class EditorWidget; +class Level; class EditorWindow: public QWidget { @@ -13,6 +14,9 @@ class EditorWindow: public QWidget EditorWindow(QWidget * parent = 0); ~EditorWindow(); + signals: + void SetupLevel(Level *); + private slots: void SetEditorLevel(int); void SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint); @@ -20,10 +24,11 @@ class EditorWindow: public QWidget void MoveLevelDown(void); void AddLevel(void); void DeleteLevel(void); + void PlayLevel(void); private: QListWidget * list; - QPushButton * up, * down, * add, * remove; + QPushButton * up, * down, * add, * remove, * play; EditorWidget * editor; }; diff --git a/src/gameboard.cpp b/src/gameboard.cpp index e39d2e5..0d1216f 100644 --- a/src/gameboard.cpp +++ b/src/gameboard.cpp @@ -15,6 +15,7 @@ #include //#include // for printf() #include "boards.h" +#include "editorwidget.h" // for Level GameBoard::GameBoard(int boardNumber) @@ -60,6 +61,42 @@ GameBoard::GameBoard(int boardNumber) } +GameBoard::GameBoard(Level * level) +{ + Point size, corner; + + EditorWidget::GetSizeAndCorner(level, size, corner); + + width = size.x; + height = size.y; + boardLength = width * height; + board = new char[boardLength]; + initialBoard = new char[boardLength]; + name = level->name; + + int current = 0; + + for(int y=0; yboard[corner.x + x][corner.y + y]; + + if (tile & GTMan) + { + initialX = x; + initialY = y; + tile &= ~GTMan; + } + + initialBoard[current++] = tile; + } + } + + ResetGame(); +} + + GameBoard::~GameBoard() { if (board) diff --git a/src/gameboard.h b/src/gameboard.h index 0bca19b..6e65e2b 100644 --- a/src/gameboard.h +++ b/src/gameboard.h @@ -9,10 +9,13 @@ struct UndoMove int dx, dy, type; }; +class Level; + class GameBoard { public: GameBoard(int);//, int, int, int); + GameBoard(Level *); ~GameBoard(); protected: diff --git a/src/gamewidget.cpp b/src/gamewidget.cpp index 037a26f..f66cd58 100644 --- a/src/gamewidget.cpp +++ b/src/gamewidget.cpp @@ -366,6 +366,16 @@ void GameWidget::UndoLastMove(void) } +void GameWidget::HandlePlayGameFromEditor(Level * l) +{ +// level++; + delete gameBoard; + gameBoard = new GameBoard(l); + ResizeGrid(); + update(); +} + + void GameWidget::ResizeGrid(void) { QSize s = size(); diff --git a/src/gamewidget.h b/src/gamewidget.h index f6ba76b..bc35b8d 100644 --- a/src/gamewidget.h +++ b/src/gamewidget.h @@ -4,6 +4,7 @@ #include class GameBoard; +class Level; class GameWidget: public QWidget { @@ -27,6 +28,9 @@ class GameWidget: public QWidget void UpdateScore(int); void GameWasWon(void); + public slots: + void HandlePlayGameFromEditor(Level *); + public: void CreateBackground(void); void NextLevel(void); diff --git a/src/mainwin.cpp b/src/mainwin.cpp index a4aec01..3c37a7b 100644 --- a/src/mainwin.cpp +++ b/src/mainwin.cpp @@ -65,6 +65,11 @@ MainWin::MainWin() // connect(gameBoard, SIGNAL(UpdateScore(int)), this, SLOT(OnUpdateScore(int))); connect(gameWidget, SIGNAL(GameWasWon()), this, SLOT(WeHaveAWinner())); + // Connect editor to game widget... + connect(editorWindow, SIGNAL(SetupLevel(Level *)), gameWidget, + SLOT(HandlePlayGameFromEditor(Level *))); + + QMenu * menu = menuBar()->addMenu(tr("&Game")); menu->addAction(newGame); menu->addSeparator(); -- 2.37.2