Subversion Repositories Kolibri OS

Rev

Rev 5310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5293 alpine 1
#include "rsgamelogic.h"
2
 
3
#include "rsgametext.h"
4
 
5
#include "rsgamemenu.h"
6
 
7
#include "rsgamedraw.h"
8
 
9
#include "rskos.h"
10
 
11
#include "rsgentex.h"
12
#include "rssoundgen.h"
13
#include "rsnoise.h"
14
 
15
#include "rs/rsplatform.h"
16
 
17
 
18
#ifdef RS_USE_C_LIBS // linux version
19
    #include 
20
    #include 
21
    #include 
22
 
23
    #include "rs/rskeyboard.h"
24
#endif
25
 
26
 
27
 
28
 
29
int next_rock_timer = 0;
30
 
31
void next_stage_now() {
32
    game.stage_timer = 0;
33
    game.stage++;
34
};
35
 
36
void next_stage_after(int t) {
37
    if (game.stage_timer > t) {
38
        next_stage_now();
39
    };
40
};
41
 
42
void next_stage_after_sec(int t) {
43
    next_stage_after(t*25);
44
};
45
 
46
 
47
 
48
 
49
int check_collision(int obj1, int obj2) {
50
    // obj1 must be bullet
51
    // obj2 can be anything
52
 
53
    if ( game.objs[obj1].obj_type == OBJ_BULLET ) {
54
 
55
        if ( ( abs(game.objs[obj1].y - game.objs[obj2].y) < ( 4 + game.objs[obj2].radius ) )
56
            && ( abs( (game.objs[obj1].x - 8) - game.objs[obj2].x) < ( 8 + game.objs[obj2].radius ) )  ){
57
            return 1;
58
        };
59
 
60
    };
61
 
62
    return 0;
63
 
64
};
65
 
66
int check_collision_with_player(int obj1) {
67
 
68
    const int player_radius = 3;
69
 
70
    int obj_radius = game.objs[obj1].radius;
71
//    if (game.objs[obj1].obj_type == OBJ_RED_BULLET) {
72
//        obj_radius = 3;
73
//    };
74
 
75
//    if ( game.objs[obj1].obj_type == OBJ_RED_BULLET ) {
76
 
77
        if ( ( abs(game.objs[obj1].y - game.player_y) < ( obj_radius + player_radius ) )
78
            && ( abs( (game.objs[obj1].x ) - game.player_x) < ( obj_radius + 4 + player_radius ) )  ){
79
            return 1;
80
        };
81
 
82
//    };
83
 
84
    return 0;
85
 
86
};
87
 
88
void player_hit() {
89
 
90
    game.health--;
5298 alpine 91
    game.bg_color = COLOR_DARK_RED;
92
 
5302 alpine 93
    soundbuf_play( &game.sound_hit, 0 );
5293 alpine 94
 
95
 
96
    if (game.health < 1) {
97
        game.status = STATUS_MENU;
98
        menu_open( MENU_GAME_OVER );
5315 alpine 99
 
100
        level_passed_score_str[1] = '0' + (game.score / 1000) % 10;
101
        level_passed_score_str[2] = '0' + (game.score / 100) % 10;
102
        level_passed_score_str[3] = '0' + (game.score / 10) % 10;
103
        level_passed_score_str[4] = '0' + (game.score / 1) % 10;
104
 
5293 alpine 105
    };
106
 
107
};
108
 
109
 
110
void GameProcess() {
111
 
112
    if (game.status == STATUS_PLAYING) {
113
 
5315 alpine 114
        int c_shoot_restore_delay = (6 + 2*game.stage_level);
115
        int c_shoot_restore_period = (game.stage_level < 2) ? (3 + game.stage_level) : 5;
116
 
5293 alpine 117
        // shoot
118
 
119
        if ( ( (game.shoot_keypressed) || (is_key_pressed(RS_ATTACK_KEY_MASK)) ) && (game.ammo>0) ) {
120
 
121
            game.shoot_delay ++;
122
 
123
            if (game.shoot_delay > GAME_SHOOT_PERIOD) {
124
 
125
//                if (game.ammo > 0) {
126
 
127
                    game.shoot_restore_delay = 0;
128
                    game.ammo--;
5315 alpine 129
                    soundbuf_play(&game.sound_shoot, 0);
5293 alpine 130
                    game_obj_add( game_obj( OBJ_BULLET, 0, 0, 0, game.player_x+5, game.player_y, 0, 0.0) );
131
 
132
//                };
133
 
5315 alpine 134
                game.shoot_delay = 1; // -= GAME_SHOOT_PERIOD;
135
 
136
 
137
 
5293 alpine 138
                game.shoot_keypressed = 0;
139
 
140
            };
141
        }
142
        else {
143
 
144
            if (game.ammo < GAME_AMMO_MAX) {
145
                game.shoot_restore_delay++;
146
 
5315 alpine 147
                if (game.shoot_restore_delay > c_shoot_restore_delay) {
5293 alpine 148
 
149
                    game.shoot_delay++;
150
 
5315 alpine 151
                    if (game.shoot_delay > c_shoot_restore_period) {
5293 alpine 152
                        game.ammo++;
5315 alpine 153
                        game.shoot_delay -= c_shoot_restore_period;
5293 alpine 154
                    };
155
 
156
                };
157
 
158
            };
159
 
160
        };
161
 
162
 
163
 
164
 
165
        int speed = 4;
166
        int bullet_speed = 11;
167
        int red_bullet_speed = 8;
168
        int rock_speed = 6;
169
 
170
        game.player_x += speed * ( is_key_pressed(RS_ARROW_RIGHT_MASK) - is_key_pressed(RS_ARROW_LEFT_MASK) );
171
        game.player_y += speed * ( is_key_pressed(RS_ARROW_DOWN_MASK) - is_key_pressed(RS_ARROW_UP_MASK) );
172
 
5315 alpine 173
        game.player_x = rs_clamp_i(game.player_x, 15, GAME_WIDTH - 45);
174
        game.player_y = rs_clamp_i(game.player_y, 15, GAME_HEIGHT - 45);
5293 alpine 175
 
176
        game.tz += 1;
177
 
178
 
179
 
5315 alpine 180
        int c_rocktimer_const = (game.stage_level < 4) ? (9 - 2*game.stage_level) : 4;
181
        int c_rocktimer_var = (game.stage_level < 6) ? (16 - 2*game.stage_level) : 6;
5293 alpine 182
 
183
 
184
 
185
        game.stage_timer++;
186
 
187
        if (game.stage == 0) {
188
 
189
            // level start
190
 
191
            next_stage_after_sec(3);
192
 
193
        }
194
 
195
        else if (game.stage == 1) {
196
 
197
//            game.stage = 4;
198
 
199
            // rocks
200
            next_rock_timer--;
201
            if (next_rock_timer < 1) {
5315 alpine 202
                next_rock_timer = c_rocktimer_const + rs_rand()%c_rocktimer_var;
5293 alpine 203
                //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) );
5315 alpine 204
 
205
                int flagsin = 0;
206
                if ( game.stage_level > 4 ) {
207
                    if ( rs_rand()%1024 < (16*game.stage_level) ) {
208
                        flagsin = OBJ_FLAG_SIN;
209
                    };
210
                };
211
 
212
                game_obj_add( game_obj( OBJ_ROCK, OBJ_FLAG_ENEMY | flagsin, rs_rand() % ROCKS_COUNT , game.tex_rocks[0].w/2, GAME_WIDTH + 50, 30 + rs_rand()%(GAME_HEIGHT-90), 0, 0.0 ) );
5293 alpine 213
            };
214
 
215
            next_stage_after_sec(12);
216
 
217
        }
218
 
219
        if (game.stage == 2) {
5315 alpine 220
 
221
            BIT_SET (game.flags, GAME_FLAG_INSTRUCTIONS_PASSED);
5293 alpine 222
 
223
            next_stage_after_sec(4);
224
 
225
        }
226
 
227
        else if (game.stage == 3) {
228
 
229
            // rocks
230
            next_rock_timer--;
231
            if (next_rock_timer < 1) {
5315 alpine 232
                next_rock_timer = c_rocktimer_const + 1 + rs_rand()%c_rocktimer_var;
5293 alpine 233
                //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) );
5315 alpine 234
 
235
                int flagsin = 0;
236
                if ( game.stage_level > 2 ) {
237
                    if ( rs_rand()%1024 < (16*game.stage_level) ) {
238
                        flagsin = OBJ_FLAG_SIN;
239
                    };
240
                };
241
 
242
                game_obj_add( game_obj( OBJ_MINIROCK, OBJ_FLAG_ENEMY|flagsin, rs_rand() % ROCKS_COUNT , game.tex_minirocks[0].w/2, GAME_WIDTH + 50, 30 + rs_rand()%(GAME_HEIGHT-90), 0, 0.0 ) );
5293 alpine 243
            };
244
 
245
            next_stage_after_sec(16);
246
 
247
        }
248
 
249
        else if (game.stage == 4) {
250
 
251
            next_stage_after_sec(4);
252
 
253
        }
254
 
255
        else if (game.stage == 5) {
256
 
257
            // rocks
258
            next_rock_timer--;
259
            if (next_rock_timer < 1) {
260
                next_rock_timer = 5;
261
                //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) );
262
                game_obj_add( game_obj( OBJ_MINIROCK, OBJ_FLAG_ENEMY | OBJ_FLAG_SIN, rs_rand() % ROCKS_COUNT , game.tex_minirocks[0].w/2, GAME_WIDTH + 50, GAME_HEIGHT/8, 0, 0.0 ) );
263
            };
264
 
265
            next_stage_after_sec(6);
266
 
267
        }
268
 
269
        else if (game.stage == 6) {
270
 
271
            // mix rocks
272
            next_rock_timer--;
273
            if (next_rock_timer < 1) {
5315 alpine 274
                next_rock_timer = c_rocktimer_const + rs_rand()%(c_rocktimer_var-3);
275
 
276
                int flagsin = 0;
277
                if ( game.stage_level > 3 ) {
278
                    if ( rs_rand()%1024 < (16*game.stage_level) ) {
279
                        flagsin = OBJ_FLAG_SIN;
280
                    };
281
                };
282
 
5293 alpine 283
                //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) );
5315 alpine 284
                game_obj_add( game_obj( rs_rand()%1024 < 768 ? OBJ_MINIROCK : OBJ_ROCK, OBJ_FLAG_ENEMY | flagsin, rs_rand() % ROCKS_COUNT ,
5293 alpine 285
                                       rs_rand()%1024 < 768 ? game.tex_minirocks[0].w/2 : game.tex_rocks[0].w/2, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-90), 0, 0.0 ) );
286
            };
287
 
288
            next_stage_after_sec(10);
289
 
290
        }
291
 
292
        else if (game.stage == 7) {
293
 
294
 
295
            if (game.stage_timer > 3*25) {
296
                next_stage_now();
297
 
5315 alpine 298
                BIT_CLEAR(game.flags, GAME_FLAG_BOSS_DESTROYED);
299
 
5293 alpine 300
                game_obj_add( game_obj( OBJ_TURRET, OBJ_FLAG_ENEMY | OBJ_FLAG_BOSS, 60, game.tex_rocks[0].w/2, GAME_WIDTH+60, GAME_HEIGHT/2, 0, 0.0 ) );
301
 
302
            };
303
 
304
        }
305
 
306
        else if (game.stage == 8) {
307
 
308
            if ( IS_BIT_SET(game.flags, GAME_FLAG_BOSS_DESTROYED) ) {
309
                next_stage_now();
310
            };
311
 
312
        }
313
 
314
        else if (game.stage == 9) {
315
            next_stage_after_sec(2);
316
        }
317
        else if (game.stage == 10) {
318
 
5315 alpine 319
            /*
5293 alpine 320
            game.status = STATUS_MENU;
321
            menu_open( MENU_LEVEL_PASSED );
322
 
323
            level_passed_score_str[1] = '0' + (game.score / 100) % 10;
324
            level_passed_score_str[2] = '0' + (game.score / 10) % 10;
325
            level_passed_score_str[3] = '0' + (game.score / 1) % 10;
5315 alpine 326
            */
5293 alpine 327
 
5315 alpine 328
            game.stage_level++;
329
 
330
            game.stage = 0;
331
            game.stage_timer = 0;
332
 
5293 alpine 333
        };
334
 
335
 
336
 
337
 
338
 
339
        int i, j;
340
        game_obj_t *obj;
341
 
342
        for (i = 0; i < game.objs_count; i++) {
343
 
344
            obj = &(game.objs[i]);
345
 
346
            if (obj->obj_type == OBJ_BULLET) {
347
 
348
                obj->x += bullet_speed;
349
                if (obj->x > GAME_WIDTH) {
350
                    // destroy object
351
                    game_obj_remove(i);
352
                    i--;
353
                    continue;
354
                };
355
 
356
                for (j = 0; j < game.objs_count; j++) {
357
                    if (IS_BIT_SET(game.objs[j].flags, OBJ_FLAG_ENEMY)) {
358
                        if (check_collision(i, j)) {
359
                            if (IS_BIT_SET( game.objs[j].flags, OBJ_FLAG_BOSS)) {
360
                                game.objs[j].tag--;
361
                                if (game.objs[j].tag < 1) {
362
                                    BIT_SET( game.objs[j].flags, OBJ_FLAG_DESTROYED );
363
                                    BIT_SET( game.flags, GAME_FLAG_BOSS_DESTROYED );
364
                                    game.score += 50;
365
                                };
366
                            }
367
                            else {
368
                                BIT_SET( game.objs[j].flags, OBJ_FLAG_DESTROYED );
369
                                game.score += game.objs[j].obj_type == OBJ_ROCK ? 2 : 3;
370
                            };
371
                            game_obj_remove(i);
372
                            i--;
373
                            break; // continue parent loop
374
                        };
375
                    };
376
                };
377
 
378
            }
379
 
380
            else if (obj->obj_type == OBJ_RED_BULLET) {
381
 
382
                obj->x -= red_bullet_speed;
383
                if (obj->x < 4) {
384
                    // destroy object
385
                    game_obj_remove(i);
386
                    i--;
387
                    continue;
388
                };
389
 
390
                if (check_collision_with_player(i)) {
391
                    player_hit();
392
                    game_obj_remove(i);
393
                    i--;
394
                    continue;
395
                };
396
 
397
            }
398
 
399
            else if (obj->obj_type == OBJ_EXPLOSION) {
400
 
401
                obj->t++;
402
                if (obj->t >= EXPLOSIONS_COUNT) {
403
                    game_obj_remove(i);
404
                    i--;
405
                    continue;
406
                };
407
 
408
            }
409
            else if ( (obj->obj_type == OBJ_ROCK) || (obj->obj_type == OBJ_MINIROCK) ) {
410
 
411
                obj->x -= rock_speed;
412
                if (obj->x < - obj->radius * 2) {
413
                    game_obj_remove(i);
414
                    i--;
415
                    continue;
416
                };
417
 
418
                if ( IS_BIT_SET(obj->flags, OBJ_FLAG_SIN) ) {
419
                    obj->f += 0.2;
420
                    obj->y += 7.0 * sin(obj->f);
421
                };
422
 
423
                if ( check_collision_with_player(i) ) {
424
                    player_hit();
425
                    game_obj_remove(i);
426
                    i--;
427
                    continue;
428
                };
429
 
430
            }
431
            else if ( obj->obj_type == OBJ_TURRET ) {
432
 
433
                if (obj->x > GAME_WIDTH*3/4) {
434
                    obj->x -= 2;
435
                }
436
                else {
437
                    obj->f += 0.03;
438
                    obj->y = GAME_HEIGHT * ( 0.5 + 0.3*sin(obj->f) );
439
                };
440
 
5315 alpine 441
                if ( obj->x < GAME_WIDTH*4/5 ) {
442
                    // turret shoot
443
                    obj->t--;
444
                    if (obj->t < 1) {
445
                        soundbuf_play(&game.sound_turret_shoot, 0);
446
                        game_obj_add( game_obj( OBJ_RED_BULLET, 0, 0, 3, obj->x - 30, obj->y, 0, 0) );
447
 
448
                        int c_const = (game.stage_level < 4) ? (10 - 2*game.stage_level) : 3;
449
                        int c_var = (game.stage_level < 6) ? (20 - 3*game.stage_level) : 3;
450
 
451
                        obj->t = c_const + rs_rand() % c_var;
452
 
453
                        if ( (rs_rand()%1024) < 80 ) {
454
                            obj->t += 18;
455
                        };
456
 
457
                    };
5293 alpine 458
                };
459
 
460
            };
461
 
462
        };
463
 
464
 
465
 
466
        for (i = 0; i < game.objs_count; i++) {
467
            if ( IS_BIT_SET( game.objs[i].flags, OBJ_FLAG_DESTROYED ) ) {
5302 alpine 468
                soundbuf_play( &game.sound_explosions[ rs_rand() % SOUND_EXPLOSIONS_COUNT ], 0 );
5293 alpine 469
                game_obj_add( game_obj( OBJ_EXPLOSION, 0, 0, EXPLOSION_RADIUS, game.objs[i].x, game.objs[i].y, 0, 0.0 ) );
470
                game_obj_remove(i);
471
                i--;
472
 
473
            };
474
        };
475
 
476
 
477
 
478
    };
479
 
480
    game_draw();
5302 alpine 481
 
5310 alpine 482
    if ( (game.status == STATUS_MENU) && (game.menu_index != MENU_PAUSE) ) {
5302 alpine 483
        soundbuf_loop_check( &game.sound_music );
484
    };
5293 alpine 485
 
486
}
487