]> Shamusworld >> Repos - warehouse-man-deluxe/commitdiff
Added/fixed editor, added play level functionality.
authorShamus Hammons <jlhamm@acm.org>
Thu, 10 Jul 2014 20:16:53 +0000 (15:16 -0500)
committerShamus Hammons <jlhamm@acm.org>
Thu, 10 Jul 2014 20:16:53 +0000 (15:16 -0500)
src/editorwidget.cpp
src/editorwidget.h
src/editorwindow.cpp
src/editorwindow.h
src/gameboard.cpp
src/gameboard.h
src/gamewidget.cpp
src/gamewidget.h
src/mainwin.cpp

index 1431edc243c3db38397d694558e28d24f0d4f356..40596326abe800ed1f6ea6bee42e31058dbe203b 100644 (file)
@@ -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<BOARDSIZE; x++)
+       {
+               for(int y=0; y<BOARDSIZE; y++)
+               {
+                       if (level.board[x][y] & GTBox)
+                               boxes++;
+
+                       if (level.board[x][y] & GTBoxSpot)
+                               spots++;
+               }
+       }
+}
+
+
 // Note: Only one parameter can be non-zero if you want expected behavior!
 void EditorWidget::ShiftLevel(int dx, int dy)
 {
@@ -509,7 +552,7 @@ void EditorWidget::ShiftLevel(int dx, int dy)
 }
 
 
-void EditorWidget::GetSizeAndCorner(Level * level, Point & size, Point & corner)
+/*static*/ void EditorWidget::GetSizeAndCorner(Level * level, Point & size, Point & corner)
 {
        size.x = size.y = corner.x = corner.y = 0;
        Point min, max;
index eb89e0dd8519e50b6f8ed99bb077c559f7a95662..87ab02fda3d1aa97299c951ebbadb05fe4e4b836 100644 (file)
@@ -54,9 +54,10 @@ class EditorWidget: public QWidget
                bool Save(void);
                void SetNameOnCurrentLevel(const char *);
                void AddNewLevelAtCurrentPosition(void);
+               static void GetSizeAndCorner(Level *, Point &, Point &);
        private:
+               void CountBoxesAndSpots(int &, int &);
                void ShiftLevel(int, int);
-               void GetSizeAndCorner(Level *, Point &, Point &);
                void ResizeGrid(void);
                void Pause(int);
 
index 9c95ba17accfafabfa3e5c1d9875301d0eed83ad..713dc740caebea5e0eb2721f00b42a140c95a4e4 100644 (file)
@@ -23,6 +23,7 @@ EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
        down = new QPushButton("v");
        add = new QPushButton("+");
        remove = new QPushButton("-");
+       play = new QPushButton(">");
        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]));
+}
+
index d758cf01a4ad5768e3563a6529e27cce24c05bb1..c17c7b0c5700e9f756a01e9905b24397243d41c2 100644 (file)
@@ -4,6 +4,7 @@
 #include <QtGui>
 
 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;
 };
 
index e39d2e56b22e2f202326c6e73915d9bc7eb2d5e7..0d1216f1c0bfbd1b16bfbadc2dc1ad0fb9197554 100644 (file)
@@ -15,6 +15,7 @@
 #include <string.h>
 //#include <stdio.h>   // 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; y<size.y; y++)
+       {
+               for(int x=0; x<size.x; x++)
+               {
+                       uint8_t tile = level->board[corner.x + x][corner.y + y];
+
+                       if (tile & GTMan)
+                       {
+                               initialX = x;
+                               initialY = y;
+                               tile &= ~GTMan;
+                       }
+
+                       initialBoard[current++] = tile;
+               }
+       }
+
+       ResetGame();
+}
+
+
 GameBoard::~GameBoard()
 {
        if (board)
index 0bca19b171550ae450b670f67d1db2683090768b..6e65e2bded76d41fde572d5e152c1085487fceca 100644 (file)
@@ -9,10 +9,13 @@ struct UndoMove
        int dx, dy, type;
 };
 
+class Level;
+
 class GameBoard
 {
        public:
                GameBoard(int);//, int, int, int);
+               GameBoard(Level *);
                ~GameBoard();
 
        protected:
index 037a26f71471a76d8462354801fa83c53acb6916..f66cd588e80ff3cb112f36409c831f3201ef4330 100644 (file)
@@ -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();
index f6ba76bf6cb883189501c362b907a386240bebb3..bc35b8d53ade734d74e5eb716100511f1afd8672 100644 (file)
@@ -4,6 +4,7 @@
 #include <QtGui>
 
 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);
index a4aec01054e143afd19db13a563d66f235bbac5f..3c37a7bc4f5342cd005338dde6304820e5bcf478 100644 (file)
@@ -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();