]> Shamusworld >> Repos - warehouse-man-deluxe/blobdiff - src/gamewidget.cpp
Added undo move system, various improvements.
[warehouse-man-deluxe] / src / gamewidget.cpp
index 4a3092205f74f0c4170a8b3036d1e93d10d88dd9..1b815f0fb6ec74ccc2395417826c56533e166269 100644 (file)
@@ -21,8 +21,9 @@ GameWidget::GameWidget(QWidget * parent/*= 0*/): QWidget(parent),
        animating(false), boxMoving(false)
 //     score(new QLabel)
 {
+       CreateBackground();
 //     score->setTextFormat(Qt::PlainText);
-       setFixedSize(580, 520);
+//     setFixedSize(580, 520);
 //     gameBoard = new GameBoard(1);
 }
 
@@ -44,7 +45,8 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/)
 //     QRect rcUpdate = dc.m_ps.rcPaint; // Update rect...
 //     QRect rcUpdate = QRect(0, 0, 580, 520);                 // Update rect?
 
-       maxLength = 60;
+       painter.translate(QPoint(offsetX, offsetY));
+//     maxLength = 60;
        int ptr = 0;
 
        for(int y=0; y<gameBoard->height; y++)
@@ -54,24 +56,25 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/)
                        int tile = gameBoard->board[ptr++];
                        painter.setPen(QPen(Qt::black, 2.0, Qt::SolidLine));
                        painter.setBrush(QBrush(Qt::black));
+                       int tileType = tile & (~GTBoxSpot);
 
-                       if (tile == GTWall)
+                       if (tileType == GTWall)
                        {
                                painter.setBrush(QBrush(Qt::white));
                                painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
                        }
-                       else if (tile == GTBox)
+                       else if ((tileType == GTBox)
+                               && (!(boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y))))
                        {
-                               if (!(boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y)))
-                               {
-                                       painter.setBrush(QBrush(Qt::red));
-                                       painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
-                               }
+                               painter.setBrush(QBrush(tile & GTBoxSpot ? Qt::green : Qt::red));
+                               painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
                        }
+#if 0
                        else if (tile == GTBoxSpot)
                        {
                                painter.setBrush(QBrush(Qt::magenta));
-                               painter.drawRect((x * maxLength) + 20, (y * maxLength) + 20, maxLength - 40, maxLength - 40);
+//                             painter.drawRect((x * maxLength) + 20, (y * maxLength) + 20, maxLength - 40, maxLength - 40);
+                               painter.drawRect((x * maxLength) + (int)(20.0/60.0*(float)maxLength), (y * maxLength) + (int)(20.0/60.0*(float)maxLength), maxLength - (int)(40.0/60.0*(float)maxLength), maxLength - (int)(40.0/60.0*(float)maxLength));
                        }
                        else if (tile == (GTBox | GTBoxSpot))
                        {
@@ -83,21 +86,38 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/)
                                else
                                {
                                        painter.setBrush(QBrush(Qt::magenta));
-                                       painter.drawRect((x * maxLength) + 20, (y * maxLength) + 20, maxLength - 40, maxLength - 40);
+//                                     painter.drawRect((x * maxLength) + 20, (y * maxLength) + 20, maxLength - 40, maxLength - 40);
+                               painter.drawRect((x * maxLength) + (int)(20.0/60.0*(float)maxLength), (y * maxLength) + (int)(20.0/60.0*(float)maxLength), maxLength - (int)(40.0/60.0*(float)maxLength), maxLength - (int)(40.0/60.0*(float)maxLength));
+                               }
+                       }
+#endif
+                       else if ((tileType == GTSpace)
+                               || ((tileType == GTBox) && (boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y))))
+                       {
+                               painter.setBrush(QBrush(QPixmap(":/bg_marble_g.bmp")));
+                               painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+
+                               if (tile & GTBoxSpot)
+                               {
+                                       painter.setBrush(QBrush(Qt::magenta));
+//                             painter.drawRect((x * maxLength) + 20, (y * maxLength) + 20, maxLength - 40, maxLength - 40);
+                                       painter.drawRect((x * maxLength) + (int)(20.0/60.0*(float)maxLength), (y * maxLength) + (int)(20.0/60.0*(float)maxLength), maxLength - (int)(40.0/60.0*(float)maxLength), maxLength - (int)(40.0/60.0*(float)maxLength));
                                }
                        }
 
                        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.drawEllipse((x * maxLength) + 10, (y * maxLength) + 10, maxLength - 20, maxLength - 20);
+                               painter.drawEllipse((x * maxLength) + (int)(10.0/60.0*(float)maxLength), (y * maxLength) + (int)(10.0/60.0*(float)maxLength), maxLength - (int)(20.0/60.0*(float)maxLength), maxLength - (int)(20.0/60.0*(float)maxLength));
                        }
                }
 
                if (animating)
                {
                        painter.setBrush(QBrush(Qt::yellow));
-                       painter.drawEllipse(playerX + 10, playerY + 10, maxLength - 20, maxLength - 20);
+//                     painter.drawEllipse(playerX + 10, playerY + 10, maxLength - 20, maxLength - 20);
+                       painter.drawEllipse(playerX + (int)(10.0/60.0*(float)maxLength), playerY + (int)(10.0/60.0*(float)maxLength), maxLength - (int)(20.0/60.0*(float)maxLength), maxLength - (int)(20.0/60.0*(float)maxLength));
                }
 
                if (boxMoving)
@@ -106,44 +126,6 @@ void GameWidget::paintEvent(QPaintEvent * /*event*/)
                        painter.drawRect(boxX, boxY, maxLength, maxLength);
                }
        }
-
-
-#if 0
-       DrawBoard(&painter, rcUpdate);
-
-       // Draw the "loose" cards....
-
-       if (hitStack)
-       {
-               for(int i=stackStart.y(); i<19; i++)
-               {
-                       int card = solBoard[stackStart.x()][i];
-
-                       if (card != -1)
-                       {
-                               if (solBoard[stackStart.x()][i + 1] == -1) // Draw full card
-                                       cdtDraw(&painter, stackPos.x(), stackPos.y() + ((i - stackStart.y()) * 18),
-                                               myDeck.GetCardAtPos(card), FACEUP);
-                               else                                   // Draw partial card
-                                       cdtDraw(&painter, stackPos.x(), stackPos.y() + ((i - stackStart.y()) * 18),
-                                               myDeck.GetCardAtPos(card), FACEUP, 20);
-                       }
-               }
-       }
-
-       if (hitDiscard)
-               cdtDraw(&painter, stackPos.x(), stackPos.y(), myDeck.GetCardAtPos(stack2[stack2p]),
-                       FACEUP);
-
-       if (nHitAce != 0)
-               cdtDraw(&painter, stackPos.x(), stackPos.y(), nAce[nHitAce - 1], FACEUP);
-
-       if (m_bFreeCard)  // For card animations...
-       {
-               for(SCardInfo * pCard=m_pFirstCard; pCard; pCard=pCard->pNext)
-                       cdtDraw(&painter, pCard->nXPos, pCard->nYPos, pCard->nCard, FACEUP);
-       }
-#endif
 }
 
 
@@ -151,7 +133,6 @@ void GameWidget::mousePressEvent(QMouseEvent * event)
 {
        if (event->button() == Qt::LeftButton)
        {
-//             OnLButtonDown(event->pos());
                event->accept();
        }
 }
@@ -161,7 +142,6 @@ void GameWidget::mouseMoveEvent(QMouseEvent * event)
 {
        if (event->buttons() & Qt::LeftButton)
        {
-//             OnMouseMove(event->pos());
                event->accept();
        }
 }
@@ -171,7 +151,6 @@ void GameWidget::mouseReleaseEvent(QMouseEvent * event)
 {
        if (event->button() == Qt::LeftButton)
        {
-//             OnLButtonUp(event->pos());
                event->accept();
        }
 }
@@ -181,7 +160,6 @@ void GameWidget::mouseDoubleClickEvent(QMouseEvent * event)
 {
        if (event->button() == Qt::LeftButton)
        {
-//             OnLButtonDblClk(event->pos());
                event->accept();
        }
 }
@@ -236,7 +214,7 @@ void GameWidget::keyPressEvent(QKeyEvent * event)
                return;
 
        animating = true;
-       update();
+//     update();
 
        if (boxMoving)
        {
@@ -274,30 +252,32 @@ void GameWidget::keyPressEvent(QKeyEvent * event)
 }
 
 
-void GameWidget::keyReleaseEvent(QKeyEvent * event)
+void GameWidget::keyReleaseEvent(QKeyEvent * /*event*/)
 {
 }
 
 
-void GameWidget::NextLevel(void)
+void GameWidget::resizeEvent(QResizeEvent * /*event*/)
 {
-       level++;
-       delete gameBoard;
-       gameBoard = new GameBoard(level);
-       update();
-}
+//     QSize s = event->size();
 
+//printf("Size of window is: %i x %i\n", s.width(), s.height());
+//printf("Size of game grid is: %i x %i\n", gameBoard->width, gameBoard->height);
+#if 0
+       // Find the constraints
+       float boxSizeX = s.width() / gameBoard->width;
+       float boxSizeY = s.height() / gameBoard->height;
 
-void GameWidget::ResetLevel(void)
-{
-       gameBoard->ResetGame();
-       update();
+       maxLength = (int)(boxSizeX > boxSizeY ? boxSizeY : boxSizeX);
+#else
+       ResizeGrid();
+#endif
 }
 
 
-#if 0
-bool GameWidget::CreateBackground(void)
+void GameWidget::CreateBackground(void)
 {
+#if 0
        char BGRes[27][64] = {
                ":/res/grfttile.bmp",
                ":/res/cloth_6.bmp",
@@ -334,9 +314,90 @@ bool GameWidget::CreateBackground(void)
        setPalette(pal);
 
        return true; // Ignore errors for now...
+#else
+//     QPalette pal = palette();
+//     pal.setBrush(backgroundRole(), QBrush(QPixmap(":/bg_marble_g.bmp")));
+//     setAutoFillBackground(true);
+//     setPalette(pal);
+#endif
 }
 
 
+void GameWidget::NextLevel(void)
+{
+       level++;
+       delete gameBoard;
+       gameBoard = new GameBoard(level);
+       ResizeGrid();
+       update();
+}
+
+
+void GameWidget::ResetLevel(void)
+{
+       gameBoard->ResetGame();
+       update();
+}
+
+
+void GameWidget::UndoLastMove(void)
+{
+       int dx, dy, type;
+
+       float deltaX = 0, deltaY = 0;
+       float px = (float)(gameBoard->playerX * maxLength);
+       float py = (float)(gameBoard->playerY * maxLength);
+       float bx = px, by = py;
+
+       // Return if nothing to undo
+       if (!gameBoard->UndoLastMove(dx, dy, type))
+               return;
+
+       deltaX = (float)-dx;
+       deltaY = (float)-dy;
+
+       if (type == PMPush)
+               boxMoving = true;
+
+       animating = true;
+//     update();
+
+       if (boxMoving)
+       {
+//             movingBoxPositionX = gameBoard->playerX + (int)deltaX;
+//             movingBoxPositionY = gameBoard->playerY + (int)deltaY;
+               movingBoxPositionX = gameBoard->playerX + dx;
+               movingBoxPositionY = gameBoard->playerY + dy;
+//             bx += deltaX * (float)maxLength;
+//             by += deltaY * (float)maxLength;
+               bx += (float)(dx * maxLength);
+               by += (float)(dy * 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;
+       update();
+}
+
+
+#if 0
 void GameWidget::DrawBoard(QPainter * painter, QRect r)
 {
        // Use "r" as clipping rect--only draw what's necessary
@@ -1395,6 +1456,21 @@ void GameWidget::HandleStatistics(void)
 #endif
 
 
+void GameWidget::ResizeGrid(void)
+{
+       QSize s = size();
+
+       // Find the constraints
+       float boxSizeX = s.width() / gameBoard->width;
+       float boxSizeY = s.height() / gameBoard->height;
+
+       maxLength = (int)(boxSizeX > boxSizeY ? boxSizeY : boxSizeX);
+
+       offsetX = (s.width() - (maxLength * gameBoard->width)) / 2;
+       offsetY = (s.height() - (maxLength * gameBoard->height)) / 2;
+}
+
+
 //
 // Halt processing for 'count' milliseconds
 //