From 795f0ef5c10b59d34a69c5dbb0d41391a1b23af2 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 21 Apr 2021 17:51:37 +0200 Subject: [PATCH] Improved main menu and added GamePause state --- include/game_state.h | 2 +- include/input.h | 4 ++-- src/main.c | 16 ++++++++++++---- src/mainmenu/update.c | 22 ++++++++++++++-------- src/player/update.c | 8 ++++++-- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/include/game_state.h b/include/game_state.h index a6688a8..456df51 100644 --- a/include/game_state.h +++ b/include/game_state.h @@ -2,4 +2,4 @@ /* Copyright (C) 2021 KikooDX */ #pragma once -enum GameState { TitleScreen, MainMenu, LevelSelection, Playing, PackDone }; +enum GameState { TitleScreen, MainMenu, LevelSelection, Playing, GamePause, PackDone }; diff --git a/include/input.h b/include/input.h index a494587..2f3ba3a 100644 --- a/include/input.h +++ b/include/input.h @@ -2,8 +2,8 @@ /* Copyright (C) 2021 KikooDX */ #pragma once -#define KEYS_COUNT 5 -enum Keys { K_LEFT, K_RIGHT, K_UP, K_DOWN, K_A }; +#define KEYS_COUNT 6 +enum Keys { K_LEFT, K_RIGHT, K_UP, K_DOWN, K_A, K_START }; enum KeyState { KS_UP = 0, KS_DOWN, KS_PRESS }; struct Input { diff --git a/src/main.c b/src/main.c index dc0c7c9..49f4693 100644 --- a/src/main.c +++ b/src/main.c @@ -50,7 +50,7 @@ int main(void) int i; int timer; int player_return_code; - int level_pack_beaten; + /* int level_pack_beaten; */ enum TransitionMode transition_previous_mode; enum GameState game_state = TitleScreen; volatile int has_ticked = 1; @@ -121,13 +121,16 @@ int main(void) player_return_code = player_update(&player, input); switch (player_return_code) { + case -1: + game_state = GamePause; + break; case 1: level_id += 1; transition = transition_init( H_TRANS_SPD, ZX_BLUE, TransitionHOut); break; - case -1: + case 2: transition = transition_init( V_TRANS_SPD, ZX_RED, TransitionVOut); @@ -152,8 +155,8 @@ int main(void) /* end level pack */ if (level_id % 4 == 0) { game_state = PackDone; - level_pack_beaten = - level_id / 4; + /* level_pack_beaten = + level_id / 4; */ } LOAD_LEVEL(); transition = transition_init( @@ -175,6 +178,9 @@ int main(void) } } break; + case GamePause: + game_state = Playing; + break; case PackDone: game_state = LevelSelection; break; @@ -201,6 +207,8 @@ int main(void) player_draw(player); transition_draw(transition); break; + case GamePause: + break; case PackDone: break; default: diff --git a/src/mainmenu/update.c b/src/mainmenu/update.c index 56d3a7e..ba3cac2 100644 --- a/src/mainmenu/update.c +++ b/src/mainmenu/update.c @@ -10,15 +10,21 @@ int mainmenu_update(struct MainMenu *mainmenu, struct Input input) { /* decrease selected pack id */ - if ((input.keystates[K_UP] == KS_PRESS || - input.keystates[K_LEFT] == KS_PRESS) && - mainmenu->cursor > 0) - mainmenu->cursor -= 1; + if (input.keystates[K_UP] == KS_PRESS || + input.keystates[K_LEFT] == KS_PRESS) { + if (mainmenu->cursor > 0) + mainmenu->cursor -= 1; + else + mainmenu->cursor = MENU_ENTRIES - 1; + } /* increase selected pack id */ - if ((input.keystates[K_DOWN] == KS_PRESS || - input.keystates[K_RIGHT] == KS_PRESS) && - mainmenu->cursor < MENU_ENTRIES - 1) - mainmenu->cursor += 1; + if (input.keystates[K_DOWN] == KS_PRESS || + input.keystates[K_RIGHT] == KS_PRESS) { + if (mainmenu->cursor < MENU_ENTRIES - 1) + mainmenu->cursor += 1; + else + mainmenu->cursor = 0; + } /* confirm */ if (input.keystates[K_A] == KS_PRESS) return 1; diff --git a/src/player/update.c b/src/player/update.c index 2cb799b..b1c1199 100644 --- a/src/player/update.c +++ b/src/player/update.c @@ -12,7 +12,7 @@ static void player_move(struct Player *player, int x, int y); extern struct Level level; -/* return 1 if exit reached, -1 on death/reset and 0 otherwise */ +/* return -1 on pause, return 1 if exit reached, 2 on death/reset and 0 otherwise */ int player_update(struct Player *player, struct Input input) { Tile collisions[COLLIDE_POINTS]; @@ -25,6 +25,10 @@ int player_update(struct Player *player, struct Input input) const int k_jump = input.keystates[K_A] != KS_UP; const int kp_jump = input.keystates[K_A] == KS_PRESS; + /* pause the game? */ + if (input.keystates[K_START] == KS_PRESS) + return -1; + /* horizontal friction/acceleration */ player->spd_x *= (on_ground) ? (1 - FRC_GND) : (1 - FRC_AIR); player->spd_x += dir_x * ((on_ground) ? (ACC_GND) : (ACC_AIR)); @@ -122,7 +126,7 @@ int player_update(struct Player *player, struct Input input) } if (player_collide_tile(collisions, player->x, player->y, TILE_LETAL, MARGIN_LETAL, 1)) { - return -1; + return 2; } return 0; }