diff options
Diffstat (limited to 'game.c')
-rw-r--r-- | game.c | 134 |
1 files changed, 113 insertions, 21 deletions
@@ -1,15 +1,38 @@ #include "asset.h" #include "enemy.h" #include "game.h" +#include "input.h" #include "platform.h" #include "sprite.h" #include "standard.h" #include "systems.h" -static void menu_new(Menu* menu) { +void gameplay_new(Game* game) { + World* world; + + world = &game->world; + + seed_rng(platform_get_time()); + init_world(&game->world); + init_map(&world->map, world); + init_player(&world->player, world); + init_waver(&world->waver); + world->gmemory = gmemory_max; +} + +void gameplay_next_wave(Game* game) { + World* world; + + world = &game->world; + next_wave(&world->waver); + game_change_state(game, game_state_game); +} + +static void menu_start(Menu* menu) { Game* game; game = menu->ptr; + gameplay_new(game); game_change_state(game, game_state_game); } @@ -17,6 +40,7 @@ static void menu_restart(Menu* menu) { Game* game; game = menu->ptr; + gameplay_new(game); game_change_state(game, game_state_game); } @@ -45,7 +69,7 @@ static void menu_init(Game* game) { font = get_default_font(); init_menu(&game->menu, game); - menu_add(&game->menu, "Play", menu_new, font); + menu_add(&game->menu, "Play", menu_start, font); menu_add(&game->menu, "Credits", menu_credits, font); menu_add(&game->menu, "Quit", menu_quit, font); } @@ -95,17 +119,11 @@ static void menu_deinit(Game* game) { } static void gameplay_init(Game* game) { - World* world; - - world = &game->world; - - seed_rng(500); - init_world(&game->world); - init_map(&world->map, world); - init_player(&world->player, world); - init_waver(&world->waver); + start_wave(&game->world); - world->gmemory = gmemory_max; + game->want_next = 0; + game->wave_timer = 0; + game->display_wave_text = 100; } static void render_hud(Game* game) { @@ -164,27 +182,73 @@ static void render_hud(Game* game) { } static void gameplay_update(Game* game) { - int cx, cy; + int cx, cy, ec, l; World* world; Player* player; - cx = game->world.cam_x; - cy = game->world.cam_y; + const Waver* waver; + const BM_Font* font; + Colour col; + char buf[32]; world = &game->world; player = &world->player; + waver = &world->waver; + cx = world->cam_x; + cy = world->cam_y; + + font = get_default_font(); update_waver(&world->waver, world); + ec = wave_enemy_count(&world->waver); + + if ( + world->enemies_killed >= ec && + !game->want_next + ) { + if (waver->idx >= get_wave_count() - 1) { + world->oom = -1; + game_change_state(game, game_state_dead); + } + + game->wave_timer = 0; + game->want_next = 1; + } + + if (game->want_next && game->wave_timer > 200) { + gameplay_next_wave(game); + } update_player(player, world); enemy_system(world); debris_system(world); bullet_system(world); collision_system(world); + spawner_system(world); animation_system(world); update_camera(player, world); render_map(&world->map, cx, cy); sprite_system(world); + if (game->display_wave_text) { + col = make_colour(0x7f8dcf, 255); + buf[0] = 'W'; + buf[1] = 'a'; + buf[2] = 'v'; + buf[3] = 'e'; + buf[4] = ' '; + l = 5 + int_to_buf(world->waver.idx + 1, buf + 5); + rfont_text_col( + font, + renderer_w / 2 - (l * font->char_w) / 2, + renderer_h / 2, + buf, + col + ); + game->display_wave_text--; + } + + game->wave_timer++; + render_hud(game); if (world->gmemory <= 0) { @@ -202,18 +266,38 @@ static void credits_init(Game* game) { } static void credits_update(Game* game) { + Colour colour; + const BM_Font* font; + font = get_default_font(); + + if ( + button_just_pressed(btn_shoot) || + button_just_pressed(btn_jump) + ) { + game_change_state(game, game_state_menu); + } + + rfont_text(font, 0, 0, "Programming: quou"); + rfont_text(font, 0, 10, "Design: quou"); + rfont_text(font, 0, 20, "Sound: quou"); + rfont_text(font, 0, 30, "Graphics: quou"); + rfont_text(font, 0, 40, "Music: drummyfish"); + rfont_text(font, 0, 50, "Source Code: git.quou.xyz"); } static void credits_deinit(Game* game) { - } static void dead_init(Game* game) { - const BM_Font* font = get_default_font(); - init_menu(&game->menu, game); - menu_add(&game->menu, "Restart", menu_restart, font); - menu_add(&game->menu, "Main Menu", menu_main_menu, font); + const BM_Font* font; + Menu* menu; + + menu = &game->menu; + font = get_default_font(); + init_menu(menu, game); + menu_add(menu, "Restart", menu_restart, font); + menu_add(menu, "Main Menu", menu_main_menu, font); } static void dead_update(Game* game) { @@ -230,14 +314,22 @@ static void dead_update(Game* game) { cy = game->world.cam_y; render_map(&game->world.map, cx, cy); sprite_system(&game->world); + render_hud(game); - if (world->oom) { + if (world->oom == 1) { rfont_text( font, renderer_w / 2 - 65, game->menu.y - 15, "Out of Memory" ); + } else if (world->oom == -1) { + rfont_text( + font, + renderer_w / 2 - 35, + game->menu.y - 15, + "You win" + ); } else { rfont_text( font, |