]> Shamusworld >> Repos - warehouse-man-deluxe/commitdiff
Added rudimentary animation.
authorShamus Hammons <jlhamm@acm.org>
Thu, 6 Mar 2014 15:14:37 +0000 (09:14 -0600)
committerShamus Hammons <jlhamm@acm.org>
Thu, 6 Mar 2014 15:14:37 +0000 (09:14 -0600)
src/gameboard.cpp
src/gameboard.h
src/gamewidget.cpp
src/gamewidget.h

index 0c4a89ec148a0e3f65dbb7a8eadf990a5e4479ea..b18fe9a1e2010d016022b9b5499981ecf9030f77 100644 (file)
@@ -166,3 +166,40 @@ int GameBoard::Move(int & var, int direction, char cell1, char cell2)
        return PMInvalid;
 }
 
+
+bool GameBoard::IsBoxNOfPlayer(void)
+{
+       if ((board[playerX + ((playerY - 1) * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
+bool GameBoard::IsBoxSOfPlayer(void)
+{
+       if ((board[playerX + ((playerY + 1) * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
+bool GameBoard::IsBoxEOfPlayer(void)
+{
+       if ((board[(playerX + 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
+bool GameBoard::IsBoxWOfPlayer(void)
+{
+       if ((board[(playerX - 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
index b32dc885b66d536fec7067ee6287b026fa776d48..4452e2595d2d0bd8476822a16dd38e70ad87c92c 100644 (file)
@@ -19,6 +19,10 @@ class GameBoard
                int MovePlayerS(void);
                int MovePlayerE(void);
                int MovePlayerW(void);
+               bool IsBoxNOfPlayer(void);
+               bool IsBoxSOfPlayer(void);
+               bool IsBoxEOfPlayer(void);
+               bool IsBoxWOfPlayer(void);
 
        private:
                int Move(int & var, int direction, char, char);
index 837edd8b55aea62fab3b5d8dcfc16d44a145b28c..4a3092205f74f0c4170a8b3036d1e93d10d88dd9 100644 (file)
 //
 
 #include "gamewidget.h"
-#include <unistd.h>            // for usleep()
+#include <unistd.h>                    // for usleep()
 #include "gameboard.h"
 
 
-GameWidget::GameWidget(QWidget * parent/*= 0*/): QWidget(parent)//,
+GameWidget::GameWidget(QWidget * parent/*= 0*/): QWidget(parent),
+       level(1), gameBoard(new GameBoard(level)),
+       animating(false), boxMoving(false)
 //     score(new QLabel)
 {
 //     score->setTextFormat(Qt::PlainText);
        setFixedSize(580, 520);
-       gameBoard = new GameBoard(1);
+//     gameBoard = new GameBoard(1);
 }
 
 
@@ -42,7 +44,7 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/)
 //     QRect rcUpdate = dc.m_ps.rcPaint; // Update rect...
 //     QRect rcUpdate = QRect(0, 0, 580, 520);                 // Update rect?
 
-       int maxLength = 60;
+       maxLength = 60;
        int ptr = 0;
 
        for(int y=0; y<gameBoard->height; y++)
@@ -60,8 +62,11 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/)
                        }
                        else if (tile == GTBox)
                        {
-                               painter.setBrush(QBrush(Qt::red));
-                               painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+                               if (!(boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y)))
+                               {
+                                       painter.setBrush(QBrush(Qt::red));
+                                       painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+                               }
                        }
                        else if (tile == GTBoxSpot)
                        {
@@ -70,17 +75,36 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/)
                        }
                        else if (tile == (GTBox | GTBoxSpot))
                        {
-                               painter.setBrush(QBrush(Qt::green));
-                               painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+                               if (!(boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y)))
+                               {
+                                       painter.setBrush(QBrush(Qt::green));
+                                       painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+                               }
+                               else
+                               {
+                                       painter.setBrush(QBrush(Qt::magenta));
+                                       painter.drawRect((x * maxLength) + 20, (y * maxLength) + 20, maxLength - 40, maxLength - 40);
+                               }
                        }
 
-                       if ((gameBoard->playerX == x) && (gameBoard->playerY == y))
+                       if ((gameBoard->playerX == x) && (gameBoard->playerY == y) && !animating)
                        {
                                painter.setBrush(QBrush(Qt::yellow));
                                painter.drawEllipse((x * maxLength) + 10, (y * maxLength) + 10, maxLength - 20, maxLength - 20);
-//                             painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
                        }
                }
+
+               if (animating)
+               {
+                       painter.setBrush(QBrush(Qt::yellow));
+                       painter.drawEllipse(playerX + 10, playerY + 10, maxLength - 20, maxLength - 20);
+               }
+
+               if (boxMoving)
+               {
+                       painter.setBrush(QBrush(Qt::red));
+                       painter.drawRect(boxX, boxY, maxLength, maxLength);
+               }
        }
 
 
@@ -166,30 +190,82 @@ void GameWidget::mouseDoubleClickEvent(QMouseEvent * event)
 void GameWidget::keyPressEvent(QKeyEvent * event)
 {
        int key = event->key();
+       float deltaX = 0, deltaY = 0;
+       float px = (float)(gameBoard->playerX * maxLength);
+       float py = (float)(gameBoard->playerY * maxLength);
+       float bx = px, by = py;
 
        if (key == Qt::Key_Up)
        {
-               if (gameBoard->MovePlayerN() == PMInvalid)
+               boxMoving = gameBoard->IsBoxNOfPlayer();
+               int moveType = gameBoard->MovePlayerN();
+
+               if (moveType == PMInvalid)
                        return;
+
+               deltaY = -1.0;
        }
        else if (key == Qt::Key_Down)
        {
+               boxMoving = gameBoard->IsBoxSOfPlayer();
+
                if (gameBoard->MovePlayerS() == PMInvalid)
                        return;
+
+               deltaY = +1.0;
        }
        else if (key == Qt::Key_Left)
        {
+               boxMoving = gameBoard->IsBoxWOfPlayer();
+
                if (gameBoard->MovePlayerW() == PMInvalid)
                        return;
+
+               deltaX = -1.0;
        }
        else if (key == Qt::Key_Right)
        {
+               boxMoving = gameBoard->IsBoxEOfPlayer();
+
                if (gameBoard->MovePlayerE() == PMInvalid)
                        return;
+
+               deltaX = +1.0;
        }
        else
                return;
 
+       animating = true;
+       update();
+
+       if (boxMoving)
+       {
+               movingBoxPositionX = gameBoard->playerX + (int)deltaX;
+               movingBoxPositionY = gameBoard->playerY + (int)deltaY;
+               bx += deltaX * (float)maxLength;
+               by += deltaY * (float)maxLength;
+       }
+
+       int steps = 15;
+       float stepSize = (float)maxLength / (float)steps;
+       deltaX *= stepSize, deltaY *= stepSize;
+
+       for(int i=0; i<steps; i++)
+       {
+               px += deltaX;
+               py += deltaY;
+               playerX = (int)px;
+               playerY = (int)py;
+               bx += deltaX;
+               by += deltaY;
+               boxX = (int)bx;
+               boxY = (int)by;
+               repaint();
+               Pause(3);
+       }
+
+       animating = boxMoving = false;
+
        // Only update if a key we recognize has been pressed!
        update();
 
@@ -1316,6 +1392,7 @@ void GameWidget::HandleStatistics(void)
                        m_nHiScore = m_nScore;
        }
 }
+#endif
 
 
 //
@@ -1336,5 +1413,4 @@ void GameWidget::Pause(int count)
                ; // Do nothing...
 #endif
 }
-#endif
 
index c587e084b4b265f02b14a25b7c70de583ab7d934..f58bafdfeb735e2607653072e529dbb9c99e97ba 100644 (file)
@@ -42,12 +42,22 @@ class GameWidget: public QWidget
                bool IsValidMoveToAce(int nAce, int nCard);
                bool IsValidMoveToTableaux(int nStack);
                bool PlayerWon(void);
-               void HandleStatistics(void);
-               void Pause(int);*/
+               void HandleStatistics(void);*/
+       private:
+               void Pause(int);
 
        public:
-               GameBoard * gameBoard;
                int level;
+               GameBoard * gameBoard;
+
+       private:
+               bool animating;
+               bool boxMoving;
+               int maxLength;
+               int playerX, playerY;
+               int boxX, boxY;
+               int movingBoxPositionX;
+               int movingBoxPositionY;
 };
 
 #endif // __GAMEWIDGET_H__