From c5e37bf9d5248c45b87bae080781ab2163026f39 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Thu, 27 Sep 2012 02:48:45 +0200 Subject: [PATCH] refactor Level gamestate and introduce level modules --- src/CMakeLists.txt | 6 +- src/level.c | 422 ++++++++-------------------- src/levels/actions.c | 203 ------------- src/levels/actions.h | 21 -- src/levels/dodger.c | 269 ++++++++++++++++++ src/levels/dodger.h | 31 ++ src/levels/dodger/actions.c | 228 +++++++++++++++ src/levels/dodger/actions.h | 44 +++ src/levels/{ => dodger}/callbacks.c | 2 +- src/levels/{ => dodger}/callbacks.h | 4 +- src/{ => levels}/moonwalk.c | 25 +- src/{ => levels}/moonwalk.h | 9 +- src/main.c | 4 - src/main.h | 41 ++- src/timeline.c | 13 +- 15 files changed, 735 insertions(+), 587 deletions(-) create mode 100644 src/levels/dodger.c create mode 100644 src/levels/dodger.h create mode 100644 src/levels/dodger/actions.c create mode 100644 src/levels/dodger/actions.h rename src/levels/{ => dodger}/callbacks.c (97%) rename src/levels/{ => dodger}/callbacks.h (93%) rename src/{ => levels}/moonwalk.c (80%) rename src/{ => levels}/moonwalk.h (78%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1301650..8f99863 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,11 +9,13 @@ SET(SRC_LIST main.c map.c menu.c - moonwalk.c pause.c timeline.c + levels/moonwalk.c + levels/dodger.c levels/actions.c - levels/callbacks.c + levels/dodger/actions.c + levels/dodger/callbacks.c ) find_package(Allegro5 REQUIRED) diff --git a/src/level.c b/src/level.c index c610641..de69d31 100644 --- a/src/level.c +++ b/src/level.c @@ -21,14 +21,14 @@ #include #include #include "levels/actions.h" -#include "moonwalk.h" +#include "levels/dodger/actions.h" +#include "levels/moonwalk.h" +#include "levels/dodger.h" #include "config.h" #include "pause.h" #include "level.h" #include "timeline.h" -/* TODO: check if spritesheet speed still depends on FPS */ - void SelectDerpySpritesheet(struct Game *game, char* name) { struct Spritesheet *tmp = game->level.derpy_sheets; PrintConsole(game, "Selecting Derpy spritesheet: %s", name); @@ -100,131 +100,39 @@ void Level_Passed(struct Game *game) { } void Level_Logic(struct Game *game) { - if (game->level.current_level!=1) { - Moonwalk_Logic(game); + if (game->level.current_level==1) { + Dodger_Logic(game); } else { - struct ALLEGRO_KEYBOARD_STATE keyboard; - al_get_keyboard_state(&keyboard); - if (game->level.handle_input) { - if (game->level.derpy_angle > 0) { game->level.derpy_angle -= 0.02; if (game->level.derpy_angle < 0) game->level.derpy_angle = 0; } - if (game->level.derpy_angle < 0) { game->level.derpy_angle += 0.02; if (game->level.derpy_angle > 0) game->level.derpy_angle = 0; } - if (al_key_down(&keyboard, ALLEGRO_KEY_UP)) { - game->level.derpy_y -= 0.005; - game->level.derpy_angle -= 0.03; - if (game->level.derpy_angle < -0.15) game->level.derpy_angle = -0.15; - /*PrintConsole(game, "Derpy Y position: %f", game->level.derpy_y);*/ - } - if (al_key_down(&keyboard, ALLEGRO_KEY_DOWN)) { - game->level.derpy_y += 0.005; - game->level.derpy_angle += 0.03; - if (game->level.derpy_angle > 0.15) game->level.derpy_angle = 0.15; - /*PrintConsole(game, "Derpy Y position: %f", game->level.derpy_y);*/ - } - /*if ((game->level.derpy_y > 0.6) && (game->level.flying)) { - SelectDerpySpritesheet(game, "run"); - game->level.flying = false; - game->level.sheet_speed = tps(game, 60*0.0020/game->level.speed); - } - else if ((game->level.derpy_y <= 0.6) && (!game->level.flying)) { - SelectDerpySpritesheet(game, "fly"); - game->level.flying = true; - game->level.sheet_speed = tps(game, 60*2.4); - } - if (!game->level.flying) game->level.sheet_speed = tps(game, 60*0.0020/game->level.speed); */ - if (game->level.derpy_y < 0) game->level.derpy_y=0; - else if (game->level.derpy_y > 0.8) game->level.derpy_y=0.8; - - game->level.derpy_y += game->level.derpy_angle / 30; - } - - int derpyx = game->level.derpy_x*game->viewportWidth; - int derpyy = game->level.derpy_y*game->viewportHeight; - int derpyw = al_get_bitmap_width(game->level.derpy); - int derpyh = al_get_bitmap_height(game->level.derpy); - int derpyo = game->viewportWidth*0.1953125-al_get_bitmap_width(game->level.derpy); /* offset */ - struct Obstacle *tmp = game->level.obstacles; - while (tmp) { - /*PrintConsole(game, "DRAWING %f %f", tmp->x, tmp->y);*/ - int x = (tmp->x/100.0)*game->viewportWidth; - int y = (tmp->y/100.0)*game->viewportHeight; - int w = 0, h = 0; - if (tmp->bitmap) { - w = al_get_bitmap_width(*(tmp->bitmap))/tmp->cols; - h = al_get_bitmap_height(*(tmp->bitmap))/tmp->rows; - } - if (x > -w) { - /*if (!tmp->hit)*/ - if ((((x>=derpyx+0.38*derpyw+derpyo) && (x<=derpyx+0.94*derpyw+derpyo)) || ((x+w>=derpyx+0.38*derpyw+derpyo) && (x+w<=derpyx+0.94*derpyw+derpyo)) || ((x<=derpyx+0.38*derpyw+derpyo) && (x+w>=derpyx+0.94*derpyw+derpyo))) && - (((y>=derpyy+0.26*derpyh) && (y<=derpyy+0.76*derpyh)) || ((y+h>=derpyy+0.26*derpyh) && (y+h<=derpyy+0.76*derpyh)) || ((y<=derpyy+0.26*derpyh) && (y+h>=derpyy+0.76*derpyh)))) { - tmp->hit=true; - } - - if (tmp->anim_speed) { - tmp->tmp_pos+=1; - if (tmp->tmp_pos >= tmp->anim_speed) { - tmp->pos++; - tmp->tmp_pos = 0; - } - if (tmp->pos>=tmp->cols*tmp->rows-tmp->blanks) tmp->pos=0; - } - - if (tmp->hit) { - if (tmp->points>=0) tmp->bitmap = NULL; - game->level.hp+=0.0002*tmp->points*(((1-game->level.speed_modifier)/2.0)+1); - if (game->level.hp>1) game->level.hp=1; - //PrintConsole(game, "POINTS: %d, %f", tmp->points, tps(game, 60*0.0002*tmp->points*game->level.speed_modifier)); - if ((game->level.hp<=0) && (!game->level.failed)) { - game->level.failed = true; - game->level.handle_input = false; - game->level.speed_modifier = 1; - TM_AddBackgroundAction(&LevelFailed, NULL, 0, "levelfailed"); - } - } - tmp->x -= game->level.speed*game->level.speed_modifier*tmp->speed*100*al_get_bitmap_width(game->level.stage)/(float)game->viewportWidth; - if (tmp->callback) tmp->callback(game, tmp); - tmp = tmp->next; - } else { - if (tmp->next) - tmp->next->prev = tmp->prev; - if (tmp->prev) - tmp->prev->next = tmp->next; - else - game->level.obstacles = tmp->next; - struct Obstacle *t = tmp; - tmp = tmp->next; - free(t); - } - } - /*if (colision) game->level.hp-=tps(game, 60*0.002);*/ - - if ((game->level.sheet_speed) && (game->level.sheet_speed_modifier)) { - game->level.sheet_tmp+=1; - if (game->level.sheet_tmp >= (game->level.sheet_speed/game->level.speed_modifier)/game->level.sheet_speed_modifier) { - game->level.sheet_pos++; - game->level.sheet_tmp = 0; - } - if (game->level.sheet_pos>=game->level.sheet_cols*game->level.sheet_rows-game->level.sheet_blanks) { - game->level.sheet_pos=0; - if (game->level.sheet_successor) { - SelectDerpySpritesheet(game, game->level.sheet_successor); - } - } - } - - if (game->level.speed > 0) { - game->level.cl_pos += game->level.speed*game->level.speed_modifier * 0.2; - game->level.bg_pos += game->level.speed*game->level.speed_modifier * 0.6; - game->level.st_pos += game->level.speed*game->level.speed_modifier * 1; - game->level.fg_pos += game->level.speed*game->level.speed_modifier * 1.75; - if (game->level.bg_pos >= 1) game->level.bg_pos=game->level.bg_pos-1; - if (game->level.st_pos >= 1) game->level.st_pos=game->level.st_pos-1; - if (game->level.fg_pos >= 1) game->level.fg_pos=game->level.fg_pos-1; - } - game->level.cl_pos += 0.00005; - if (game->level.cl_pos >= 1) game->level.cl_pos=game->level.cl_pos-1; - - TM_Process(); + Moonwalk_Logic(game); } + + if ((game->level.sheet_speed) && (game->level.sheet_speed_modifier)) { + game->level.sheet_tmp+=1; + if (game->level.sheet_tmp >= (game->level.sheet_speed/game->level.speed_modifier)/game->level.sheet_speed_modifier) { + game->level.sheet_pos++; + game->level.sheet_tmp = 0; + } + if (game->level.sheet_pos>=game->level.sheet_cols*game->level.sheet_rows-game->level.sheet_blanks) { + game->level.sheet_pos=0; + if (game->level.sheet_successor) { + SelectDerpySpritesheet(game, game->level.sheet_successor); + } + } + } + + if (game->level.speed > 0) { + game->level.cl_pos += game->level.speed*game->level.speed_modifier * 0.2; + game->level.bg_pos += game->level.speed*game->level.speed_modifier * 0.6; + game->level.st_pos += game->level.speed*game->level.speed_modifier * 1; + game->level.fg_pos += game->level.speed*game->level.speed_modifier * 1.75; + if (game->level.bg_pos >= 1) game->level.bg_pos=game->level.bg_pos-1; + if (game->level.st_pos >= 1) game->level.st_pos=game->level.st_pos-1; + if (game->level.fg_pos >= 1) game->level.fg_pos=game->level.fg_pos-1; + } + game->level.cl_pos += 0.00005; + if (game->level.cl_pos >= 1) game->level.cl_pos=game->level.cl_pos-1; + + TM_Process(); } void Level_Resume(struct Game *game) { @@ -244,93 +152,33 @@ void Level_Pause(struct Game *game) { } void Level_Draw(struct Game *game) { - if (game->level.current_level!=1) Moonwalk_Draw(game); - else { + al_draw_bitmap(game->level.clouds, (-game->level.cl_pos)*al_get_bitmap_width(game->level.clouds), 0, 0); + al_draw_bitmap(game->level.clouds, (1+(-game->level.cl_pos))*al_get_bitmap_width(game->level.clouds), 0, 0); + al_draw_bitmap(game->level.background, (-game->level.bg_pos)*al_get_bitmap_width(game->level.background), 0, 0); + al_draw_bitmap(game->level.background, (1+(-game->level.bg_pos))*al_get_bitmap_width(game->level.background), 0, 0); + al_draw_bitmap(game->level.stage, (-game->level.st_pos)*al_get_bitmap_width(game->level.stage), 0 ,0); + al_draw_bitmap(game->level.stage, (1+(-game->level.st_pos))*al_get_bitmap_width(game->level.stage), 0 ,0); - al_draw_bitmap(game->level.clouds, (-game->level.cl_pos)*al_get_bitmap_width(game->level.clouds), 0, 0); - al_draw_bitmap(game->level.clouds, (1+(-game->level.cl_pos))*al_get_bitmap_width(game->level.clouds), 0, 0); - al_draw_bitmap(game->level.background, (-game->level.bg_pos)*al_get_bitmap_width(game->level.background), 0, 0); - al_draw_bitmap(game->level.background, (1+(-game->level.bg_pos))*al_get_bitmap_width(game->level.background), 0, 0); - al_draw_bitmap(game->level.stage, (-game->level.st_pos)*al_get_bitmap_width(game->level.stage), 0 ,0); - al_draw_bitmap(game->level.stage, (1+(-game->level.st_pos))*al_get_bitmap_width(game->level.stage), 0 ,0); + if (game->level.current_level==1) Dodger_Draw(game); + else Moonwalk_Draw(game); - int derpyx = game->level.derpy_x*game->viewportWidth; - int derpyy = game->level.derpy_y*game->viewportHeight; - int derpyw = al_get_bitmap_width(game->level.derpy); - int derpyh = al_get_bitmap_height(game->level.derpy); - int derpyo = game->viewportWidth*0.1953125-al_get_bitmap_width(game->level.derpy); /* offset */ - bool colision = false; - struct Obstacle *tmp = game->level.obstacles; - while (tmp) { - /*PrintConsole(game, "DRAWING %f %f", tmp->x, tmp->y);*/ - int x = (tmp->x/100.0)*game->viewportWidth; - int y = (tmp->y/100.0)*game->viewportHeight; - int w = 0, h = 0; - if (tmp->bitmap) { - w = al_get_bitmap_width(*(tmp->bitmap))/tmp->cols; - h = al_get_bitmap_height(*(tmp->bitmap))/tmp->rows; - } - if (x > -w) { - if ((tmp->hit) && (tmp->points<0)) { - colision = true; - } + if (game->level.unloading) return; - if (tmp->bitmap) { - ALLEGRO_BITMAP* subbitmap = al_create_sub_bitmap(*(tmp->bitmap),w*(tmp->pos%tmp->cols), h*(tmp->pos/tmp->cols),w,h); - al_draw_rotated_bitmap(subbitmap,w/2.0, h/2.0, x+w/2.0,y+h/2.0, tmp->angle, 0); - al_destroy_bitmap(subbitmap); - } + al_draw_bitmap(game->level.foreground, (-game->level.fg_pos)*al_get_bitmap_width(game->level.foreground), 0 ,0); + al_draw_bitmap(game->level.foreground, (1+(-game->level.fg_pos))*al_get_bitmap_width(game->level.foreground), 0 ,0); - /*al_draw_bitmap(*(tmp->bitmap), x, y, 0);*/ - if (game->level.debug_show_sprite_frames) al_draw_rectangle(x, y, x+w, y+h, al_map_rgba(255,0,0,255), 3); + al_set_target_bitmap(game->level.meter_bmp); + al_clear_to_color(al_map_rgba(0,0,0,0)); + al_draw_filled_rounded_rectangle(al_get_bitmap_width(game->level.meter_bmp)*0.1, al_get_bitmap_height(game->level.meter_bmp)*0.34, al_get_bitmap_width(game->level.meter_bmp)*0.993, al_get_bitmap_height(game->level.meter_bmp)*0.66, + 6,6, al_map_rgb(232,234,239)); + al_draw_horizontal_gradient_rect(al_get_bitmap_width(game->level.meter_bmp)-game->viewportWidth*0.215, (al_get_bitmap_height(game->level.meter_bmp)-game->viewportHeight*0.025)/2, game->viewportWidth*0.215*0.975, game->viewportHeight*0.025, al_map_rgb(150,159,182), al_map_rgb(130,139,162)); + al_draw_filled_rectangle(al_get_bitmap_width(game->level.meter_bmp)-game->viewportWidth*0.215, (al_get_bitmap_height(game->level.meter_bmp)-game->viewportHeight*0.025)/2, al_get_bitmap_width(game->level.meter_bmp)-game->viewportWidth*0.215+(game->viewportWidth*0.215*0.975)*game->level.hp, (al_get_bitmap_height(game->level.meter_bmp)-game->viewportHeight*0.025)/2+game->viewportHeight*0.025, al_map_rgb(214,172,55)); + al_draw_bitmap(game->level.meter_image, 0, 0, 0); + al_set_target_bitmap(al_get_backbuffer(game->display)); - tmp = tmp->next; - } else { - if (tmp->next) - tmp->next->prev = tmp->prev; - if (tmp->prev) - tmp->prev->next = tmp->next; - else - game->level.obstacles = tmp->next; - struct Obstacle *t = tmp; - tmp = tmp->next; - free(t); - } - } - /*if (colision) game->level.hp-=tps(game, 60*0.002);*/ + al_draw_tinted_bitmap(game->level.meter_bmp, al_map_rgba(game->level.meter_alpha,game->level.meter_alpha,game->level.meter_alpha,game->level.meter_alpha), game->viewportWidth*0.95-al_get_bitmap_width(game->level.meter_bmp), game->viewportHeight*0.975-al_get_bitmap_height(game->level.meter_bmp), 0); - al_set_target_bitmap(game->level.derpy); - al_clear_to_color(al_map_rgba(0,0,0,0)); - al_draw_bitmap_region(*(game->level.derpy_sheet),al_get_bitmap_width(game->level.derpy)*(game->level.sheet_pos%game->level.sheet_cols),al_get_bitmap_height(game->level.derpy)*(game->level.sheet_pos/game->level.sheet_cols),al_get_bitmap_width(game->level.derpy), al_get_bitmap_height(game->level.derpy),0,0,0); - al_set_target_bitmap(al_get_backbuffer(game->display)); - - al_draw_tinted_rotated_bitmap(game->level.derpy, al_map_rgba(255,255-colision*255,255-colision*255,255), al_get_bitmap_width(game->level.derpy), al_get_bitmap_height(game->level.derpy)/2, derpyx+game->viewportWidth*0.1953125, derpyy + al_get_bitmap_height(game->level.derpy)/2, game->level.derpy_angle, 0); - - /* if ((((x>=derpyx+0.36*derpyw) && (x<=derpyx+0.94*derpyw)) || ((x+w>=derpyx+0.36*derpyw) && (x+w<=derpyx+0.94*derpyw))) && - (((y>=derpyy+0.26*derpyh) && (y<=derpyy+0.76*derpyh)) || ((y+h>=derpyy+0.26*derpyh) && (y+h<=derpyy+0.76*derpyh)))) { - */ - if (game->level.debug_show_sprite_frames) { - al_draw_rectangle(derpyx+derpyo, derpyy, derpyx+derpyw+derpyo, derpyy+derpyh, al_map_rgba(0,255,0,255), 3); - al_draw_rectangle(derpyx+0.38*derpyw+derpyo, derpyy+0.26*derpyh, derpyx+0.94*derpyw+derpyo, derpyy+0.76*derpyh, al_map_rgba(0,0,255,255), 3); - } - - al_draw_bitmap(game->level.foreground, (-game->level.fg_pos)*al_get_bitmap_width(game->level.foreground), 0 ,0); - al_draw_bitmap(game->level.foreground, (1+(-game->level.fg_pos))*al_get_bitmap_width(game->level.foreground), 0 ,0); - - - al_set_target_bitmap(game->level.meter_bmp); - al_clear_to_color(al_map_rgba(0,0,0,0)); - al_draw_filled_rounded_rectangle(al_get_bitmap_width(game->level.meter_bmp)*0.1, al_get_bitmap_height(game->level.meter_bmp)*0.34, al_get_bitmap_width(game->level.meter_bmp)*0.993, al_get_bitmap_height(game->level.meter_bmp)*0.66, - 6,6, al_map_rgb(232,234,239)); - al_draw_horizontal_gradient_rect(al_get_bitmap_width(game->level.meter_bmp)-game->viewportWidth*0.215, (al_get_bitmap_height(game->level.meter_bmp)-game->viewportHeight*0.025)/2, game->viewportWidth*0.215*0.975, game->viewportHeight*0.025, al_map_rgb(150,159,182), al_map_rgb(130,139,162)); - al_draw_filled_rectangle(al_get_bitmap_width(game->level.meter_bmp)-game->viewportWidth*0.215, (al_get_bitmap_height(game->level.meter_bmp)-game->viewportHeight*0.025)/2, al_get_bitmap_width(game->level.meter_bmp)-game->viewportWidth*0.215+(game->viewportWidth*0.215*0.975)*game->level.hp, (al_get_bitmap_height(game->level.meter_bmp)-game->viewportHeight*0.025)/2+game->viewportHeight*0.025, al_map_rgb(214,172,55)); - al_draw_bitmap(game->level.meter_image, 0, 0, 0); - al_set_target_bitmap(al_get_backbuffer(game->display)); - - al_draw_tinted_bitmap(game->level.meter_bmp, al_map_rgba(game->level.meter_alpha,game->level.meter_alpha,game->level.meter_alpha,game->level.meter_alpha), game->viewportWidth*0.95-al_get_bitmap_width(game->level.meter_bmp), game->viewportHeight*0.975-al_get_bitmap_height(game->level.meter_bmp), 0); - - TM_Draw(); - } + TM_Draw(); } void Level_Load(struct Game *game) { @@ -345,16 +193,16 @@ void Level_Load(struct Game *game) { game->level.derpy_x = -0.2; game->level.derpy_y = 0.6; game->level.derpy_angle = 0; - game->level.sheet_speed = tps(game, 60*2.4); + game->level.sheet_speed = 2.4; game->level.sheet_tmp = 0; game->level.handle_input = false; - game->level.obstacles = NULL; game->level.meter_alpha=0; game->level.debug_show_sprite_frames=false; al_clear_to_color(al_map_rgb(0,0,0)); + TM_Init(game); if (game->level.current_level!=1) Moonwalk_Load(game); else { - TM_Init(game); + Dodger_Load(game); TM_AddBackgroundAction(&FadeIn, NULL, 0, "fadein"); TM_AddDelay(1000); TM_AddQueuedBackgroundAction(&Welcome, NULL, 0, "welcome"); @@ -385,6 +233,7 @@ void Level_Load(struct Game *game) { // cutscene goes here */ TM_AddAction(&PassLevel, NULL, "passlevel"); + // init level specific obstacle for Dodger module struct Obstacle *obst = malloc(sizeof(struct Obstacle)); obst->prev = NULL; obst->next = NULL; @@ -403,7 +252,7 @@ void Level_Load(struct Game *game) { obst->callback = NULL; obst->data = NULL; obst->bitmap = &(game->level.owl); - game->level.obstacles = obst; + game->level.dodger.obstacles = obst; } } @@ -417,14 +266,8 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { } else if ((game->debug) && (ev->keyboard.keycode==ALLEGRO_KEY_F4)) { game->level.debug_show_sprite_frames = !game->level.debug_show_sprite_frames; } - if (game->level.current_level!=1) Moonwalk_Keydown(game, ev); - else if (game->level.handle_input) { - if (ev->keyboard.keycode==ALLEGRO_KEY_LEFT) { - game->level.speed_modifier = 0.75; - } else if (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT) { - game->level.speed_modifier = 1.3; - } - } + if (game->level.current_level==1) Dodger_Keydown(game, ev); + else Moonwalk_Keydown(game, ev); if (ev->keyboard.keycode==ALLEGRO_KEY_ESCAPE) { game->gamestate = GAMESTATE_PAUSE; game->loadstate = GAMESTATE_LEVEL; @@ -434,24 +277,9 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { } void Level_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev) { - if (game->level.handle_input) { - if ((ev->type==ALLEGRO_EVENT_KEY_UP) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) { - game->level.speed_modifier = 1; - struct ALLEGRO_KEYBOARD_STATE keyboard; - al_get_keyboard_state(&keyboard); - if (al_key_down(&keyboard, ALLEGRO_KEY_RIGHT)) { - game->level.speed_modifier = 1.3; - } - } else if ((ev->type==ALLEGRO_EVENT_KEY_UP) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) { - game->level.speed_modifier = 1; - struct ALLEGRO_KEYBOARD_STATE keyboard; - al_get_keyboard_state(&keyboard); - if (al_key_down(&keyboard, ALLEGRO_KEY_LEFT)) { - game->level.speed_modifier = 0.75; - } - } - } - if (game->level.current_level==1) TM_HandleEvent(ev); + if (game->level.current_level==1) Dodger_ProcessEvent(game, ev); + else Moonwalk_ProcessEvent(game, ev); + TM_HandleEvent(ev); } void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)) { @@ -460,12 +288,18 @@ void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)) { game->level.derpy = NULL; game->level.unloading = false; Pause_Preload(game); - RegisterDerpySpritesheet(game, "walk"); - RegisterDerpySpritesheet(game, "fly"); - RegisterDerpySpritesheet(game, "run"); RegisterDerpySpritesheet(game, "stand"); - if (game->level.current_level!=1) Moonwalk_Preload(game); + if (game->level.current_level!=1) { + RegisterDerpySpritesheet(game, "walk"); + Moonwalk_Preload(game, progress); + } else { + RegisterDerpySpritesheet(game, "walk"); + RegisterDerpySpritesheet(game, "fly"); + RegisterDerpySpritesheet(game, "run"); + + Dodger_Preload(game, progress); + game->level.sample = al_load_sample( GetDataFilePath("levels/1/music.flac") ); game->level.music = al_create_sample_instance(game->level.sample); al_attach_sample_instance_to_mixer(game->level.music, game->audio.music); @@ -484,28 +318,12 @@ void Level_Unload(struct Game *game) { game->level.unloading = true; Pause_Unload_Real(game); FadeGameState(game, false); - if (game->level.current_level!=1) Moonwalk_Unload(game); - else { - TM_Destroy(); - } al_destroy_sample_instance(game->level.music); al_destroy_sample(game->level.sample); - struct Obstacle *t = game->level.obstacles; - if (t) { - while (t->next) { - if (t->prev) free(t->prev); - t = t->next; - } - free(t); - } Level_UnloadBitmaps(game); - struct Spritesheet *tmp, *s = game->level.derpy_sheets; - tmp = s; - while (s) { - tmp = s; - s = s->next; - free(tmp); - } + if (game->level.current_level!=1) Moonwalk_Unload(game); + else Dodger_Unload(game); + TM_Destroy(); } void Level_UnloadBitmaps(struct Game *game) { @@ -517,24 +335,18 @@ void Level_UnloadBitmaps(struct Game *game) { } if (game->level.current_level!=1) Moonwalk_UnloadBitmaps(game); else { + Dodger_UnloadBitmaps(game); al_destroy_font(game->level.letter_font); - al_destroy_bitmap(game->level.foreground); - al_destroy_bitmap(game->level.background); - al_destroy_bitmap(game->level.stage); - al_destroy_bitmap(game->level.clouds); - al_destroy_bitmap(game->level.welcome); al_destroy_bitmap(game->level.letter); al_destroy_bitmap(game->level.owl); - al_destroy_bitmap(game->level.obst_bmps.pie1); - al_destroy_bitmap(game->level.obst_bmps.pie2); - al_destroy_bitmap(game->level.obst_bmps.pig); - al_destroy_bitmap(game->level.obst_bmps.cherry); - al_destroy_bitmap(game->level.obst_bmps.muffin); - al_destroy_bitmap(game->level.obst_bmps.badmuffin); - al_destroy_bitmap(game->level.obst_bmps.screwball); - al_destroy_bitmap(game->level.meter_bmp); - al_destroy_bitmap(game->level.meter_image); } + al_destroy_bitmap(game->level.foreground); + al_destroy_bitmap(game->level.background); + al_destroy_bitmap(game->level.clouds); + al_destroy_bitmap(game->level.stage); + al_destroy_bitmap(game->level.meter_bmp); + al_destroy_bitmap(game->level.meter_image); + al_destroy_bitmap(game->level.welcome); } void Level_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, float)) { @@ -546,7 +358,7 @@ void Level_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, floa tmp = tmp->next; } if (game->level.current_level==1) load_a+=x; - else load_a=2+x; + else load_a=9+x; tmp = game->level.derpy_sheets; while (tmp) { @@ -561,31 +373,30 @@ void Level_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, floa game->level.derpy = al_create_bitmap(al_get_bitmap_width(*(game->level.derpy_sheet))/game->level.sheet_cols, al_get_bitmap_height(*(game->level.derpy_sheet))/game->level.sheet_rows); - if (game->level.current_level!=1) Moonwalk_PreloadBitmaps(game); + /* TODO: maybe handle strange display aspects */ + game->level.clouds = LoadScaledBitmap("levels/1/clouds.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); + PROGRESS; + game->level.foreground = LoadScaledBitmap("levels/1/foreground.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); + PROGRESS; + game->level.background = LoadScaledBitmap("levels/1/background.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); + PROGRESS; + game->level.stage = LoadScaledBitmap("levels/1/stage.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); + PROGRESS; + game->level.meter_image = LoadScaledBitmap("levels/meter.png", game->viewportWidth*0.075, game->viewportWidth*0.075*0.96470588235294117647); + PROGRESS; + game->level.meter_bmp = al_create_bitmap(game->viewportWidth*0.2+al_get_bitmap_width(game->level.meter_image), al_get_bitmap_height(game->level.meter_image)); + PROGRESS; + game->level.welcome = al_create_bitmap(game->viewportWidth, game->viewportHeight/2); + PROGRESS; + al_set_target_bitmap(game->level.welcome); + al_clear_to_color(al_map_rgba(0,0,0,0)); + al_draw_text_with_shadow(game->menu.font_title, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.1, ALLEGRO_ALIGN_CENTRE, "Level 1"); + al_draw_text_with_shadow(game->menu.font_subtitle, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.275, ALLEGRO_ALIGN_CENTRE, "Fluttershy"); + PROGRESS; + + + if (game->level.current_level!=1) Moonwalk_PreloadBitmaps(game, progress); else { - /* TODO: handle strange display aspects */ - game->level.clouds = LoadScaledBitmap("levels/1/clouds.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); - PROGRESS; - game->level.foreground = LoadScaledBitmap("levels/1/foreground.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); - PROGRESS; - game->level.background = LoadScaledBitmap("levels/1/background.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); - PROGRESS; - game->level.stage = LoadScaledBitmap("levels/1/stage.png", game->viewportHeight*4.73307291666666666667, game->viewportHeight); - PROGRESS; - game->level.obst_bmps.pie1 = LoadScaledBitmap("levels/pie1.png", game->viewportWidth*0.1, game->viewportHeight*0.08); - PROGRESS; - game->level.obst_bmps.pie2 = LoadScaledBitmap("levels/pie2.png", game->viewportWidth*0.1, game->viewportHeight*0.08); - PROGRESS; - game->level.obst_bmps.pig = LoadScaledBitmap("levels/pig.png", (int)(game->viewportWidth*0.15)*3, (int)(game->viewportHeight*0.2)*3); - PROGRESS; - game->level.obst_bmps.screwball = LoadScaledBitmap("levels/screwball.png", (int)(game->viewportHeight*0.2)*4*1.4, (int)(game->viewportHeight*0.2)*4); - PROGRESS; - game->level.obst_bmps.muffin = LoadScaledBitmap("levels/muffin.png", game->viewportWidth*0.07, game->viewportHeight*0.1); - PROGRESS; - game->level.obst_bmps.cherry = LoadScaledBitmap("levels/cherry.png", game->viewportWidth*0.03, game->viewportHeight*0.08); - PROGRESS; - game->level.obst_bmps.badmuffin = LoadScaledBitmap("levels/badmuffin.png", game->viewportWidth*0.07, game->viewportHeight*0.1); - PROGRESS; game->level.owl = LoadScaledBitmap("levels/owl.png", game->viewportWidth*0.08, game->viewportWidth*0.08); PROGRESS; game->level.letter_font = al_load_ttf_font(GetDataFilePath("fonts/DejaVuSans.ttf"),game->viewportHeight*0.0225,0 ); @@ -632,19 +443,8 @@ void Level_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, floa al_draw_text_with_shadow(game->menu.font, al_map_rgb(255,255,255), al_get_bitmap_width(game->level.letter)*0.5, al_get_bitmap_height(game->level.letter)*0.8, ALLEGRO_ALIGN_CENTRE, "Press enter to continue..."); al_set_target_bitmap(al_get_backbuffer(game->display)); PROGRESS; - game->level.welcome = al_create_bitmap(game->viewportWidth, game->viewportHeight/2); - PROGRESS; - al_set_target_bitmap(game->level.welcome); - al_clear_to_color(al_map_rgba(0,0,0,0)); - al_draw_text_with_shadow(game->menu.font_title, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.1, ALLEGRO_ALIGN_CENTRE, "Level 1"); - al_draw_text_with_shadow(game->menu.font_subtitle, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.275, ALLEGRO_ALIGN_CENTRE, "Fluttershy"); - PROGRESS; - - game->level.meter_image = LoadScaledBitmap("levels/meter.png", game->viewportWidth*0.075, game->viewportWidth*0.075*0.96470588235294117647); - PROGRESS; - game->level.meter_bmp = al_create_bitmap(game->viewportWidth*0.2+al_get_bitmap_width(game->level.meter_image), al_get_bitmap_height(game->level.meter_image)); + Dodger_PreloadBitmaps(game, progress); al_set_target_bitmap(al_get_backbuffer(game->display)); } - PROGRESS; } diff --git a/src/levels/actions.c b/src/levels/actions.c index 6f72120..b5f7b74 100644 --- a/src/levels/actions.c +++ b/src/levels/actions.c @@ -20,7 +20,6 @@ */ #include "actions.h" -#include "callbacks.h" #include "../level.h" bool LevelFailed(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { @@ -36,30 +35,6 @@ bool LevelFailed(struct Game *game, struct TM_Action *action, enum TM_ActionStat return true; } -bool Accelerate(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state != TM_ACTIONSTATE_RUNNING) return false; - game->level.speed+=0.000015; - if (game->level.speed<0.0025) return false; - return true; -} - -bool Walk(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!(action->arguments)) SelectDerpySpritesheet(game, "walk"); - action->arguments++; - game->level.derpy_x+=0.001; - if (game->level.derpy_x<0.05) return false; - return true; -} - -bool Move(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state != TM_ACTIONSTATE_RUNNING) return false; - game->level.speed=0.00035; - if (game->level.st_pos<0.275) return false; - return true; -} - bool ShowMeter(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { if (state != TM_ACTIONSTATE_RUNNING) return false; game->level.meter_alpha+=4; @@ -70,137 +45,6 @@ bool ShowMeter(struct Game *game, struct TM_Action *action, enum TM_ActionState return false; } -bool Fly(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!(action->arguments)) { - SelectDerpySpritesheet(game, "fly"); - game->level.derpy_angle = -0.15; - /*game->level.gg = true;*/ - TM_AddBackgroundAction(&ShowMeter, NULL, 0, "showmeter"); - action->arguments++; - } - game->level.derpy_y-=0.004; - if (game->level.derpy_y>0.2) return false; - game->level.handle_input=true; - return true; -} - -bool Run(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!(action->arguments)) { - game->level.handle_input=false; - game->level.speed_modifier=1; - action->arguments++; - } - game->level.derpy_y+=0.0042; - if (game->level.derpy_angle > 0) { game->level.derpy_angle -= 0.02; if (game->level.derpy_angle < 0) game->level.derpy_angle = 0; } - if (game->level.derpy_angle < 0) { game->level.derpy_angle += 0.02; if (game->level.derpy_angle > 0) game->level.derpy_angle = 0; } - if (game->level.derpy_y<0.65) return false; - game->level.derpy_angle = 0; - SelectDerpySpritesheet(game, "run"); - return true; -} - -bool GenerateObstacles(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - /*float* tmp; bool* in;*/ - int* count; - if (!action->arguments) { - action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(int))); - /* action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(bool))); */ - } - count = (int*)action->arguments->value; - /*tmp = (float*)action->arguments->value; - in = (bool*)action->arguments->next->value;*/ - if (state == TM_ACTIONSTATE_INIT) { - *count = 0; - /* *tmp = 0; - *in = true;*/ - } - else if (state == TM_ACTIONSTATE_RUNNING) { - if (rand()%(10000/(int)(85*game->level.speed_modifier))<=3) { - PrintConsole(game, "OBSTACLE %d", *count); - (*count)++; - struct Obstacle *obst = malloc(sizeof(struct Obstacle)); - obst->prev = NULL; - obst->x = 100; - obst->y = (rand()%91)-1; - obst->speed = 1; - obst->points = -10; - obst->hit = false; - obst->rows = 1; - obst->cols = 1; - obst->pos = 0; - obst->blanks = 0; - obst->anim_speed = 0; - obst->tmp_pos = 0; - obst->angle = 0; - if (rand()%100<=50) { - obst->callback= NULL; - obst->data = NULL; - obst->points = -5; - obst->bitmap = &(game->level.obst_bmps.badmuffin); - } else if (rand()%100<=12) { - obst->callback= &Obst_RotateSin; - obst->data = malloc(sizeof(float)); - *((float*)obst->data) = 0; - obst->points = 8; - obst->bitmap = &(game->level.obst_bmps.muffin); - } else if (rand()%100<=12) { - obst->callback= &Obst_RotateSin; - obst->data = malloc(sizeof(float)); - *((float*)obst->data) = 0; - obst->points = 4; - obst->bitmap = &(game->level.obst_bmps.cherry); - } else if (rand()%100<=65) { - obst->callback= &Obst_MoveUp; - if (rand()%100<=80) obst->bitmap = &(game->level.obst_bmps.pie1); - else { - obst->bitmap = &(game->level.obst_bmps.pie2); - obst->points = -12; - } - obst->data = malloc(sizeof(float)); - *((float*)obst->data) = 0.25+(rand()%50/100.0); - obst->y*=1.8; - obst->angle = ((rand()%50)/100.0)-0.25; - } else if (rand()%100<=80) { - obst->callback = &Obst_MoveSin; - obst->data = malloc(sizeof(float)); - *((float*)obst->data) = 0; - obst->bitmap = &(game->level.obst_bmps.pig); - obst->rows = 3; - obst->cols = 3; - obst->speed = 1.2; - obst->anim_speed = 2; - obst->points = -20; - } else { - obst->callback = &Obst_MoveUpDown; - obst->bitmap = &(game->level.obst_bmps.screwball); - obst->data = (void*)(rand()%2); - obst->rows = 4; - obst->cols = 4; - obst->speed = 1.1; - obst->anim_speed = 2; - obst->points = -25; - } - if (game->level.obstacles) { - game->level.obstacles->prev = obst; - obst->next = game->level.obstacles; - } else { - obst->next = NULL; - } - game->level.obstacles = obst; - if (*count > 128) return true; - } - } else if (state == TM_ACTIONSTATE_DESTROY) { - free(action->arguments->value); - TM_DestroyArgs(action->arguments); - action->arguments = NULL; - } - return false; -} - bool Stop(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { if (state != TM_ACTIONSTATE_RUNNING) return false; game->level.speed=0; @@ -208,53 +52,6 @@ bool Stop(struct Game *game, struct TM_Action *action, enum TM_ActionState state return true; } -bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state == TM_ACTIONSTATE_DESTROY) { - ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; - al_set_audio_stream_playing(*stream, false); - al_destroy_audio_stream(*stream); - free(action->arguments->next->value); - free(action->arguments->value); - TM_DestroyArgs(action->arguments); - } - if (state == TM_ACTIONSTATE_DRAW) { - float* f = (float*)action->arguments->value; - al_draw_tinted_bitmap(game->level.letter, al_map_rgba(*f,*f,*f,*f), (game->viewportWidth-al_get_bitmap_width(game->level.letter))/2.0, al_get_bitmap_height(game->level.letter)*-0.05, 0); - return false; - } - else if (state == TM_ACTIONSTATE_PAUSE) { - ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; - al_set_audio_stream_playing(*stream, false); - } else if (state == TM_ACTIONSTATE_RESUME) { - ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; - al_set_audio_stream_playing(*stream, true); - } - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!action->arguments) { - action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(float))); - float* f = (float*)action->arguments->value; - *f = 0; - action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(ALLEGRO_AUDIO_STREAM*))); - ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; - *stream = al_load_audio_stream(GetDataFilePath("levels/letter.flac"), 4, 1024); - al_attach_audio_stream_to_mixer(*stream, game->audio.voice); - al_set_audio_stream_playing(*stream, true); - al_set_audio_stream_gain(*stream, 2.00); - action->arguments->next->next = NULL; - } - float* f = (float*)action->arguments->value; - *f+=5; - if (*f>255) *f=255; - al_draw_tinted_bitmap(game->level.letter, al_map_rgba(*f,*f,*f,*f), (game->viewportWidth-al_get_bitmap_width(game->level.letter))/2.0, al_get_bitmap_height(game->level.letter)*-0.05, 0); - struct ALLEGRO_KEYBOARD_STATE keyboard; - al_get_keyboard_state(&keyboard); - if (al_key_down(&keyboard, ALLEGRO_KEY_ENTER)) { - return true; - } - return false; -} - bool FadeIn(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { if (!action->arguments) { action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(float))); diff --git a/src/levels/actions.h b/src/levels/actions.h index 3207065..6a2e07c 100644 --- a/src/levels/actions.h +++ b/src/levels/actions.h @@ -25,33 +25,12 @@ /*! \brief Display level failed screen and set fade-out to be run after few seconds. */ bool LevelFailed(struct Game *game, struct TM_Action *action, enum TM_ActionState state); -/*! \brief Accelerate current speed game until threshold is reached */ -bool Accelerate(struct Game *game, struct TM_Action *action, enum TM_ActionState state); - -/*! \brief Set Derpy to walk and move her position on screen. */ -bool Walk(struct Game *game, struct TM_Action *action, enum TM_ActionState state); - -/*! \brief Move screen until some position is reached. */ -bool Move(struct Game *game, struct TM_Action *action, enum TM_ActionState state); - /*! \brief Fade-in HP meter. */ bool ShowMeter(struct Game *game, struct TM_Action *action, enum TM_ActionState state); -/*! \brief Fly Derpy, fly! */ -bool Fly(struct Game *game, struct TM_Action *action, enum TM_ActionState state); - -/*! \brief Run Derpy, run! */ -bool Run(struct Game *game, struct TM_Action *action, enum TM_ActionState state); - -/*! \brief Generates obstacles. */ -bool GenerateObstacles(struct Game *game, struct TM_Action *action, enum TM_ActionState state); - /*! \brief Stops Derpy. */ bool Stop(struct Game *game, struct TM_Action *action, enum TM_ActionState state); -/*! \brief Shows letter from Twilight on screen. */ -bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState state); - /*! \brief Fade-in screen. */ bool FadeIn(struct Game *game, struct TM_Action *action, enum TM_ActionState state); diff --git a/src/levels/dodger.c b/src/levels/dodger.c new file mode 100644 index 0000000..4be15d1 --- /dev/null +++ b/src/levels/dodger.c @@ -0,0 +1,269 @@ +/*! \file dodger.c + * \brief Dodger Level module code. + */ +/* + * Copyright (c) Sebastian Krzyszkowiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ +#include +#include +#include "../level.h" +#include "dodger.h" +#include "actions.h" +#include "dodger/actions.h" + +void Dodger_Logic(struct Game *game) { + struct ALLEGRO_KEYBOARD_STATE keyboard; + al_get_keyboard_state(&keyboard); + if (game->level.handle_input) { + if (game->level.derpy_angle > 0) { game->level.derpy_angle -= 0.02; if (game->level.derpy_angle < 0) game->level.derpy_angle = 0; } + if (game->level.derpy_angle < 0) { game->level.derpy_angle += 0.02; if (game->level.derpy_angle > 0) game->level.derpy_angle = 0; } + if (al_key_down(&keyboard, ALLEGRO_KEY_UP)) { + game->level.derpy_y -= 0.005; + game->level.derpy_angle -= 0.03; + if (game->level.derpy_angle < -0.15) game->level.derpy_angle = -0.15; + /*PrintConsole(game, "Derpy Y position: %f", game->level.derpy_y);*/ + } + if (al_key_down(&keyboard, ALLEGRO_KEY_DOWN)) { + game->level.derpy_y += 0.005; + game->level.derpy_angle += 0.03; + if (game->level.derpy_angle > 0.15) game->level.derpy_angle = 0.15; + /*PrintConsole(game, "Derpy Y position: %f", game->level.derpy_y);*/ + } + /*if ((game->level.derpy_y > 0.6) && (game->level.flying)) { + SelectDerpySpritesheet(game, "run"); + game->level.flying = false; + game->level.sheet_speed = tps(game, 60*0.0020/game->level.speed); + } + else if ((game->level.derpy_y <= 0.6) && (!game->level.flying)) { + SelectDerpySpritesheet(game, "fly"); + game->level.flying = true; + game->level.sheet_speed = tps(game, 60*2.4); + } + if (!game->level.flying) game->level.sheet_speed = tps(game, 60*0.0020/game->level.speed); */ + if (game->level.derpy_y < 0) game->level.derpy_y=0; + else if (game->level.derpy_y > 0.8) game->level.derpy_y=0.8; + + game->level.derpy_y += game->level.derpy_angle / 30; + } + + int derpyx = game->level.derpy_x*game->viewportWidth; + int derpyy = game->level.derpy_y*game->viewportHeight; + int derpyw = al_get_bitmap_width(game->level.derpy); + int derpyh = al_get_bitmap_height(game->level.derpy); + int derpyo = game->viewportWidth*0.1953125-al_get_bitmap_width(game->level.derpy); /* offset */ + struct Obstacle *tmp = game->level.dodger.obstacles; + while (tmp) { + /*PrintConsole(game, "DRAWING %f %f", tmp->x, tmp->y);*/ + int x = (tmp->x/100.0)*game->viewportWidth; + int y = (tmp->y/100.0)*game->viewportHeight; + int w = 0, h = 0; + if (tmp->bitmap) { + w = al_get_bitmap_width(*(tmp->bitmap))/tmp->cols; + h = al_get_bitmap_height(*(tmp->bitmap))/tmp->rows; + } + if (x > -w) { + /*if (!tmp->hit)*/ + if ((((x>=derpyx+0.38*derpyw+derpyo) && (x<=derpyx+0.94*derpyw+derpyo)) || ((x+w>=derpyx+0.38*derpyw+derpyo) && (x+w<=derpyx+0.94*derpyw+derpyo)) || ((x<=derpyx+0.38*derpyw+derpyo) && (x+w>=derpyx+0.94*derpyw+derpyo))) && + (((y>=derpyy+0.26*derpyh) && (y<=derpyy+0.76*derpyh)) || ((y+h>=derpyy+0.26*derpyh) && (y+h<=derpyy+0.76*derpyh)) || ((y<=derpyy+0.26*derpyh) && (y+h>=derpyy+0.76*derpyh)))) { + tmp->hit=true; + } + + if (tmp->anim_speed) { + tmp->tmp_pos+=1; + if (tmp->tmp_pos >= tmp->anim_speed) { + tmp->pos++; + tmp->tmp_pos = 0; + } + if (tmp->pos>=tmp->cols*tmp->rows-tmp->blanks) tmp->pos=0; + } + + if (tmp->hit) { + if (tmp->points>=0) tmp->bitmap = NULL; + game->level.hp+=0.0002*tmp->points*(((1-game->level.speed_modifier)/2.0)+1); + if (game->level.hp>1) game->level.hp=1; + //PrintConsole(game, "POINTS: %d, %f", tmp->points, tps(game, 60*0.0002*tmp->points*game->level.speed_modifier)); + if ((game->level.hp<=0) && (!game->level.failed)) { + game->level.failed = true; + game->level.handle_input = false; + game->level.speed_modifier = 1; + TM_AddBackgroundAction(&LevelFailed, NULL, 0, "levelfailed"); + } + } + tmp->x -= game->level.speed*game->level.speed_modifier*tmp->speed*100*al_get_bitmap_width(game->level.stage)/(float)game->viewportWidth; + if (tmp->callback) tmp->callback(game, tmp); + tmp = tmp->next; + } else { + if (tmp->next) + tmp->next->prev = tmp->prev; + if (tmp->prev) + tmp->prev->next = tmp->next; + else + game->level.dodger.obstacles = tmp->next; + struct Obstacle *t = tmp; + tmp = tmp->next; + free(t); + } + } + /*if (colision) game->level.hp-=tps(game, 60*0.002);*/ + +} + +void Dodger_Draw(struct Game *game) { + int derpyx = game->level.derpy_x*game->viewportWidth; + int derpyy = game->level.derpy_y*game->viewportHeight; + int derpyw = al_get_bitmap_width(game->level.derpy); + int derpyh = al_get_bitmap_height(game->level.derpy); + int derpyo = game->viewportWidth*0.1953125-al_get_bitmap_width(game->level.derpy); /* offset */ + bool colision = false; + struct Obstacle *tmp = game->level.dodger.obstacles; + while (tmp) { + /*PrintConsole(game, "DRAWING %f %f", tmp->x, tmp->y);*/ + int x = (tmp->x/100.0)*game->viewportWidth; + int y = (tmp->y/100.0)*game->viewportHeight; + int w = 0, h = 0; + if (tmp->bitmap) { + w = al_get_bitmap_width(*(tmp->bitmap))/tmp->cols; + h = al_get_bitmap_height(*(tmp->bitmap))/tmp->rows; + } + if (x > -w) { + if ((tmp->hit) && (tmp->points<0)) { + colision = true; + } + + if (tmp->bitmap) { + ALLEGRO_BITMAP* subbitmap = al_create_sub_bitmap(*(tmp->bitmap),w*(tmp->pos%tmp->cols), h*(tmp->pos/tmp->cols),w,h); + al_draw_rotated_bitmap(subbitmap,w/2.0, h/2.0, x+w/2.0,y+h/2.0, tmp->angle, 0); + al_destroy_bitmap(subbitmap); + } + + /*al_draw_bitmap(*(tmp->bitmap), x, y, 0);*/ + if (game->level.debug_show_sprite_frames) al_draw_rectangle(x, y, x+w, y+h, al_map_rgba(255,0,0,255), 3); + + tmp = tmp->next; + } else { + if (tmp->next) + tmp->next->prev = tmp->prev; + if (tmp->prev) + tmp->prev->next = tmp->next; + else + game->level.dodger.obstacles = tmp->next; + struct Obstacle *t = tmp; + tmp = tmp->next; + free(t); + } + } + /*if (colision) game->level.hp-=tps(game, 60*0.002);*/ + + al_set_target_bitmap(game->level.derpy); + al_clear_to_color(al_map_rgba(0,0,0,0)); + al_draw_bitmap_region(*(game->level.derpy_sheet),al_get_bitmap_width(game->level.derpy)*(game->level.sheet_pos%game->level.sheet_cols),al_get_bitmap_height(game->level.derpy)*(game->level.sheet_pos/game->level.sheet_cols),al_get_bitmap_width(game->level.derpy), al_get_bitmap_height(game->level.derpy),0,0,0); + al_set_target_bitmap(al_get_backbuffer(game->display)); + + al_draw_tinted_rotated_bitmap(game->level.derpy, al_map_rgba(255,255-colision*255,255-colision*255,255), al_get_bitmap_width(game->level.derpy), al_get_bitmap_height(game->level.derpy)/2, derpyx+game->viewportWidth*0.1953125, derpyy + al_get_bitmap_height(game->level.derpy)/2, game->level.derpy_angle, 0); + + /* if ((((x>=derpyx+0.36*derpyw) && (x<=derpyx+0.94*derpyw)) || ((x+w>=derpyx+0.36*derpyw) && (x+w<=derpyx+0.94*derpyw))) && + (((y>=derpyy+0.26*derpyh) && (y<=derpyy+0.76*derpyh)) || ((y+h>=derpyy+0.26*derpyh) && (y+h<=derpyy+0.76*derpyh)))) { +*/ + if (game->level.debug_show_sprite_frames) { + al_draw_rectangle(derpyx+derpyo, derpyy, derpyx+derpyw+derpyo, derpyy+derpyh, al_map_rgba(0,255,0,255), 3); + al_draw_rectangle(derpyx+0.38*derpyw+derpyo, derpyy+0.26*derpyh, derpyx+0.94*derpyw+derpyo, derpyy+0.76*derpyh, al_map_rgba(0,0,255,255), 3); + } +} + +void Dodger_Load(struct Game *game) { + game->level.dodger.obstacles = NULL; +} + +int Dodger_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { + if (game->level.handle_input) { + if (ev->keyboard.keycode==ALLEGRO_KEY_LEFT) { + game->level.speed_modifier = 0.75; + } else if (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT) { + game->level.speed_modifier = 1.3; + } + } + return 0; +} + +void Dodger_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev) { + if (game->level.handle_input) { + if ((ev->type==ALLEGRO_EVENT_KEY_UP) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) { + game->level.speed_modifier = 1; + struct ALLEGRO_KEYBOARD_STATE keyboard; + al_get_keyboard_state(&keyboard); + if (al_key_down(&keyboard, ALLEGRO_KEY_RIGHT)) { + game->level.speed_modifier = 1.3; + } + } else if ((ev->type==ALLEGRO_EVENT_KEY_UP) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) { + game->level.speed_modifier = 1; + struct ALLEGRO_KEYBOARD_STATE keyboard; + al_get_keyboard_state(&keyboard); + if (al_key_down(&keyboard, ALLEGRO_KEY_LEFT)) { + game->level.speed_modifier = 0.75; + } + } + } +} + +void Dodger_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, float)) { + float load_p = 12/19.0, load_a = 19; // TODO: FIXME! + game->level.dodger.obst_bmps.pie1 = LoadScaledBitmap("levels/pie1.png", game->viewportWidth*0.1, game->viewportHeight*0.08); + PROGRESS; + game->level.dodger.obst_bmps.pie2 = LoadScaledBitmap("levels/pie2.png", game->viewportWidth*0.1, game->viewportHeight*0.08); + PROGRESS; + game->level.dodger.obst_bmps.pig = LoadScaledBitmap("levels/pig.png", (int)(game->viewportWidth*0.15)*3, (int)(game->viewportHeight*0.2)*3); + PROGRESS; + game->level.dodger.obst_bmps.screwball = LoadScaledBitmap("levels/screwball.png", (int)(game->viewportHeight*0.2)*4*1.4, (int)(game->viewportHeight*0.2)*4); + PROGRESS; + game->level.dodger.obst_bmps.muffin = LoadScaledBitmap("levels/muffin.png", game->viewportWidth*0.07, game->viewportHeight*0.1); + PROGRESS; + game->level.dodger.obst_bmps.cherry = LoadScaledBitmap("levels/cherry.png", game->viewportWidth*0.03, game->viewportHeight*0.08); + PROGRESS; + game->level.dodger.obst_bmps.badmuffin = LoadScaledBitmap("levels/badmuffin.png", game->viewportWidth*0.07, game->viewportHeight*0.1); + PROGRESS; +} + +void Dodger_Preload(struct Game *game, void (*progress)(struct Game*, float)) { +} + +void Dodger_UnloadBitmaps(struct Game *game) { + al_destroy_bitmap(game->level.dodger.obst_bmps.pie1); + al_destroy_bitmap(game->level.dodger.obst_bmps.pie2); + al_destroy_bitmap(game->level.dodger.obst_bmps.pig); + al_destroy_bitmap(game->level.dodger.obst_bmps.cherry); + al_destroy_bitmap(game->level.dodger.obst_bmps.muffin); + al_destroy_bitmap(game->level.dodger.obst_bmps.badmuffin); + al_destroy_bitmap(game->level.dodger.obst_bmps.screwball); +} + +void Dodger_Unload(struct Game *game) { + struct Obstacle *t = game->level.dodger.obstacles; + if (t) { + while (t->next) { + if (t->prev) free(t->prev); + t = t->next; + } + free(t); + } + struct Spritesheet *tmp, *s = game->level.derpy_sheets; + tmp = s; + while (s) { + tmp = s; + s = s->next; + free(tmp); + } +} diff --git a/src/levels/dodger.h b/src/levels/dodger.h new file mode 100644 index 0000000..62c242c --- /dev/null +++ b/src/levels/dodger.h @@ -0,0 +1,31 @@ +/*! \file dodger.h + * \brief Dodger Level module headers. + */ +/* + * Copyright (c) Sebastian Krzyszkowiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ +#include "../main.h" + +void Dodger_Draw(struct Game *game); +void Dodger_Logic(struct Game *game); +void Dodger_Preload(struct Game *game, void (*progress)(struct Game*, float)); +void Dodger_Unload(struct Game *game); +void Dodger_Load(struct Game *game); +int Dodger_Keydown(struct Game *game, ALLEGRO_EVENT *ev); +void Dodger_UnloadBitmaps(struct Game *game); +void Dodger_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, float)); +void Dodger_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev); diff --git a/src/levels/dodger/actions.c b/src/levels/dodger/actions.c new file mode 100644 index 0000000..defc9dc --- /dev/null +++ b/src/levels/dodger/actions.c @@ -0,0 +1,228 @@ +/*! \file actions.c + * \brief Dodger Level module actions for Timeline Manager. + */ +/* + * Copyright (c) Sebastian Krzyszkowiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "actions.h" +#include "callbacks.h" +#include "../actions.h" +#include "../../level.h" + + +bool Accelerate(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state != TM_ACTIONSTATE_RUNNING) return false; + game->level.speed+=0.000015; + if (game->level.speed<0.0025) return false; + return true; +} + +bool Walk(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; + if (state != TM_ACTIONSTATE_RUNNING) return false; + if (!(action->arguments)) SelectDerpySpritesheet(game, "walk"); + action->arguments++; + game->level.derpy_x+=0.001; + if (game->level.derpy_x<0.05) return false; + return true; +} + +bool Move(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state != TM_ACTIONSTATE_RUNNING) return false; + game->level.speed=0.00035; + if (game->level.st_pos<0.275) return false; + return true; +} + +bool Fly(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; + if (state != TM_ACTIONSTATE_RUNNING) return false; + if (!(action->arguments)) { + SelectDerpySpritesheet(game, "fly"); + game->level.derpy_angle = -0.15; + /*game->level.gg = true;*/ + TM_AddBackgroundAction(&ShowMeter, NULL, 0, "showmeter"); + action->arguments++; + } + game->level.derpy_y-=0.004; + if (game->level.derpy_y>0.2) return false; + game->level.handle_input=true; + return true; +} + +bool Run(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; + if (state != TM_ACTIONSTATE_RUNNING) return false; + if (!(action->arguments)) { + game->level.handle_input=false; + game->level.speed_modifier=1; + action->arguments++; + } + game->level.derpy_y+=0.0042; + if (game->level.derpy_angle > 0) { game->level.derpy_angle -= 0.02; if (game->level.derpy_angle < 0) game->level.derpy_angle = 0; } + if (game->level.derpy_angle < 0) { game->level.derpy_angle += 0.02; if (game->level.derpy_angle > 0) game->level.derpy_angle = 0; } + if (game->level.derpy_y<0.65) return false; + game->level.derpy_angle = 0; + SelectDerpySpritesheet(game, "run"); + return true; +} + +bool GenerateObstacles(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + /*float* tmp; bool* in;*/ + int* count; + if (!action->arguments) { + action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(int))); + /* action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(bool))); */ + } + count = (int*)action->arguments->value; + /*tmp = (float*)action->arguments->value; + in = (bool*)action->arguments->next->value;*/ + if (state == TM_ACTIONSTATE_INIT) { + *count = 0; + /* *tmp = 0; + *in = true;*/ + } + else if (state == TM_ACTIONSTATE_RUNNING) { + if (rand()%(10000/(int)(85*game->level.speed_modifier))<=3) { + PrintConsole(game, "OBSTACLE %d", *count); + (*count)++; + struct Obstacle *obst = malloc(sizeof(struct Obstacle)); + obst->prev = NULL; + obst->x = 100; + obst->y = (rand()%91)-1; + obst->speed = 1; + obst->points = -10; + obst->hit = false; + obst->rows = 1; + obst->cols = 1; + obst->pos = 0; + obst->blanks = 0; + obst->anim_speed = 0; + obst->tmp_pos = 0; + obst->angle = 0; + if (rand()%100<=50) { + obst->callback= NULL; + obst->data = NULL; + obst->points = -5; + obst->bitmap = &(game->level.dodger.obst_bmps.badmuffin); + } else if (rand()%100<=12) { + obst->callback= &Obst_RotateSin; + obst->data = malloc(sizeof(float)); + *((float*)obst->data) = 0; + obst->points = 8; + obst->bitmap = &(game->level.dodger.obst_bmps.muffin); + } else if (rand()%100<=12) { + obst->callback= &Obst_RotateSin; + obst->data = malloc(sizeof(float)); + *((float*)obst->data) = 0; + obst->points = 4; + obst->bitmap = &(game->level.dodger.obst_bmps.cherry); + } else if (rand()%100<=65) { + obst->callback= &Obst_MoveUp; + if (rand()%100<=80) obst->bitmap = &(game->level.dodger.obst_bmps.pie1); + else { + obst->bitmap = &(game->level.dodger.obst_bmps.pie2); + obst->points = -12; + } + obst->data = malloc(sizeof(float)); + *((float*)obst->data) = 0.25+(rand()%50/100.0); + obst->y*=1.8; + obst->angle = ((rand()%50)/100.0)-0.25; + } else if (rand()%100<=80) { + obst->callback = &Obst_MoveSin; + obst->data = malloc(sizeof(float)); + *((float*)obst->data) = 0; + obst->bitmap = &(game->level.dodger.obst_bmps.pig); + obst->rows = 3; + obst->cols = 3; + obst->speed = 1.2; + obst->anim_speed = 2; + obst->points = -20; + } else { + obst->callback = &Obst_MoveUpDown; + obst->bitmap = &(game->level.dodger.obst_bmps.screwball); + obst->data = (void*)(rand()%2); + obst->rows = 4; + obst->cols = 4; + obst->speed = 1.1; + obst->anim_speed = 2; + obst->points = -25; + } + if (game->level.dodger.obstacles) { + game->level.dodger.obstacles->prev = obst; + obst->next = game->level.dodger.obstacles; + } else { + obst->next = NULL; + } + game->level.dodger.obstacles = obst; + if (*count > 128) return true; + } + } else if (state == TM_ACTIONSTATE_DESTROY) { + free(action->arguments->value); + TM_DestroyArgs(action->arguments); + action->arguments = NULL; + } + return false; +} + +bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; + if (state == TM_ACTIONSTATE_DESTROY) { + ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; + al_set_audio_stream_playing(*stream, false); + al_destroy_audio_stream(*stream); + free(action->arguments->next->value); + free(action->arguments->value); + TM_DestroyArgs(action->arguments); + } + if (state == TM_ACTIONSTATE_DRAW) { + float* f = (float*)action->arguments->value; + al_draw_tinted_bitmap(game->level.letter, al_map_rgba(*f,*f,*f,*f), (game->viewportWidth-al_get_bitmap_width(game->level.letter))/2.0, al_get_bitmap_height(game->level.letter)*-0.05, 0); + return false; + } + else if (state == TM_ACTIONSTATE_PAUSE) { + ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; + al_set_audio_stream_playing(*stream, false); + } else if (state == TM_ACTIONSTATE_RESUME) { + ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; + al_set_audio_stream_playing(*stream, true); + } + if (state != TM_ACTIONSTATE_RUNNING) return false; + if (!action->arguments) { + action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(float))); + float* f = (float*)action->arguments->value; + *f = 0; + action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(ALLEGRO_AUDIO_STREAM*))); + ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; + *stream = al_load_audio_stream(GetDataFilePath("levels/letter.flac"), 4, 1024); + al_attach_audio_stream_to_mixer(*stream, game->audio.voice); + al_set_audio_stream_playing(*stream, true); + al_set_audio_stream_gain(*stream, 2.00); + action->arguments->next->next = NULL; + } + float* f = (float*)action->arguments->value; + *f+=5; + if (*f>255) *f=255; + al_draw_tinted_bitmap(game->level.letter, al_map_rgba(*f,*f,*f,*f), (game->viewportWidth-al_get_bitmap_width(game->level.letter))/2.0, al_get_bitmap_height(game->level.letter)*-0.05, 0); + struct ALLEGRO_KEYBOARD_STATE keyboard; + al_get_keyboard_state(&keyboard); + if (al_key_down(&keyboard, ALLEGRO_KEY_ENTER)) { + return true; + } + return false; +} diff --git a/src/levels/dodger/actions.h b/src/levels/dodger/actions.h new file mode 100644 index 0000000..88d8b45 --- /dev/null +++ b/src/levels/dodger/actions.h @@ -0,0 +1,44 @@ +/*! \file actions.h + * \brief Header file with Dodger Level module actions for Timeline Manager. + */ +/* + * Copyright (c) Sebastian Krzyszkowiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "../../main.h" +#include "../../timeline.h" + +/*! \brief Accelerate current speed game until threshold is reached */ +bool Accelerate(struct Game *game, struct TM_Action *action, enum TM_ActionState state); + +/*! \brief Set Derpy to walk and move her position on screen. */ +bool Walk(struct Game *game, struct TM_Action *action, enum TM_ActionState state); + +/*! \brief Move screen until some position is reached. */ +bool Move(struct Game *game, struct TM_Action *action, enum TM_ActionState state); + +/*! \brief Fly Derpy, fly! */ +bool Fly(struct Game *game, struct TM_Action *action, enum TM_ActionState state); + +/*! \brief Run Derpy, run! */ +bool Run(struct Game *game, struct TM_Action *action, enum TM_ActionState state); + +/*! \brief Generates obstacles. */ +bool GenerateObstacles(struct Game *game, struct TM_Action *action, enum TM_ActionState state); + +/*! \brief Shows letter from Twilight on screen. */ +bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState state); diff --git a/src/levels/callbacks.c b/src/levels/dodger/callbacks.c similarity index 97% rename from src/levels/callbacks.c rename to src/levels/dodger/callbacks.c index d71e065..d5c1bab 100644 --- a/src/levels/callbacks.c +++ b/src/levels/dodger/callbacks.c @@ -1,5 +1,5 @@ /*! \file callbacks.c - * \brief Obstacle callbacks for Level. + * \brief Obstacle callbacks for Dodger Level module. */ /* * Copyright (c) Sebastian Krzyszkowiak diff --git a/src/levels/callbacks.h b/src/levels/dodger/callbacks.h similarity index 93% rename from src/levels/callbacks.h rename to src/levels/dodger/callbacks.h index eb4f75d..860fcde 100644 --- a/src/levels/callbacks.h +++ b/src/levels/dodger/callbacks.h @@ -1,5 +1,5 @@ /*! \file callbacks.h - * \brief Headers with Obstacle callbacks for Level. + * \brief Headers with Obstacle callbacks for Dodger Level module. */ /* * Copyright (c) Sebastian Krzyszkowiak @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "../main.h" +#include "../../main.h" /*! \brief Move up or down until reaching the edge of the screen. After that - change direction. */ void Obst_MoveUpDown(struct Game *game, struct Obstacle *obstacle); diff --git a/src/moonwalk.c b/src/levels/moonwalk.c similarity index 80% rename from src/moonwalk.c rename to src/levels/moonwalk.c index d137ec8..f01c88f 100644 --- a/src/moonwalk.c +++ b/src/levels/moonwalk.c @@ -1,5 +1,5 @@ /*! \file moonwalk.c - * \brief Moonwalk Level placeholder code. + * \brief Moonwalk Level module code. */ /* * Copyright (c) Sebastian Krzyszkowiak @@ -20,7 +20,7 @@ */ #include #include -#include "level.h" +#include "../level.h" #include "moonwalk.h" void Moonwalk_Logic(struct Game *game) { @@ -53,7 +53,7 @@ void Moonwalk_Draw(struct Game *game) { } return; } - al_draw_scaled_bitmap(game->level.moonwalk.image,0,0,al_get_bitmap_width(game->level.moonwalk.image),al_get_bitmap_height(game->level.moonwalk.image),0,0,game->viewportWidth, game->viewportHeight,0); + al_draw_scaled_bitmap(game->level.stage,0,0,al_get_bitmap_width(game->level.stage),al_get_bitmap_height(game->level.stage),0,0,game->viewportWidth, game->viewportHeight,0); al_draw_bitmap(game->level.derpy, game->level.moonwalk.derpy_pos*game->viewportWidth, game->viewportHeight*0.95-al_get_bitmap_height(game->level.derpy), ALLEGRO_FLIP_HORIZONTAL); al_draw_textf(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/2.2, ALLEGRO_ALIGN_CENTRE, "Level %d: Not implemented yet!", game->level.current_level); al_draw_text(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/1.8, ALLEGRO_ALIGN_CENTRE, "Have some moonwalk instead."); @@ -72,16 +72,23 @@ int Moonwalk_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { return 0; } -void Moonwalk_PreloadBitmaps(struct Game *game) { - game->level.moonwalk.image =LoadScaledBitmap("levels/disco.jpg", game->viewportWidth, game->viewportHeight); +void Moonwalk_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, float)) { /*game->level.derpy_sheet = LoadScaledBitmap("levels/derpcycle.png", game->viewportWidth*0.1953125*6, game->viewportHeight*0.25*4);*/ - game->level.derpy = al_create_bitmap(game->viewportWidth*0.1953125, game->viewportHeight*0.25); + // nasty hack: overwrite level backgrounds + al_destroy_bitmap(game->level.background); + al_destroy_bitmap(game->level.foreground); + al_destroy_bitmap(game->level.stage); + al_destroy_bitmap(game->level.clouds); + game->level.background=al_create_bitmap(0,0); + game->level.foreground=al_create_bitmap(0,0); + game->level.clouds=al_create_bitmap(0,0); + game->level.stage = LoadScaledBitmap("levels/disco.jpg", game->viewportWidth, game->viewportHeight); al_set_target_bitmap(al_get_backbuffer(game->display)); } -void Moonwalk_Preload(struct Game *game) { +void Moonwalk_Preload(struct Game *game, void (*progress)(struct Game*, float)) { PrintConsole(game, "Initializing level %d...", game->level.current_level); game->level.sample = al_load_sample( GetDataFilePath("levels/moonwalk.flac") ); game->level.music = al_create_sample_instance(game->level.sample); @@ -95,9 +102,11 @@ void Moonwalk_Preload(struct Game *game) { } void Moonwalk_UnloadBitmaps(struct Game *game) { - al_destroy_bitmap(game->level.moonwalk.image); } void Moonwalk_Unload(struct Game *game) { //FadeGameState(game, false); } + +void Moonwalk_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev) { +} diff --git a/src/moonwalk.h b/src/levels/moonwalk.h similarity index 78% rename from src/moonwalk.h rename to src/levels/moonwalk.h index c3ed961..9496ca9 100644 --- a/src/moonwalk.h +++ b/src/levels/moonwalk.h @@ -1,5 +1,5 @@ /*! \file moonwalk.h - * \brief Moonwalk Level placeholder headers. + * \brief Moonwalk Level module headers. */ /* * Copyright (c) Sebastian Krzyszkowiak @@ -18,13 +18,14 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "main.h" +#include "../main.h" void Moonwalk_Draw(struct Game *game); void Moonwalk_Logic(struct Game *game); -void Moonwalk_Preload(struct Game *game); +void Moonwalk_Preload(struct Game *game, void (*progress)(struct Game*, float)); void Moonwalk_Unload(struct Game *game); void Moonwalk_Load(struct Game *game); int Moonwalk_Keydown(struct Game *game, ALLEGRO_EVENT *ev); void Moonwalk_UnloadBitmaps(struct Game *game); -void Moonwalk_PreloadBitmaps(struct Game *game); +void Moonwalk_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, float)); +void Moonwalk_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev); diff --git a/src/main.c b/src/main.c index 7d8d30b..ad2e9c6 100644 --- a/src/main.c +++ b/src/main.c @@ -374,10 +374,6 @@ ALLEGRO_BITMAP* LoadScaledBitmap(char* filename, int width, int height) { return target;*/ } -float tps(struct Game *game, float t) { - return t/60; -} - void SetupViewport(struct Game *game) { game->viewportWidth = al_get_display_width(game->display); game->viewportHeight = al_get_display_height(game->display); diff --git a/src/main.h b/src/main.h index b34e35b..29523d2 100644 --- a/src/main.h +++ b/src/main.h @@ -52,8 +52,6 @@ enum gamestate_enum { GAMESTATE_DISCLAIMER }; -/* TODO: move Obstacle and Spritesheet to level.h if possible */ - /*! \brief Structure representing obstacles and power-ups flying through the level. */ struct Obstacle { ALLEGRO_BITMAP **bitmap; /*!< Pointer to bitmap used by obstacle. */ @@ -93,15 +91,28 @@ struct Spritesheet { /* Gamestate structs */ -/*! \brief Resources used by moonwalk level placeholder. */ +/*! \brief Resources used by Moonwalk level module. */ struct Moonwalk { - ALLEGRO_BITMAP *image; /*!< Background texture. */ int derpy_frame; /*!< Current frame of Derpy animation. */ int derpy_frame_tmp; /*!< Counter used to slow down Derpy animation. */ double derpy_pos; /*!< Position of Derpy on screen. */ }; -/*! \brief Resources used by Level state. */ +/*! \brief Resources used by Dodger level module. */ +struct Dodger { + struct { + ALLEGRO_BITMAP *pie1; /*!< Pie bitmap. */ + ALLEGRO_BITMAP *pie2; /*!< Pie bitmap (crossed). */ + ALLEGRO_BITMAP *muffin; /*!< Good muffin bitmap. */ + ALLEGRO_BITMAP *badmuffin; /*!< Bad muffin bitmap. */ + ALLEGRO_BITMAP *cherry; /*!< Cherry bitmap. */ + ALLEGRO_BITMAP *pig; /*!< Pig spritesheet bitmap. */ + ALLEGRO_BITMAP *screwball; /*!< Screwball spritesheet bitmap. */ + } obst_bmps; /*!< Obstacle bitmaps. */ + struct Obstacle *obstacles; /*!< List of obstacles being currently rendered. */ +}; + +/*! \brief Resources used by Level state and shared between level modules. */ struct Level { struct { int current_level; /*!< Level number. */ @@ -146,19 +157,10 @@ struct Level { ALLEGRO_BITMAP *meter_image; /*!< Derpy image used in the HP meter. */ ALLEGRO_BITMAP *letter; /*!< Bitmap with letter from Twilight. */ bool debug_show_sprite_frames; /*!< When true, displays colorful borders around spritesheets and their active areas. */ - struct Spritesheet* derpy_sheets; /*!< List of sprite sheets of Derpy character. */ - struct Spritesheet* pony_sheets; /*!< List of sprite sheets of character rescued by Derpy. */ - struct { - ALLEGRO_BITMAP *pie1; /*!< Pie bitmap. */ - ALLEGRO_BITMAP *pie2; /*!< Pie bitmap (crossed). */ - ALLEGRO_BITMAP *muffin; /*!< Good muffin bitmap. */ - ALLEGRO_BITMAP *badmuffin; /*!< Bad muffin bitmap. */ - ALLEGRO_BITMAP *cherry; /*!< Cherry bitmap. */ - ALLEGRO_BITMAP *pig; /*!< Pig spritesheet bitmap. */ - ALLEGRO_BITMAP *screwball; /*!< Screwball spritesheet bitmap. */ - } obst_bmps; /*!< Obstacle bitmaps. */ - struct Obstacle *obstacles; /*!< List of obstacles being currently rendered. */ - struct Moonwalk moonwalk; /*!< Moonwalk placeholder data. */ + struct Spritesheet* derpy_sheets; /*!< List of spritesheets of Derpy character. */ + //struct Spritesheet* pony_sheets; /*!< List of spritesheets of character rescued by Derpy. */ + struct Moonwalk moonwalk; /*!< Moonwalk module data. */ + struct Dodger dodger; /*!< Dodger module data. */ }; /*! \brief Enum of menu states in Menu and Pause game states. */ @@ -339,9 +341,6 @@ void DrawConsole(struct Game *game); /*! \brief Loads bitmap into memory and scales it with software linear filtering. */ ALLEGRO_BITMAP* LoadScaledBitmap(char* filename, int width, int height); -/*! \brief Draws console bitmap on screen. */ -float tps(struct Game *game, float t); - /*! \brief Draws frame from current gamestate. */ void DrawGameState(struct Game *game); diff --git a/src/timeline.c b/src/timeline.c index 3793b90..6f608f2 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -25,7 +25,7 @@ unsigned int lastid; struct Game* game = NULL; struct TM_Action *queue, *background; -bool paused; +// TODO: consider moving globals to structure void TM_Init(struct Game* g) { PrintConsole(g, "Timeline Manager: init"); @@ -33,16 +33,12 @@ void TM_Init(struct Game* g) { lastid = 0; queue = NULL; background = NULL; - paused = false; } void TM_Process() { if (!game) return; - if (paused) return; - /* - process first element from queue - if returns true, then delete it - */ + /* process first element from queue + if returns true, delete it */ if (queue) { if (*queue->function) { queue->active = true; @@ -152,14 +148,12 @@ void TM_Draw() { void TM_Pause() { PrintConsole(game, "Timeline Manager: Pause."); - paused = true; PauseTimers(true); Propagate(TM_ACTIONSTATE_PAUSE); } void TM_Resume() { PrintConsole(game, "Timeline Manager: Resume."); - paused = false; Propagate(TM_ACTIONSTATE_RESUME); PauseTimers(false); } @@ -167,7 +161,6 @@ void TM_Resume() { void TM_HandleEvent(ALLEGRO_EVENT *ev) { if (ev->type != ALLEGRO_EVENT_TIMER) return; if (!game) return; - if (paused) return; if (queue) { if (ev->timer.source == queue->timer) { queue->active=true;