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

0001 /*
0002     SPDX-FileCopyrightText: 2003 Marco Krüger <grisuji@gmx.de>
0003     SPDX-FileCopyrightText: 2003 Ian Wadham <iandw.au@gmail.com>
0004     SPDX-FileCopyrightText: 2009 Ian Wadham <iandw.au@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KGRGAME_H
0010 #define KGRGAME_H
0011 
0012 #include "kgrglobals.h"
0013 
0014 #include <QObject>
0015 #include <QList>
0016 
0017 /**
0018  * Sets up games and levels in KGoldrunner and controls the play.
0019  *
0020  * @short   KGoldrunner Game Controller.
0021  */
0022 class KGrView;
0023 class KGrScene;
0024 class QDialog;
0025 
0026 class KGrSounds;
0027 
0028 class KGrEditor;
0029 class KGrLevelPlayer;
0030 class QRandomGenerator;
0031 class QTimer;
0032 
0033 class KGrGame : public QObject
0034 {
0035 Q_OBJECT
0036 public:
0037     KGrGame (KGrView * theView,
0038              const QString & theSystemDir, const QString & theUserDir);
0039     ~KGrGame() override;
0040 
0041     bool initGameLists();
0042 
0043     void setInitialTheme (const QString & themeFilepath);
0044 
0045     bool inEditMode();          // True if the game is in editor mode.
0046 
0047     bool saveOK();          // Check if edits were saved.
0048 
0049     // Flags to control author's debugging aids.
0050     static bool bugFix;
0051     static bool logging;
0052 
0053 public Q_SLOTS:
0054     void initGame();            // Do the game object's first painting.
0055 
0056     void gameActions        (const int action);
0057     void editActions        (const int action);
0058     void editToolbarActions (const int action);
0059     void settings           (const int action);
0060 
0061     void kbControl          (const int dirn, const bool pressed = true);
0062 
0063     void incScore           (const int n);  // Update the score.
0064 
0065     // Play or stop sound.  Default is play: only FallSound can be stopped.
0066     void playSound          (const int n, const bool onOff = true);
0067 
0068 private:
0069     void quickStartDialog();
0070 
0071     bool modeSwitch (const int action,
0072                      int & selectedGame, int & selectedLevel);
0073     bool selectGame (const SelectAction slAction,
0074                      int & selectedGame, int & selectedLevel);
0075 
0076     void toggleSoundsOnOff (const int action);  // Enable or disable sounds.
0077 
0078     // Set mouse, keyboard or laptop-hybrid control of the hero.
0079     void setControlMode (const int mode);
0080     void setHoldKeyOption (const int option);
0081     void setTimeScale (const int action);
0082 
0083     void newGame   (const int lev, const int gameIndex);
0084     void runReplay (const int action,
0085                     const int selectedGame, const int selectedLevel);
0086     bool getRecordingName (const QString & dir, const QString & pPrefix,
0087                            QString & filename);
0088     bool startDemo (const Owner demoOwner, const QString & pPrefix,
0089                                            const int levelNo);
0090     void runNextDemoLevel();
0091     void finishDemo();
0092 
0093 private Q_SLOTS:
0094     void interruptDemo();
0095 
0096 private:
0097     void startInstantReplay();
0098     void replayLastLevel();
0099 
0100     void showHint();            // Show hint for current level.
0101 
0102     QString getTitle();     // Collection - Level NNN, Name.
0103 
0104     void showHighScores();      // Show high scores for current game.
0105 
0106     void freeze (const bool userAction, const bool on_off);
0107 
0108     QString getDirectory (Owner o);
0109 
0110     void herosDead();           // Hero was caught or he quit (key Q).
0111     void levelCompleted();      // Hero completed the level.
0112 
0113     // Save game ID, score and level.
0114     void saveGame();
0115 
0116     // Select a saved game, score and level.
0117     bool selectSavedGame (int & selectedGame, int & selectedLevel);
0118 
0119     // Load and run a saved game, score and level.
0120     void loadGame (const int index, const int lev);
0121 
0122     QString loadedData;
0123 
0124 private Q_SLOTS:
0125     void endLevel (const int result);   // Hero completed the level or he died.
0126 
0127     void finalBreath();         // Hero is dead: end the death-scene.
0128     void repeatLevel();         // Hero is dead: repeat the level.
0129     void goUpOneLevel();        // Start next level.
0130 
0131 Q_SIGNALS:
0132     // These signals go to the GUI in most cases.
0133     void showScore (long);      // For main window to show the score.
0134     void showLives (long);      // For main window to show lives left.
0135 
0136     void hintAvailable (bool);      // For main window to adjust menu text.
0137 
0138     void setEditMenu (bool);        // Enable/Disable edit menu items.
0139 
0140     void gameFreeze (bool);     // Do visual feedback in the GUI.
0141 
0142     void quitGame();            // Used for Quit option in Quick Start.
0143 
0144     // Used to set/clear toggle actions and enable/disable actions.
0145     void setToggle (const QString &actionName, const bool onOff);
0146     void setAvail  (const QString &actionName, const bool onOff);
0147 
0148 private:
0149     QDialog * qs;           // Pointer to Quick Start dialog box.
0150     QString initialThemeFilepath;
0151 
0152 private Q_SLOTS:
0153     void quickStartPlay();
0154     void quickStartNewGame();
0155     void quickStartUseMenu();
0156     void quickStartQuit();
0157 
0158 private:
0159     bool playLevel (const Owner fileOwner, const QString & prefix,
0160                     const int levelNo, const bool newLevel);
0161     void setupLevelPlayer();
0162     void showTutorialMessages (int levelNo);
0163     void setPlayback (const bool onOff);
0164 
0165     void checkHighScore();      // Check if high score for current game.
0166 
0167     int  selectedGame;
0168 
0169 /******************************************************************************/
0170 /**************************  PLAYFIELD AND GAME DATA  *************************/
0171 /******************************************************************************/
0172 
0173     QRandomGenerator *          randomGen;  // Random number generator.
0174     KGrLevelPlayer *            levelPlayer;    // Where the level is played.
0175     KGrRecording *              recording;  // A recording of the play.
0176     bool                        playback;   // Play back or record?
0177 
0178     KGrView     *       view;       // Where the game is displayed.
0179     KGrScene    *               scene;          // Where the graphics are.
0180 
0181     QString         systemDataDir;  // System games are stored here.
0182     QString         userDataDir;    // User games are stored here.
0183     int                         timeScale;  // The speed of the game (2-20).
0184 
0185     QList<KGrGameData *>        gameList;   // A list of available games.
0186     int             gameIndex;  // The index in the game-list.
0187     Owner           owner;      // The game's owner.
0188 
0189     QString                     prefix;     // Prefix for game or demo file.
0190     int             level;      // Current play/edit/demo level.
0191     int                         levelMax;   // Last level no in game/demo.
0192     QString         levelName;  // Level name (optional).
0193     QString         levelHint;  // Level hint (optional).
0194 
0195     QString                     mainDemoName;   // File-prefix for Main Demo.
0196     GameAction                  demoType;   // The type of replay or demo.
0197     bool                        startupDemo;    // Startup demo running?
0198 
0199     Owner                       playbackOwner;  // Owner for current demo-file.
0200     QString                     playbackPrefix; // File-prefix for current demo.
0201     int                         playbackIndex;  // Record-index for curr demo.
0202     int                         playbackMax;    // Max index for current demo.
0203 
0204     long            lives;      // Lives remaining.
0205     long            score;      // Current score.
0206     long            startScore; // Score at start of level.
0207 
0208     bool            gameFrozen; // Game stopped.
0209     bool            programFreeze;  // Stop game during dialog, etc.
0210 
0211     QTimer *            dyingTimer; // For pause when the hero dies.
0212 
0213     int             lgHighlight;    // Row selected in "loadGame()".
0214 
0215 /******************************************************************************/
0216 /*******************************  SOUND SUPPORT *******************************/
0217 /******************************************************************************/
0218     KGrSounds * effects;
0219     QList<int> fx;
0220     bool soundOn;
0221     bool stepsOn;
0222 
0223 public Q_SLOTS:
0224     void dbgControl (const int code);   // Authors' debugging aids.
0225 
0226 private:
0227     KGrEditor * editor;     // The level-editor object.
0228 
0229     int controlMode;        // How to control the hero (e.g. K/B or mouse).
0230     int holdKeyOption;      // Whether K/B control is by holding or clicking keys.
0231 
0232 /******************************************************************************/
0233 /***********************   GAME PROPERTIES AND METHODS   **********************/
0234 /******************************************************************************/
0235 
0236     bool loadGameData      (Owner);
0237     void saveSolution      (const QString & prefix, const int levelNo);
0238     bool initRecordingData (const Owner fileOwner, const QString & prefix,
0239                             const int levelNo, const bool pPlayback);
0240     void saveRecording     (const QString & filetype); // Type "rec_" or "sol_".
0241     bool loadRecording     (const QString & dir,   const QString & prefix,
0242                                                    const int levelNo);
0243     void loadSounds();
0244 
0245 /******************************************************************************/
0246 /**********************    WORD-WRAPPED MESSAGE BOX    ************************/
0247 /******************************************************************************/
0248 
0249     void myMessage (QWidget * parent, const QString &title, const QString &contents);
0250 };
0251 
0252 #endif // KGRGAME_H