Changeset View
Changeset View
Standalone View
Standalone View
KBlocksSingleGame.cpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** | ||
|---|---|---|---|---|---|
| 2 | * KBlocks, a falling blocks game by KDE * | 2 | * KBlocks, a falling blocks game by KDE * | ||
| 3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * | 3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * | ||
| 4 | * * | 4 | * * | ||
| 5 | * This program is free software; you can redistribute it and/or modify * | 5 | * This program is free software; you can redistribute it and/or modify * | ||
| 6 | * it under the terms of the GNU General Public License as published by * | 6 | * it under the terms of the GNU General Public License as published by * | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or * | 7 | * the Free Software Foundation; either version 2 of the License, or * | ||
| 8 | * (at your option) any later version. * | 8 | * (at your option) any later version. * | ||
| 9 | ***************************************************************************/ | 9 | ***************************************************************************/ | ||
| 10 | #include "KBlocksSingleGame.h" | 10 | #include "KBlocksSingleGame.h" | ||
| 11 | #include "KBlocksField.h" | 11 | #include "KBlocksField.h" | ||
| 12 | #include "KBlocksPiece.h" | 12 | #include "KBlocksPiece.h" | ||
| 13 | 13 | #include "utils.h" | |||
| 14 | 14 | | |||
| 15 | KBlocksSingleGame::KBlocksSingleGame(int gameIndex, int fieldWidth, int fieldHeight, int showPieceCount, int messagePoolSize) | 15 | KBlocksSingleGame::KBlocksSingleGame(int gameIndex, int fieldWidth, int fieldHeight, int showPieceCount, int messagePoolSize) | ||
| 16 | { | 16 | { | ||
| 17 | mGameIndex = gameIndex; | 17 | mGameIndex = gameIndex; | ||
| 18 | 18 | | |||
| 19 | mpField = new KBlocksField(fieldWidth, fieldHeight); | 19 | mpField = new KBlocksField(fieldWidth, fieldHeight); | ||
| 20 | 20 | | |||
| 21 | mPieceCount = showPieceCount; | 21 | mPieceCount = showPieceCount; | ||
| ▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Line(s) | 192 | { | |||
| 206 | mpPieceList[1]->setPosX(2); | 206 | mpPieceList[1]->setPosX(2); | ||
| 207 | mpPieceList[1]->setPosY(2); | 207 | mpPieceList[1]->setPosY(2); | ||
| 208 | 208 | | |||
| 209 | mpGameMessage->clearGameResult(); | 209 | mpGameMessage->clearGameResult(); | ||
| 210 | mpGameMessage->clearGameAction(); | 210 | mpGameMessage->clearGameAction(); | ||
| 211 | 211 | | |||
| 212 | mCurrentGameState = GameState_Running; | 212 | mCurrentGameState = GameState_Running; | ||
| 213 | 213 | | |||
| 214 | mGameStartTime = getMillisecOfNow(); | 214 | mGameStartTime = Utils::getMillisecOfNow(); | ||
| 215 | 215 | | |||
| 216 | return mCurrentGameState; | 216 | return mCurrentGameState; | ||
| 217 | } | 217 | } | ||
| 218 | 218 | | |||
| 219 | int KBlocksSingleGame::stopGame() | 219 | int KBlocksSingleGame::stopGame() | ||
| 220 | { | 220 | { | ||
| 221 | if (mCurrentGameState != GameState_Stop) { | 221 | if (mCurrentGameState != GameState_Stop) { | ||
| 222 | mCurrentGameState = GameState_Stop; | 222 | mCurrentGameState = GameState_Stop; | ||
| 223 | emit gameStopped(); | 223 | emit gameStopped(); | ||
| 224 | } | 224 | } | ||
| 225 | 225 | | |||
| 226 | return mCurrentGameState; | 226 | return mCurrentGameState; | ||
| 227 | } | 227 | } | ||
| 228 | 228 | | |||
| 229 | int KBlocksSingleGame::pauseGame(bool flag) | 229 | int KBlocksSingleGame::pauseGame(bool flag) | ||
| 230 | { | 230 | { | ||
| 231 | if ((mCurrentGameState == GameState_Running) && flag) { | 231 | if ((mCurrentGameState == GameState_Running) && flag) { | ||
| 232 | mCurrentGameState = GameState_Pause; | 232 | mCurrentGameState = GameState_Pause; | ||
| 233 | } else if ((mCurrentGameState == GameState_Pause) && (!flag)) { | 233 | } else if ((mCurrentGameState == GameState_Pause) && (!flag)) { | ||
| 234 | mCurrentGameState = GameState_Running; | 234 | mCurrentGameState = GameState_Running; | ||
| 235 | 235 | | |||
| 236 | mGameStartTime = getMillisecOfNow(); | 236 | mGameStartTime = Utils::getMillisecOfNow(); | ||
| 237 | } | 237 | } | ||
| 238 | 238 | | |||
| 239 | return mCurrentGameState; | 239 | return mCurrentGameState; | ||
| 240 | } | 240 | } | ||
| 241 | 241 | | |||
| 242 | int KBlocksSingleGame::continueGame() | 242 | int KBlocksSingleGame::continueGame() | ||
| 243 | { | 243 | { | ||
| 244 | if ((mCurrentGameState != GameState_Stop) && mStandbyFlag) { | 244 | if ((mCurrentGameState != GameState_Stop) && mStandbyFlag) { | ||
| 245 | mStandbyFlag = false; | 245 | mStandbyFlag = false; | ||
| 246 | 246 | | |||
| 247 | mGameStartTime = getMillisecOfNow(); | 247 | mGameStartTime = Utils::getMillisecOfNow(); | ||
| 248 | } | 248 | } | ||
| 249 | 249 | | |||
| 250 | return mCurrentGameState; | 250 | return mCurrentGameState; | ||
| 251 | } | 251 | } | ||
| 252 | 252 | | |||
| 253 | bool KBlocksSingleGame::pickGameResult(int *result) | 253 | bool KBlocksSingleGame::pickGameResult(int *result) | ||
| 254 | { | 254 | { | ||
| 255 | return mpGameMessage->pickGameResult(result); | 255 | return mpGameMessage->pickGameResult(result); | ||
| 256 | } | 256 | } | ||
| 257 | 257 | | |||
| 258 | bool KBlocksSingleGame::pickGameAction(int *type, int *action) | 258 | bool KBlocksSingleGame::pickGameAction(int *type, int *action) | ||
| 259 | { | 259 | { | ||
| 260 | return mpGameMessage->pickGameAction(type, action); | 260 | return mpGameMessage->pickGameAction(type, action); | ||
| 261 | } | 261 | } | ||
| 262 | 262 | | |||
| 263 | int KBlocksSingleGame::doUpdateGame(bool force) | 263 | int KBlocksSingleGame::doUpdateGame(bool force) | ||
| 264 | { | 264 | { | ||
| 265 | if (mCurrentGameState == GameState_Stop) { | 265 | if (mCurrentGameState == GameState_Stop) { | ||
| 266 | return GameResult_Game_Over; | 266 | return GameResult_Game_Over; | ||
| 267 | } else if ((mCurrentGameState != GameState_Running) || mStandbyFlag) { | 267 | } else if ((mCurrentGameState != GameState_Running) || mStandbyFlag) { | ||
| 268 | return GameResult_None; | 268 | return GameResult_None; | ||
| 269 | } | 269 | } | ||
| 270 | 270 | | |||
| 271 | timeLong tmpCurTime = getMillisecOfNow(); | 271 | timeLong tmpCurTime = Utils::getMillisecOfNow(); | ||
| 272 | 272 | | |||
| 273 | int gameResult = GameResult_None; | 273 | int gameResult = GameResult_None; | ||
| 274 | 274 | | |||
| 275 | if (force) { | 275 | if (force) { | ||
| 276 | runGameOneStep(&gameResult); | 276 | runGameOneStep(&gameResult); | ||
| 277 | } else { | 277 | } else { | ||
| 278 | if (mGameInterval < 0) { | 278 | if (mGameInterval < 0) { | ||
| 279 | return gameResult; | 279 | return gameResult; | ||
| ▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Line(s) | 375 | { | |||
| 389 | for (int i = 0; i < 4; i++) { | 389 | for (int i = 0; i < 4; i++) { | ||
| 390 | int posX = mpPieceList[0]->getCellPosX(i); | 390 | int posX = mpPieceList[0]->getCellPosX(i); | ||
| 391 | int posY = mpPieceList[0]->getCellPosY(i); | 391 | int posY = mpPieceList[0]->getCellPosY(i); | ||
| 392 | mpGameMessage->putGameAction(GameAction_New_Piece_X, posX); | 392 | mpGameMessage->putGameAction(GameAction_New_Piece_X, posX); | ||
| 393 | mpGameMessage->putGameAction(GameAction_New_Piece_Y, posY); | 393 | mpGameMessage->putGameAction(GameAction_New_Piece_Y, posY); | ||
| 394 | } | 394 | } | ||
| 395 | } | 395 | } | ||
| 396 | 396 | | |||
| 397 | timeLong KBlocksSingleGame::getMillisecOfNow() | | |||
| 398 | { | | |||
| 399 | timeval tmpCurTime; | | |||
| 400 | | ||||
| 401 | gettimeofday(&tmpCurTime, NULL); | | |||
| 402 | | ||||
| 403 | timeLong tmpMilliTime = (timeLong)tmpCurTime.tv_usec / 1000; | | |||
| 404 | tmpMilliTime += (timeLong)tmpCurTime.tv_sec * 1000; | | |||
| 405 | | ||||
| 406 | return tmpMilliTime; | | |||
| 407 | } | | |||