author | andyq <andyq> | 2002-12-11 17:58:53 (UTC) |
---|---|---|
committer | andyq <andyq> | 2002-12-11 17:58:53 (UTC) |
commit | f4865ac8e114baedd13c95160631e4574da9dc84 (patch) (unidiff) | |
tree | ecc74f212ffde8b9733ad6a7dd9e2360717917ef | |
parent | 676f39d78772b19d11471db55ece82262278f942 (diff) | |
download | opie-f4865ac8e114baedd13c95160631e4574da9dc84.zip opie-f4865ac8e114baedd13c95160631e4574da9dc84.tar.gz opie-f4865ac8e114baedd13c95160631e4574da9dc84.tar.bz2 |
Added check for no replay file on loading and fixed bugs where you could screw up the replay by pressing stuff
-rw-r--r-- | noncore/games/sfcave/sfcave.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp index 93f5f82..d551afe 100644 --- a/noncore/games/sfcave/sfcave.cpp +++ b/noncore/games/sfcave/sfcave.cpp | |||
@@ -1,1096 +1,1105 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <math.h> | 3 | #include <math.h> |
4 | #include <time.h> | 4 | #include <time.h> |
5 | 5 | ||
6 | #ifdef QWS | 6 | #ifdef QWS |
7 | #include <qpe/qpeapplication.h> | 7 | #include <qpe/qpeapplication.h> |
8 | #include <qpe/config.h> | 8 | #include <qpe/config.h> |
9 | #else | 9 | #else |
10 | #include <qapplication.h> | 10 | #include <qapplication.h> |
11 | #endif | 11 | #endif |
12 | #include <qdir.h> | 12 | #include <qdir.h> |
13 | 13 | ||
14 | #include "helpwindow.h" | 14 | #include "helpwindow.h" |
15 | #include "sfcave.h" | 15 | #include "sfcave.h" |
16 | 16 | ||
17 | #define CAPTION "SFCave 1.10 by AndyQ" | 17 | #define CAPTION "SFCave 1.10 by AndyQ" |
18 | 18 | ||
19 | #define UP_THRUST 0.6 | 19 | #define UP_THRUST 0.6 |
20 | #define NO_THRUST 0.8 | 20 | #define NO_THRUST 0.8 |
21 | #define MAX_DOWN_THRUST 4.0 | 21 | #define MAX_DOWN_THRUST 4.0 |
22 | #define MAX_UP_THRUST -3.5 | 22 | #define MAX_UP_THRUST -3.5 |
23 | 23 | ||
24 | // States | 24 | // States |
25 | #define STATE_BOSS 0 | 25 | #define STATE_BOSS 0 |
26 | #define STATE_RUNNING 1 | 26 | #define STATE_RUNNING 1 |
27 | #define STATE_CRASHING 2 | 27 | #define STATE_CRASHING 2 |
28 | #define STATE_CRASHED 3 | 28 | #define STATE_CRASHED 3 |
29 | #define STATE_NEWGAME 4 | 29 | #define STATE_NEWGAME 4 |
30 | #define STATE_MENU 5 | 30 | #define STATE_MENU 5 |
31 | #define STATE_REPLAY 6 | 31 | #define STATE_REPLAY 6 |
32 | 32 | ||
33 | // Menus | 33 | // Menus |
34 | #define MENU_MAIN_MENU 0 | 34 | #define MENU_MAIN_MENU 0 |
35 | #define MENU_OPTIONS_MENU 1 | 35 | #define MENU_OPTIONS_MENU 1 |
36 | 36 | ||
37 | // Main Menu Options | 37 | // Main Menu Options |
38 | #define MENU_START_GAME 0 | 38 | #define MENU_START_GAME 0 |
39 | #define MENU_OPTIONS 1 | 39 | #define MENU_OPTIONS 1 |
40 | #define MENU_HELP 2 | 40 | #define MENU_HELP 2 |
41 | #define MENU_QUIT 3 | 41 | #define MENU_QUIT 3 |
42 | 42 | ||
43 | // Option Menu Options | 43 | // Option Menu Options |
44 | #define MENU_GAME_TYPE 0 | 44 | #define MENU_GAME_TYPE 0 |
45 | #define MENU_GAME_DIFFICULTY 1 | 45 | #define MENU_GAME_DIFFICULTY 1 |
46 | #define MENU_CLEAR_HIGHSCORES 2 | 46 | #define MENU_CLEAR_HIGHSCORES 2 |
47 | #define MENU_BACK 3 | 47 | #define MENU_BACK 3 |
48 | 48 | ||
49 | 49 | ||
50 | #define NR_GAME_DIFFICULTIES 3 | 50 | #define NR_GAME_DIFFICULTIES 3 |
51 | #define NR_GAME_TYPES 3 | 51 | #define NR_GAME_TYPES 3 |
52 | 52 | ||
53 | #define DIFICULTY_EASY 0 | 53 | #define DIFICULTY_EASY 0 |
54 | #define DIFICULTY_NORMAL 1 | 54 | #define DIFICULTY_NORMAL 1 |
55 | #define DIFICULTY_HARD 2 | 55 | #define DIFICULTY_HARD 2 |
56 | #define EASY "Easy" | 56 | #define EASY "Easy" |
57 | #define NORMAL "Normal" | 57 | #define NORMAL "Normal" |
58 | #define HARD "Hard" | 58 | #define HARD "Hard" |
59 | 59 | ||
60 | #define SFCAVE_GAME_TYPE 0 | 60 | #define SFCAVE_GAME_TYPE 0 |
61 | #define GATES_GAME_TYPE 1 | 61 | #define GATES_GAME_TYPE 1 |
62 | #define FLY_GAME_TYPE 2 | 62 | #define FLY_GAME_TYPE 2 |
63 | #define SFCAVE_GAME "SFCave" | 63 | #define SFCAVE_GAME "SFCave" |
64 | #define GATES_GAME "Gates" | 64 | #define GATES_GAME "Gates" |
65 | #define FLY_GAME "Fly" | 65 | #define FLY_GAME "Fly" |
66 | #define CURRENT_GAME_TYPE gameTypes[currentGameType] | 66 | #define CURRENT_GAME_TYPE gameTypes[currentGameType] |
67 | #define CURRENT_GAME_DIFFICULTY difficultyOption[currentGameDifficulty]; | 67 | #define CURRENT_GAME_DIFFICULTY difficultyOption[currentGameDifficulty]; |
68 | 68 | ||
69 | QString SFCave::dificultyOption[] = { EASY, NORMAL, HARD }; | 69 | QString SFCave::dificultyOption[] = { EASY, NORMAL, HARD }; |
70 | QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME }; | 70 | QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME }; |
71 | 71 | ||
72 | QString SFCave::menuOptions[2][5] = { { "Start Game", "Options", "Help", "Quit", "" }, | 72 | QString SFCave::menuOptions[2][5] = { { "Start Game", "Options", "Help", "Quit", "" }, |
73 | { "Game Type - %s", "Game Difficulty - %s", "Clear High Scores for this game", "Back", "" } }; | 73 | { "Game Type - %s", "Game Difficulty - %s", "Clear High Scores for this game", "Back", "" } }; |
74 | 74 | ||
75 | #define UP_THRUST 0.6 | 75 | #define UP_THRUST 0.6 |
76 | #define NO_THRUST 0.8 | 76 | #define NO_THRUST 0.8 |
77 | #define MAX_DOWN_THRUST 4.0 | 77 | #define MAX_DOWN_THRUST 4.0 |
78 | #define MAX_UP_THRUST -3.5 | 78 | #define MAX_UP_THRUST -3.5 |
79 | double SFCave::UpThrustVals[3][3] = {{ 0.6, 0.6, 0.6 }, // SFCave | 79 | double SFCave::UpThrustVals[3][3] = {{ 0.6, 0.6, 0.6 }, // SFCave |
80 | { 0.6, 0.6, 0.8 }, // Gates | 80 | { 0.6, 0.6, 0.8 }, // Gates |
81 | { 0.4, 0.7, 1.0 } }; // Fly | 81 | { 0.4, 0.7, 1.0 } }; // Fly |
82 | 82 | ||
83 | double SFCave::DownThrustVals[3][3] = {{ 0.8, 0.8, 0.8 }, // SFCave | 83 | double SFCave::DownThrustVals[3][3] = {{ 0.8, 0.8, 0.8 }, // SFCave |
84 | { 0.8, 0.8, 1.0 }, // Gates | 84 | { 0.8, 0.8, 1.0 }, // Gates |
85 | { 0.4, 0.7, 1.0 } }; // Fly | 85 | { 0.4, 0.7, 1.0 } }; // Fly |
86 | 86 | ||
87 | double SFCave::MaxUpThrustVals[3][3] = {{ -3.5, -3.5, -3.5 }, // SFCave | 87 | double SFCave::MaxUpThrustVals[3][3] = {{ -3.5, -3.5, -3.5 }, // SFCave |
88 | { -3.5, -4.0, -5.0 }, // Gates | 88 | { -3.5, -4.0, -5.0 }, // Gates |
89 | { -3.5, -4.0, -5.0 } }; // Fly | 89 | { -3.5, -4.0, -5.0 } }; // Fly |
90 | 90 | ||
91 | double SFCave::MaxDownThrustVals[3][3] = {{ 4.0, 4.0, 4.0 }, // SFCave | 91 | double SFCave::MaxDownThrustVals[3][3] = {{ 4.0, 4.0, 4.0 }, // SFCave |
92 | { 4.0, 5.0, 5.5 }, // Gates | 92 | { 4.0, 5.0, 5.5 }, // Gates |
93 | { 3.5, 4.0, 5.0 } }; // Fly | 93 | { 3.5, 4.0, 5.0 } }; // Fly |
94 | 94 | ||
95 | int SFCave::initialGateGaps[] = { 75, 50, 25 }; | 95 | int SFCave::initialGateGaps[] = { 75, 50, 25 }; |
96 | 96 | ||
97 | int SFCave::nrMenuOptions[2] = { 4, 4 }; | 97 | int SFCave::nrMenuOptions[2] = { 4, 4 }; |
98 | int SFCave ::currentMenuOption[2] = { 0, 0 }; | 98 | int SFCave ::currentMenuOption[2] = { 0, 0 }; |
99 | 99 | ||
100 | bool movel; | 100 | bool movel; |
101 | 101 | ||
102 | 102 | ||
103 | int main( int argc, char *argv[] ) | 103 | int main( int argc, char *argv[] ) |
104 | { | 104 | { |
105 | movel = true; | 105 | movel = true; |
106 | #ifdef QWS | 106 | #ifdef QWS |
107 | QPEApplication a( argc, argv ); | 107 | QPEApplication a( argc, argv ); |
108 | #else | 108 | #else |
109 | QApplication a( argc, argv ); | 109 | QApplication a( argc, argv ); |
110 | #endif | 110 | #endif |
111 | 111 | ||
112 | int speed = 3; | 112 | int speed = 3; |
113 | for ( int i = 0 ; i < argc ; ++i ) | 113 | for ( int i = 0 ; i < argc ; ++i ) |
114 | { | 114 | { |
115 | if ( strcmp( argv[i], "-s" ) == 0 ) | 115 | if ( strcmp( argv[i], "-s" ) == 0 ) |
116 | { | 116 | { |
117 | if ( i+1 < argc ) | 117 | if ( i+1 < argc ) |
118 | speed = atoi( argv[i+1] ); | 118 | speed = atoi( argv[i+1] ); |
119 | } | 119 | } |
120 | } | 120 | } |
121 | 121 | ||
122 | SFCave app( speed ); | 122 | SFCave app( speed ); |
123 | a.setMainWidget( &app ); | 123 | a.setMainWidget( &app ); |
124 | app.show(); | 124 | app.show(); |
125 | app.start(); | 125 | app.start(); |
126 | a.exec(); | 126 | a.exec(); |
127 | } | 127 | } |
128 | 128 | ||
129 | SFCave :: SFCave( int spd, QWidget *w, char *name ) | 129 | SFCave :: SFCave( int spd, QWidget *w, char *name ) |
130 | : QMainWindow( w, name ) | 130 | : QMainWindow( w, name ) |
131 | 131 | ||
132 | { | 132 | { |
133 | replayIt = 0; | 133 | replayIt = 0; |
134 | #ifdef QWS | 134 | #ifdef QWS |
135 | showMaximized(); | 135 | showMaximized(); |
136 | #else | 136 | #else |
137 | resize( 240, 284 ); | 137 | resize( 240, 284 ); |
138 | #endif | 138 | #endif |
139 | 139 | ||
140 | replayFile = QDir::home().path(); | 140 | replayFile = QDir::home().path(); |
141 | replayFile += "/sfcave.replay"; | 141 | replayFile += "/sfcave.replay"; |
142 | printf( "%s\n", (const char *)replayFile ); | 142 | printf( "%s\n", (const char *)replayFile ); |
143 | 143 | ||
144 | sWidth = width(); | 144 | sWidth = width(); |
145 | sHeight = height(); | 145 | sHeight = height(); |
146 | segSize = sWidth/(MAPSIZE-1)+1; | 146 | segSize = sWidth/(MAPSIZE-1)+1; |
147 | 147 | ||
148 | currentMenuNr = 0; | 148 | currentMenuNr = 0; |
149 | currentGameType = 0; | 149 | currentGameType = 0; |
150 | currentGameDifficulty = 0; | 150 | currentGameDifficulty = 0; |
151 | 151 | ||
152 | setCaption( CAPTION ); | 152 | setCaption( CAPTION ); |
153 | showScoreZones = false; | 153 | showScoreZones = false; |
154 | 154 | ||
155 | #ifdef QWS | 155 | #ifdef QWS |
156 | Config cfg( "sfcave" ); | 156 | Config cfg( "sfcave" ); |
157 | cfg.setGroup( "settings" ); | 157 | cfg.setGroup( "settings" ); |
158 | QString key = "highScore_"; | 158 | QString key = "highScore_"; |
159 | 159 | ||
160 | for ( int i = 0 ; i < 3 ; ++i ) | 160 | for ( int i = 0 ; i < 3 ; ++i ) |
161 | { | 161 | { |
162 | for ( int j = 0 ; j < 3 ; ++j ) | 162 | for ( int j = 0 ; j < 3 ; ++j ) |
163 | highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 ); | 163 | highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 ); |
164 | } | 164 | } |
165 | 165 | ||
166 | currentGameType = cfg.readNumEntry( "gameType", 0 ); | 166 | currentGameType = cfg.readNumEntry( "gameType", 0 ); |
167 | currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 ); | 167 | currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 ); |
168 | #endif | 168 | #endif |
169 | speed = spd; // Change to 2 for PC | 169 | speed = spd; // Change to 2 for PC |
170 | press = false; | 170 | press = false; |
171 | 171 | ||
172 | offscreen = new QPixmap( sWidth, sHeight ); | 172 | offscreen = new QPixmap( sWidth, sHeight ); |
173 | offscreen->fill( Qt::black ); | 173 | offscreen->fill( Qt::black ); |
174 | 174 | ||
175 | // setUp(); | 175 | // setUp(); |
176 | crashLineLength = -1; | 176 | crashLineLength = -1; |
177 | state = STATE_MENU; | 177 | state = STATE_MENU; |
178 | prevState = STATE_MENU; | 178 | prevState = STATE_MENU; |
179 | 179 | ||
180 | gameTimer = new QTimer( this, "game timer" ); | 180 | gameTimer = new QTimer( this, "game timer" ); |
181 | connect( gameTimer, SIGNAL( timeout() ), | 181 | connect( gameTimer, SIGNAL( timeout() ), |
182 | this, SLOT( run() ) ); | 182 | this, SLOT( run() ) ); |
183 | } | 183 | } |
184 | 184 | ||
185 | SFCave :: ~SFCave() | 185 | SFCave :: ~SFCave() |
186 | { | 186 | { |
187 | } | 187 | } |
188 | 188 | ||
189 | void SFCave :: start() | 189 | void SFCave :: start() |
190 | { | 190 | { |
191 | gameTimer->start( 10 ); | 191 | gameTimer->start( 10 ); |
192 | 192 | ||
193 | } | 193 | } |
194 | 194 | ||
195 | void SFCave :: setSeed( int seed ) | 195 | void SFCave :: setSeed( int seed ) |
196 | { | 196 | { |
197 | if ( seed == -1 ) | 197 | if ( seed == -1 ) |
198 | currentSeed = ((unsigned long) time((time_t *) NULL)); | 198 | currentSeed = ((unsigned long) time((time_t *) NULL)); |
199 | else | 199 | else |
200 | currentSeed = seed; | 200 | currentSeed = seed; |
201 | PutSeed( currentSeed ); | 201 | PutSeed( currentSeed ); |
202 | } | 202 | } |
203 | 203 | ||
204 | int SFCave :: nextInt( int range ) | 204 | int SFCave :: nextInt( int range ) |
205 | { | 205 | { |
206 | int val = (int)(Random( ) * range); | 206 | int val = (int)(Random( ) * range); |
207 | 207 | ||
208 | return val; | 208 | return val; |
209 | 209 | ||
210 | } | 210 | } |
211 | 211 | ||
212 | void SFCave :: setUp() | 212 | void SFCave :: setUp() |
213 | { | 213 | { |
214 | score = 0; | 214 | score = 0; |
215 | offset = 0; | 215 | offset = 0; |
216 | nrFrames = 0; | 216 | nrFrames = 0; |
217 | dir = 1; | 217 | dir = 1; |
218 | thrust = 0; | 218 | thrust = 0; |
219 | 219 | ||
220 | if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) | 220 | if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) |
221 | { | 221 | { |
222 | thrustUp = UpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; | 222 | thrustUp = UpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; |
223 | noThrust = DownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; | 223 | noThrust = DownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; |
224 | maxUpThrust = MaxUpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; | 224 | maxUpThrust = MaxUpThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; |
225 | maxDownThrust = MaxDownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; | 225 | maxDownThrust = MaxDownThrustVals[SFCAVE_GAME_TYPE][currentGameDifficulty];; |
226 | } | 226 | } |
227 | else if ( CURRENT_GAME_TYPE == GATES_GAME ) | 227 | else if ( CURRENT_GAME_TYPE == GATES_GAME ) |
228 | { | 228 | { |
229 | thrustUp = UpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; | 229 | thrustUp = UpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; |
230 | noThrust = DownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; | 230 | noThrust = DownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; |
231 | maxUpThrust = MaxUpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; | 231 | maxUpThrust = MaxUpThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; |
232 | maxDownThrust = MaxDownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; | 232 | maxDownThrust = MaxDownThrustVals[GATES_GAME_TYPE][currentGameDifficulty];; |
233 | } | 233 | } |
234 | else | 234 | else |
235 | { | 235 | { |
236 | thrustUp = UpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; | 236 | thrustUp = UpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; |
237 | noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; | 237 | noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; |
238 | maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; | 238 | maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; |
239 | maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; | 239 | maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; |
240 | } | 240 | } |
241 | 241 | ||
242 | crashLineLength = 0; | 242 | crashLineLength = 0; |
243 | lastGateBottomY = 0; | 243 | lastGateBottomY = 0; |
244 | 244 | ||
245 | user.setRect( 50, sWidth/2, 4, 4 ); | 245 | user.setRect( 50, sWidth/2, 4, 4 ); |
246 | 246 | ||
247 | blockWidth = 20; | 247 | blockWidth = 20; |
248 | blockHeight = 70; | 248 | blockHeight = 70; |
249 | gapHeight = initialGateGaps[currentGameDifficulty]; | 249 | gapHeight = initialGateGaps[currentGameDifficulty]; |
250 | gateDistance = 75; | 250 | gateDistance = 75; |
251 | nextGate = nextInt( 50 ) + gateDistance; | 251 | nextGate = nextInt( 50 ) + gateDistance; |
252 | 252 | ||
253 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) | 253 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) |
254 | { | 254 | { |
255 | trail[i].setX( -1 ); | 255 | trail[i].setX( -1 ); |
256 | trail[i].setY( 0 ); | 256 | trail[i].setY( 0 ); |
257 | } | 257 | } |
258 | 258 | ||
259 | if ( CURRENT_GAME_TYPE != FLY_GAME ) | 259 | if ( CURRENT_GAME_TYPE != FLY_GAME ) |
260 | { | 260 | { |
261 | maxHeight = 50; | 261 | maxHeight = 50; |
262 | 262 | ||
263 | mapTop[0] = (int)(nextInt(50)) + 5; | 263 | mapTop[0] = (int)(nextInt(50)) + 5; |
264 | mapBottom[0] = (int)(nextInt(50)) + 5; | 264 | mapBottom[0] = (int)(nextInt(50)) + 5; |
265 | for ( int i = 1 ; i < MAPSIZE ; ++i ) | 265 | for ( int i = 1 ; i < MAPSIZE ; ++i ) |
266 | setPoint( i ); | 266 | setPoint( i ); |
267 | } | 267 | } |
268 | else | 268 | else |
269 | { | 269 | { |
270 | maxHeight = 100; | 270 | maxHeight = 100; |
271 | 271 | ||
272 | for ( int i = 0 ; i < MAPSIZE ; ++i ) | 272 | for ( int i = 0 ; i < MAPSIZE ; ++i ) |
273 | mapBottom[i] = sHeight - 10; | 273 | mapBottom[i] = sHeight - 10; |
274 | } | 274 | } |
275 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | 275 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) |
276 | blocks[i].setY( -1 ); | 276 | blocks[i].setY( -1 ); |
277 | 277 | ||
278 | } | 278 | } |
279 | 279 | ||
280 | void SFCave :: run() | 280 | void SFCave :: run() |
281 | { | 281 | { |
282 | switch ( state ) | 282 | switch ( state ) |
283 | { | 283 | { |
284 | case STATE_MENU: | 284 | case STATE_MENU: |
285 | displayMenu(); | 285 | displayMenu(); |
286 | break; | 286 | break; |
287 | case STATE_NEWGAME: | 287 | case STATE_NEWGAME: |
288 | setSeed( -1 ); | 288 | setSeed( -1 ); |
289 | setUp(); | 289 | setUp(); |
290 | draw(); | 290 | draw(); |
291 | state = STATE_RUNNING; | 291 | state = STATE_RUNNING; |
292 | replay = false; | 292 | replay = false; |
293 | replayList.clear(); | 293 | replayList.clear(); |
294 | break; | 294 | break; |
295 | case STATE_REPLAY: | 295 | case STATE_REPLAY: |
296 | setSeed( currentSeed ); | 296 | setSeed( currentSeed ); |
297 | setUp(); | 297 | setUp(); |
298 | draw(); | 298 | draw(); |
299 | state = STATE_RUNNING; | 299 | state = STATE_RUNNING; |
300 | replay = true; | 300 | replay = true; |
301 | if ( replayIt ) | 301 | if ( replayIt ) |
302 | delete replayIt; | 302 | delete replayIt; |
303 | replayIt = new QListIterator<int>( replayList ); | 303 | replayIt = new QListIterator<int>( replayList ); |
304 | case STATE_BOSS: | 304 | case STATE_BOSS: |
305 | drawBoss(); | 305 | drawBoss(); |
306 | break; | 306 | break; |
307 | 307 | ||
308 | case STATE_CRASHING: | 308 | case STATE_CRASHING: |
309 | case STATE_CRASHED: | 309 | case STATE_CRASHED: |
310 | draw(); | 310 | draw(); |
311 | break; | 311 | break; |
312 | 312 | ||
313 | case STATE_RUNNING: | 313 | case STATE_RUNNING: |
314 | { | 314 | { |
315 | if ( nrFrames % 2 == 0 ) | 315 | if ( nrFrames % 2 == 0 ) |
316 | handleKeys(); | 316 | handleKeys(); |
317 | 317 | ||
318 | // Apply Game rules | 318 | // Apply Game rules |
319 | nrFrames ++; | 319 | nrFrames ++; |
320 | 320 | ||
321 | if ( replay ) | 321 | if ( replay ) |
322 | { | 322 | { |
323 | while( replayIt->current() && *(replayIt->current()) == nrFrames ) | 323 | while( replayIt->current() && *(replayIt->current()) == nrFrames ) |
324 | { | 324 | { |
325 | press = !press; | 325 | press = !press; |
326 | ++(*replayIt); | 326 | ++(*replayIt); |
327 | } | 327 | } |
328 | } | 328 | } |
329 | 329 | ||
330 | if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) | 330 | if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) |
331 | handleGameSFCave(); | 331 | handleGameSFCave(); |
332 | else if ( CURRENT_GAME_TYPE == GATES_GAME ) | 332 | else if ( CURRENT_GAME_TYPE == GATES_GAME ) |
333 | handleGameGates(); | 333 | handleGameGates(); |
334 | else if ( CURRENT_GAME_TYPE == FLY_GAME ) | 334 | else if ( CURRENT_GAME_TYPE == FLY_GAME ) |
335 | handleGameFly(); | 335 | handleGameFly(); |
336 | 336 | ||
337 | draw(); | 337 | draw(); |
338 | break; | 338 | break; |
339 | } | 339 | } |
340 | } | 340 | } |
341 | } | 341 | } |
342 | 342 | ||
343 | void SFCave :: handleGameSFCave() | 343 | void SFCave :: handleGameSFCave() |
344 | { | 344 | { |
345 | // Update score | 345 | // Update score |
346 | if ( nrFrames % 5 == 0 ) | 346 | if ( nrFrames % 5 == 0 ) |
347 | score ++; | 347 | score ++; |
348 | 348 | ||
349 | if ( nrFrames % 500 == 0 ) | 349 | if ( nrFrames % 500 == 0 ) |
350 | { | 350 | { |
351 | if ( maxHeight < sHeight - 100 ) | 351 | if ( maxHeight < sHeight - 100 ) |
352 | { | 352 | { |
353 | maxHeight += 10; | 353 | maxHeight += 10; |
354 | 354 | ||
355 | // Reduce block height | 355 | // Reduce block height |
356 | if ( maxHeight > sHeight - 150 ) | 356 | if ( maxHeight > sHeight - 150 ) |
357 | blockHeight -= 5; | 357 | blockHeight -= 5; |
358 | } | 358 | } |
359 | } | 359 | } |
360 | 360 | ||
361 | if ( nrFrames % 100 == 0 ) | 361 | if ( nrFrames % 100 == 0 ) |
362 | addBlock(); | 362 | addBlock(); |
363 | 363 | ||
364 | if ( checkCollision() ) | 364 | if ( checkCollision() ) |
365 | { | 365 | { |
366 | if ( score > highestScore[currentGameType][currentGameDifficulty] ) | 366 | if ( score > highestScore[currentGameType][currentGameDifficulty] ) |
367 | { | 367 | { |
368 | highestScore[currentGameType][currentGameDifficulty] = score; | 368 | highestScore[currentGameType][currentGameDifficulty] = score; |
369 | saveScore(); | 369 | saveScore(); |
370 | } | 370 | } |
371 | state = STATE_CRASHING; | 371 | state = STATE_CRASHING; |
372 | } | 372 | } |
373 | else | 373 | else |
374 | { | 374 | { |
375 | moveLandscape(); | 375 | moveLandscape(); |
376 | } | 376 | } |
377 | 377 | ||
378 | } | 378 | } |
379 | 379 | ||
380 | 380 | ||
381 | void SFCave :: handleGameGates() | 381 | void SFCave :: handleGameGates() |
382 | { | 382 | { |
383 | // Update score | 383 | // Update score |
384 | if ( nrFrames % 5 == 0 ) | 384 | if ( nrFrames % 5 == 0 ) |
385 | score ++; | 385 | score ++; |
386 | 386 | ||
387 | // Slightly random gap distance | 387 | // Slightly random gap distance |
388 | if ( nrFrames >= nextGate ) | 388 | if ( nrFrames >= nextGate ) |
389 | { | 389 | { |
390 | nextGate = nrFrames + nextInt( 50 ) + gateDistance; | 390 | nextGate = nrFrames + nextInt( 50 ) + gateDistance; |
391 | addGate(); | 391 | addGate(); |
392 | } | 392 | } |
393 | 393 | ||
394 | if ( nrFrames % 500 == 0 ) | 394 | if ( nrFrames % 500 == 0 ) |
395 | { | 395 | { |
396 | if ( gapHeight > 75 ) | 396 | if ( gapHeight > 75 ) |
397 | gapHeight -= 5; | 397 | gapHeight -= 5; |
398 | } | 398 | } |
399 | 399 | ||
400 | if ( checkCollision() ) | 400 | if ( checkCollision() ) |
401 | { | 401 | { |
402 | if ( score > highestScore[currentGameType][currentGameDifficulty] ) | 402 | if ( score > highestScore[currentGameType][currentGameDifficulty] ) |
403 | { | 403 | { |
404 | highestScore[currentGameType][currentGameDifficulty] = score; | 404 | highestScore[currentGameType][currentGameDifficulty] = score; |
405 | saveScore(); | 405 | saveScore(); |
406 | } | 406 | } |
407 | state = STATE_CRASHING; | 407 | state = STATE_CRASHING; |
408 | } | 408 | } |
409 | else | 409 | else |
410 | { | 410 | { |
411 | moveLandscape(); | 411 | moveLandscape(); |
412 | } | 412 | } |
413 | 413 | ||
414 | } | 414 | } |
415 | 415 | ||
416 | void SFCave :: handleGameFly() | 416 | void SFCave :: handleGameFly() |
417 | { | 417 | { |
418 | if ( nrFrames % 4 == 0 ) | 418 | if ( nrFrames % 4 == 0 ) |
419 | { | 419 | { |
420 | // Update score | 420 | // Update score |
421 | // get distance between landscape and ship | 421 | // get distance between landscape and ship |
422 | int diff = mapBottom[10] - user.y(); | 422 | int diff = mapBottom[10] - user.y(); |
423 | 423 | ||
424 | // the closer the difference is to 0 means more points | 424 | // the closer the difference is to 0 means more points |
425 | if ( diff < 10 ) | 425 | if ( diff < 10 ) |
426 | score += 5; | 426 | score += 5; |
427 | else if ( diff < 20 ) | 427 | else if ( diff < 20 ) |
428 | score += 3; | 428 | score += 3; |
429 | else if ( diff < 30 ) | 429 | else if ( diff < 30 ) |
430 | score += 2; | 430 | score += 2; |
431 | else if ( diff < 40 ) | 431 | else if ( diff < 40 ) |
432 | score += 1; | 432 | score += 1; |
433 | } | 433 | } |
434 | 434 | ||
435 | if ( checkFlyGameCollision() ) | 435 | if ( checkFlyGameCollision() ) |
436 | { | 436 | { |
437 | if ( score > highestScore[currentGameType][currentGameDifficulty] ) | 437 | if ( score > highestScore[currentGameType][currentGameDifficulty] ) |
438 | { | 438 | { |
439 | highestScore[currentGameType][currentGameDifficulty] = score; | 439 | highestScore[currentGameType][currentGameDifficulty] = score; |
440 | saveScore(); | 440 | saveScore(); |
441 | } | 441 | } |
442 | state = STATE_CRASHING; | 442 | state = STATE_CRASHING; |
443 | } | 443 | } |
444 | else | 444 | else |
445 | { | 445 | { |
446 | moveFlyGameLandscape(); | 446 | moveFlyGameLandscape(); |
447 | } | 447 | } |
448 | } | 448 | } |
449 | 449 | ||
450 | bool SFCave :: checkFlyGameCollision() | 450 | bool SFCave :: checkFlyGameCollision() |
451 | { | 451 | { |
452 | if ( (user.y() + user.width()) >= mapBottom[11] ) | 452 | if ( (user.y() + user.width()) >= mapBottom[11] ) |
453 | return true; | 453 | return true; |
454 | 454 | ||
455 | return false; | 455 | return false; |
456 | } | 456 | } |
457 | 457 | ||
458 | void SFCave :: moveFlyGameLandscape() | 458 | void SFCave :: moveFlyGameLandscape() |
459 | { | 459 | { |
460 | offset++; | 460 | offset++; |
461 | 461 | ||
462 | if ( offset >= segSize ) | 462 | if ( offset >= segSize ) |
463 | { | 463 | { |
464 | offset = 0; | 464 | offset = 0; |
465 | for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) | 465 | for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) |
466 | mapBottom[i] = mapBottom[i+speed]; | 466 | mapBottom[i] = mapBottom[i+speed]; |
467 | 467 | ||
468 | for ( int i = speed ; i > 0 ; --i ) | 468 | for ( int i = speed ; i > 0 ; --i ) |
469 | setFlyPoint( MAPSIZE-i ); | 469 | setFlyPoint( MAPSIZE-i ); |
470 | } | 470 | } |
471 | } | 471 | } |
472 | 472 | ||
473 | void SFCave :: setFlyPoint( int point ) | 473 | void SFCave :: setFlyPoint( int point ) |
474 | { | 474 | { |
475 | static int fly_difficulty_levels[] = { 5, 10, 15 }; | 475 | static int fly_difficulty_levels[] = { 5, 10, 15 }; |
476 | if ( nextInt(100) >= 75 ) | 476 | if ( nextInt(100) >= 75 ) |
477 | dir *= -1; | 477 | dir *= -1; |
478 | 478 | ||
479 | int prevPoint = mapBottom[point-1]; | 479 | int prevPoint = mapBottom[point-1]; |
480 | 480 | ||
481 | int nextPoint = prevPoint + (dir * nextInt( fly_difficulty_levels[currentGameDifficulty] ) ); | 481 | int nextPoint = prevPoint + (dir * nextInt( fly_difficulty_levels[currentGameDifficulty] ) ); |
482 | 482 | ||
483 | if ( nextPoint > sHeight ) | 483 | if ( nextPoint > sHeight ) |
484 | { | 484 | { |
485 | nextPoint = sHeight; | 485 | nextPoint = sHeight; |
486 | dir *= -1; | 486 | dir *= -1; |
487 | } | 487 | } |
488 | else if ( nextPoint < maxHeight ) | 488 | else if ( nextPoint < maxHeight ) |
489 | { | 489 | { |
490 | nextPoint = maxHeight; | 490 | nextPoint = maxHeight; |
491 | dir *= 1; | 491 | dir *= 1; |
492 | } | 492 | } |
493 | 493 | ||
494 | mapBottom[point] = nextPoint; | 494 | mapBottom[point] = nextPoint; |
495 | } | 495 | } |
496 | 496 | ||
497 | bool SFCave :: checkCollision() | 497 | bool SFCave :: checkCollision() |
498 | { | 498 | { |
499 | if ( (user.y() + user.width()) >= mapBottom[11] || user.y() <= mapTop[11] ) | 499 | if ( (user.y() + user.width()) >= mapBottom[11] || user.y() <= mapTop[11] ) |
500 | return true; | 500 | return true; |
501 | 501 | ||
502 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | 502 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) |
503 | { | 503 | { |
504 | if ( blocks[i].y() != -1 ) | 504 | if ( blocks[i].y() != -1 ) |
505 | { | 505 | { |
506 | if ( blocks[i].intersects( user ) ) | 506 | if ( blocks[i].intersects( user ) ) |
507 | return true; | 507 | return true; |
508 | } | 508 | } |
509 | } | 509 | } |
510 | return false; | 510 | return false; |
511 | } | 511 | } |
512 | 512 | ||
513 | void SFCave :: moveLandscape() | 513 | void SFCave :: moveLandscape() |
514 | { | 514 | { |
515 | offset++; | 515 | offset++; |
516 | 516 | ||
517 | if ( offset >= segSize ) | 517 | if ( offset >= segSize ) |
518 | { | 518 | { |
519 | offset = 0; | 519 | offset = 0; |
520 | for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) | 520 | for ( int i = 0 ; i < MAPSIZE-speed ; ++i ) |
521 | { | 521 | { |
522 | mapTop[i] = mapTop[i+speed]; | 522 | mapTop[i] = mapTop[i+speed]; |
523 | mapBottom[i] = mapBottom[i+speed]; | 523 | mapBottom[i] = mapBottom[i+speed]; |
524 | } | 524 | } |
525 | 525 | ||
526 | for ( int i = speed ; i > 0 ; --i ) | 526 | for ( int i = speed ; i > 0 ; --i ) |
527 | setPoint( MAPSIZE-i ); | 527 | setPoint( MAPSIZE-i ); |
528 | } | 528 | } |
529 | 529 | ||
530 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | 530 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) |
531 | { | 531 | { |
532 | if ( blocks[i].y() != -1 ) | 532 | if ( blocks[i].y() != -1 ) |
533 | { | 533 | { |
534 | blocks[i].moveBy( -speed, 0 ); | 534 | blocks[i].moveBy( -speed, 0 ); |
535 | if ( blocks[i].x() + blocks[i].width() < 0 ) | 535 | if ( blocks[i].x() + blocks[i].width() < 0 ) |
536 | blocks[i].setY( -1 ); | 536 | blocks[i].setY( -1 ); |
537 | } | 537 | } |
538 | } | 538 | } |
539 | } | 539 | } |
540 | 540 | ||
541 | void SFCave :: addBlock() | 541 | void SFCave :: addBlock() |
542 | { | 542 | { |
543 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | 543 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) |
544 | { | 544 | { |
545 | if ( blocks[i].y() == -1 ) | 545 | if ( blocks[i].y() == -1 ) |
546 | { | 546 | { |
547 | int x = sWidth; | 547 | int x = sWidth; |
548 | 548 | ||
549 | int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight)); | 549 | int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight)); |
550 | 550 | ||
551 | blocks[i].setRect( x, y, blockWidth, blockHeight ); | 551 | blocks[i].setRect( x, y, blockWidth, blockHeight ); |
552 | 552 | ||
553 | break; | 553 | break; |
554 | } | 554 | } |
555 | } | 555 | } |
556 | } | 556 | } |
557 | 557 | ||
558 | void SFCave :: addGate() | 558 | void SFCave :: addGate() |
559 | { | 559 | { |
560 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | 560 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) |
561 | { | 561 | { |
562 | if ( blocks[i].y() == -1 ) | 562 | if ( blocks[i].y() == -1 ) |
563 | { | 563 | { |
564 | int x1 = sWidth; | 564 | int x1 = sWidth; |
565 | int y1 = mapTop[50]; | 565 | int y1 = mapTop[50]; |
566 | int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight); | 566 | int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight); |
567 | 567 | ||
568 | // See if height between last gate and this one is too big | 568 | // See if height between last gate and this one is too big |
569 | if ( b1Height - 100 > lastGateBottomY ) | 569 | if ( b1Height - 100 > lastGateBottomY ) |
570 | b1Height -= 25; | 570 | b1Height -= 25; |
571 | else if ( b1Height + 100 < lastGateBottomY ) | 571 | else if ( b1Height + 100 < lastGateBottomY ) |
572 | b1Height += 25; | 572 | b1Height += 25; |
573 | lastGateBottomY = b1Height; | 573 | lastGateBottomY = b1Height; |
574 | 574 | ||
575 | 575 | ||
576 | int x2 = sWidth; | 576 | int x2 = sWidth; |
577 | int y2 = y1 + b1Height + gapHeight; | 577 | int y2 = y1 + b1Height + gapHeight; |
578 | int b2Height = mapBottom[50] - y2; | 578 | int b2Height = mapBottom[50] - y2; |
579 | 579 | ||
580 | 580 | ||
581 | blocks[i].setRect( x1, y1, blockWidth, b1Height ); | 581 | blocks[i].setRect( x1, y1, blockWidth, b1Height ); |
582 | blocks[i+1].setRect( x2, y2, blockWidth, b2Height ); | 582 | blocks[i+1].setRect( x2, y2, blockWidth, b2Height ); |
583 | 583 | ||
584 | break; | 584 | break; |
585 | } | 585 | } |
586 | } | 586 | } |
587 | } | 587 | } |
588 | 588 | ||
589 | void SFCave :: setPoint( int point ) | 589 | void SFCave :: setPoint( int point ) |
590 | { | 590 | { |
591 | if ( nextInt(100) >= 80 ) | 591 | if ( nextInt(100) >= 80 ) |
592 | dir *= -1; | 592 | dir *= -1; |
593 | 593 | ||
594 | mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) ); | 594 | mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) ); |
595 | if ( mapTop[point] < 0 ) | 595 | if ( mapTop[point] < 0 ) |
596 | { | 596 | { |
597 | mapTop[point] = 0; | 597 | mapTop[point] = 0; |
598 | dir *= -1; | 598 | dir *= -1; |
599 | } | 599 | } |
600 | else if ( mapTop[point] >= maxHeight ) | 600 | else if ( mapTop[point] >= maxHeight ) |
601 | { | 601 | { |
602 | mapTop[point] = maxHeight; | 602 | mapTop[point] = maxHeight; |
603 | dir *= -1; | 603 | dir *= -1; |
604 | } | 604 | } |
605 | 605 | ||
606 | // mapBottom[point] = sHeight - (maxHeight - mapBottom[point]); | 606 | // mapBottom[point] = sHeight - (maxHeight - mapBottom[point]); |
607 | mapBottom[point] = sHeight - (maxHeight - mapTop[point]); | 607 | mapBottom[point] = sHeight - (maxHeight - mapTop[point]); |
608 | } | 608 | } |
609 | 609 | ||
610 | void SFCave :: drawBoss() | 610 | void SFCave :: drawBoss() |
611 | { | 611 | { |
612 | offscreen->fill( Qt::black ); | 612 | offscreen->fill( Qt::black ); |
613 | 613 | ||
614 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); | 614 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); |
615 | } | 615 | } |
616 | 616 | ||
617 | void SFCave :: draw() | 617 | void SFCave :: draw() |
618 | { | 618 | { |
619 | //printf( "Paint\n" ); | 619 | //printf( "Paint\n" ); |
620 | offscreen->fill( Qt::black ); | 620 | offscreen->fill( Qt::black ); |
621 | 621 | ||
622 | QPainter p( offscreen ); | 622 | QPainter p( offscreen ); |
623 | QFontMetrics fm = p.fontMetrics(); | 623 | QFontMetrics fm = p.fontMetrics(); |
624 | p.setPen( Qt::white ); | 624 | p.setPen( Qt::white ); |
625 | 625 | ||
626 | for ( int i = 0 ; i < MAPSIZE -3; ++i ) | 626 | for ( int i = 0 ; i < MAPSIZE -3; ++i ) |
627 | { | 627 | { |
628 | // Only display top landscape if not running FLY_GAME | 628 | // Only display top landscape if not running FLY_GAME |
629 | if ( CURRENT_GAME_TYPE != FLY_GAME ) | 629 | if ( CURRENT_GAME_TYPE != FLY_GAME ) |
630 | p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] ); | 630 | p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] ); |
631 | 631 | ||
632 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] ); | 632 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] ); |
633 | 633 | ||
634 | if ( CURRENT_GAME_TYPE == FLY_GAME && showScoreZones ) | 634 | if ( CURRENT_GAME_TYPE == FLY_GAME && showScoreZones ) |
635 | { | 635 | { |
636 | p.setPen( Qt::red ); | 636 | p.setPen( Qt::red ); |
637 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-10, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-10 ); | 637 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-10, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-10 ); |
638 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-20, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-20 ); | 638 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-20, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-20 ); |
639 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-30, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-30 ); | 639 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-30, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-30 ); |
640 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-40, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-40 ); | 640 | p.drawLine( (i*segSize) - (offset*speed), mapBottom[i]-40, ((i+1)*segSize)-(offset*speed), mapBottom[i+1]-40 ); |
641 | p.setPen( Qt::white ); | 641 | p.setPen( Qt::white ); |
642 | } | 642 | } |
643 | } | 643 | } |
644 | 644 | ||
645 | // Uncomment this to show user segment (usful for checking collision boundary with landscape | 645 | // Uncomment this to show user segment (usful for checking collision boundary with landscape |
646 | // p.setPen( Qt::red ); | 646 | // p.setPen( Qt::red ); |
647 | // p.drawLine( (11*segSize) - (offset*speed), 0, ((11)*segSize)-(offset*speed), sHeight ); | 647 | // p.drawLine( (11*segSize) - (offset*speed), 0, ((11)*segSize)-(offset*speed), sHeight ); |
648 | // p.setPen( Qt::white ); | 648 | // p.setPen( Qt::white ); |
649 | 649 | ||
650 | // Draw user | 650 | // Draw user |
651 | p.drawRect( user ); | 651 | p.drawRect( user ); |
652 | 652 | ||
653 | // Draw trails | 653 | // Draw trails |
654 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) | 654 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) |
655 | if ( trail[i].x() >= 0 ) | 655 | if ( trail[i].x() >= 0 ) |
656 | p.drawRect( trail[i].x(), trail[i].y(), 2, 2 ); | 656 | p.drawRect( trail[i].x(), trail[i].y(), 2, 2 ); |
657 | 657 | ||
658 | // Draw blocks | 658 | // Draw blocks |
659 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | 659 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) |
660 | if ( blocks[i].y() != -1 ) | 660 | if ( blocks[i].y() != -1 ) |
661 | { | 661 | { |
662 | p.fillRect( blocks[i], Qt::black ); | 662 | p.fillRect( blocks[i], Qt::black ); |
663 | p.drawRect( blocks[i] ); | 663 | p.drawRect( blocks[i] ); |
664 | } | 664 | } |
665 | 665 | ||
666 | // draw score | 666 | // draw score |
667 | QString s; | 667 | QString s; |
668 | s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] ); | 668 | s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] ); |
669 | p.drawText( 5, 10, s ); | 669 | p.drawText( 5, 10, s ); |
670 | 670 | ||
671 | 671 | ||
672 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) | 672 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) |
673 | { | 673 | { |
674 | // add next crash line | 674 | // add next crash line |
675 | 675 | ||
676 | if ( crashLineLength != -1 ) | 676 | if ( crashLineLength != -1 ) |
677 | { | 677 | { |
678 | for ( int i = 0 ; i < 36 ; ++i ) | 678 | for ( int i = 0 ; i < 36 ; ++i ) |
679 | { | 679 | { |
680 | int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) ); | 680 | int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) ); |
681 | int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y ); | 681 | int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y ); |
682 | } | 682 | } |
683 | } | 683 | } |
684 | 684 | ||
685 | if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) ) | 685 | if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) ) |
686 | state = STATE_CRASHED; | 686 | state = STATE_CRASHED; |
687 | 687 | ||
688 | if ( state == STATE_CRASHED ) | 688 | if ( state == STATE_CRASHED ) |
689 | { | 689 | { |
690 | QString text = "Press up or down to start"; | 690 | QString text = "Press up or down to start"; |
691 | p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text ); | 691 | p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text ); |
692 | 692 | ||
693 | text = "Press OK for menu"; | 693 | text = "Press OK for menu"; |
694 | p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); | 694 | p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); |
695 | 695 | ||
696 | text = "Press r to replay"; | 696 | text = "Press r to replay"; |
697 | p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); | 697 | p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); |
698 | 698 | ||
699 | text = "Press s to save the replay"; | 699 | text = "Press s to save the replay"; |
700 | p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text ); | 700 | p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text ); |
701 | 701 | ||
702 | text = "Press r to load a saved replay"; | 702 | text = "Press r to load a saved replay"; |
703 | p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); | 703 | p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); |
704 | } | 704 | } |
705 | else | 705 | else |
706 | crashLineLength ++; | 706 | crashLineLength ++; |
707 | } | 707 | } |
708 | 708 | ||
709 | p.end(); | 709 | p.end(); |
710 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); | 710 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); |
711 | //printf( "endpaint\n" ); | 711 | //printf( "endpaint\n" ); |
712 | } | 712 | } |
713 | 713 | ||
714 | void SFCave :: handleKeys() | 714 | void SFCave :: handleKeys() |
715 | { | 715 | { |
716 | // Find enpty trail and move others | 716 | // Find enpty trail and move others |
717 | bool done = false; | 717 | bool done = false; |
718 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) | 718 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) |
719 | { | 719 | { |
720 | if ( trail[i].x() < 0 ) | 720 | if ( trail[i].x() < 0 ) |
721 | { | 721 | { |
722 | if ( !done ) | 722 | if ( !done ) |
723 | { | 723 | { |
724 | trail[i].setX( user.x() - 5 ); | 724 | trail[i].setX( user.x() - 5 ); |
725 | trail[i].setY( user.y() ); | 725 | trail[i].setY( user.y() ); |
726 | done = true; | 726 | done = true; |
727 | } | 727 | } |
728 | } | 728 | } |
729 | else | 729 | else |
730 | { | 730 | { |
731 | trail[i].setX( trail[i].x() - (2) ); | 731 | trail[i].setX( trail[i].x() - (2) ); |
732 | } | 732 | } |
733 | } | 733 | } |
734 | 734 | ||
735 | if ( speed <= 3 ) | 735 | if ( speed <= 3 ) |
736 | { | 736 | { |
737 | if ( press ) | 737 | if ( press ) |
738 | thrust -= thrustUp; | 738 | thrust -= thrustUp; |
739 | else | 739 | else |
740 | thrust += noThrust; | 740 | thrust += noThrust; |
741 | 741 | ||
742 | if ( thrust > maxDownThrust ) | 742 | if ( thrust > maxDownThrust ) |
743 | thrust = maxDownThrust; | 743 | thrust = maxDownThrust; |
744 | else if ( thrust < maxUpThrust ) | 744 | else if ( thrust < maxUpThrust ) |
745 | thrust = maxUpThrust; | 745 | thrust = maxUpThrust; |
746 | } | 746 | } |
747 | else | 747 | else |
748 | { | 748 | { |
749 | if ( press ) | 749 | if ( press ) |
750 | thrust -= 0.5; | 750 | thrust -= 0.5; |
751 | else | 751 | else |
752 | thrust += 0.8; | 752 | thrust += 0.8; |
753 | 753 | ||
754 | if ( thrust > 5.0 ) | 754 | if ( thrust > 5.0 ) |
755 | thrust = 5.0; | 755 | thrust = 5.0; |
756 | else if ( thrust < -3.5 ) | 756 | else if ( thrust < -3.5 ) |
757 | thrust = -3.5; | 757 | thrust = -3.5; |
758 | } | 758 | } |
759 | user.moveBy( 0, (int)thrust ); | 759 | user.moveBy( 0, (int)thrust ); |
760 | } | 760 | } |
761 | 761 | ||
762 | void SFCave :: keyPressEvent( QKeyEvent *e ) | 762 | void SFCave :: keyPressEvent( QKeyEvent *e ) |
763 | { | 763 | { |
764 | if ( state == STATE_MENU ) | 764 | if ( state == STATE_MENU ) |
765 | { | 765 | { |
766 | switch( e->key() ) | 766 | switch( e->key() ) |
767 | { | 767 | { |
768 | case Qt::Key_Down: | 768 | case Qt::Key_Down: |
769 | currentMenuOption[currentMenuNr] ++; | 769 | currentMenuOption[currentMenuNr] ++; |
770 | if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" ) | 770 | if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" ) |
771 | currentMenuOption[currentMenuNr] = 0; | 771 | currentMenuOption[currentMenuNr] = 0; |
772 | break; | 772 | break; |
773 | case Qt::Key_Up: | 773 | case Qt::Key_Up: |
774 | currentMenuOption[currentMenuNr] --; | 774 | currentMenuOption[currentMenuNr] --; |
775 | if ( currentMenuOption[currentMenuNr] < 0 ) | 775 | if ( currentMenuOption[currentMenuNr] < 0 ) |
776 | currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1; | 776 | currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1; |
777 | break; | 777 | break; |
778 | 778 | ||
779 | case Qt::Key_Left: | 779 | case Qt::Key_Left: |
780 | if ( currentMenuNr == MENU_OPTIONS_MENU ) | 780 | if ( currentMenuNr == MENU_OPTIONS_MENU ) |
781 | { | 781 | { |
782 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) | 782 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) |
783 | { | 783 | { |
784 | currentGameType --; | 784 | currentGameType --; |
785 | if ( currentGameType < 0 ) | 785 | if ( currentGameType < 0 ) |
786 | currentGameType = NR_GAME_TYPES - 1; | 786 | currentGameType = NR_GAME_TYPES - 1; |
787 | } | 787 | } |
788 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) | 788 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) |
789 | { | 789 | { |
790 | currentGameDifficulty --; | 790 | currentGameDifficulty --; |
791 | if ( currentGameDifficulty < 0 ) | 791 | if ( currentGameDifficulty < 0 ) |
792 | currentGameDifficulty = NR_GAME_DIFFICULTIES - 1; | 792 | currentGameDifficulty = NR_GAME_DIFFICULTIES - 1; |
793 | } | 793 | } |
794 | } | 794 | } |
795 | break; | 795 | break; |
796 | 796 | ||
797 | case Qt::Key_Right: | 797 | case Qt::Key_Right: |
798 | if ( currentMenuNr == MENU_OPTIONS_MENU ) | 798 | if ( currentMenuNr == MENU_OPTIONS_MENU ) |
799 | { | 799 | { |
800 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) | 800 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) |
801 | { | 801 | { |
802 | currentGameType ++; | 802 | currentGameType ++; |
803 | if ( currentGameType == NR_GAME_TYPES ) | 803 | if ( currentGameType == NR_GAME_TYPES ) |
804 | currentGameType = 0; | 804 | currentGameType = 0; |
805 | } | 805 | } |
806 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) | 806 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) |
807 | { | 807 | { |
808 | currentGameDifficulty ++; | 808 | currentGameDifficulty ++; |
809 | if ( currentGameDifficulty == NR_GAME_DIFFICULTIES ) | 809 | if ( currentGameDifficulty == NR_GAME_DIFFICULTIES ) |
810 | currentGameDifficulty = 0; | 810 | currentGameDifficulty = 0; |
811 | } | 811 | } |
812 | } | 812 | } |
813 | break; | 813 | break; |
814 | 814 | ||
815 | case Qt::Key_Space: | 815 | case Qt::Key_Space: |
816 | case Qt::Key_Return: | 816 | case Qt::Key_Return: |
817 | case Qt::Key_Enter: | 817 | case Qt::Key_Enter: |
818 | dealWithMenuSelection(); | 818 | dealWithMenuSelection(); |
819 | break; | 819 | break; |
820 | } | 820 | } |
821 | } | 821 | } |
822 | else | 822 | else |
823 | { | 823 | { |
824 | switch( e->key() ) | 824 | switch( e->key() ) |
825 | { | 825 | { |
826 | case Qt::Key_Up: | 826 | case Qt::Key_Up: |
827 | case Qt::Key_F9: | 827 | case Qt::Key_F9: |
828 | case Qt::Key_Space: | 828 | case Qt::Key_Space: |
829 | if ( !press ) | 829 | if ( !replay && !press ) |
830 | { | 830 | { |
831 | press = true; | 831 | press = true; |
832 | replayList.append( new int( nrFrames ) ); | 832 | replayList.append( new int( nrFrames ) ); |
833 | } | 833 | } |
834 | break; | 834 | break; |
835 | case Qt::Key_M: | 835 | case Qt::Key_M: |
836 | case Qt::Key_Return: | 836 | case Qt::Key_Return: |
837 | case Qt::Key_Enter: | 837 | case Qt::Key_Enter: |
838 | if ( state == STATE_CRASHED ) | 838 | if ( state == STATE_CRASHED ) |
839 | state = STATE_MENU; | 839 | state = STATE_MENU; |
840 | break; | 840 | break; |
841 | 841 | ||
842 | case Qt::Key_Z: | 842 | case Qt::Key_Z: |
843 | showScoreZones = !showScoreZones; | 843 | showScoreZones = !showScoreZones; |
844 | break; | 844 | break; |
845 | 845 | ||
846 | default: | 846 | default: |
847 | e->ignore(); | 847 | e->ignore(); |
848 | break; | 848 | break; |
849 | } | 849 | } |
850 | } | 850 | } |
851 | } | 851 | } |
852 | 852 | ||
853 | void SFCave :: keyReleaseEvent( QKeyEvent *e ) | 853 | void SFCave :: keyReleaseEvent( QKeyEvent *e ) |
854 | { | 854 | { |
855 | if ( state == STATE_MENU ) | 855 | if ( state == STATE_MENU ) |
856 | { | 856 | { |
857 | } | 857 | } |
858 | else | 858 | else |
859 | { | 859 | { |
860 | switch( e->key() ) | 860 | switch( e->key() ) |
861 | { | 861 | { |
862 | case Qt::Key_F9: | 862 | case Qt::Key_F9: |
863 | case Qt::Key_Space: | 863 | case Qt::Key_Space: |
864 | case Qt::Key_Up: | 864 | case Qt::Key_Up: |
865 | if ( press ) | 865 | if ( !replay && press ) |
866 | { | 866 | { |
867 | press = false; | 867 | press = false; |
868 | 868 | ||
869 | replayList.append( new int( nrFrames ) ); | 869 | replayList.append( new int( nrFrames ) ); |
870 | } | 870 | } |
871 | break; | 871 | break; |
872 | 872 | ||
873 | case Qt::Key_R: | 873 | case Qt::Key_R: |
874 | if ( state == STATE_CRASHED ) | 874 | if ( state == STATE_CRASHED ) |
875 | { | 875 | { |
876 | state = STATE_REPLAY; | 876 | state = STATE_REPLAY; |
877 | } | 877 | } |
878 | break; | 878 | break; |
879 | 879 | ||
880 | case Qt::Key_Down: | 880 | case Qt::Key_Down: |
881 | if ( state == STATE_CRASHED ) | 881 | if ( state == STATE_CRASHED ) |
882 | state = STATE_NEWGAME; | 882 | state = STATE_NEWGAME; |
883 | break; | 883 | break; |
884 | 884 | ||
885 | case Qt::Key_S: | 885 | case Qt::Key_S: |
886 | saveReplay(); | 886 | if ( state == STATE_CRASHED ) |
887 | saveReplay(); | ||
887 | break; | 888 | break; |
888 | 889 | ||
889 | case Qt::Key_L: | 890 | case Qt::Key_L: |
890 | loadReplay(); | 891 | if ( state == STATE_CRASHED ) |
892 | loadReplay(); | ||
891 | break; | 893 | break; |
892 | default: | 894 | default: |
893 | e->ignore(); | 895 | e->ignore(); |
894 | break; | 896 | break; |
895 | } | 897 | } |
896 | } | 898 | } |
897 | 899 | ||
898 | } | 900 | } |
899 | 901 | ||
900 | void SFCave :: displayMenu() | 902 | void SFCave :: displayMenu() |
901 | { | 903 | { |
902 | offscreen->fill( Qt::black ); | 904 | offscreen->fill( Qt::black ); |
903 | 905 | ||
904 | QPainter p( offscreen ); | 906 | QPainter p( offscreen ); |
905 | p.setPen( Qt::white ); | 907 | p.setPen( Qt::white ); |
906 | 908 | ||
907 | QFont f( "Helvetica", 16 ); | 909 | QFont f( "Helvetica", 16 ); |
908 | p.setFont( f ); | 910 | p.setFont( f ); |
909 | 911 | ||
910 | QFontMetrics fm = p.fontMetrics(); | 912 | QFontMetrics fm = p.fontMetrics(); |
911 | 913 | ||
912 | QString text = "SFCave"; | 914 | QString text = "SFCave"; |
913 | p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text ); | 915 | p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text ); |
914 | 916 | ||
915 | text = "Written by Andy Qua"; | 917 | text = "Written by Andy Qua"; |
916 | p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text ); | 918 | p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text ); |
917 | 919 | ||
918 | // Draw options | 920 | // Draw options |
919 | int pos = 170; | 921 | int pos = 170; |
920 | for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 ) | 922 | for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 ) |
921 | { | 923 | { |
922 | if ( currentMenuOption[currentMenuNr] == i ) | 924 | if ( currentMenuOption[currentMenuNr] == i ) |
923 | p.setPen( Qt::yellow ); | 925 | p.setPen( Qt::yellow ); |
924 | else | 926 | else |
925 | p.setPen( Qt::white ); | 927 | p.setPen( Qt::white ); |
926 | 928 | ||
927 | QString text; | 929 | QString text; |
928 | if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 ) | 930 | if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 ) |
929 | { | 931 | { |
930 | QString val; | 932 | QString val; |
931 | if ( i == MENU_GAME_TYPE ) | 933 | if ( i == MENU_GAME_TYPE ) |
932 | val = gameTypes[currentGameType]; | 934 | val = gameTypes[currentGameType]; |
933 | else | 935 | else |
934 | val = dificultyOption[currentGameDifficulty]; | 936 | val = dificultyOption[currentGameDifficulty]; |
935 | 937 | ||
936 | text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val ); | 938 | text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val ); |
937 | } | 939 | } |
938 | else | 940 | else |
939 | text = menuOptions[currentMenuNr][i]; | 941 | text = menuOptions[currentMenuNr][i]; |
940 | 942 | ||
941 | p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text ); | 943 | p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text ); |
942 | } | 944 | } |
943 | 945 | ||
944 | p.end(); | 946 | p.end(); |
945 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); | 947 | bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); |
946 | } | 948 | } |
947 | 949 | ||
948 | void SFCave :: dealWithMenuSelection() | 950 | void SFCave :: dealWithMenuSelection() |
949 | { | 951 | { |
950 | switch( currentMenuNr ) | 952 | switch( currentMenuNr ) |
951 | { | 953 | { |
952 | case MENU_MAIN_MENU: | 954 | case MENU_MAIN_MENU: |
953 | { | 955 | { |
954 | switch( currentMenuOption[currentMenuNr] ) | 956 | switch( currentMenuOption[currentMenuNr] ) |
955 | { | 957 | { |
956 | case MENU_START_GAME: | 958 | case MENU_START_GAME: |
957 | state = STATE_NEWGAME; | 959 | state = STATE_NEWGAME; |
958 | break; | 960 | break; |
959 | 961 | ||
960 | case MENU_OPTIONS: | 962 | case MENU_OPTIONS: |
961 | currentMenuNr = MENU_OPTIONS_MENU; | 963 | currentMenuNr = MENU_OPTIONS_MENU; |
962 | currentMenuOption[currentMenuNr] = 0; | 964 | currentMenuOption[currentMenuNr] = 0; |
963 | break; | 965 | break; |
964 | 966 | ||
965 | case MENU_HELP: | 967 | case MENU_HELP: |
966 | { | 968 | { |
967 | // Display Help Menu | 969 | // Display Help Menu |
968 | HelpWindow *dlg = new HelpWindow( this ); | 970 | HelpWindow *dlg = new HelpWindow( this ); |
969 | dlg->exec(); | 971 | dlg->exec(); |
970 | delete dlg; | 972 | delete dlg; |
971 | break; | 973 | break; |
972 | } | 974 | } |
973 | 975 | ||
974 | case MENU_QUIT: | 976 | case MENU_QUIT: |
975 | QApplication::exit(); | 977 | QApplication::exit(); |
976 | break; | 978 | break; |
977 | } | 979 | } |
978 | 980 | ||
979 | break; | 981 | break; |
980 | } | 982 | } |
981 | 983 | ||
982 | case MENU_OPTIONS_MENU: | 984 | case MENU_OPTIONS_MENU: |
983 | { | 985 | { |
984 | switch( currentMenuOption[currentMenuNr] ) | 986 | switch( currentMenuOption[currentMenuNr] ) |
985 | { | 987 | { |
986 | case MENU_GAME_TYPE: | 988 | case MENU_GAME_TYPE: |
987 | break; | 989 | break; |
988 | 990 | ||
989 | case MENU_GAME_DIFFICULTY: | 991 | case MENU_GAME_DIFFICULTY: |
990 | break; | 992 | break; |
991 | 993 | ||
992 | case MENU_CLEAR_HIGHSCORES: | 994 | case MENU_CLEAR_HIGHSCORES: |
993 | for ( int i = 0 ; i < 3 ; ++i ) | 995 | for ( int i = 0 ; i < 3 ; ++i ) |
994 | highestScore[currentGameType][i] = 0; | 996 | highestScore[currentGameType][i] = 0; |
995 | break; | 997 | break; |
996 | 998 | ||
997 | case MENU_BACK: | 999 | case MENU_BACK: |
998 | currentMenuNr = MENU_MAIN_MENU; | 1000 | currentMenuNr = MENU_MAIN_MENU; |
999 | 1001 | ||
1000 | #ifdef QWS | 1002 | #ifdef QWS |
1001 | Config cfg( "sfcave" ); | 1003 | Config cfg( "sfcave" ); |
1002 | cfg.setGroup( "settings" ); | 1004 | cfg.setGroup( "settings" ); |
1003 | cfg.writeEntry( "difficulty", currentGameDifficulty ); | 1005 | cfg.writeEntry( "difficulty", currentGameDifficulty ); |
1004 | cfg.writeEntry( "gameType", currentGameType ); | 1006 | cfg.writeEntry( "gameType", currentGameType ); |
1005 | #endif | 1007 | #endif |
1006 | break; | 1008 | break; |
1007 | } | 1009 | } |
1008 | 1010 | ||
1009 | break; | 1011 | break; |
1010 | } | 1012 | } |
1011 | } | 1013 | } |
1012 | } | 1014 | } |
1013 | 1015 | ||
1014 | void SFCave :: saveScore() | 1016 | void SFCave :: saveScore() |
1015 | { | 1017 | { |
1016 | #ifdef QWS | 1018 | #ifdef QWS |
1017 | Config cfg( "sfcave" ); | 1019 | Config cfg( "sfcave" ); |
1018 | cfg.setGroup( "settings" ); | 1020 | cfg.setGroup( "settings" ); |
1019 | QString key = "highScore_"; | 1021 | QString key = "highScore_"; |
1020 | 1022 | ||
1021 | cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); | 1023 | cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); |
1022 | key += CURRENT_GAME_TYPE; | 1024 | key += CURRENT_GAME_TYPE; |
1023 | cfg.writeEntry( key, highestScore[currentGameType] ); | 1025 | cfg.writeEntry( key, highestScore[currentGameType] ); |
1024 | #endif | 1026 | #endif |
1025 | } | 1027 | } |
1026 | 1028 | ||
1027 | void SFCave :: saveReplay() | 1029 | void SFCave :: saveReplay() |
1028 | { | 1030 | { |
1029 | FILE *out; | 1031 | FILE *out; |
1030 | out = fopen( (const char *)replayFile, "w" ); | 1032 | out = fopen( (const char *)replayFile, "w" ); |
1031 | if ( !out ) | 1033 | if ( !out ) |
1032 | { | 1034 | { |
1033 | printf( "Couldn't write to /home/root/sfcave.replay\n" ); | 1035 | printf( "Couldn't write to /home/root/sfcave.replay\n" ); |
1034 | return; | 1036 | return; |
1035 | } | 1037 | } |
1036 | 1038 | ||
1037 | // Build up string of values | 1039 | // Build up string of values |
1038 | // Format is:: <landscape seed> <framenr> <framenr>....... | 1040 | // Format is:: <landscape seed> <framenr> <framenr>....... |
1039 | QString val; | 1041 | QString val; |
1040 | val.sprintf( "%d ", currentSeed ); | 1042 | val.sprintf( "%d ", currentSeed ); |
1041 | 1043 | ||
1042 | QListIterator<int> it( replayList ); | 1044 | QListIterator<int> it( replayList ); |
1043 | while( it.current() ) | 1045 | while( it.current() ) |
1044 | { | 1046 | { |
1045 | QString tmp; | 1047 | QString tmp; |
1046 | tmp.sprintf( "%d ", (*it.current()) ); | 1048 | tmp.sprintf( "%d ", (*it.current()) ); |
1047 | val += tmp; | 1049 | val += tmp; |
1048 | 1050 | ||
1049 | ++it; | 1051 | ++it; |
1050 | } | 1052 | } |
1051 | val += "\n"; | 1053 | val += "\n"; |
1052 | 1054 | ||
1053 | QString line; | 1055 | QString line; |
1054 | line.sprintf( "%d\n", val.length() ); | 1056 | line.sprintf( "%d\n", val.length() ); |
1055 | fwrite( (const char *)line, 1, line.length(), out ); | 1057 | fwrite( (const char *)line, 1, line.length(), out ); |
1056 | fwrite( (const char *)val, 1, val.length(), out ); | 1058 | fwrite( (const char *)val, 1, val.length(), out ); |
1057 | 1059 | ||
1058 | fclose( out ); | 1060 | fclose( out ); |
1059 | 1061 | ||
1060 | printf( "Replay saved to %s\n", (const char *)replayFile ); | 1062 | printf( "Replay saved to %s\n", (const char *)replayFile ); |
1061 | 1063 | ||
1062 | } | 1064 | } |
1063 | 1065 | ||
1064 | void SFCave :: loadReplay() | 1066 | void SFCave :: loadReplay() |
1065 | { | 1067 | { |
1066 | FILE *in = fopen( (const char *)replayFile, "r" ); | 1068 | FILE *in = fopen( (const char *)replayFile, "r" ); |
1067 | 1069 | ||
1070 | if ( in == 0 ) | ||
1071 | { | ||
1072 | printf( "Couldn't load replay file!\n" ); | ||
1073 | return; | ||
1074 | } | ||
1068 | // Read size of next line | 1075 | // Read size of next line |
1069 | char line[10+1]; | 1076 | char line[10+1]; |
1070 | fgets( line, 10, in ); | 1077 | fgets( line, 10, in ); |
1071 | 1078 | ||
1072 | int length = -1; | 1079 | int length = -1; |
1073 | sscanf( line, "%d", &length ); | 1080 | sscanf( line, "%d", &length ); |
1074 | char *data = new char[length+1]; | 1081 | char *data = new char[length+1]; |
1075 | 1082 | ||
1076 | fread( data, 1, length, in ); | 1083 | fread( data, 1, length, in ); |
1077 | 1084 | ||
1078 | QString sep = " "; | 1085 | QString sep = " "; |
1079 | QStringList list = QStringList::split( sep, QString( data ) ); | 1086 | QStringList list = QStringList::split( sep, QString( data ) ); |
1080 | 1087 | ||
1081 | // print it out | 1088 | // print it out |
1082 | QStringList::Iterator it = list.begin(); | 1089 | QStringList::Iterator it = list.begin(); |
1083 | currentSeed = (*it).toInt(); | 1090 | currentSeed = (*it).toInt(); |
1084 | ++it; | 1091 | ++it; |
1085 | 1092 | ||
1086 | replayList.clear(); | 1093 | replayList.clear(); |
1087 | for ( ; it != list.end(); ++it ) | 1094 | for ( ; it != list.end(); ++it ) |
1088 | { | 1095 | { |
1089 | int v = (*it).toInt(); | 1096 | int v = (*it).toInt(); |
1090 | replayList.append( new int( v ) ); | 1097 | replayList.append( new int( v ) ); |
1091 | } | 1098 | } |
1092 | 1099 | ||
1093 | delete data; | 1100 | delete data; |
1094 | 1101 | ||
1095 | fclose( in ); | 1102 | fclose( in ); |
1103 | |||
1104 | printf( "Replay loaded from %s\n", (const char *)replayFile ); | ||
1096 | } \ No newline at end of file | 1105 | } \ No newline at end of file |