From edd9e34c4b775518e2a18580029fd9491444cce5 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sat, 1 Feb 2025 13:28:37 +0100 Subject: [PATCH] state: Added first call to state machine in player --- src/defs.s | 1 + src/player.s | 17 +++++++++++++++++ src/state.s | 30 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/defs.s b/src/defs.s index 41ec347..19791ac 100644 --- a/src/defs.s +++ b/src/defs.s @@ -204,3 +204,4 @@ ; state table entries .se 1 .de smt_end_turn, 1 +.de smt_player_poll_inputs, 1 diff --git a/src/player.s b/src/player.s index 1e0a216..1c6afd1 100644 --- a/src/player.s +++ b/src/player.s @@ -62,6 +62,23 @@ player_update: ld a, [who] cp a, WHO_PLAYER jp nz, @skip_input + + ; call state update + ld a, smt_player_poll_inputs + ; load initial state if required + call sm_load_initial_state + + ld de, state_machine + push hl + push hl + pop bc + dec bc ; bc = actor_ptr for player + + ld hl, state_table + ld a, [de] + call call_tbl + ; restore hl + pop hl ; play move animation ; and skip inputs if it is still diff --git a/src/state.s b/src/state.s index 259fd76..4f4e4a1 100644 --- a/src/state.s +++ b/src/state.s @@ -1,10 +1,14 @@ ; the state machine can be used by the current actor (who) ; to store their current state and call small re-usable routines +; at the end of a chain the state should always be 0 ; table of all states + ; expected inputs: + ; bc: actor_ptr state_table: dw sm_nop dw sm_end_turn + dw sm_player_poll_inputs sm_nop: ret @@ -21,3 +25,29 @@ sm_clear: ld a, [hl+] ld a, [hl+] ret + +sm_player_poll_inputs: + ret + + ; loads initial state if + ; state is 0 + ; inputs: + ; a: initial state + ; uses: + ; tmp +sm_load_initial_state: + push de + + ld [tmp], a + ld de, state_machine + ld a, [de] + cp a, 0 ; if state is not 0 ret + jr nz, @done REL + + ; set initial state + ld a, [tmp] + ld [de], a + +@done: + pop de + ret -- 2.30.2