- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# maingame.c

Go to the documentation of this file.

    00001
    00005 //    /**
    00006 //      *@defgroup maingame maingame.c
    00007 //      *Updates the game state every frame, performs all animations and game logic
    00008 //      *@{
    00009 //      */
    00010
    00011 /*----------------------------------------------------------------------------------------------------------*/
    00012
    00013 #ifndef _ALLEGRO5_
    00014 #define _ALLEGRO5_
    00015 #include <allegro5/allegro5.h>
    00016 #endif // _ALLEGRO5_
    00017
    00018 #ifndef _ALLEGRO5FONT_
    00019 #define _ALLEGRO5FONT_
    00020 #include <allegro5/allegro_font.h>
    00021 #endif // _ALLEGRO5FONT_
    00022
    00023 #ifndef _ALLEGRO5PRIMITIVES_
    00024 #define _ALLEGRO5PRIMITIVES_
    00025 #include <allegro5/allegro_primitives.h>
    00026 #endif // _ALLEGRO5PRIMITIVES_
    00027
    00028 #ifndef _CHIPMUNK_
    00029 #define _CHIPMUNK_
    00030 #include "chipmunk.h"
    00031 #endif // _CHIPMUNK_
    00032
    00033 #ifndef _STDIO_
    00034 #define _STDIO_
    00035 #include <stdio.h>
    00036 #endif // _STDIO_
    00037
    00038 #ifndef _COMMONS_
    00039 #define _COMMONS_
    00040 #include "../include/commons.h"
    00041 #endif // _COMMONS_
    00042
    00043 #ifndef _SPRITES_
    00044 #define _SPRITES_
    00045 #include "../include/sprites.h"
    00046 #endif // _SPRITES_
    00047
    00048 #ifndef _RENDER_
    00049 #define _RENDER_
    00050 #include "../include/render.h"
    00051 #endif // _RENDER_
    00052
    00053 #ifndef _PHYSCOL_
    00054 #define _PHYSCOL_
    00055 #include "../include/physcol.h"
    00056 #endif // _PHYSCOL_
    00057
    00058 #ifndef _PROGRESSBAR_
    00059 #define _PROGRESSBAR_
    00060 #include "../include/progressbar.h"
    00061 #endif // _PROGRESSBAR_
    00062
    00063 #ifndef _OPTIONS_
    00064 #define _OPTIONS_
    00065 #include "../include/options.h"
    00066 #endif // _OPTIONS_
    00067
    00068 #ifndef _HOWTOPLAY_
    00069 #define _HOWTOPLAY_
    00070 #include "../include/howtoplay.h"
    00071 #endif // _HOWTOPLAY_
    00072
    00073 #ifndef _HIGHSCORES_
    00074 #define _HIGHSCORES_
    00075 #include "../include/highscores.h"
    00076 #endif // _HIGHSCORES_
    00077
    00078 #ifndef _PAUSE_
    00079 #define _PAUSE_
    00080 #include "../include/pause.h"
    00081 #endif // _PAUSE_
    00082
    00083 #ifndef _GAMEOVER_
    00084 #define _GAMEOVER_
    00085 #include "../include/gameover.h"
    00086 #endif // _GAMEOVER_
    00087
    00088 #ifndef _ENEMYSTORE_
    00089 #define _ENEMYSTORE_
    00090 #include "../include/enemystore.h"
    00091 #endif // _ENEMYSTORE_
    00092
    00093 #ifndef _MAINGAME_
    00094 #define _MAINGAME_
    00095 #include "../include/maingame.h"
    00096 #endif // _MAINGAME_
    00097
    00098 /*----------------------------------------------------------------------------------------------------------*/
    00099
    00100 static _Bool start = false;
    00101 static int lives = 3;
    00102 static int score = 0;
    00103 static int minutes = 0;
    00104 static int seconds = 0;
    00105 static char *survival_time = "00:00";
    00106
    00107 /*----------------------------------------------------------------------------------------------------------*/
    00108
    00109 // Updates all nescessary sprites, images, buttons etc.
    00110 void update(void)
    00111 {
    00112     static _Bool called = false;
    00113     GDATAPTR game = get_game_data();
    00114     update_background();
    00115     if(game->gamestarted && game->paused)
    00116     {
    00117         paused(score, survival_time, lives);
    00118     }
    00119     else if(lives == 0)
    00120     {
    00121         gameover(score, survival_time);
    00122     }
    00123     else if(game->gamestarted)
    00124     {
    00125         if(!called)
    00126         {
    00127             cpSpaceResizeStaticHash(get_global_cpSpace(), 700.0, 4*10);
    00128             cpSpaceAddCollisionHandler(get_global_cpSpace(), 1, 2, collision_begin, NULL, NULL, NULL, NULL);
    00129             cpSpaceAddCollisionHandler(get_global_cpSpace(), 0, 2, collision_static_begin, NULL, NULL, NULL, NULL);
    00130             cpSpaceAddCollisionHandler(get_global_cpSpace(), 0, 1, collision_static_begin, NULL, NULL, NULL, NULL);
    00131             called = false;
    00132         }
    00133         logic();
    00134         update_clouds();
    00135         cpSpaceStep(get_global_cpSpace(), 1.0f/60.0f);
    00136         cpSpaceHashEach(get_global_cpSpace()->activeShapes, &update_sprites, NULL);
    00137         update_ground();
    00138         update_lives();
    00139         add_element_to_render_queue(NULL, 0, 0, 0, RCOLOR(0, 0, 0, 255), update_score);
    00140         add_element_to_render_queue(NULL, 290, 0, 0, RCOLOR(0, 0, 0, 255), update_survivaltime);
    00141     }
    00142     else if(game->options)
    00143     {
    00144         options();
    00145     }
    00146     else if(game->howtoplay)
    00147     {
    00148         howtoplay();
    00149     }
    00150     else if(game->highscores)
    00151     {
    00152         highscores();
    00153     }
    00154 }
    00155
    00156 /*----------------------------------------------------------------------------------------------------------*/
    00157
    00158 // Performs animations for the stickman and adds the sprites to the render queue
    00159 void update_stickman(void)
    00160 {
    00161     SPRITESPTR stman = search_sprite_list_for_element("STICKMAN");
    00162     SPRITESPTR ground = search_sprite_list_for_element("GROUND");
    00163     float height = (search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_MOVE_1"))->height;
    00164     float y = stman->shape->body->p.y-height/2;
    00165     int cx = 0, cy = 0, nflags = 0;
    00166     static _Bool up = false, down = false;
    00167     ALLEGRO_COLOR color = RCOLOR(255, 255, 255, 255);
    00168
    00169     if(!start)
    00170     {
    00171         static int sx = 0;
    00172         static int sy = 0;
    00173         if(y+height >= ground->sdata.y)
    00174             stman->sdata.anim_counter++;
    00175         stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_MOVE_1");
    00176         if(stman->sdata.anim_counter >= 20)
    00177         {
    00178             stman->sdata.anim_counter = 0;
    00179             stman->shape->e = 0.1;
    00180             stman->shape->u = 0.9;
    00181             sx = stman->shape->body->p.x;
    00182             sy = stman->shape->body->p.y;
    00183             start = true;
    00184         }
    00185     }
    00186     else if((stman->sdata.animflags & MUP) && !down)
    00187     {
    00188         if(y+height >= ground->sdata.y && !up)
    00189         {
    00190             cpVect currentvel = stman->shape->body->v;
    00191             cpBodySlew(stman->shape->body, cpv(stman->shape->body->p.x, stman->shape->body->p.y-stman->sdata.dy), 1);
    00192             cpVect newvel = stman->shape->body->v;
    00193             stman->shape->body->v = cpvadd(currentvel, newvel);
    00194             up = true;
    00195         }
    00196         else if(y+height >= ground->sdata.y && up)
    00197         {
    00198            remove_anim_flags_on_sprite(stman, MUP);
    00199            up = false;
    00200         }
    00201         stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_MOVE_1");
    00202     }
    00203     else if((stman->sdata.animflags & MDOWN))
    00204     {
    00205         down = true;
    00206         static int dcounter = 0;
    00207         dcounter++;
    00208         if(dcounter > 65)
    00209         {
    00210             down = false;
    00211             remove_anim_flags_on_sprite(stman, MDOWN);
    00212             dcounter = 0;
    00213         }
    00214         stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_SLIDE");
    00215         cy = 15;
    00216     }
    00217     else
    00218     {
    00219         stman->sdata.anim_counter++;
    00220         if(stman->sdata.anim_counter < 5)
    00221             stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_MOVE_1");
    00222         else if(stman->sdata.anim_counter > 5 && stman->sdata.anim_counter < 10)
    00223             stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_MOVE_2");
    00224         else if(stman->sdata.anim_counter > 10)
    00225             stman->sdata.anim_counter = 0;
    00226     }
    00227
    00228     if((stman->sdata.animflags & MATTACK))
    00229     {
    00230         static int counter = 0;
    00231         counter++;
    00232         if(counter > 44)
    00233         {
    00234             remove_anim_flags_on_sprite(stman, MATTACK);
    00235             counter = 0;
    00236         }
    00237         if(up)
    00238         {
    00239             stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_ATTACK_JUMP");
    00240         }
    00241         else if(down)
    00242         {
    00243             stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_ATTACK_SLIDE");
    00244             cy = 20;
    00245         }
    00246         else
    00247             stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_ATTACK_RUN");
    00248     }
    00249     if ((stman->sdata.animflags & MDAMAGED))
    00250     {
    00251         static int counter = 0;
    00252         counter++;
    00253         stman->sdata.currentimg = search_image_list_for_element(&stman->ifirstptr, "STICKFIGURE_DEAD");
    00254
    00255         if(stman->sdata.x+stman->sdata.currentimg->width <= 0 || counter >= 60)
    00256         {
    00257             remove_anim_flags_on_sprite(stman, MDAMAGED);
    00258             set_anim_flags_on_sprite(stman, MRECOVER);
    00259             stman->shape->body->p.x = 70;
    00260             stman->shape->body->v = cpvzero;
    00261             counter = 0;
    00262         }
    00263     }
    00264     if((stman->sdata.animflags & MRECOVER))
    00265     {
    00266         static int counter = 0;
    00267         counter++;
    00268
    00269         if(counter >= 15 && counter <= 30)
    00270             color = RCOLOR(255, 255, 255, 105);
    00271         else if(counter >= 45 && counter <= 60)
    00272             color = RCOLOR(255, 255, 255, 105);
    00273         else if(counter >= 75 && counter <= 90)
    00274             color = RCOLOR(255, 255, 255, 105);
    00275         else if(counter >= 105 && counter <= 120)
    00276             color = RCOLOR(255, 255, 255, 105);
    00277
    00278         if(counter >= 120)
    00279         {
    00280             counter = 0;
    00281             remove_anim_flags_on_sprite(stman, MRECOVER);
    00282         }
    00283     }
    00284
    00285     if((int)stman->shape->body->p.x != 50 && start && y+height >= ground->sdata.y && !(stman->sdata.animflags & MDAMAGED))
    00286     {
    00287         stman->shape->body->p.x = 70;
    00288     }
    00289
    00290     add_element_to_render_queue(stman->sdata.currentimg->image, stman->sdata.x+cx, stman->sdata.y+cy, nflags,
    00291                                 color, NULL);
    00292     stman->sdata.x = stman->shape->body->p.x-stman->sdata.currentimg->width/2;
    00293     stman->sdata.y = stman->shape->body->p.y-stman->sdata.currentimg->height/2;
    00294 }
    00295
    00296 /*----------------------------------------------------------------------------------------------------------*/
    00297
    00298 void update_lives(void)
    00299 {
    00300     SPRITESPTR sprite = search_sprite_list_for_element("STICKMAN");
    00301     SIMAGEPTR life = search_image_list_for_element(&(sprite->ifirstptr), "STICKFIGURE_LIVES");
    00302
    00303     int x = res_width-life->width;
    00304     int y = 0;
    00305
    00306     for(int i = 0; i < lives; i++)
    00307     {
    00308         add_element_to_render_queue(life->image, x, y, 0, RCOLOR(255, 255, 255, 255), NULL);
    00309         x -= life->width+10;
    00310     }
    00311 }
    00312
    00313 /*----------------------------------------------------------------------------------------------------------*/
    00314
    00315 void update_score(ALLEGRO_BITMAP *image, float x, float y, int nflags, ALLEGRO_COLOR color)
    00316 {
    00317     GDATAPTR game = get_game_data();
    00318     al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, color);
    00319     al_draw_textf(game->font, x, y, nflags, "Score: %d", score);
    00320     al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, RDEFAULT);
    00321 }
    00322
    00323 /*----------------------------------------------------------------------------------------------------------*/
    00324
    00325 void collision_end_win(cpSpace *Space, void *obj, void *unused)
    00326 {
    00327     cpShape *shape = (cpShape*)obj;
    00328     SPRITESPTR sprite = (SPRITESPTR)shape->data;
    00329
    00330     if(strcmp(sprite->slabel, "AMEAN") == 0)
    00331         score += 2;
    00332     else if(strcmp(sprite->slabel, "PLANT") == 0)
    00333         score += 6;
    00334     else if(strcmp(sprite->slabel, "RAW") == 0)
    00335         score += 10;
    00336
    00337     remove_physics_object(sprite);
    00338     remove_element_from_sprite_list_ptr(sprite);
    00339 }
    00340
    00341 /*----------------------------------------------------------------------------------------------------------*/
    00342
    00343 void collision_end_lose(cpSpace *Space, void *obj, void *unused)
    00344 {
    00345     cpShape *shape = (cpShape*)obj;
    00346     SPRITESPTR sprite = (SPRITESPTR)shape->data;
    00347     SPRITESPTR stman = search_sprite_list_for_element("STICKMAN");
    00348     lives--;
    00349     set_anim_flags_on_sprite(stman, MDAMAGED);
    00350 }
    00351
    00352 /*----------------------------------------------------------------------------------------------------------*/
    00353
    00354 void update_survivaltime(ALLEGRO_BITMAP *image, float x, float y, int nflags, ALLEGRO_COLOR color)
    00355 {
    00356     static int counter = 0;
    00357     GDATAPTR game = get_game_data();
    00358     if(game->paused);
    00359     else if(lives <= 0);
    00360     else
    00361         counter++;
    00362     if(counter >= 60)
    00363     {
    00364         counter = 0;
    00365         seconds++;
    00366     }
    00367     if(seconds >= 60)
    00368     {
    00369         seconds = 0;
    00370         minutes++;
    00371     }
    00372     free(survival_time);
    00373     survival_time = (char*)malloc(7*sizeof(char));
    00374     if(minutes < 10 && seconds < 10)
    00375         sprintf(survival_time, "0%d:0%d", minutes, seconds);
    00376     else if(minutes < 10)
    00377         sprintf(survival_time, "0%d:%d", minutes, seconds);
    00378     else if(seconds < 10)
    00379         sprintf(survival_time, "%d:0%d", minutes, seconds);
    00380     else
    00381         sprintf(survival_time, "%d:%d", minutes, seconds);
    00382
    00383     al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, color);
    00384     al_draw_textf(game->font, x, y, nflags, "Survival Time: %s", survival_time);
    00385     al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, RDEFAULT);
    00386 }
    00387
    00388 /*----------------------------------------------------------------------------------------------------------*/
    00389
    00390 void update_ameans(SPRITESPTR amean)
    00391 {
    00392     int nflags = 0;
    00393
    00394     cpBodySlew(amean->shape->body, cpv(amean->shape->body->p.x-180, amean->shape->body->p.y), 1);
    00395
    00396     amean->sdata.anim_counter++;
    00397     if(amean->sdata.anim_counter < 7)
    00398     {
    00399         amean->sdata.currentimg = search_image_list_for_element(&amean->ifirstptr, "AMEAN_1");
    00400     }
    00401     else if(amean->sdata.anim_counter > 7 && amean->sdata.anim_counter < 14)
    00402     {
    00403         amean->sdata.currentimg = search_image_list_for_element(&amean->ifirstptr, "AMEAN_2");
    00404     }
    00405     else if(amean->sdata.anim_counter > 14 && amean->sdata.anim_counter < 21)
    00406     {
    00407         amean->sdata.currentimg = search_image_list_for_element(&amean->ifirstptr, "AMEAN_3");
    00408     }
    00409     else if(amean->sdata.anim_counter > 21 && amean->sdata.anim_counter < 28)
    00410     {
    00411         amean->sdata.currentimg = search_image_list_for_element(&amean->ifirstptr, "AMEAN_2");
    00412     }
    00413     else if(amean->sdata.anim_counter > 28)
    00414         amean->sdata.anim_counter = 0;
    00415
    00416     add_element_to_render_queue(amean->sdata.currentimg->image, amean->sdata.x, amean->sdata.y, nflags,
    00417                                 RCOLOR(255, 255, 255, 255), NULL);
    00418     amean->sdata.x = amean->shape->body->p.x-amean->sdata.currentimg->width/2;
    00419     amean->sdata.y = amean->shape->body->p.y-amean->sdata.currentimg->height/2;
    00420
    00421     if(amean->sdata.x+amean->sdata.currentimg->width <= 0)
    00422         remove_enemy(amean);
    00423 }
    00424
    00425 /*----------------------------------------------------------------------------------------------------------*/
    00426
    00427 void update_brickwall(SPRITESPTR brickwall)
    00428 {
    00429     int nflags = 0;
    00430
    00431     cpBodySlew(brickwall->shape->body, cpv(brickwall->shape->body->p.x-180, brickwall->shape->body->p.y), 1);
    00432
    00433     add_element_to_render_queue(brickwall->sdata.currentimg->image, brickwall->sdata.x, brickwall->sdata.y, nflags,
    00434                                 RCOLOR(255, 255, 255, 255), NULL);
    00435     brickwall->sdata.x = brickwall->shape->body->p.x-brickwall->sdata.currentimg->width/2;
    00436     brickwall->sdata.y = brickwall->shape->body->p.y-brickwall->sdata.currentimg->height/2;
    00437
    00438     if((brickwall->sdata.animflags & MDOWN))
    00439     {
    00440         ALLEGRO_MOUSE_STATE mouse;
    00441         al_get_mouse_state(&mouse);
    00442         float x = brickwall->sdata.x;
    00443         float y = brickwall->sdata.y;
    00444         float width = brickwall->sdata.currentimg->width;
    00445         float height = brickwall->sdata.currentimg->height;
    00446
    00447         if((mouse.x >= x && mouse.x <= x+width) && (mouse.y >= y && mouse.y <= y+height))
    00448         {
    00449             remove_enemy(brickwall);
    00450             score += 2;
    00451         }
    00452         remove_anim_flags_on_sprite(brickwall, MDOWN);
    00453     }
    00454
    00455     if(brickwall->sdata.x+brickwall->sdata.currentimg->width <= 0)
    00456         remove_enemy(brickwall);
    00457 }
    00458
    00459 /*----------------------------------------------------------------------------------------------------------*/
    00460
    00461 void update_clamp(SPRITESPTR clamp)
    00462 {
    00463     int nflags = 0;
    00464     SIMAGEPTR arm = search_image_list_for_element(&(clamp->ifirstptr), "ARM");
    00465
    00466     if(clamp->sdata.x <= 200)
    00467     {
    00468         if(clamp->sdata.y+clamp->sdata.currentimg->height >= 470)
    00469             cpBodySlew(clamp->shape->body, cpv(clamp->shape->body->p.x-180, clamp->shape->body->p.y), 1);
    00470         else
    00471             cpBodySlew(clamp->shape->body, cpv(clamp->shape->body->p.x-180, clamp->shape->body->p.y+500), 1);
    00472     }
    00473     else
    00474         cpBodySlew(clamp->shape->body, cpv(clamp->shape->body->p.x-180, clamp->shape->body->p.y), 1);
    00475
    00476     if((clamp->sdata.animflags & MATTACK))
    00477         clamp->sdata.currentimg = search_image_list_for_element(&clamp->ifirstptr, "CLAMP_CLOSE");
    00478
    00479     add_element_to_render_queue(clamp->sdata.currentimg->image, clamp->sdata.x, clamp->sdata.y, nflags,
    00480                                 RCOLOR(255, 255, 255, 255), NULL);
    00481     add_element_to_render_queue(arm->image, clamp->sdata.x, clamp->sdata.y-arm->height, nflags,
    00482                                 RCOLOR(255, 255, 255, 255), NULL);
    00483     add_element_to_render_queue(arm->image, clamp->sdata.x, clamp->sdata.y-arm->height-arm->height, nflags,
    00484                                 RCOLOR(255, 255, 255, 255), NULL);
    00485     clamp->sdata.x = clamp->shape->body->p.x-clamp->sdata.currentimg->width/2;
    00486     clamp->sdata.y = clamp->shape->body->p.y-clamp->sdata.currentimg->height/2;
    00487
    00488     if(clamp->sdata.x+clamp->sdata.currentimg->width <= 0)
    00489         remove_enemy(clamp);
    00490 }
    00491
    00492 /*----------------------------------------------------------------------------------------------------------*/
    00493
    00494 void update_steelwall(SPRITESPTR steelwall)
    00495 {
    00496     int nflags = 0;
    00497
    00498     cpBodySlew(steelwall->shape->body, cpv(steelwall->shape->body->p.x-180, steelwall->shape->body->p.y), 1);
    00499
    00500     add_element_to_render_queue(steelwall->sdata.currentimg->image, steelwall->sdata.x, steelwall->sdata.y, nflags,
    00501                                 RCOLOR(255, 255, 255, 255), NULL);
    00502     steelwall->sdata.x = steelwall->shape->body->p.x-steelwall->sdata.currentimg->width/2;
    00503     steelwall->sdata.y = steelwall->shape->body->p.y-steelwall->sdata.currentimg->height/2;
    00504
    00505     if(steelwall->sdata.x+steelwall->sdata.currentimg->width <= 0)
    00506         remove_enemy(steelwall);
    00507 }
    00508
    00509 /*----------------------------------------------------------------------------------------------------------*/
    00510
    00511 void update_fist(SPRITESPTR fist)
    00512 {
    00513     int nflags = 0;
    00514
    00515     cpBodySlew(fist->shape->body, cpv(fist->shape->body->p.x-180, fist->shape->body->p.y), 1);
    00516
    00517     add_element_to_render_queue(fist->sdata.currentimg->image, fist->sdata.x, fist->sdata.y, nflags,
    00518                                 RCOLOR(255, 255, 255, 255), NULL);
    00519     fist->sdata.x = fist->shape->body->p.x-fist->sdata.currentimg->width/2;
    00520     fist->sdata.y = fist->shape->body->p.y-fist->sdata.currentimg->height/2;
    00521
    00522     if(fist->sdata.x+fist->sdata.currentimg->width <= 0)
    00523         remove_enemy(fist);
    00524 }
    00525
    00526 /*----------------------------------------------------------------------------------------------------------*/
    00527
    00528 void update_spikes(SPRITESPTR spikes)
    00529 {
    00530     int nflags = 0;
    00531
    00532     cpBodySlew(spikes->shape->body, cpv(spikes->shape->body->p.x-180, spikes->shape->body->p.y), 1);
    00533
    00534     add_element_to_render_queue(spikes->sdata.currentimg->image, spikes->sdata.x, spikes->sdata.y, nflags,
    00535                                 RCOLOR(255, 255, 255, 255), NULL);
    00536     spikes->sdata.x = spikes->shape->body->p.x-spikes->sdata.currentimg->width/2;
    00537     spikes->sdata.y = spikes->shape->body->p.y-spikes->sdata.currentimg->height/2;
    00538
    00539     if(spikes->sdata.x+spikes->sdata.currentimg->width <= 0)
    00540         remove_enemy(spikes);
    00541 }
    00542
    00543 /*----------------------------------------------------------------------------------------------------------*/
    00544
    00545 void update_raw(SPRITESPTR raw)
    00546 {
    00547     int nflags = 0;
    00548
    00549     cpBodySlew(raw->shape->body, cpv(raw->shape->body->p.x-180, raw->shape->body->p.y), 1);
    00550
    00551     if(raw->sdata.x <= 600 && raw->sdata.x >= 550)
    00552     {
    00553         set_anim_flags_on_sprite(raw, MATTACK);
    00554     }
    00555
    00556     add_element_to_render_queue(raw->sdata.currentimg->image, raw->sdata.x, raw->sdata.y, nflags,
    00557                                 RCOLOR(255, 255, 255, 255), NULL);
    00558
    00559     if((raw->sdata.animflags & MATTACK))
    00560     {
    00561         int x = raw->sdata.x-5*raw->sdata.anim_counter;
    00562         int y = raw->sdata.y+30;
    00563         SIMAGEPTR lazer = search_image_list_for_element(&(raw->ifirstptr), "LAZER");
    00564         raw->sdata.anim_counter++;
    00565         raw->sdata.currentimg = search_image_list_for_element(&(raw->ifirstptr), "RAW_2");
    00566         if(x+lazer->width <= raw->sdata.x)
    00567         {
    00568             raw->sdata.currentimg = search_image_list_for_element(&(raw->ifirstptr), "RAW_1");
    00569         }
    00570         if(x+lazer->width <= 0)
    00571         {
    00572             raw->sdata.anim_counter = 0;
    00573             remove_anim_flags_on_sprite(raw, MATTACK);
    00574         }
    00575         SPRITESPTR stman = search_sprite_list_for_element("STICKMAN");
    00576         if((x >= stman->sdata.x && x <= stman->sdata.x+stman->sdata.currentimg->width) &&
    00577            (y >= stman->sdata.y && y <= stman->sdata.y+stman->sdata.currentimg->height) &&
    00578            !(stman->sdata.animflags & MDAMAGED) && !(stman->sdata.animflags & MRECOVER))
    00579         {
    00580             lives--;
    00581             set_anim_flags_on_sprite(stman, MDAMAGED);
    00582             raw->sdata.anim_counter = 0;
    00583             remove_anim_flags_on_sprite(raw, MATTACK);
    00584         }
    00585         add_element_to_render_queue(lazer->image, x, y, nflags,
    00586                                     RCOLOR(255, 255, 255, 255), NULL);
    00587     }
    00588
    00589     raw->sdata.x = raw->shape->body->p.x-raw->sdata.currentimg->width/2;
    00590     raw->sdata.y = raw->shape->body->p.y-raw->sdata.currentimg->height/2;
    00591
    00592     if(raw->sdata.x+raw->sdata.currentimg->width <= 0)
    00593         remove_enemy(raw);
    00594 }
    00595
    00596 /*----------------------------------------------------------------------------------------------------------*/
    00597
    00598 void update_plant(SPRITESPTR plant)
    00599 {
    00600     int nflags = 0;
    00601     SIMAGEPTR stem = search_image_list_for_element(&(plant->ifirstptr), "STEM");
    00602
    00603     if(plant->sdata.x <= res_width/4)
    00604     {
    00605         if(plant->sdata.y+plant->sdata.currentimg->height <= 440)
    00606         {
    00607             cpBodySlew(plant->shape->body, cpv(plant->shape->body->p.x-180, plant->shape->body->p.y), 1);
    00608             plant->sdata.currentimg = search_image_list_for_element(&(plant->ifirstptr), "PLANT_1");
    00609         }
    00610         else
    00611             cpBodySlew(plant->shape->body, cpv(plant->shape->body->p.x-180, plant->shape->body->p.y-200), 1);
    00612     }
    00613     else
    00614         cpBodySlew(plant->shape->body, cpv(plant->shape->body->p.x-180, plant->shape->body->p.y), 1);
    00615
    00616     add_element_to_render_queue(plant->sdata.currentimg->image, plant->sdata.x, plant->sdata.y, nflags,
    00617                                 RCOLOR(255, 255, 255, 255), NULL);
    00618     add_element_to_render_queue(stem->image, plant->sdata.x, plant->sdata.y+plant->sdata.currentimg->height, nflags,
    00619                                 RCOLOR(255, 255, 255, 255), NULL);
    00620     plant->sdata.x = plant->shape->body->p.x-plant->sdata.currentimg->width/2;
    00621     plant->sdata.y = plant->shape->body->p.y-plant->sdata.currentimg->height/2;
    00622
    00623     if(plant->sdata.x+plant->sdata.currentimg->width <= 0)
    00624         remove_enemy(plant);
    00625 }
    00626
    00627 /*----------------------------------------------------------------------------------------------------------*/
    00628
    00629 void update_clouds(void)
    00630 {
    00631     SPRITESPTR clouds = search_sprite_list_for_element("CLOUD");
    00632     int nflags = 0;
    00633     static int m = -1;
    00634
    00635     clouds->sdata.x += m*clouds->sdata.dx;
    00636     if(clouds->sdata.x+clouds->sdata.currentimg->width < 0)
    00637     {
    00638        clouds->sdata.y = rand_int(res_height-90-clouds->sdata.currentimg->height);
    00639        m *= -1;
    00640     }
    00641     if(clouds->sdata.x > res_width)
    00642     {
    00643         clouds->sdata.y = rand_int(res_height-90-clouds->sdata.currentimg->height);
    00644         m *= -1;
    00645     }
    00646     add_element_to_render_queue(clouds->sdata.currentimg->image, clouds->sdata.x, clouds->sdata.y, nflags,
    00647                                 RCOLOR(255, 255, 255, 255), NULL);
    00648 }
    00649
    00650 /*----------------------------------------------------------------------------------------------------------*/
    00651
    00652 void update_ground(void)
    00653 {
    00654     SPRITESPTR ground = search_sprite_list_for_element("GROUND");
    00655     if(start)
    00656     {
    00657         ground->sdata.x -= ground->sdata.dx;
    00658         add_element_to_render_queue(ground->sdata.currentimg->image, ground->sdata.x, ground->sdata.y, 0,
    00659                                     RCOLOR(255, 255, 255, 255), NULL);
    00660         add_element_to_render_queue(ground->sdata.currentimg->image, ground->sdata.x+ground->sdata.currentimg->width,
    00661                                     ground->sdata.y, 0, RCOLOR(255, 255, 255, 255), NULL);
    00662         if(ground->sdata.x+ground->sdata.currentimg->width < 0)
    00663             ground->sdata.x = 0;
    00664     }
    00665     else
    00666         add_element_to_render_queue(ground->sdata.currentimg->image, ground->sdata.x, ground->sdata.y, 0,
    00667                                     RCOLOR(255, 255, 255, 255), NULL);
    00668 }
    00669
    00670 /*----------------------------------------------------------------------------------------------------------*/
    00671
    00672 // Updates the background image
    00673 void update_background(void)
    00674 {
    00675     SPRITESPTR background = search_sprite_list_for_element("BACKGROUND");
    00676     add_element_to_render_queue(background->sdata.currentimg->image, background->sdata.x, background->sdata.y,
    00677                                 0, RDEFAULT, NULL);
    00678 }
    00679
    00680 /*----------------------------------------------------------------------------------------------------------*/
    00681
    00682 void update_sprites(void *ptr, void* unused)
    00683 {
    00684     cpShape *shape = (cpShape*)ptr;
    00685
    00686     if(shape == NULL || shape->body == NULL || shape->data == NULL)
    00687         return;
    00688
    00689     if(strcmp((char*)shape->data, "STICKMAN") == 0)
    00690         update_stickman();
    00691     else if(strcmp(((SPRITES*)shape->data)->slabel, "AMEAN") == 0)
    00692         update_ameans((SPRITES*)shape->data);
    00693     else if(strcmp(((SPRITES*)shape->data)->slabel, "BRICK_WALL") == 0)
    00694         update_brickwall((SPRITES*)shape->data);
    00695     else if(strcmp(((SPRITES*)shape->data)->slabel, "CLAMP") == 0)
    00696         update_clamp((SPRITES*)shape->data);
    00697     else if(strcmp(((SPRITES*)shape->data)->slabel, "STEEL_WALL") == 0)
    00698         update_steelwall((SPRITES*)shape->data);
    00699     else if(strcmp(((SPRITES*)shape->data)->slabel, "FLYING_FIST") == 0)
    00700         update_fist((SPRITES*)shape->data);
    00701     else if(strcmp(((SPRITES*)shape->data)->slabel, "SPIKES") == 0)
    00702         update_spikes((SPRITES*)shape->data);
    00703     else if(strcmp(((SPRITES*)shape->data)->slabel, "RAW") == 0)
    00704         update_raw((SPRITES*)shape->data);
    00705     else if(strcmp(((SPRITES*)shape->data)->slabel, "PLANT") == 0)
    00706         update_plant((SPRITES*)shape->data);
    00707 }
    00708
    00709 /*----------------------------------------------------------------------------------------------------------*/
    00710
    00711 void logic(void)
    00712 {
    00713     static int counter = 0;
    00714     static _Bool startcounter = false;
    00715     if(!startcounter)
    00716     {
    00717         if(rand_int(1000) == 76 && start)
    00718         {
    00719             create_new_enemy("AMEAN");
    00720             startcounter = true;
    00721         }
    00722         else if(rand_int(1000) == 112 && start)
    00723         {
    00724             create_new_enemy("BRICK_WALL");
    00725             startcounter = true;
    00726         }
    00727         else if(rand_int(500) == 255 && start)
    00728         {
    00729             create_new_enemy("STEEL_WALL");
    00730             startcounter = true;
    00731         }
    00732         else if(rand_int(1000) == 305 && start)
    00733         {
    00734             create_new_enemy("SPIKES");
    00735             startcounter = true;
    00736         }
    00737         else if(rand_int(1000) == 456 && start)
    00738         {
    00739             create_new_enemy("FLYING_FIST");
    00740             startcounter = true;
    00741         }
    00742         else if(rand_int(5000) == 12 && start)
    00743         {
    00744             create_new_enemy("CLAMP");
    00745             startcounter = true;
    00746         }
    00747         else if(rand_int(1000) == 91 && start)
    00748         {
    00749             create_new_enemy("RAW");
    00750             startcounter = true;
    00751         }
    00752         else if(rand_int(1000) == 945 && start)
    00753         {
    00754             create_new_enemy("PLANT");
    00755             startcounter = true;
    00756         }
    00757     }
    00758     else
    00759     {
    00760         counter++;
    00761         if(counter >= 120)
    00762         {
    00763             counter = 0;
    00764             startcounter = false;
    00765         }
    00766     }
    00767 }
    00768
    00769 /*----------------------------------------------------------------------------------------------------------*/
    00770
    00771 void maingame_cleanup(void)
    00772 {
    00773     SPRITESPTR stman = search_sprite_list_for_element("STICKMAN");
    00774     SPRITESPTR ground = search_sprite_list_for_element("GROUND");
    00775     GDATAPTR game = get_game_data();
    00776     cpSpace *space = get_global_cpSpace();
    00777     cpSpaceRemoveCollisionHandler(space, 1, 2);
    00778     remove_physics_object(stman);
    00779     remove_element_from_sprite_list_ptr(stman);
    00780     remove_element_from_sprite_list("CLOUD");
    00781
    00782     ground->sdata.x = 0;
    00783     ground->sdata.y = 510;
    00784
    00785     remove_all_enemies();
    00786
    00787     game->gamestarted = false;
    00788
    00789     start = false;
    00790     lives = 3;
    00791     score = 0;
    00792     minutes = 0;
    00793     seconds = 0;
    00794 }
    00795
    00796 /*----------------------------------------------------------------------------------------------------------*/
    00797
    00798 //    /**
    00799 //      *@}
    00800 //      */