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
; 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
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