unit: improved unit switching
authorLukas Krickl <lukas@krickl.dev>
Tue, 20 May 2025 16:03:11 +0000 (18:03 +0200)
committerLukas Krickl <lukas@krickl.dev>
Tue, 20 May 2025 16:03:11 +0000 (18:03 +0200)
Unit switching now forces the current unti to sleep and the new unit to
wake up.

Unit switching now is controlled by a flag and done in the gameplay
update loop.

src/defs.s
src/map.s
src/unit.s
src/update.s
src/wram.s

index cf84ffd855846fe1cac520554ac3b9caae2e001a..232845ab5f612f8b83def07cf8d5c6e167030e5e 100644 (file)
 .se 1
 .de DRAWF_UPDATE_UI, 1
 
+  ; gameplay flags
+.se 1
+.de GPF_UNIT_NEXT, 1
+
   ; cell flags
 .se 1 
 .de CF_COLLISION, 1
index 493d6854b550c39a13458947fd65edf77634fdfb..780a5d335d061a579b57bfa38d801cbfc5b10d18 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -114,7 +114,8 @@ map_load_demo_actors:
   ld bc, act_size
   call memcpy
   
-  call unit_next_no_current
+  ld de, p0_units
+  call unit_next_request
 
   ret
 
index 66bc1578a874da567cf26ff2befe13509692f6e9..73f9a85061bb1baa505b4a8a43eac81f23d46ee0 100644 (file)
@@ -1,4 +1,3 @@
-  ; updates a unit table
   ; runs state for each unit
   ; does not update if actor is ACT_T_NULL
   ; inputs:
@@ -62,6 +61,7 @@ unit_demo_1_update:
   push bc
   call unit_demo_1_draw
   pop bc
+
   ret
   
   ; inputs
@@ -237,8 +237,8 @@ unit_try_move_right:
 
   inc a
   ld [hl], a
-  ret
-  
+  ret 
+
   ; consumes a move
   ; sets UI redraw flag
   ; fails (z flag set) if no moves left
@@ -257,7 +257,7 @@ unit_use_move:
   cp a, 0
   
   push_all
-  call z, unit_next
+  call z, unit_next_request
   pop_all
   ret z
 
@@ -326,10 +326,23 @@ unit_switch_to_active:
   ld c, a
   ld a, [hl]
   ld b, a 
-
   ; bc = next active state
 
   ret
+
+  ; forces a unit into an active state
+  ; inputs:
+  ;   de: unit
+unit_wake_up:
+  push de
+  call unit_switch_to_active
+  ; bc = active state
+  push bc
+  pop de ; de = new state 
+  pop hl ; unit ptr
+  ld bc, st_size
+  call memcpy
+  ret
   
   ; switches the current unit to idle
   ; sets moves to 0 
@@ -344,17 +357,60 @@ unit_switch_to_idle:
   ld b, a
   ret
 
+  ; forces a unit to go into idle state
+  ; inputs:
+  ;   de: unit
+unit_sleep:
+  push de
+  call unit_switch_to_idle
+  ; bc = active state
+  push bc
+  pop de ; de = new state 
+  pop hl ; unit ptr
+  ld bc, st_size
+  call memcpy
+  ret
+
+  ; requests unit next
+  ; to be executed
+  ; inputs: 
+  ;   de: current unit
+unit_next_request:
+  ld a, [gameplay_flags]
+  or a, GPF_UNIT_NEXT
+  ld [gameplay_flags], a
+
+  ld a, e
+  ld [gameplay_unit_current], a
+  ld a, d
+  ld [gameplay_unit_current+1], a
+
+  ret
+
   ; finds a unit with a higher initiative value 
   ; than the current one that has moves > 0 set
   ; if all unts have have 0 moves, set moves to 
   ; initial value and try again
   ; skips any ACT_T_NULL types
   ; inputs:
-  ;   de: current unit
+  ;   gameplay_unit_current: unit that requested a change
   ; uses:
   ;   unit_next_best_init: as a temporary buffer
 unit_next:
-  
+  ; unset flag
+  ld a, [gameplay_flags]
+  xor a, GPF_UNIT_NEXT
+  ld [gameplay_flags], a
+
+  ; load requestor unit
+  ld a, [gameplay_unit_current]
+  ld e, a
+  ld a, [gameplay_unit_current+1]
+  ld d, a
+
+  push de
+  call unit_sleep
+  pop de
   ; set moves of current unit to 0
   ld hl, act_moves
   add hl, de
@@ -448,7 +504,7 @@ unit_next_no_current:
   ld d, a
 
   push de
-  call unit_switch_to_active
+  call unit_wake_up
   pop de
   call ui_unit_need_draw
   ret
@@ -489,7 +545,7 @@ unit_demo_1:
 
 unit_demo_2:
   st_def 0x00, unit_demo_1_init, st_unit_demo_1_idle
-  act_def ACT_T_DEMO_1, 0, 1, 2, 3, 4, 7, 3, 3, 0 
+  act_def ACT_T_DEMO_1, 0, 1, 2, 3, 4, 5, 3, 3, 0 
   act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_demo_1_idle
 
 st_unit_demo_1_update:
index 2c83a230af9eb68ed1b745f3375889366bfc7624..ea6accdb04b1a939eadfd19b4b37018f04b14db3 100644 (file)
@@ -16,6 +16,10 @@ update_game:
   ld hl, p1_units
   call units_update
 
+  ld a, [gameplay_flags]
+  and a, GPF_UNIT_NEXT
+  call nz, unit_next
+
   ldnull bc
   ret
 
index 51e1fe8fac6cf394ea81462e9a70bfbc8e59f1c9..9ccd82f42193151e09f106e105387c6a7ad7af8a 100644 (file)
@@ -32,6 +32,10 @@ bg_update_index: .adv 2
 bg_update_queue: .adv bge_size * BGE_MAX 
 
 draw_flags: .adv 1
+gameplay_flags: .adv 1
+
+  ; the unit that requested unit_next
+gameplay_unit_current: .adv 2
 
   ; ptr to actor for next UI update
 ui_draw_actor: .adv 2