1 /*
2     SPDX-FileCopyrightText: 2003 Marco Krüger <grisuji@gmx.de>
3     SPDX-FileCopyrightText: 2003 Ian Wadham <iandw.au@gmail.com>
4     SPDX-FileCopyrightText: 2009 Ian Wadham <iandw.au@gmail.com>
5 
6     SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 
9 #ifndef KGRGAME_H
10 #define KGRGAME_H
11 
12 #include "kgrglobals.h"
13 
14 #include <QObject>
15 #include <QList>
16 #include <QVector>
17 
18 /**
19  * Sets up games and levels in KGoldrunner and controls the play.
20  *
21  * @short   KGoldrunner Game Controller.
22  */
23 class KGrView;
24 class KGrScene;
25 class QDialog;
26 
27 class KGrSounds;
28 
29 class KGrEditor;
30 class KGrLevelPlayer;
31 class QRandomGenerator;
32 class QTimer;
33 
34 class KGrGame : public QObject
35 {
36 Q_OBJECT
37 public:
38     KGrGame (KGrView * theView,
39              const QString & theSystemDir, const QString & theUserDir);
40     ~KGrGame() override;
41 
42     bool initGameLists();
43 
44     void setInitialTheme (const QString & themeFilepath);
45 
46     bool inEditMode();			// True if the game is in editor mode.
47 
48     bool saveOK();			// Check if edits were saved.
49 
50     // Flags to control author's debugging aids.
51     static bool bugFix;
52     static bool logging;
53 
54 public Q_SLOTS:
55     void initGame();			// Do the game object's first painting.
56 
57     void gameActions        (const int action);
58     void editActions        (const int action);
59     void editToolbarActions (const int action);
60     void settings           (const int action);
61 
62     void kbControl          (const int dirn, const bool pressed = true);
63 
64     void incScore           (const int n);	// Update the score.
65 
66     // Play or stop sound.  Default is play: only FallSound can be stopped.
67     void playSound          (const int n, const bool onOff = true);
68 
69 private:
70     void quickStartDialog();
71 
72     bool modeSwitch (const int action,
73                      int & selectedGame, int & selectedLevel);
74     bool selectGame (const SelectAction slAction,
75                      int & selectedGame, int & selectedLevel);
76 
77     void toggleSoundsOnOff (const int action);	// Enable or disable sounds.
78 
79     // Set mouse, keyboard or laptop-hybrid control of the hero.
80     void setControlMode (const int mode);
81     void setHoldKeyOption (const int option);
82     void setTimeScale (const int action);
83 
84     void newGame   (const int lev, const int gameIndex);
85     void runReplay (const int action,
86                     const int selectedGame, const int selectedLevel);
87     bool getRecordingName (const QString & dir, const QString & pPrefix,
88                            QString & filename);
89     bool startDemo (const Owner demoOwner, const QString & pPrefix,
90                                            const int levelNo);
91     void runNextDemoLevel();
92     void finishDemo();
93 
94 private Q_SLOTS:
95     void interruptDemo();
96 
97 private:
98     void startInstantReplay();
99     void replayLastLevel();
100 
101     void showHint();			// Show hint for current level.
102 
103     QString	getTitle();		// Collection - Level NNN, Name.
104 
105     void showHighScores();		// Show high scores for current game.
106 
107     void freeze (const bool userAction, const bool on_off);
108 
109     QString getDirectory (Owner o);
110 
111     void herosDead();			// Hero was caught or he quit (key Q).
112     void levelCompleted();		// Hero completed the level.
113 
114     // Save game ID, score and level.
115     void saveGame();
116 
117     // Select a saved game, score and level.
118     bool selectSavedGame (int & selectedGame, int & selectedLevel);
119 
120     // Load and run a saved game, score and level.
121     void loadGame (const int index, const int lev);
122 
123     QString loadedData;
124 
125 private Q_SLOTS:
126     void endLevel (const int result);	// Hero completed the level or he died.
127 
128     void finalBreath();			// Hero is dead: end the death-scene.
129     void repeatLevel();			// Hero is dead: repeat the level.
130     void goUpOneLevel();		// Start next level.
131 
132 Q_SIGNALS:
133     // These signals go to the GUI in most cases.
134     void showScore (long);		// For main window to show the score.
135     void showLives (long);		// For main window to show lives left.
136 
137     void hintAvailable (bool);		// For main window to adjust menu text.
138 
139     void setEditMenu (bool);		// Enable/Disable edit menu items.
140 
141     void gameFreeze (bool);		// Do visual feedback in the GUI.
142 
143     void quitGame();			// Used for Quit option in Quick Start.
144 
145     // Used to set/clear toggle actions and enable/disable actions.
146     void setToggle (const char * actionName, const bool onOff);
147     void setAvail  (const char * actionName, const bool onOff);
148 
149 private:
150     QDialog * qs;			// Pointer to Quick Start dialog box.
151     QString initialThemeFilepath;
152 
153 private Q_SLOTS:
154     void quickStartPlay();
155     void quickStartNewGame();
156     void quickStartUseMenu();
157     void quickStartQuit();
158 
159 private:
160     bool playLevel (const Owner fileOwner, const QString & prefix,
161                     const int levelNo, const bool newLevel);
162     void setupLevelPlayer();
163     void showTutorialMessages (int levelNo);
164     void setPlayback (const bool onOff);
165 
166     void checkHighScore();		// Check if high score for current game.
167 
168     int  selectedGame;
169 
170 /******************************************************************************/
171 /**************************  PLAYFIELD AND GAME DATA  *************************/
172 /******************************************************************************/
173 
174     QRandomGenerator *          randomGen;	// Random number generator.
175     KGrLevelPlayer *            levelPlayer;	// Where the level is played.
176     KGrRecording *              recording;	// A recording of the play.
177     bool                        playback;	// Play back or record?
178 
179     KGrView     *		view;		// Where the game is displayed.
180     KGrScene    *               scene;          // Where the graphics are.
181 
182     QString			systemDataDir;	// System games are stored here.
183     QString			userDataDir;	// User games are stored here.
184     int                         timeScale;	// The speed of the game (2-20).
185 
186     QList<KGrGameData *>        gameList;	// A list of available games.
187     int				gameIndex;	// The index in the game-list.
188     Owner			owner;		// The game's owner.
189 
190     QString                     prefix;		// Prefix for game or demo file.
191     int				level;		// Current play/edit/demo level.
192     int                         levelMax;	// Last level no in game/demo.
193     QString			levelName;	// Level name (optional).
194     QString			levelHint;	// Level hint (optional).
195 
196     QString                     mainDemoName;	// File-prefix for Main Demo.
197     GameAction                  demoType;	// The type of replay or demo.
198     bool                        startupDemo;	// Startup demo running?
199 
200     Owner                       playbackOwner;	// Owner for current demo-file.
201     QString                     playbackPrefix;	// File-prefix for current demo.
202     int                         playbackIndex;	// Record-index for curr demo.
203     int                         playbackMax;	// Max index for current demo.
204 
205     long			lives;		// Lives remaining.
206     long			score;		// Current score.
207     long			startScore;	// Score at start of level.
208 
209     bool			gameFrozen;	// Game stopped.
210     bool			programFreeze;	// Stop game during dialog, etc.
211 
212     QTimer *			dyingTimer;	// For pause when the hero dies.
213 
214     int				lgHighlight;	// Row selected in "loadGame()".
215 
216 /******************************************************************************/
217 /*******************************  SOUND SUPPORT *******************************/
218 /******************************************************************************/
219     KGrSounds * effects;
220     QVector<int> fx;
221     bool soundOn;
222     bool stepsOn;
223 
224 public Q_SLOTS:
225     void dbgControl (const int code);	// Authors' debugging aids.
226 
227 private:
228     KGrEditor * editor;		// The level-editor object.
229 
230     int controlMode;		// How to control the hero (e.g. K/B or mouse).
231     int holdKeyOption;		// Whether K/B control is by holding or clicking keys.
232 
233 /******************************************************************************/
234 /***********************   GAME PROPERTIES AND METHODS   **********************/
235 /******************************************************************************/
236 
237     bool loadGameData      (Owner);
238     void saveSolution      (const QString & prefix, const int levelNo);
239     bool initRecordingData (const Owner fileOwner, const QString & prefix,
240                             const int levelNo, const bool pPlayback);
241     void saveRecording     (const QString & filetype); // Type "rec_" or "sol_".
242     bool loadRecording     (const QString & dir,   const QString & prefix,
243                                                    const int levelNo);
244     void loadSounds();
245 
246 /******************************************************************************/
247 /**********************    WORD-WRAPPED MESSAGE BOX    ************************/
248 /******************************************************************************/
249 
250     void myMessage (QWidget * parent, const QString &title, const QString &contents);
251 };
252 
253 #endif // KGRGAME_H
254