File indexing completed on 2024-04-21 04:02:08

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 #ifndef KBLOCKSSINGLEGAME_H
0008 #define KBLOCKSSINGLEGAME_H
0009 
0010 #include <QObject>
0011 
0012 #include "SingleGameInterface.h"
0013 
0014 #include "KBlocksField.h"
0015 #include "KBlocksPiece.h"
0016 #include "KBlocksPieceGenerator.h"
0017 #include "KBlocksGameMessage.h"
0018 
0019 #include "KBlocksGameRecorder.h"
0020 
0021 #include "KBlocksDefine.h"
0022 
0023 class KBlocksSingleGame : public QObject, public SingleGameInterface
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     explicit KBlocksSingleGame(int gameIndex, int fieldWidth = 10, int fieldHeight = 20, int showPieceCount = 2, int messagePoolSize = 256);
0029     ~KBlocksSingleGame() override;
0030 
0031 public:
0032     KBlocksField *getField() override;
0033 
0034     int getPieceCount() override;
0035     KBlocksPiece *getPiece(int index) override;
0036 
0037     bool isActive() override;
0038     bool isGameRunning() override;
0039 
0040     void setGameStandbyMode(bool flag);
0041     void setGameInterval(int interval);
0042     void setGameRecorder(KBlocksGameRecorder *p);
0043 
0044     int forceUpdateGame() override;
0045     int updateGame() override;
0046     int punishGame(int lineCount, int punishSeed);
0047 
0048     bool setCurrentPiece(int xPos, int yPos, int rotation) override;
0049 
0050     int startGame(int seed);
0051     int stopGame();
0052 
0053     int pauseGame(bool flag);
0054     int continueGame() override;
0055 
0056     bool pickGameResult(int *result) override;
0057     bool pickGameAction(int *type, int *action) override;
0058 
0059 Q_SIGNALS:
0060     void gameStopped();
0061 
0062 private:
0063     int doUpdateGame(bool force);
0064     bool runGameOneStep(int *gameResult);
0065     bool checkPieceTouchGround(KBlocksPiece *p);
0066     void freezePieceToField(KBlocksPiece *p);
0067     int removeFieldLines();
0068     void prepareNextPiece();
0069 
0070 protected:
0071     KBlocksField *mpField = nullptr;
0072 
0073     int mPieceCount;
0074     KBlocksPiece **mpPieceList = nullptr;
0075 
0076 private:
0077     KBlocksPieceGenerator *mpPieceGenerator = nullptr;
0078     KBlocksGameMessage *mpGameMessage = nullptr;
0079     KBlocksGameRecorder *mpGameRecorder = nullptr;
0080 
0081     int mGameIndex;
0082     int mCurrentGameState;
0083 
0084     bool mStandbyMode;
0085     bool mStandbyFlag;
0086     int mGameInterval;
0087     timeLong mGameStartTime;
0088 };
0089 
0090 #endif
0091