units: Draw calls are not done outside of the state machine
authorLukas Krickl <lukas@krickl.dev>
Thu, 22 May 2025 03:25:27 +0000 (05:25 +0200)
committerLukas Krickl <lukas@krickl.dev>
Thu, 22 May 2025 03:25:27 +0000 (05:25 +0200)
This allows drawing to continue even if the unit state is awaiting
an idle state.
It also allows states to be shared between units more easily.

src/defs.s
src/macros.inc
src/unit.s

index 4e97540662517cca8f9d0da608a3af329ba127da..f82900309010171d0151b16323990989c6eee3a9 100644 (file)
 .de act_st_interact, 2
 .de act_st_active, 2
 .de act_st_idle, 2
+
+  ; ptr to function for drawing the actor
+  ; called after state update
+  ; replace in state code if drawing needs to be 
+  ; changed at runtime
+  ; inputs:
+  ;   de: actor
+  ; returns:
+  ;   bc: null
+.de act_draw, 2 
 .de act_size, 0
   
   ; max bge queue size
index 632dfee92a4ed944be85b58266a34d28ea485140..c51fcb0eb55ec697a7b520d3bb3a7fceba9795e2 100644 (file)
   dw $2
   dw $3
 #endmacro
-  
+ ; defines draw call for actor
+ ; inputs:
+ ;    $1: draw call
+#macro act_def_draw
+  dw $1
+#endmacro
+
   ; define an actor without state
   ; to define an actor call 
   ; st_def and act_def
index c856bf24cf8605a7bd8d2cf55918eced4657c509..2c9219a7da28d147d0363ffdd029ab41f9c264b6 100644 (file)
@@ -28,6 +28,10 @@ units_update:
       call st_update
       
       pop hl
+      
+      push hl
+      call unit_update_draw
+      pop hl
 @skip:
     ; next actor
     ld de, act_size
@@ -40,6 +44,25 @@ units_update:
 
   ret
 
+  ; calls unit's draw function
+  ; inputs:
+  ;   hl: actor ptr
+unit_update_draw:
+  push hl
+  ld de, act_draw
+  add hl, de
+  ; hl = draw ptr
+  ld a, [hl+]
+  ld e, a
+  ld a, [hl]
+  ld d, a
+  ; de = draw function
+  push de
+  pop hl ; hl = draw function
+  pop de ; de = actor
+  call_hl
+  ret
+
 unit_demo_1_init:
   ldnull bc
   ret
@@ -58,17 +81,8 @@ unit_demo_1_update:
   call unit_scroll_center
   pop de
 
-  push bc
-  call unit_demo_1_draw
-  pop bc
-
   ret
   
-  ; inputs
-  ;   de: actor
-unit_demo_1_idle:
-  call unit_demo_1_draw
-  ret
 
   ; inputs
   ;   de: actor
@@ -81,11 +95,6 @@ unit_demo_1_draw:
   ldnull bc
   ret
 
-  ; inputs:
-  ;   de: actor
-unit_demo_1_delay_to_active:
-  jp unit_delay_to_active
-
   ; draws any unit
   ; inputs:
   ;  de: actor
@@ -142,7 +151,7 @@ unit_handle_inputs:
   jr z, @notup REL
 
     call unit_try_move_up
-    ld bc, st_unit_demo_1_delay_to_active
+    ld bc, st_unit_delay_to_active
     ret
 @notup:
 
@@ -150,7 +159,7 @@ unit_handle_inputs:
   jr z, @notdown REL
     
     call unit_try_move_down
-    ld bc, st_unit_demo_1_delay_to_active
+    ld bc, st_unit_delay_to_active
     ret
 @notdown:
 
@@ -158,7 +167,7 @@ unit_handle_inputs:
   jr z, @notleft REL
 
     call unit_try_move_left
-    ld bc, st_unit_demo_1_delay_to_active
+    ld bc, st_unit_delay_to_active
     ret
 @notleft:
 
@@ -166,7 +175,7 @@ unit_handle_inputs:
   jr z, @notright REL
 
     call unit_try_move_right
-    ld bc, st_unit_demo_1_delay_to_active
+    ld bc, st_unit_delay_to_active
     ret
 @notright:
 
@@ -265,8 +274,6 @@ unit_use_move:
   dec a
   ld [hl], a
   
-  call unit_pause_objs
-
   ; we need the z flag to not be set ehre
   ; so just do something that will always unset it
   or a, 1
@@ -571,6 +578,12 @@ unit_delay_to_active:
   ldnull bc
   ret
 
+  ; inputs
+  ;   de: actor
+unit_idle:
+  ldnull bc
+  ret
+
   ; pauses object redraws
   ; until unpause is called
 unit_pause_objs:
@@ -587,29 +600,32 @@ unit_resume_objs:
   ret
 
 unit_demo_1:
-  st_def 0x00, unit_demo_1_init, st_unit_demo_1_idle
+  st_def 0x00, unit_demo_1_init, st_unit_idle
   act_def ACT_T_DEMO_1, 0, 1, 2, 3, 4, 6, 2, 2, 0 
-  act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_demo_1_idle
+  act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_idle
+  act_def_draw unit_demo_1_draw
 
 
 unit_demo_2:
-  st_def 0x00, unit_demo_1_init, st_unit_demo_1_idle
+  st_def 0x00, unit_demo_1_init, st_unit_idle
   act_def ACT_T_DEMO_1, 0, 1, 2, 3, 1, 5, 3, 3, 0 
-  act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_demo_1_idle
+  act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_idle
+  act_def_draw unit_demo_1_draw
 
 unit_demo_3:
-  st_def 0x00, unit_demo_1_init, st_unit_demo_1_idle
+  st_def 0x00, unit_demo_1_init, st_unit_idle
   act_def ACT_T_DEMO_1, 0, 1, 2, 3, 0, 5, 4, 4, 0 
-  act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_demo_1_idle
+  act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_idle
+  act_def_draw unit_demo_1_draw
 
 st_unit_demo_1_update:
   st_def 0x00, unit_demo_1_update, st_unit_demo_1_update 
 
-st_unit_demo_1_idle:
-  st_def 0x00, unit_demo_1_idle, st_unit_demo_1_idle
+st_unit_idle:
+  st_def 0x00, unit_idle, st_unit_idle
 
-st_unit_demo_1_delay_to_active:
-  st_def CURSOR_MOVE_TIMER, unit_demo_1_delay_to_active, st_unit_switch_to_active
+st_unit_delay_to_active:
+  st_def CURSOR_MOVE_TIMER, unit_delay_to_active, st_unit_switch_to_active
 
 st_unit_switch_to_active:
   st_def 0, unit_switch_to_active, st_unit_switch_to_active