summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave/sfcave.cpp
blob: 8b9c8441d0860330d69d9a28cf749c6706bc79c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#ifdef QWS
#include <qpe/qpeapplication.h>
#include <qpe/config.h>
#else
#include <qapplication.h>
#endif

#include "sfcave.h"

#define CAPTION "SFCave 1.7 by AndyQ"

// States
#define STATE_BOSS              0
#define STATE_RUNNING           1
#define STATE_CRASHED           2
#define STATE_NEWGAME           3
#define STATE_MENU              4

// Menus
#define MENU_MAIN_MENU          0
#define MENU_OPTIONS_MENU       1

// Main Menu Options
#define MENU_START_GAME         0
#define MENU_OPTIONS            1
#define MENU_QUIT               2

// Option Menu Options
#define MENU_GAME_TYPE          0
#define MENU_GAME_DIFFICULTY     1
#define MENU_BACK               2

QString SFCave::menuOptions[2][5] = { { "Start Game", "Options", "Quit", "", "" },
                                    { "Game Type - %s", "Game Difficulty - %s", "Back", "", "" } };


#define NR_GAME_DIFFICULTIES    3
#define NR_GAME_TYPES           3

#define EASY                    "Easy"
#define NORMAL                  "Normal"
#define HARD                    "Hard"

#define SFCAVE_GAME_TYPE        0
#define GATES_GAME_TYPE         1
#define FLY_GAME_TYPE           2
#define SFCAVE_GAME             "SFCave"
#define GATES_GAME              "Gates"
#define FLY_GAME                "Fly"
QString SFCave::dificultyOption[] = { EASY, NORMAL, HARD };
QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME };

#define CURRENT_GAME_TYPE       gameTypes[currentGameType]
#define CURRENT_GAME_DIFFICULTY difficultyOption[currentGameDifficulty];

bool movel;


int main( int argc, char *argv[] )
{
    movel = true;

#ifdef QWS
    QPEApplication a( argc, argv );
#else
    QApplication a( argc, argv );
#endif

    int speed = 3;
    for ( int i = 0 ; i < argc ; ++i )
    {
        if ( strcmp( argv[i], "-s" ) == 0 )
        {
            if ( i+1 < argc )
                speed = atoi( argv[i+1] );
        }
    }

    SFCave app( speed );
    a.setMainWidget( &app );
    app.show();
    app.start();
    a.exec();
}

SFCave :: SFCave( int spd, QWidget *w, char *name )
    : QMainWindow( w, name )

{
#ifdef QWS
    showMaximized();
#else
    resize( 240, 284 );
#endif

    sWidth = width();
    sHeight = height();
    segSize = sWidth/(MAPSIZE-1)+1;

    currentMenuNr = 0;
    nrMenuOptions[0] = 3;
    nrMenuOptions[1] = 3;
    currentMenuOption[0] = 0;
    currentMenuOption[1] = 0;
    currentGameType = 0;
    currentGameDifficulty = 0;

    setCaption( CAPTION );

#ifdef QWS
    Config cfg( "sfcave" );
    cfg.setGroup( "settings" );
    QString key = "highScore_";
    highestScore[SFCAVE_GAME_TYPE] = cfg.readNumEntry( key + SFCAVE_GAME, 0 );
    highestScore[GATES_GAME_TYPE] = cfg.readNumEntry( key + GATES_GAME, 0 );
    highestScore[FLY_GAME_TYPE] = cfg.readNumEntry( key + FLY_GAME, 0 );
    currentGameType = cfg.readNumEntry( "gameType", 0 );
    currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 );
#endif
    speed = spd; // Change to 2 for PC
    press = false;

    offscreen = new QPixmap( sWidth, sHeight );
    offscreen->fill( Qt::black );

    setUp();
    crashLineLength = -1;

    gameTimer = new QTimer( this, "game timer" );
    connect( gameTimer, SIGNAL( timeout() ),
             this, SLOT( run() ) );
}

SFCave :: ~SFCave()
{
}

void SFCave :: start()
{
    gameTimer->start( 10 );

}

int SFCave :: nextInt( int range )
{
    return rand() % range;
}

void SFCave :: setUp()
{
    state = STATE_MENU;
    prevState = STATE_MENU;

    score = 0;
    offset = 0;
    maxHeight = 50;
    nrFrames = 0;
    dir = 1;
    thrust = 0;
    crashLineLength = 0;

    user.setRect( 50, sWidth/2, 4, 4 );

    blockWidth = 20;
    blockHeight = 70;
    gapHeight = 125;
    gateDistance = 75;
    nextGate = nextInt( 50 ) + gateDistance;

    for ( int i = 0 ; i < TRAILSIZE ; ++i )
    {
        trail[i].setX( -1 );
        trail[i].setY( 0 );
    }

    mapTop[0] = (int)(nextInt(50)) + 5;
    for ( int i = 1 ; i < MAPSIZE ; ++i )
        setPoint( i );

    for ( int i = 0 ; i < BLOCKSIZE ; ++i )
        blocks[i].setY( -1 );
}

void SFCave :: run()
{
    //running = true;
    //setUp();
        switch ( state )
        {
            case STATE_MENU:
                displayMenu();
                break;
            case STATE_NEWGAME:
                setUp();
                draw();
                state = STATE_RUNNING;
                break;
            case STATE_BOSS:
                drawBoss();
                break;

            case STATE_CRASHED:
                draw();
                break;

            case STATE_RUNNING:
            {
                if ( nrFrames % 5 == 0 )
                    score ++;
                if ( nrFrames % 2 == 0 )
                    handleKeys();

                // Apply Game rules
                nrFrames ++;
                if ( CURRENT_GAME_TYPE == SFCAVE_GAME )
                {
                    if ( nrFrames % 500 == 0 )
                    {
                        if ( maxHeight < sHeight - 100 )
                        {
                            maxHeight += 10;

                            // Reduce block height
                            if ( maxHeight > sHeight - 150 )
                                blockHeight -= 5;
                        }
                    }

                    if ( nrFrames % 100 == 0 )
                        addBlock();
                }
                else if ( CURRENT_GAME_TYPE == GATES_GAME )
                {
                    // Slightly random gap distance
                    if ( nrFrames >= nextGate )
                    {
                        nextGate = nrFrames + nextInt( 50 ) + gateDistance;
                        addGate();
                    }

                    if ( nrFrames % 500 == 0 )
                    {
                        if ( gapHeight > 75 )
                            gapHeight -= 5;
                    }
                }
                else if ( CURRENT_GAME_TYPE == FLY_GAME )
                {
                }
                    
                
                if ( checkCollision() )
                {
                    if ( score > highestScore[currentGameType] )
                        highestScore[currentGameType] = score;

#ifdef QWS
    Config cfg( "sfcave" );
    cfg.setGroup( "settings" );
    QString key = "highScore_";
    key += CURRENT_GAME_TYPE;
    cfg.writeEntry( key, highestScore[currentGameType] );
#endif

                    state = STATE_CRASHED;
                }
                else
                {
                    if ( movel )
                        moveLandscape();
                    //movel = false;
                }

                draw();
                break;
            }
        }
}

bool SFCave :: checkCollision()
{
    if ( (user.y() + user.width()) >= mapBottom[10] || user.y() <= mapTop[10] )
        return true;

    for ( int i = 0 ; i < BLOCKSIZE ; ++i )
    {
        if ( blocks[i].y() != -1 )
        {
            if ( blocks[i].intersects( user ) )
                return true;
        }
    }
    return false;
}

void SFCave :: moveLandscape()
{
    offset++;

    if ( offset >= segSize )
    {
        offset = 0;
        for ( int i = 0 ; i < MAPSIZE-speed ; ++i )
        {
            mapTop[i] = mapTop[i+speed];
            mapBottom[i] = mapBottom[i+speed];
        }

        for ( int i = speed ; i > 0 ; --i )
            setPoint( MAPSIZE-i );
    }

    for ( int i = 0 ; i < BLOCKSIZE ; ++i )
    {
        if ( blocks[i].y() != -1 )
        {
            blocks[i].moveBy( -speed, 0 );
            if ( blocks[i].x() + blocks[i].width() < 0 )
                blocks[i].setY( -1 );
        }
    }
}

void SFCave :: addBlock()
{
    for ( int i = 0 ; i < BLOCKSIZE ; ++i )
    {
        if ( blocks[i].y() == -1 )
        {
            int x = sWidth;

            int y = mapTop[50] + (int)(nextInt(mapBottom[50] - mapTop[50] - blockHeight));

            blocks[i].setRect( x, y, blockWidth, blockHeight );

            break;
        }
    }
}

void SFCave :: addGate()
{
    for ( int i = 0 ; i < BLOCKSIZE ; ++i )
    {
        if ( blocks[i].y() == -1 )
        {
            int x1 = sWidth;
            int y1 = mapTop[50];
            int b1Height = nextInt(mapBottom[50] - mapTop[50] - gateDistance);

            // See if height between last gate and this one is too big
            if ( b1Height - 200 > lastGateBottomY )
                b1Height -= 25;
            else if ( b1Height + 200 < lastGateBottomY )
                b1Height += 25;
            lastGateBottomY = b1Height;

            int x2 = sWidth;
            int y2 = y1 + b1Height + gateDistance;
            int b2Height = mapBottom[50] - y2;
            

            blocks[i].setRect( x1, y1, blockWidth, b1Height );
            blocks[i+1].setRect( x2, y2, blockWidth, b2Height );

            break;
        }
    }
}

void SFCave :: setPoint( int point )
{
    if ( nextInt(100) >= 80 )
        dir *= -1;

    mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ) );
    if ( mapTop[point] < 0 )
    {
        mapTop[point] = 0;
        dir *= -1;
    }
    else if ( mapTop[point] >= maxHeight )
    {
        mapTop[point] = maxHeight;
        dir *= -1;
    }

    mapBottom[point] = sHeight - (maxHeight - mapBottom[point]);
    mapBottom[point] = sHeight - (maxHeight - mapTop[point]);
}

void SFCave :: drawBoss()
{
    offscreen->fill( Qt::black );

    bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
}

void SFCave :: draw()
{
    //printf( "Paint\n" );
    offscreen->fill( Qt::black );

    QPainter p( offscreen );
    QFontMetrics fm = p.fontMetrics();
    p.setPen( Qt::white );

    for ( int i = 0 ; i < MAPSIZE -3; ++i )
    {
        p.drawLine( (i*segSize) - (offset*speed), mapTop[i], ((i+1)*segSize)-(offset*speed), mapTop[i+1] );
        p.drawLine( (i*segSize) - (offset*speed), mapBottom[i], ((i+1)*segSize)-(offset*speed), mapBottom[i+1] );
    }

    // Draw user
    p.drawRect( user );

    // Draw trails
    for ( int i = 0 ; i < TRAILSIZE ; ++i )
        if ( trail[i].x() >= 0 )
            p.drawRect( trail[i].x(), trail[i].y(), 2, 2 );

    // Draw blocks
    for ( int i = 0 ; i < BLOCKSIZE ; ++i )
        if ( blocks[i].y() != -1 )
        {
            p.fillRect( blocks[i], Qt::black );
            p.drawRect( blocks[i] );
        }

    // draw score
    QString s;
    s.sprintf( "score %06d   high score %06d", score, highestScore[currentGameType] );
    p.drawText( 5, 10, s );


    if ( state == STATE_CRASHED )
    {
        // add next crash line

        if ( crashLineLength != -1 )
        {
            for ( int i = 0 ; i < 36 ; ++i )
            {
                int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) );
                int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) );            p.drawLine( user.x(), user.y(), x, y );
            }
        }

        if ( crashLineLength >= 15 || crashLineLength == -1 )
        {
            QString text = "Press up or down to start";
            p.drawText( (sWidth/2) - (fm.width( text )/2), 140, text );

            text = "or press OK for menu";
            p.drawText( (sWidth/2) - (fm.width( text )/2), 155, text );
//           p.drawText( 70, 140, QString( "Press down to start" ) );
        }
        else
            crashLineLength ++;
    }

    p.end();
    bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
    //printf( "endpaint\n" );
}

void SFCave :: handleKeys()
{
    // Find enpty trail and move others
    bool done = false;
    for ( int i = 0 ; i < TRAILSIZE ; ++i )
    {
        if ( trail[i].x() < 0 )
        {
            if ( !done )
            {
                trail[i].setX( user.x() - 5 );
                trail[i].setY( user.y() );
                done = true;
            }
        }
        else
        {
            trail[i].setX( trail[i].x() - (2) );
        }
    }

    if ( speed <= 3 )
    {
        if ( press )
            thrust -= 0.6;
        else
            thrust += 0.8;

        if ( thrust > 4.0 )
            thrust = 4.0;
        else if ( thrust < -3.5 )
            thrust = -3.5;
    }
    else
    {
        if ( press )
            thrust -= 0.5;
        else
            thrust += 0.8;

        if ( thrust > 5.0 )
            thrust = 5.0;
        else if ( thrust < -3.5 )
            thrust = -3.5;
    }
    user.moveBy( 0, (int)thrust );
}

void SFCave :: keyPressEvent( QKeyEvent *e )
{
    if ( state == STATE_MENU )
    {
        switch( e->key() )
        {
            case Qt::Key_Down:
                currentMenuOption[currentMenuNr] ++;
                if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" )
                    currentMenuOption[currentMenuNr] = 0;
                break;
            case Qt::Key_Up:
                currentMenuOption[currentMenuNr] --;
                if ( currentMenuOption[currentMenuNr] < 0 )
                    currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1;
                break;

            case Qt::Key_Left:
                if ( currentMenuNr == MENU_OPTIONS_MENU )
                {
                    if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
                    {
                        currentGameType --;
                        if ( currentGameType < 0 )
                            currentGameType = NR_GAME_TYPES - 1;
                    }
                    else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
                    {
                        currentGameDifficulty --;
                        if ( currentGameDifficulty < 0 )
                            currentGameDifficulty = NR_GAME_DIFFICULTIES - 1;
                    }
                }
                break;

            case Qt::Key_Right:
                if ( currentMenuNr == MENU_OPTIONS_MENU )
                {
                    if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
                    {
                        currentGameType ++;
                        if ( currentGameType == NR_GAME_TYPES )
                            currentGameType = 0;
                    }
                    else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
                    {
                        currentGameDifficulty ++;
                        if ( currentGameDifficulty == NR_GAME_DIFFICULTIES )
                            currentGameDifficulty = 0;
                    }
                }
                break;

            case Qt::Key_Space:
            case Qt::Key_Return:
            case Qt::Key_Enter:
                dealWithMenuSelection();
                break;
        }
    }
    else
    {
        switch( e->key() )
        {
            case Qt::Key_Up:
            case Qt::Key_F9:
            case Qt::Key_Space:
                press = true;
                break;
            case Qt::Key_M:
            case Qt::Key_Return:
            case Qt::Key_Enter:
                if ( state == STATE_CRASHED && crashLineLength >= 15 || crashLineLength == -1 )
                    state = STATE_MENU;
           default:
                e->ignore();
                break;
        }
    }
}

void SFCave :: keyReleaseEvent( QKeyEvent *e )
{
    if ( state == STATE_MENU )
    {
    }
    else
    {
        switch( e->key() )
        {
            case Qt::Key_F9:
            case Qt::Key_Space:
                press = false;
                break;

            case Qt::Key_Up:
                press = false;
                
            case Qt::Key_R:
            case Qt::Key_Down:
                if ( state == STATE_CRASHED && crashLineLength >= 15 || crashLineLength == -1 )
                {
                    state = STATE_NEWGAME;
                }
                else
                    movel = true;
                break;

           default:
                e->ignore();
                break;
        }
    }

}

void SFCave :: displayMenu()
{
    offscreen->fill( Qt::black );

    QPainter p( offscreen );
    p.setPen( Qt::white );

    QFont f( "Helvetica", 16 );
    p.setFont( f );

    QFontMetrics fm = p.fontMetrics();

    QString text = "SFCave";
    p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text );

    text = "Written by Andy Qua";
    p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text );

    // Draw options
    int pos = 170;
    for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 )
    {
        if ( currentMenuOption[currentMenuNr] == i )
            p.setPen( Qt::yellow );
        else
            p.setPen( Qt::white );

        QString text;
        if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 )
        {
            QString val;
            if ( i == MENU_GAME_TYPE )
                val = gameTypes[currentGameType];
            else
                val = dificultyOption[currentGameDifficulty];

            text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val );
        }
        else
            text = menuOptions[currentMenuNr][i];

        p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text );
    }

    p.end();
    bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
}

void SFCave :: dealWithMenuSelection()
{
    switch( currentMenuNr )
    {
        case MENU_MAIN_MENU:
        {
            switch( currentMenuOption[currentMenuNr] )
            {
                case MENU_START_GAME:
                    state = STATE_NEWGAME;
                    break;

                case MENU_OPTIONS:
                    currentMenuNr = MENU_OPTIONS_MENU;
                    currentMenuOption[currentMenuNr] = 0;
                    break;

                case MENU_QUIT:
                    QApplication::exit();
                    break;
            }

            break;
        }

        case MENU_OPTIONS_MENU:
        {
            switch( currentMenuOption[currentMenuNr] )
            {
                case MENU_GAME_TYPE:
                    break;
                
                case MENU_GAME_DIFFICULTY:
                    break;
                    
               case MENU_BACK:
                    currentMenuNr = MENU_MAIN_MENU;

#ifdef QWS
                    Config cfg( "sfcave" );
                    cfg.setGroup( "settings" );
                    cfg.writeEntry( "difficulty", currentGameDifficulty );
                    cfg.writeEntry( "gameType", currentGameType );
#endif
                    break;
            }

            break;
        }
    }      
}