Diffstat (limited to 'noncore/games/sfcave/sfcave.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave/sfcave.cpp | 79 |
1 files changed, 63 insertions, 16 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp index 788606f..b5bc736 100644 --- a/noncore/games/sfcave/sfcave.cpp +++ b/noncore/games/sfcave/sfcave.cpp | |||
@@ -1,9 +1,10 @@ | |||
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 | 5 | ||
5 | #ifdef QWS | 6 | #ifdef QWS |
6 | #include <qpe/qpeapplication.h> | 7 | #include <qpe/qpeapplication.h> |
7 | #include <qpe/config.h> | 8 | #include <qpe/config.h> |
8 | #else | 9 | #else |
9 | #include <qapplication.h> | 10 | #include <qapplication.h> |
@@ -23,12 +24,13 @@ | |||
23 | #define STATE_BOSS 0 | 24 | #define STATE_BOSS 0 |
24 | #define STATE_RUNNING 1 | 25 | #define STATE_RUNNING 1 |
25 | #define STATE_CRASHING 2 | 26 | #define STATE_CRASHING 2 |
26 | #define STATE_CRASHED 3 | 27 | #define STATE_CRASHED 3 |
27 | #define STATE_NEWGAME 4 | 28 | #define STATE_NEWGAME 4 |
28 | #define STATE_MENU 5 | 29 | #define STATE_MENU 5 |
30 | #define STATE_REPLAY 6 | ||
29 | 31 | ||
30 | // Menus | 32 | // Menus |
31 | #define MENU_MAIN_MENU 0 | 33 | #define MENU_MAIN_MENU 0 |
32 | #define MENU_OPTIONS_MENU 1 | 34 | #define MENU_OPTIONS_MENU 1 |
33 | 35 | ||
34 | // Main Menu Options | 36 | // Main Menu Options |
@@ -124,12 +126,13 @@ int main( int argc, char *argv[] ) | |||
124 | } | 126 | } |
125 | 127 | ||
126 | SFCave :: SFCave( int spd, QWidget *w, char *name ) | 128 | SFCave :: SFCave( int spd, QWidget *w, char *name ) |
127 | : QMainWindow( w, name ) | 129 | : QMainWindow( w, name ) |
128 | 130 | ||
129 | { | 131 | { |
132 | replayIt = 0; | ||
130 | #ifdef QWS | 133 | #ifdef QWS |
131 | showMaximized(); | 134 | showMaximized(); |
132 | #else | 135 | #else |
133 | resize( 240, 284 ); | 136 | resize( 240, 284 ); |
134 | #endif | 137 | #endif |
135 | 138 | ||
@@ -161,14 +164,16 @@ SFCave :: SFCave( int spd, QWidget *w, char *name ) | |||
161 | speed = spd; // Change to 2 for PC | 164 | speed = spd; // Change to 2 for PC |
162 | press = false; | 165 | press = false; |
163 | 166 | ||
164 | offscreen = new QPixmap( sWidth, sHeight ); | 167 | offscreen = new QPixmap( sWidth, sHeight ); |
165 | offscreen->fill( Qt::black ); | 168 | offscreen->fill( Qt::black ); |
166 | 169 | ||
167 | setUp(); | 170 | // setUp(); |
168 | crashLineLength = -1; | 171 | crashLineLength = -1; |
172 | state = STATE_MENU; | ||
173 | prevState = STATE_MENU; | ||
169 | 174 | ||
170 | gameTimer = new QTimer( this, "game timer" ); | 175 | gameTimer = new QTimer( this, "game timer" ); |
171 | connect( gameTimer, SIGNAL( timeout() ), | 176 | connect( gameTimer, SIGNAL( timeout() ), |
172 | this, SLOT( run() ) ); | 177 | this, SLOT( run() ) ); |
173 | } | 178 | } |
174 | 179 | ||
@@ -179,22 +184,31 @@ SFCave :: ~SFCave() | |||
179 | void SFCave :: start() | 184 | void SFCave :: start() |
180 | { | 185 | { |
181 | gameTimer->start( 10 ); | 186 | gameTimer->start( 10 ); |
182 | 187 | ||
183 | } | 188 | } |
184 | 189 | ||
190 | void SFCave :: setSeed( int seed ) | ||
191 | { | ||
192 | if ( seed == -1 ) | ||
193 | currentSeed = ((unsigned long) time((time_t *) NULL)); | ||
194 | else | ||
195 | currentSeed = seed; | ||
196 | PutSeed( currentSeed ); | ||
197 | } | ||
198 | |||
185 | int SFCave :: nextInt( int range ) | 199 | int SFCave :: nextInt( int range ) |
186 | { | 200 | { |
187 | return rand() % range; | 201 | int val = (int)(Random( ) * range); |
202 | |||
203 | return val; | ||
204 | |||
188 | } | 205 | } |
189 | 206 | ||
190 | void SFCave :: setUp() | 207 | void SFCave :: setUp() |
191 | { | 208 | { |
192 | state = STATE_MENU; | ||
193 | prevState = STATE_MENU; | ||
194 | |||
195 | score = 0; | 209 | score = 0; |
196 | offset = 0; | 210 | offset = 0; |
197 | nrFrames = 0; | 211 | nrFrames = 0; |
198 | dir = 1; | 212 | dir = 1; |
199 | thrust = 0; | 213 | thrust = 0; |
200 | 214 | ||
@@ -218,12 +232,13 @@ void SFCave :: setUp() | |||
218 | noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; | 232 | noThrust = DownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; |
219 | maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; | 233 | maxUpThrust = MaxUpThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; |
220 | maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; | 234 | maxDownThrust = MaxDownThrustVals[FLY_GAME_TYPE][currentGameDifficulty]; |
221 | } | 235 | } |
222 | 236 | ||
223 | crashLineLength = 0; | 237 | crashLineLength = 0; |
238 | lastGateBottomY = 0; | ||
224 | 239 | ||
225 | user.setRect( 50, sWidth/2, 4, 4 ); | 240 | user.setRect( 50, sWidth/2, 4, 4 ); |
226 | 241 | ||
227 | blockWidth = 20; | 242 | blockWidth = 20; |
228 | blockHeight = 70; | 243 | blockHeight = 70; |
229 | gapHeight = initialGateGaps[currentGameDifficulty]; | 244 | gapHeight = initialGateGaps[currentGameDifficulty]; |
@@ -251,26 +266,39 @@ void SFCave :: setUp() | |||
251 | 266 | ||
252 | for ( int i = 0 ; i < MAPSIZE ; ++i ) | 267 | for ( int i = 0 ; i < MAPSIZE ; ++i ) |
253 | mapBottom[i] = sHeight - 10; | 268 | mapBottom[i] = sHeight - 10; |
254 | } | 269 | } |
255 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | 270 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) |
256 | blocks[i].setY( -1 ); | 271 | blocks[i].setY( -1 ); |
272 | |||
257 | } | 273 | } |
258 | 274 | ||
259 | void SFCave :: run() | 275 | void SFCave :: run() |
260 | { | 276 | { |
261 | switch ( state ) | 277 | switch ( state ) |
262 | { | 278 | { |
263 | case STATE_MENU: | 279 | case STATE_MENU: |
264 | displayMenu(); | 280 | displayMenu(); |
265 | break; | 281 | break; |
266 | case STATE_NEWGAME: | 282 | case STATE_NEWGAME: |
283 | setSeed( -1 ); | ||
267 | setUp(); | 284 | setUp(); |
268 | draw(); | 285 | draw(); |
269 | state = STATE_RUNNING; | 286 | state = STATE_RUNNING; |
287 | replay = false; | ||
288 | replayList.clear(); | ||
270 | break; | 289 | break; |
290 | case STATE_REPLAY: | ||
291 | setSeed( currentSeed ); | ||
292 | setUp(); | ||
293 | draw(); | ||
294 | state = STATE_RUNNING; | ||
295 | replay = true; | ||
296 | if ( replayIt ) | ||
297 | delete replayIt; | ||
298 | replayIt = new QListIterator<int>( replayList ); | ||
271 | case STATE_BOSS: | 299 | case STATE_BOSS: |
272 | drawBoss(); | 300 | drawBoss(); |
273 | break; | 301 | break; |
274 | 302 | ||
275 | case STATE_CRASHING: | 303 | case STATE_CRASHING: |
276 | case STATE_CRASHED: | 304 | case STATE_CRASHED: |
@@ -281,12 +309,22 @@ void SFCave :: run() | |||
281 | { | 309 | { |
282 | if ( nrFrames % 2 == 0 ) | 310 | if ( nrFrames % 2 == 0 ) |
283 | handleKeys(); | 311 | handleKeys(); |
284 | 312 | ||
285 | // Apply Game rules | 313 | // Apply Game rules |
286 | nrFrames ++; | 314 | nrFrames ++; |
315 | |||
316 | if ( replay ) | ||
317 | { | ||
318 | while( replayIt->current() && *(replayIt->current()) == nrFrames ) | ||
319 | { | ||
320 | press = !press; | ||
321 | ++(*replayIt); | ||
322 | } | ||
323 | } | ||
324 | |||
287 | if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) | 325 | if ( CURRENT_GAME_TYPE == SFCAVE_GAME ) |
288 | handleGameSFCave(); | 326 | handleGameSFCave(); |
289 | else if ( CURRENT_GAME_TYPE == GATES_GAME ) | 327 | else if ( CURRENT_GAME_TYPE == GATES_GAME ) |
290 | handleGameGates(); | 328 | handleGameGates(); |
291 | else if ( CURRENT_GAME_TYPE == FLY_GAME ) | 329 | else if ( CURRENT_GAME_TYPE == FLY_GAME ) |
292 | handleGameFly(); | 330 | handleGameFly(); |
@@ -520,18 +558,19 @@ void SFCave :: addGate() | |||
520 | { | 558 | { |
521 | int x1 = sWidth; | 559 | int x1 = sWidth; |
522 | int y1 = mapTop[50]; | 560 | int y1 = mapTop[50]; |
523 | int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight); | 561 | int b1Height = nextInt(mapBottom[50] - mapTop[50] - gapHeight); |
524 | 562 | ||
525 | // See if height between last gate and this one is too big | 563 | // See if height between last gate and this one is too big |
526 | if ( b1Height - 200 > lastGateBottomY ) | 564 | if ( b1Height - 100 > lastGateBottomY ) |
527 | b1Height -= 25; | 565 | b1Height -= 25; |
528 | else if ( b1Height + 200 < lastGateBottomY ) | 566 | else if ( b1Height + 100 < lastGateBottomY ) |
529 | b1Height += 25; | 567 | b1Height += 25; |
530 | lastGateBottomY = b1Height; | 568 | lastGateBottomY = b1Height; |
531 | 569 | ||
570 | |||
532 | int x2 = sWidth; | 571 | int x2 = sWidth; |
533 | int y2 = y1 + b1Height + gapHeight; | 572 | int y2 = y1 + b1Height + gapHeight; |
534 | int b2Height = mapBottom[50] - y2; | 573 | int b2Height = mapBottom[50] - y2; |
535 | 574 | ||
536 | 575 | ||
537 | blocks[i].setRect( x1, y1, blockWidth, b1Height ); | 576 | blocks[i].setRect( x1, y1, blockWidth, b1Height ); |
@@ -770,13 +809,17 @@ void SFCave :: keyPressEvent( QKeyEvent *e ) | |||
770 | { | 809 | { |
771 | switch( e->key() ) | 810 | switch( e->key() ) |
772 | { | 811 | { |
773 | case Qt::Key_Up: | 812 | case Qt::Key_Up: |
774 | case Qt::Key_F9: | 813 | case Qt::Key_F9: |
775 | case Qt::Key_Space: | 814 | case Qt::Key_Space: |
776 | press = true; | 815 | if ( !press ) |
816 | { | ||
817 | press = true; | ||
818 | replayList.append( new int( nrFrames ) ); | ||
819 | } | ||
777 | break; | 820 | break; |
778 | case Qt::Key_M: | 821 | case Qt::Key_M: |
779 | case Qt::Key_Return: | 822 | case Qt::Key_Return: |
780 | case Qt::Key_Enter: | 823 | case Qt::Key_Enter: |
781 | if ( state == STATE_CRASHED ) | 824 | if ( state == STATE_CRASHED ) |
782 | state = STATE_MENU; | 825 | state = STATE_MENU; |
@@ -801,28 +844,32 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e ) | |||
801 | else | 844 | else |
802 | { | 845 | { |
803 | switch( e->key() ) | 846 | switch( e->key() ) |
804 | { | 847 | { |
805 | case Qt::Key_F9: | 848 | case Qt::Key_F9: |
806 | case Qt::Key_Space: | 849 | case Qt::Key_Space: |
807 | press = false; | 850 | case Qt::Key_Up: |
851 | if ( press ) | ||
852 | { | ||
853 | press = false; | ||
854 | |||
855 | replayList.append( new int( nrFrames ) ); | ||
856 | } | ||
808 | break; | 857 | break; |
809 | 858 | ||
810 | case Qt::Key_Up: | ||
811 | press = false; | ||
812 | |||
813 | case Qt::Key_R: | 859 | case Qt::Key_R: |
814 | case Qt::Key_Down: | ||
815 | if ( state == STATE_CRASHED ) | 860 | if ( state == STATE_CRASHED ) |
816 | { | 861 | { |
817 | state = STATE_NEWGAME; | 862 | state = STATE_REPLAY; |
818 | } | 863 | } |
819 | else | ||
820 | movel = true; | ||
821 | break; | 864 | break; |
822 | 865 | ||
866 | case Qt::Key_Down: | ||
867 | if ( state == STATE_CRASHED ) | ||
868 | state = STATE_NEWGAME; | ||
869 | break; | ||
823 | default: | 870 | default: |
824 | e->ignore(); | 871 | e->ignore(); |
825 | break; | 872 | break; |
826 | } | 873 | } |
827 | } | 874 | } |
828 | 875 | ||