diff --git a/Makefile b/Makefile index d90411f..b5217f1 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ SRC = src/main.c \ src/start_game.c \ src/change_map.c \ src/flags.c \ + src/menu.c \ $(addprefix src/utils/create/, \ $(addsuffix .c, \ raycasting \ diff --git a/include/menu.h b/include/menu.h new file mode 100644 index 0000000..0157252 --- /dev/null +++ b/include/menu.h @@ -0,0 +1,59 @@ +/* +** EPITECH PROJECT, 2023 +** rpg +** File description: +** menu +*/ + +/* +** game->menu is a stack of screens encoded in base 10: menu = menu * 10 + N +** pushes screen N, menu /= 10 pops. These helpers name the states and the +** predicates/transitions that were previously raw decimal arithmetic; the +** computations are kept identical to the original behaviour. +*/ + +#ifndef MENU_H_ + #define MENU_H_ + + #include "game.h" + + #define MENU_START 0 + #define MENU_SETTINGS 20 + #define MENU_GAME 5 + #define MENU_WIN 42 + #define MENU_LOSE (-42) + #define SCREEN_SAVE 3 + #define SCREEN_PLAY 4 + #define SCREEN_PAUSE 6 + #define MENU_PAUSE_INVENTORY 560 + #define MENU_PAUSE_CHARACTER 561 + #define MENU_PAUSE_QUEST 562 + #define MENU_PAUSE_SETTINGS 5630 + +void menu_pop(game_t *game); +void menu_push(game_t *game, int screen); +void menu_set_tab(game_t *game, int tab); + +bool menu_is_start(game_t *game); +bool menu_is_dialog(game_t *game); +bool menu_is_save(game_t *game); +bool menu_in_overworld(game_t *game); +bool menu_show_hud(game_t *game); +bool menu_won(game_t *game); +bool menu_lost(game_t *game); + +bool menu_pause_open(game_t *game); +bool menu_pause_active(game_t *game); +bool menu_in_pause_tab(game_t *game); +bool menu_tab_inventory(game_t *game); +bool menu_tab_character(game_t *game); +bool menu_tab_quest(game_t *game); + +bool menu_in_settings(game_t *game); +bool menu_settings_open(game_t *game); +bool menu_tab_fps(game_t *game); +bool menu_tab_music(game_t *game); +bool menu_tab_keyboard(game_t *game); +bool menu_tab_window(game_t *game); + +#endif /* !MENU_H_ */ diff --git a/src/change_map.c b/src/change_map.c index 76afa73..302c0a5 100644 --- a/src/change_map.c +++ b/src/change_map.c @@ -6,10 +6,11 @@ */ #include "game.h" +#include "menu.h" void change_map(game_t *game) { - if (game->menu == 5) { + if (game->menu == MENU_GAME) { if (game->perso->pos.x > game->map->rect.size.x * 3 - 32) { game->map->rect.position.x += game->map->rect.size.x / 2; game->perso->pos.x = (game->map->rect.size.x * 3.0) / 2; diff --git a/src/menu.c b/src/menu.c new file mode 100644 index 0000000..1fcdfce --- /dev/null +++ b/src/menu.c @@ -0,0 +1,121 @@ +/* +** EPITECH PROJECT, 2023 +** rpg +** File description: +** menu +*/ + +#include "game.h" +#include "menu.h" + +void menu_pop(game_t *game) +{ + game->menu /= 10; +} + +void menu_push(game_t *game, int screen) +{ + game->menu = game->menu * 10 + screen; +} + +void menu_set_tab(game_t *game, int tab) +{ + game->menu = game->menu / 10 * 10 + tab; +} + +bool menu_is_start(game_t *game) +{ + return game->menu == 0; +} + +bool menu_is_dialog(game_t *game) +{ + return game->menu == 4; +} + +bool menu_is_save(game_t *game) +{ + return game->menu == 3; +} + +bool menu_in_overworld(game_t *game) +{ + return game->menu >= 5 && game->menu < 10; +} + +bool menu_show_hud(game_t *game) +{ + return game->menu >= 5 && game->menu <= 6; +} + +bool menu_won(game_t *game) +{ + return game->is_finished == 1 && game->menu == 42; +} + +bool menu_lost(game_t *game) +{ + return game->is_finished == 1 && game->menu == -42; +} + +bool menu_pause_open(game_t *game) +{ + return game->menu % 10 == 6; +} + +bool menu_pause_active(game_t *game) +{ + return (game->menu % 10 == 6 || (game->menu / 10) % 10 == 6) + && game->menu < 563; +} + +bool menu_in_pause_tab(game_t *game) +{ + return (game->menu / 10) % 10 == 6; +} + +bool menu_tab_inventory(game_t *game) +{ + return (game->menu / 10) % 10 == 6 && game->menu % 10 == 0; +} + +bool menu_tab_character(game_t *game) +{ + return (game->menu / 10) % 10 == 6 && game->menu % 10 == 1; +} + +bool menu_tab_quest(game_t *game) +{ + return (game->menu / 10) % 10 == 6 && game->menu % 10 == 2; +} + +bool menu_in_settings(game_t *game) +{ + return game->menu == 2 || game->menu / 10 == 2 + || (game->menu / 10) % 10 == 3; +} + +bool menu_settings_open(game_t *game) +{ + return game->menu == 2 || game->menu / 10 == 2 || game->menu % 10 == 2; +} + +bool menu_tab_fps(game_t *game) +{ + return game->menu == 2 || game->menu == 25 || game->menu % 10 == 0; +} + +bool menu_tab_music(game_t *game) +{ + return game->menu == 21 || game->menu % 10 == 1; +} + +bool menu_tab_keyboard(game_t *game) +{ + return game->menu == 22 || game->menu % 10 == 2; +} + +bool menu_tab_window(game_t *game) +{ + return game->menu == 24 || game->menu % 10 == 4; +} diff --git a/src/utils/actions.c b/src/utils/actions.c index b2ac6f7..f7f3b2d 100644 --- a/src/utils/actions.c +++ b/src/utils/actions.c @@ -7,12 +7,13 @@ #include "game.h" #include "display.h" +#include "menu.h" void go_back(game_t *game) { - game->menu /= 10; + menu_pop(game); if (game->menu > 500) - game->menu /= 10; + menu_pop(game); relase_button(game->params->visu->navbar->button, 0, 5); } @@ -20,14 +21,14 @@ void change_to_settings(game_t *game) { game->params->tmp = 0; if (game->menu <= 563) - game->menu = 20; + game->menu = MENU_SETTINGS; else - game->menu = game->menu / 10 * 10 + 1; + menu_set_tab(game, 1); } void close_start_all(game_t *game) { - game->menu = game->menu * 10 + 4; + menu_push(game, SCREEN_PLAY); } void exit_start_all(game_t *game) @@ -37,5 +38,5 @@ void exit_start_all(game_t *game) void change_to_save(game_t *game) { - game->menu = game->menu * 10 + 3; + menu_push(game, SCREEN_SAVE); } diff --git a/src/utils/display/change_to_game.c b/src/utils/display/change_to_game.c index cf19118..85d8ab5 100644 --- a/src/utils/display/change_to_game.c +++ b/src/utils/display/change_to_game.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void fade_out(game_t *game, float duration) { @@ -31,7 +32,7 @@ static void fade_out(game_t *game, float duration) void change_to_game(game_t *game) { - if (game->menu != 4 || game->params->tmp != 0) + if (!menu_is_dialog(game) || game->params->tmp != 0) return; sfMusic_stop(game->window->song->music); sfMusic_destroy(game->window->song->music); diff --git a/src/utils/display/clients.c b/src/utils/display/clients.c index 13aef09..5fef39e 100644 --- a/src/utils/display/clients.c +++ b/src/utils/display/clients.c @@ -8,12 +8,13 @@ #include "game.h" #include "display.h" #include "network_functions.h" +#include "menu.h" void display_clients(game_t *game) { int i = 0; - if (game->network == NULL || game->menu < 5 || game->menu >= 10) + if (game->network == NULL || !menu_in_overworld(game)) return; send_player_position(game); receive_clients_infos(game); diff --git a/src/utils/display/dialog.c b/src/utils/display/dialog.c index 44db045..afbd272 100644 --- a/src/utils/display/dialog.c +++ b/src/utils/display/dialog.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static int poll_dialog(game_t *game) { @@ -39,8 +40,8 @@ int diplay_text(game_t *game, char *str) void display_dialog(game_t *game) { - if (game->menu != 4) + if (!menu_is_dialog(game)) return; diplay_text(game, game->dialogs->dialog_text[0]); - game->menu = 5; + game->menu = MENU_GAME; } diff --git a/src/utils/display/end.c b/src/utils/display/end.c index 56a69b7..c6bba81 100644 --- a/src/utils/display/end.c +++ b/src/utils/display/end.c @@ -7,17 +7,18 @@ #include "game.h" #include "display.h" +#include "menu.h" void display_end(game_t *game) { - if (game->is_finished == 1 && game->menu == 42) { + if (menu_won(game)) { sfRenderWindow_drawRectangleShape(game->window->window, game->win->container, NULL); sfRenderWindow_drawRectangleShape(game->window->window, game->win->content, NULL); sfRenderWindow_drawText(game->window->window, game->win->text, NULL); } - if (game->is_finished == 1 && game->menu == -42) { + if (menu_lost(game)) { sfRenderWindow_drawRectangleShape(game->window->window, game->loose->container, NULL); sfRenderWindow_drawRectangleShape(game->window->window, diff --git a/src/utils/display/inventory_bar.c b/src/utils/display/inventory_bar.c index 6b01d22..dd59d9f 100644 --- a/src/utils/display/inventory_bar.c +++ b/src/utils/display/inventory_bar.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void set_item_color(sfRectangleShape *content, sfRectangleShape *cont, int selected) @@ -30,7 +31,7 @@ void display_inventory(game_t *game) sfRectangleShape *content = NULL; int i = 0; - if (game->menu < 5 || game->menu > 6) + if (!menu_show_hud(game)) return; for (i = 0; i < 3; i++) { container = game->inventory->items[i]->container; diff --git a/src/utils/display/map_iso.c b/src/utils/display/map_iso.c index 1104e35..7590269 100644 --- a/src/utils/display/map_iso.c +++ b/src/utils/display/map_iso.c @@ -6,10 +6,11 @@ */ #include "game.h" +#include "menu.h" void display_iso_map(game_t *game) { - if (game->menu >= 5 && game->menu < 10) { + if (menu_in_overworld(game)) { sfRenderWindow_drawSprite(game->window->window, game->map->iso_sprite, NULL); } diff --git a/src/utils/display/menu.c b/src/utils/display/menu.c index dcd5ed3..d2f4fee 100644 --- a/src/utils/display/menu.c +++ b/src/utils/display/menu.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void active_button(game_t *game, int i) { @@ -62,12 +63,11 @@ void display_menu(game_t *game) { sfVector2i mpos = sfMouse_getPositionRenderWindow(game->window->window); - if ((game->menu % 10 != 6 && (game->menu / 10) % 10 != 6) - || game->menu >= 563) { + if (!menu_pause_active(game)) { game->params->tmp = 0; return; } - if (game->menu % 10 == 6) + if (menu_pause_open(game)) relase_button(game->game_menu->sidebar->buttons, 5, 4); draw_menu_bg(game); if (game->params->tmp == 0) { diff --git a/src/utils/display/menu/actions.c b/src/utils/display/menu/actions.c index 438bd98..be76b02 100644 --- a/src/utils/display/menu/actions.c +++ b/src/utils/display/menu/actions.c @@ -6,23 +6,24 @@ */ #include "game.h" +#include "menu.h" void change_to_inventory(game_t *game) { - game->menu = 560; + game->menu = MENU_PAUSE_INVENTORY; } void change_to_character(game_t *game) { - game->menu = 561; + game->menu = MENU_PAUSE_CHARACTER; } void change_to_quest(game_t *game) { - game->menu = 562; + game->menu = MENU_PAUSE_QUEST; } void change_to_option(game_t *game) { - game->menu = 5630; + game->menu = MENU_PAUSE_SETTINGS; } diff --git a/src/utils/display/menu/character.c b/src/utils/display/menu/character.c index 7c03c6e..de3ded0 100644 --- a/src/utils/display/menu/character.c +++ b/src/utils/display/menu/character.c @@ -7,13 +7,14 @@ #include "game.h" #include "display.h" +#include "menu.h" void display_charracter(game_t *game) { sfVector2u win = game->params->window_size; int i = 0; - if ((game->menu / 10) % 10 != 6 || (game->menu % 10) != 1) + if (!menu_tab_character(game)) return; game->perso->pos = (sfVector2f){win.x / 2 - 100, win.y / 2 - 100}; sfSprite_setPosition(game->perso->sprite, game->perso->pos); diff --git a/src/utils/display/menu/inventory.c b/src/utils/display/menu/inventory.c index 7ed84ec..c53f8a5 100644 --- a/src/utils/display/menu/inventory.c +++ b/src/utils/display/menu/inventory.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void replace_bar(game_t *game, item_t *item, int i) { @@ -49,7 +50,7 @@ void display_inventory_menu(game_t *game) { sfVector2u win = game->params->window_size; - if ((game->menu / 10) % 10 != 6 || (game->menu % 10) != 0) + if (!menu_tab_inventory(game)) return; game->perso->pos = (sfVector2f){win.x / 2 - 100, win.y / 2 - 100}; sfSprite_setPosition(game->perso->sprite, game->perso->pos); diff --git a/src/utils/display/menu/quest.c b/src/utils/display/menu/quest.c index ea01863..4035737 100644 --- a/src/utils/display/menu/quest.c +++ b/src/utils/display/menu/quest.c @@ -6,6 +6,7 @@ */ #include +#include "menu.h" #include "game.h" #include "display.h" @@ -33,7 +34,7 @@ static void modify_quest(game_t *game) void display_quest(game_t *game) { - if ((game->menu / 10) % 10 != 6 || (game->menu % 10) != 2) + if (!menu_tab_quest(game)) return; sfRenderWindow_drawRectangleShape(game->window->window, game->game_menu->quest->container, NULL); diff --git a/src/utils/display/mobs.c b/src/utils/display/mobs.c index 6b89951..e18a518 100644 --- a/src/utils/display/mobs.c +++ b/src/utils/display/mobs.c @@ -8,12 +8,13 @@ #include "game.h" #include "display.h" #include "mobs_functions.h" +#include "menu.h" void display_mobs(game_t *game) { int i = 0; - if (game->menu < 5 || game->menu > 6) + if (!menu_show_hud(game)) return; manage_mobs(game); for (i = 0; game->mobs[i] != NULL; i++) { diff --git a/src/utils/display/npc.c b/src/utils/display/npc.c index 561776a..fa0f81a 100644 --- a/src/utils/display/npc.c +++ b/src/utils/display/npc.c @@ -9,12 +9,13 @@ #include "game.h" #include "display.h" +#include "menu.h" static void try_interact(game_t *game, int i) { quest_t *quest = game->game_menu->quest; - game->menu = 5; + game->menu = MENU_GAME; diplay_text(game, game->npc[i]->dialog); if (game->npc[i]->dialog_index != 0 && game->npc[i]->dialog_index > quest->nb_achievement) @@ -39,7 +40,7 @@ void display_npc(game_t *game) sfVector2f second_pos = {0}; int i = 0; - if (game->menu < 5 || game->menu >= 10) + if (!menu_in_overworld(game)) return; for (i = 0; i < 7; i++) { second_pos = (sfVector2f){game->npc[i]->pos.x * 3 diff --git a/src/utils/display/option.c b/src/utils/display/option.c index f2b86fd..5f60b32 100644 --- a/src/utils/display/option.c +++ b/src/utils/display/option.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" void relase_button(buttons_t **button, int i, int max) { @@ -21,7 +22,7 @@ static void active_button(game_t *game, int i) { game->params->visu->navbar->button[i]->state = ACTIVE; relase_button(game->params->visu->navbar->button, i, 5); - game->menu = game->menu / 10 * 10 + i; + menu_set_tab(game, i); sfSleep((sfTime){100000}); } @@ -77,10 +78,9 @@ void display_options(game_t *game) { sfVector2i mpos = sfMouse_getPositionRenderWindow(game->window->window); - if ((game->menu != 2 && game->menu / 10 != 2) - && (game->menu / 10) % 10 != 3) + if (!menu_in_settings(game)) return; - if (game->menu == 2 || game->menu / 10 == 2 || game->menu % 10 == 2) + if (menu_settings_open(game)) game->params->visu->navbar->button[0]->state = ACTIVE; game->go_back->pos = (sfVector2f){20, 100}; sfSprite_setPosition(game->go_back->sprite, game->go_back->pos); diff --git a/src/utils/display/options/fps.c b/src/utils/display/options/fps.c index 2327651..bae54b8 100644 --- a/src/utils/display/options/fps.c +++ b/src/utils/display/options/fps.c @@ -8,6 +8,7 @@ #include "game.h" #include "display.h" #include "my_str.h" +#include "menu.h" static int active_button(game_t *game, int i) { @@ -68,7 +69,7 @@ void display_fps(game_t *game) { sfVector2i mpos = sfMouse_getPositionRenderWindow(game->window->window); - if (game->menu != 2 && game->menu != 25 && game->menu % 10 != 0) + if (!menu_tab_fps(game)) return; game->params->visu->fps->button[check_button(game)]->state = ACTIVE; display_button(game, &mpos); diff --git a/src/utils/display/options/keyboard.c b/src/utils/display/options/keyboard.c index c37372a..628d329 100644 --- a/src/utils/display/options/keyboard.c +++ b/src/utils/display/options/keyboard.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void change_keyboard(game_t *game) { @@ -70,7 +71,7 @@ void display_keyboard(game_t *game) { sfVector2i mpos = sfMouse_getPositionRenderWindow(game->window->window); - if (game->menu != 22 && game->menu % 10 != 2) + if (!menu_tab_keyboard(game)) return; display_button(game, &mpos); } diff --git a/src/utils/display/options/music.c b/src/utils/display/options/music.c index c11681d..ad64bbc 100644 --- a/src/utils/display/options/music.c +++ b/src/utils/display/options/music.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void set_volume(game_t *game, const sfVector2i *mouse, const sfVector2f *pos) @@ -26,7 +27,7 @@ void display_music(game_t *game) sfVector2i mouse = sfMouse_getPositionRenderWindow(game->window->window); sfVector2f pos = sfRectangleShape_getPosition(music->bar); - if (game->menu != 21 && game->menu % 10 != 1) + if (!menu_tab_music(game)) return; if (mouse.x >= pos.x && mouse.x <= pos.x + 500 && mouse.y >= pos.y && mouse.y <= pos.y + 50) { diff --git a/src/utils/display/options/window.c b/src/utils/display/options/window.c index efeff37..f1e173b 100644 --- a/src/utils/display/options/window.c +++ b/src/utils/display/options/window.c @@ -8,6 +8,7 @@ #include "game.h" #include "display.h" #include "my_str.h" +#include "menu.h" static void unselect_others(game_t *game, int i, int max, char *str) { @@ -108,7 +109,7 @@ void display_window_buttons(game_t *game) { sfVector2i mpos = sfMouse_getPositionRenderWindow(game->window->window); - if (game->menu != 24 && game->menu % 10 != 4) + if (!menu_tab_window(game)) return; set_active_states(game); display_size_buttons(game, &mpos); diff --git a/src/utils/display/overlay.c b/src/utils/display/overlay.c index 421ee17..b382d58 100644 --- a/src/utils/display/overlay.c +++ b/src/utils/display/overlay.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void display_strength(game_t *game) { @@ -33,7 +34,7 @@ static void change_life_color(game_t *game) void display_overlay(game_t *game) { - if (game->menu < 5 || game->menu > 6) + if (!menu_show_hud(game)) return; sfRenderWindow_drawSprite(game->window->window, game->overlay->life->sprite, NULL); diff --git a/src/utils/display/perso.c b/src/utils/display/perso.c index 54fe743..0dacac1 100644 --- a/src/utils/display/perso.c +++ b/src/utils/display/perso.c @@ -6,6 +6,7 @@ */ #include +#include "menu.h" #include "game.h" #include "collisions.h" @@ -92,7 +93,7 @@ void display_perso(game_t *game) { perso_t *perso = game->perso; - if (game->menu < 5 || game->menu >= 10) + if (!menu_in_overworld(game)) return; move_player_horizontal(game); move_player_vertical(game); diff --git a/src/utils/display/save.c b/src/utils/display/save.c index 0f0965f..9d2e3b2 100644 --- a/src/utils/display/save.c +++ b/src/utils/display/save.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void draw_slot(game_t *game, int i, const sfVector2i *mpos) { @@ -28,7 +29,7 @@ void display_save(game_t *game) sfVector2i mpos = sfMouse_getPositionRenderWindow(game->window->window); int i = 0; - if (game->menu != 3) + if (!menu_is_save(game)) return; display_go_back(game); sfRenderWindow_drawText(game->window->window, diff --git a/src/utils/display/start_menu.c b/src/utils/display/start_menu.c index 6a21a9a..ffb26ce 100644 --- a/src/utils/display/start_menu.c +++ b/src/utils/display/start_menu.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static void update_button(game_t *game, int i, const sfVector2i *mpos) { @@ -38,7 +39,7 @@ void display_start_menu(game_t *game) { sfVector2i mpos = sfMouse_getPositionRenderWindow(game->window->window); - if (game->menu != 0) + if (!menu_is_start(game)) return; display_buttons(game, &mpos); } diff --git a/src/utils/display/window.c b/src/utils/display/window.c index 6ef720e..3b06743 100644 --- a/src/utils/display/window.c +++ b/src/utils/display/window.c @@ -7,6 +7,7 @@ #include "game.h" #include "display.h" +#include "menu.h" static int show_map(int menu) { @@ -17,7 +18,7 @@ static int show_map(int menu) void display_window(game_t *game) { - if (game->menu == 0) + if (menu_is_start(game)) sfRenderWindow_drawRectangleShape(game->window->window, game->window->rect[0], NULL); else if (game->menu >= 5 && show_map(game->menu)) diff --git a/src/utils/events/menu.c b/src/utils/events/menu.c index 67bcce7..2ccc1de 100644 --- a/src/utils/events/menu.c +++ b/src/utils/events/menu.c @@ -7,15 +7,16 @@ #include "game.h" #include "events.h" +#include "menu.h" void event_menu(game_t *game) { if (game->menu < 5) exit_start_all(game); - if (game->menu < 10 && game->menu >= 5) { - game->menu = (game->menu * 10) + 6; + if (menu_in_overworld(game)) { + menu_push(game, SCREEN_PAUSE); return; } - if (game->menu % 10 == 6 || (game->menu / 10) % 10 == 6) - game->menu /= 10; + if (menu_pause_open(game) || menu_in_pause_tab(game)) + menu_pop(game); } diff --git a/src/utils/mobs/manage_mobs.c b/src/utils/mobs/manage_mobs.c index fe95fa3..b07cbd0 100644 --- a/src/utils/mobs/manage_mobs.c +++ b/src/utils/mobs/manage_mobs.c @@ -9,6 +9,7 @@ #include "game.h" #include "mobs_functions.h" +#include "menu.h" static sfVector2f update_and_draw_ennemy(game_t *game, int i) { @@ -49,7 +50,7 @@ static void reward_kill(game_t *game, mobs_t *mob) return; game->game_menu->quest->nb_achievement += 1; game->is_finished = true; - game->menu = 42; + game->menu = MENU_WIN; } static void win_perso(game_t *game, mobs_t *mob) @@ -67,7 +68,7 @@ static void win_perso(game_t *game, mobs_t *mob) if (game->perso->combat->life <= 0) { game->perso->combat->life = 0; game->is_finished = true; - game->menu = -42; + game->menu = MENU_LOSE; } }