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:
co-authored by
Copilot Autofix powered by AI
parent
2329c4c681
commit
92cb9f9883
+3
-6
@@ -40,9 +40,6 @@ typedef struct CardObject CardObject;
|
||||
typedef struct Card Card;
|
||||
typedef struct JokerObject JokerObject;
|
||||
|
||||
typedef void (*GameStateCallback)(void);
|
||||
typedef void (*SubStateActionFn)(void);
|
||||
|
||||
// Enum value names in ../include/def_state_info_table.h
|
||||
enum GameState
|
||||
{
|
||||
@@ -67,7 +64,7 @@ enum PlayState
|
||||
};
|
||||
|
||||
// Game functions
|
||||
void game_init();
|
||||
void game_init(void);
|
||||
|
||||
/**
|
||||
* @brief Called when exiting the Game Over screen (both win or lose) to reset game variables
|
||||
@@ -77,9 +74,9 @@ void game_init();
|
||||
* and shouldn't be called from other states, otherwise some data such as shop jokers
|
||||
* may not be properly reset.
|
||||
*/
|
||||
void game_reset();
|
||||
void game_reset(void);
|
||||
|
||||
void game_update();
|
||||
void game_update(void);
|
||||
void game_change_state(enum GameState new_game_state);
|
||||
|
||||
CardObject** get_played_array(void);
|
||||
|
||||
+30
-5
@@ -23,6 +23,13 @@
|
||||
*/
|
||||
#define MAX_LIST_NODES 128
|
||||
|
||||
/**
|
||||
* @brief Default list declaration for empty lists
|
||||
*/
|
||||
// clang-format off
|
||||
#define LIST_DEFAULT { .head = NULL, .tail = NULL, .len = 0 }
|
||||
// clang-format on
|
||||
|
||||
typedef struct ListNode ListNode;
|
||||
|
||||
/**
|
||||
@@ -106,15 +113,16 @@ typedef struct
|
||||
} ListItr;
|
||||
|
||||
/**
|
||||
* Create a list.
|
||||
* Initialize a list.
|
||||
*
|
||||
* While this function does not allocate memory for the list itself, the list does allocate memory
|
||||
* for each element. So every created list must be freed with @ref list_clear to ensure the list's
|
||||
* nodes are deleted properly.
|
||||
* Set the values of a list to default.
|
||||
*
|
||||
* If using this function to reset a list, the list must be freed with @ref list_clear to ensure the
|
||||
* list's nodes are deleted properly.
|
||||
*
|
||||
* @return A @ref List with head and tail reset.
|
||||
*/
|
||||
List list_create(void);
|
||||
List list_init(void);
|
||||
|
||||
/**
|
||||
* Clear a list.
|
||||
@@ -122,6 +130,8 @@ List list_create(void);
|
||||
* Go through the list and free each node and set the `head` and `tail` to `NULL`.
|
||||
* Note, it doesn't "free" the data at the node.
|
||||
*
|
||||
* @note To reset an existing list to default values, first call `list_clear` then @ref list_init
|
||||
*
|
||||
* @param list pointer to a @ref List to clear
|
||||
*/
|
||||
void list_clear(List* list);
|
||||
@@ -222,6 +232,18 @@ void* list_get_at_idx(List* list, unsigned int idx);
|
||||
*/
|
||||
bool list_remove_at_idx(List* list, unsigned int idx);
|
||||
|
||||
/**
|
||||
* Remove a List's node with the matching pointer
|
||||
*
|
||||
* @param list pointer to a @ref List
|
||||
* @param data pointer to data in node in list
|
||||
*
|
||||
* @return `true` if successfully removed, `false` otherwise
|
||||
*
|
||||
* @note When working with @ref ListItr, use @ref list_itr_remove_current_node()
|
||||
*/
|
||||
bool list_remove_data(List* list, void* data);
|
||||
|
||||
/**
|
||||
* Get the number of elements in a @ref List
|
||||
*
|
||||
@@ -265,6 +287,9 @@ void* list_itr_next(ListItr* itr);
|
||||
* most recently returned valu from @ref list_itr_next()
|
||||
*
|
||||
* @param itr pointer to the @ref ListItr
|
||||
*
|
||||
* @note When working with @ref ListItr, use this and not @ref list_remove_at() as it will
|
||||
* "break" the iterator.
|
||||
*/
|
||||
void list_itr_remove_current_node(ListItr* itr);
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* @file state_machine.h
|
||||
*
|
||||
* @brief State Machine
|
||||
*
|
||||
* This file is the interface into a generic state machine system.
|
||||
*
|
||||
* State machines are defined as an array of function callbacks where each state
|
||||
* is an index in the array with three functions per state: `on_init()`,
|
||||
* `on_update()`, and `on_exit()`.
|
||||
*
|
||||
* **`on_init()`** Ran once when transitioning into the new state
|
||||
* **`on_update()`** Ran once every frame
|
||||
* **`on_exit()`** Ran once when exiting a state, for cleanup.
|
||||
*
|
||||
* The state machine `on_update()` function is "registered" to a linked-list of
|
||||
* other state machine update functions. This list will call the state machines
|
||||
* active `on_update()` function. This allows much of the complexity of state
|
||||
* transitions to remain central to `state_machine.c`. Also, multiple state
|
||||
* machines can be registered to this list. This allows using substates within
|
||||
* states, or have one off state machines like animation controllers.
|
||||
*
|
||||
* When a state machine is finished, it can "remove" itself from the main update
|
||||
* callback list. This can be done within the update method of its own state
|
||||
* machine. This can be used to start a self destructing state machine.
|
||||
*/
|
||||
#ifndef STATE_MACHINE_H
|
||||
#define STATE_MACHINE_H
|
||||
|
||||
/**
|
||||
* @brief State machine callback function pointer type
|
||||
*/
|
||||
typedef void (*StateCallback)(void);
|
||||
|
||||
/**
|
||||
* @brief State machine callbacks
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
StateCallback on_init;
|
||||
StateCallback on_update;
|
||||
StateCallback on_exit;
|
||||
} StateInfo;
|
||||
|
||||
/**
|
||||
* @brief State machine instance
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
/**
|
||||
* @brief Pointer to the active update function in `state_infos`
|
||||
*/
|
||||
StateCallback active_update;
|
||||
|
||||
/**
|
||||
* @brief Array of @ref StateCallbacks , one entry per state
|
||||
*/
|
||||
StateInfo* state_infos;
|
||||
|
||||
/**
|
||||
* @brief Number of elements in the `state_infos` array
|
||||
*/
|
||||
unsigned int num_infos;
|
||||
|
||||
/**
|
||||
* @brief The current state of the state machine, the offset into state_infos
|
||||
*/
|
||||
int state;
|
||||
} StateMachine;
|
||||
|
||||
/**
|
||||
* @brief Register a statemachine to run it's update function once per frame
|
||||
*
|
||||
* @param state_machine pointer to @ref StateMachine to register, cannot be NULL
|
||||
*/
|
||||
void state_machine_register(StateMachine* state_machine);
|
||||
|
||||
/**
|
||||
* @brief Remove a statemachine's update function
|
||||
*
|
||||
* @param state_machine pointer to @ref StateMachine to remove, cannot be NULL
|
||||
*/
|
||||
void state_machine_remove(StateMachine* state_machine);
|
||||
|
||||
/**
|
||||
* @brief Update registered state machines' update functions
|
||||
*/
|
||||
void state_machine_update(void);
|
||||
|
||||
/**
|
||||
* @brief Calls the current state's on_exit, the new state's on_init, and sets the active update fn
|
||||
*
|
||||
* @param state_machine pointer to @ref StateMachine, cannot be NULL
|
||||
* @param new_state offset into `state_infos` array to transition to
|
||||
*/
|
||||
void state_machine_change_state(StateMachine* state_machine, int new_state);
|
||||
|
||||
/**
|
||||
* @brief no operation
|
||||
*/
|
||||
void noop(void);
|
||||
|
||||
// clang-format off
|
||||
#define STATE_INFO_UPDATE_FN_ONLY(fn) {.on_init = noop, .on_update = fn, .on_exit = noop}
|
||||
// clang-format on
|
||||
|
||||
#endif // STATE_MACHINE_H
|
||||
Reference in New Issue
Block a user