state: Added first call to state machine in player
authorLukas Krickl <lukas@krickl.dev>
Sat, 1 Feb 2025 12:28:37 +0000 (13:28 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 1 Feb 2025 12:28:37 +0000 (13:28 +0100)
src/defs.s
src/player.s
src/state.s

index 41ec347db84c5a1ba0c04248925245ef2b0f5410..19791ac7d8400f14abc842092d96cc5d30de5a67 100644 (file)
 ; state table entries 
 .se 1
 .de smt_end_turn, 1
+.de smt_player_poll_inputs, 1
index 1e0a216469f7009f4cc8ed354bcfee3eb5b31d39..1c6afd11adcb27c5115819f589db0bb9f07eadfa 100644 (file)
@@ -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 
index 259fd7695d6a6a0122f9c22ccdfdd2fbdde15462..4f4e4a179267cef44fdc3a7a1832f52f37a11bc7 100644 (file)
@@ -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