From: Shamus Hammons Date: Thu, 6 Mar 2014 15:14:37 +0000 (-0600) Subject: Added rudimentary animation. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=warehouse-man-deluxe;a=commitdiff_plain;h=39aecbb4b0703dcc20f2062fb9cb0e520ca78176 Added rudimentary animation. --- diff --git a/src/gameboard.cpp b/src/gameboard.cpp index 0c4a89e..b18fe9a 100644 --- a/src/gameboard.cpp +++ b/src/gameboard.cpp @@ -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; +} + + diff --git a/src/gameboard.h b/src/gameboard.h index b32dc88..4452e25 100644 --- a/src/gameboard.h +++ b/src/gameboard.h @@ -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); diff --git a/src/gamewidget.cpp b/src/gamewidget.cpp index 837edd8..4a30922 100644 --- a/src/gamewidget.cpp +++ b/src/gamewidget.cpp @@ -12,16 +12,18 @@ // #include "gamewidget.h" -#include // for usleep() +#include // 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; yheight; 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