From 7a0a60846a14cc60ec657deb785125eb81cb618f Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 22 Nov 2023 13:02:21 +0100 Subject: [PATCH] feat(state machine): state machine now no longer has a separate next function instead update is expected to return the next state --- core/src/state.h | 13 +++++-------- core/src/state_machine.c | 29 +++++++++++++++-------------- core/src/state_machine.h | 4 ++-- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/core/src/state.h b/core/src/state.h index 5e281fa..f986085 100644 --- a/core/src/state.h +++ b/core/src/state.h @@ -8,23 +8,20 @@ typedef struct State State; struct State { void (*const enter)(void* data); void (*const exit)(void* data); - void (*const update)(void* data, float dt); - const State* (*const next)(void* data); + const State* (*const update)(void* data, float dt); }; -#define DefineState(_StateName, enter_fn, exit_fn, update_fn, next_fn)\ +#define DefineState(_StateName, enter_fn, exit_fn, update_fn)\ static inline const State* _StateName() {\ TC_FN_TYPECHECK(void, enter_fn, void*);\ TC_FN_TYPECHECK(void, exit_fn, void*);\ - TC_FN_TYPECHECK(void, update_fn, void*, float);\ - TC_FN_TYPECHECK(const State*, next_fn, void*);\ + TC_FN_TYPECHECK(const State*, update_fn, void*, float);\ static const State instance = {\ .enter = (void(*const)(void*)) enter_fn,\ .exit = (void(*const)(void*)) exit_fn,\ - .update = (void(*const)(void*, float)) update_fn,\ - .next = (const State*(*const)(void*)) next_fn\ + .update = (const State*(*const)(void*, float)) update_fn,\ };\ - return instance;\ + return &instance;\ }\ #endif // !_fencer_state_h diff --git a/core/src/state_machine.c b/core/src/state_machine.c index 23d6ace..6bc4e2a 100644 --- a/core/src/state_machine.c +++ b/core/src/state_machine.c @@ -1,20 +1,29 @@ #include "state_machine.h" #include "stdlib.h" +#include "debug.h" struct StateMachine { const State* current_state; void* data; }; +static inline +void internal_state_machine_set_state(StateMachine* self, const State* state) { + self->current_state->exit(self->data); + self->current_state = state; + self->current_state->enter(self->data); +} + StateMachine* state_machine_init(void* data, const State* start_state) { StateMachine* self = malloc(sizeof(StateMachine)); + ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for StateMachine instance"); + *self = (StateMachine){ .current_state = start_state, .data = data }; self->current_state->enter(self->data); - return self; } @@ -23,20 +32,12 @@ void state_machine_destroy(StateMachine* self) { free(self); } -void state_machine_set_state(StateMachine* self, const State* state) { - self->current_state->exit(self->data); - self->current_state = state; - self->current_state->enter(self->data); +void state_machine_update(StateMachine* self, float dt) { + const State* next = self->current_state->update(self->data, dt); + if(next != self->current_state) + internal_state_machine_set_state(self, next); } -const State* state_machine_get_state(StateMachine* self) { +const State* state_machine_get_current_state(StateMachine* self) { return self->current_state; } - -void state_machine_update(StateMachine* self, float dt) { - self->current_state->update(self->data, dt); - const State* next = self->current_state->next(self->data); - - if(next != self->current_state) - state_machine_set_state(self, next); -} diff --git a/core/src/state_machine.h b/core/src/state_machine.h index 6e5a414..0d5c2fb 100644 --- a/core/src/state_machine.h +++ b/core/src/state_machine.h @@ -8,8 +8,8 @@ typedef struct StateMachine StateMachine; extern StateMachine* state_machine_init(void* data, const State* start_state); extern void state_machine_destroy(StateMachine* self); -extern void state_machine_set_state(StateMachine* self, const State* state); -extern const State* state_machine_get_current_state(StateMachine* self); extern void state_machine_update(StateMachine* self, float dt); +extern const State* state_machine_get_current_state(StateMachine* self); + #endif // !_fencer_state_machine_h