]> Shamusworld >> Repos - warehouse-man-deluxe/blobdiff - src/gamewidget.cpp
Added rudimentary animation.
[warehouse-man-deluxe] / src / gamewidget.cpp
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