File indexing completed on 2024-04-28 07:51:26

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                *
0003 *   SPDX-FileCopyrightText: 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
0004 *                                                                         *
0005 *   SPDX-License-Identifier: GPL-2.0-or-later
0006 ***************************************************************************/
0007 #include "KBlocksSinglePlayer.h"
0008 
0009 KBlocksSinglePlayer::KBlocksSinglePlayer(GamePlayerInterface *player, int thinkInterval, int processInterval)
0010 {
0011     mpPlayer = player;
0012     mpGame = nullptr;
0013 
0014     mPlayerState = KBlocksPlayer_ThinkingState;
0015 
0016     mThinkInterval = thinkInterval;
0017     mProcessInterval = (processInterval < 0) ? 0 : processInterval;
0018     if (mThinkInterval >= 0) {
0019         mActionTimer.setInterval(thinkInterval);
0020         connect(&mActionTimer, &QTimer::timeout, this, &KBlocksSinglePlayer::doAction);
0021     }
0022     mActionTimer.stop();
0023 
0024     mActionList.clear();
0025 }
0026 
0027 KBlocksSinglePlayer::~KBlocksSinglePlayer()
0028 {
0029 }
0030 
0031 void KBlocksSinglePlayer::startGame(SingleGameInterface *p)
0032 {
0033     mpGame = p;
0034     mpPlayer->startGame(mpGame);
0035 
0036     mPlayerState = KBlocksPlayer_ThinkingState;
0037 
0038     mActionList.clear();
0039 
0040     if (mThinkInterval > 0) {
0041         mActionTimer.start();
0042     }
0043 }
0044 
0045 void KBlocksSinglePlayer::stopGame()
0046 {
0047     mActionTimer.stop();
0048 
0049     mActionList.clear();
0050 
0051     mpPlayer->stopGame();
0052     mpGame = nullptr;
0053 }
0054 
0055 void KBlocksSinglePlayer::pauseGame(bool flag)
0056 {
0057     if (flag) {
0058         mActionTimer.stop();
0059     } else if (mThinkInterval > 0) {
0060         mActionTimer.start();
0061     }
0062 }
0063 
0064 void KBlocksSinglePlayer::think()
0065 {
0066     mActionList.clear();
0067     mpPlayer->think(&mActionList);
0068 
0069     if ((!mActionList.empty()) && (mThinkInterval > 0)) {
0070         mActionTimer.stop();
0071         mActionTimer.setInterval(mProcessInterval);
0072         mPlayerState = KBlocksPlayer_ProcessingState;
0073         mActionTimer.start();
0074     }
0075 }
0076 
0077 bool KBlocksSinglePlayer::process()
0078 {
0079     if (mActionList.empty()) {
0080         if (mThinkInterval > 0) {
0081             mActionTimer.stop();
0082             mActionTimer.setInterval(mThinkInterval);
0083             mPlayerState = KBlocksPlayer_ThinkingState;
0084             mActionTimer.start();
0085         }
0086         return false;
0087     }
0088 
0089     if (!mpGame->isActive()) {
0090         return false;
0091     }
0092 
0093     KBlocks_Player_Action tmpAction = mActionList.front();
0094     mActionList.pop_front();
0095     switch (tmpAction) {
0096     case PlayerAction_Move_Left:
0097         mpGame->setCurrentPiece(-1, 0, 0);
0098         break;
0099     case PlayerAction_Move_Right:
0100         mpGame->setCurrentPiece(1, 0, 0);
0101         break;
0102     case PlayerAction_Move_Down:
0103         mpGame->setCurrentPiece(0, 1, 0);
0104         break;
0105     case PlayerAction_Push_Down:
0106         while (mpGame->setCurrentPiece(0, 1, 0)) ;
0107         mpGame->forceUpdateGame();
0108         break;
0109     case PlayerAction_Rotate_CW:
0110         mpGame->setCurrentPiece(0, 0, 1);
0111         break;
0112     case PlayerAction_Rotate_CCW:
0113         mpGame->setCurrentPiece(0, 0, -1);
0114         break;
0115     case PlayerAction_None:
0116     default:
0117         break;
0118     }
0119 
0120     return true;
0121 }
0122 
0123 void KBlocksSinglePlayer::doAction()
0124 {
0125     if (!mpGame) {
0126         return;
0127     }
0128 
0129     if (mPlayerState == KBlocksPlayer_ThinkingState) {
0130         think();
0131     } else if (mPlayerState == KBlocksPlayer_ProcessingState) {
0132         process();
0133     }
0134 }
0135 
0136 #include "moc_KBlocksSinglePlayer.cpp"