Refactor/state machine (#470)

* initial additions

* Move current_blind to game vars

* tmp

* tmp

* tmp

* tmp

* tmp

* tmp

* tmp

* Compile but is broken

* temp fix for token location

* tmp

* Working, lets clean up

* remove todo

* Remove odd reroll blind variable

* fix skipping blind screen

* fix reroll

* remove magic for blind token locations

* clang format

* Cleanup button highlight in blind menu

* some cleanup

* Add layout file for global UI rects

* remove dead code

* Clang format

* clang format it up

* Move reset top left funct to gfx utils

* make note of soon-to-be removed vars

* Cleanup gamevars declaration

* Cleanup layout file

* Apply suggestions from code review

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Add include for stdbool.h in common_ui.h

* Cleanup rect declaration

* Start moving state machine stuff to new file

* quick tmp

* oops, add new files

* working call list

* fixed pointer stuff

* working as per norm

* working replacement

* move defines to static const

* fix compile issue

* Add some missing "void"s

* Working new substates

* add statemachine to blind select

* Fix game restart statemachine leak

* clang-format

* Clang format

* Rework shop exit logic

* Use new state machine in shop

* Remove substates entirely

* Remove debug stuff

* Clang format

* document state machine better

* update list tests

* clang-format

* Update for PR

* Claaaaang format

* another clang-format

* Remove extra state in round_end

* cleanup docs for removals in lists

* clang-format... again

* update doxygen format

* Clang format

* Update list docs

* Update lists tests

* Fix bad conflict resolution

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Rickey
2026-05-22 18:06:13 -07:00
committed by GitHub
parent 2329c4c681
commit 92cb9f9883
11 changed files with 428 additions and 165 deletions
+24 -26
View File
@@ -20,6 +20,7 @@
#include "random.h"
#include "save.h"
#include "soundbank.h"
#include "state_machine.h"
#include "timer.h"
#include "util.h"
@@ -62,8 +63,6 @@ static const Rect SHOP_REROLL_RECT = { 88, 96, UNDEFINED, UNDEFINED };
static const BG_POINT SHOP_JOKER_SPRITES_INIT_POS = {120, 160};
// clang-format on
// Shop Substates
enum GameShopStates
{
GAME_SHOP_INTRO,
@@ -72,14 +71,19 @@ enum GameShopStates
GAME_SHOP_MAX
};
static void game_shop_intro();
static void game_shop_process_user_input();
static void game_shop_outro();
static void game_shop_intro(void);
static void game_shop_process_user_input(void);
static void game_shop_outro(void);
static const SubStateActionFn shop_state_actions[] = {
game_shop_intro,
game_shop_process_user_input,
game_shop_outro
static StateInfo shop_state_actions[] = {
STATE_INFO_UPDATE_FN_ONLY(game_shop_intro),
STATE_INFO_UPDATE_FN_ONLY(game_shop_process_user_input),
STATE_INFO_UPDATE_FN_ONLY(game_shop_outro),
};
static StateMachine shop_sm = {
.state_infos = &shop_state_actions[0],
.num_infos = GAME_SHOP_MAX,
};
// Shop SelectionGrid
@@ -132,7 +136,6 @@ static Button reroll_button = {
// Shop internal variables
static int timer;
static enum GameShopStates substate;
static int reroll_cost = REROLL_BASE_COST;
@@ -165,7 +168,9 @@ void game_shop_on_init(void)
game_shop_change_background();
timer = TM_ZERO;
substate = GAME_SHOP_INTRO;
state_machine_register(&shop_sm);
state_machine_change_state(&shop_sm, GAME_SHOP_INTRO);
// The selection grid is initialized outside of bounds and moved
// to trigger the selection change so the initial selection is visible
@@ -245,7 +250,7 @@ static void game_shop_create_items(void)
List* shop_jokers_list = get_shop_jokers_list();
list_clear(shop_jokers_list);
*shop_jokers_list = list_create();
*shop_jokers_list = list_init();
for (int i = 0; i < MAX_SHOP_JOKERS; i++)
{
@@ -326,7 +331,7 @@ static void game_shop_intro()
if (timer == TM_END_GAME_SHOP_INTRO)
{
substate = GAME_SHOP_ACTIVE;
state_machine_change_state(&shop_sm, GAME_SHOP_ACTIVE);
timer = TM_ZERO; // Reset the timer
// print initial reroll cost only when the panel is in place
@@ -515,7 +520,7 @@ static inline void game_shop_reroll(int* reroll_cost)
}
list_clear(shop_jokers_list);
*shop_jokers_list = list_create();
*shop_jokers_list = list_init();
game_shop_create_items();
@@ -559,7 +564,7 @@ static void shop_reroll_row_on_key_transit(SelectionGrid* selection_grid, Select
static void next_round_on_pressed(void)
{
// Go to next blind selection game state
substate = GAME_SHOP_EXIT; // Go to the outro sequence state
state_machine_change_state(&shop_sm, GAME_SHOP_EXIT);
timer = TM_ZERO;
reroll_cost = REROLL_BASE_COST;
@@ -624,8 +629,7 @@ static void game_shop_outro()
if (timer >= MENU_POP_OUT_ANIM_FRAMES)
{
substate = GAME_SHOP_MAX; // Go to the next state
timer = TM_ZERO; // Reset the timer
game_change_state(GAME_STATE_BLIND_SELECT);
}
}
@@ -681,14 +685,6 @@ void game_shop_on_update(void)
{
game_shop_lights_anim_frame();
}
if (substate == GAME_SHOP_MAX)
{
game_change_state(GAME_STATE_BLIND_SELECT);
return;
}
shop_state_actions[substate]();
}
void game_shop_on_exit(void)
@@ -711,5 +707,7 @@ void game_shop_on_exit(void)
increment_blind(BLIND_STATE_DEFEATED); // TODO: Move to game_round_end()?
state_machine_remove(&shop_sm);
save_game();
}
}