actor: Added movement speed-based delay
authorLukas Krickl <lukas@krickl.dev>
Thu, 26 Jun 2025 16:03:22 +0000 (18:03 +0200)
committerLukas Krickl <lukas@krickl.dev>
Thu, 26 Jun 2025 16:03:22 +0000 (18:03 +0200)
This is simply done by having a writable version of
unit_delay_to_active in wram.
The timer can now be changed at will.
Also added st_custom for entierly custom runtime states :^)

BUGS.md
src/actortables.s
src/defs.s
src/mem.s
src/player.s
src/unit.s
src/wram.s

diff --git a/BUGS.md b/BUGS.md
index b81cd17b4ce141641c802e185ab8951560ec58f5..fa76b8e3bca331d9707d094128afd9e013442fb0 100644 (file)
--- a/BUGS.md
+++ b/BUGS.md
@@ -1,7 +1,6 @@
 # Known Bugs
 
-## When place a new building the game crashes eventually
+## Movement in multiple directions slows donw movement to a crawl
 
-It seems to be related to the jp to hl caused by a cell's update routine.
-There seem to be writes to weird hw registers when placing buildings.
+When pressing two direction buttons movement speed calculations slow down a lot.
 
index 82206988f8b4b01b26217a2967049bbc13b7949c..511da3df8f0be5062150465459f0aac3e23e022d 100644 (file)
@@ -1,7 +1,7 @@
 #include "unit_demo.s"
 
 default_map_actor_table:
-.db 0; 19 ; size
+.db 19 ; size
 dw unit_demo_2
 dw unit_demo_3
 dw unit_demo_3
index 008a45a8a1b0ccf3017b951953fbe3da0eb16496..7544a69b16b57b002cc85418eebd90054e7f5640 100644 (file)
@@ -7,6 +7,8 @@
 #define WRAM 0xC000
 #define WRAMLEN 0xFFF
 
+#define STAT_MAX 100
+
 #define NULL 0
 
 #define UNITS_MAX 20
index 45d102b2a8eb0bd37c09a488a62d2d86e24f9e1b..cdad297f026d6984b5e3bd88ba5eed8391d781b4 100644 (file)
--- a/src/mem.s
+++ b/src/mem.s
@@ -18,7 +18,13 @@ mem_init:
   call unit_load_default_player 
   call game_init 
   call mbc1_init
-  
+  ; copy delay to active template to wram
+  ld de, st_unit_delay_to_active_template
+  ld hl, st_unit_delay_to_active
+  ld bc, st_size
+  call memcpy
+
   ret
 
 game_init:
index 0a186ebdc28f29208e4b66031033f7eae00864b2..c5d3169320f8ada803b2cb9d64b911250731f319 100644 (file)
@@ -41,7 +41,7 @@ unit_player:
   st_def 0x00, unit_player_init, st_unit_idle
   act_def ACT_T_DEMO_1, 0, 2, 2, 0
   act_stat_def1 1, 1, 1, 1, 1
-  act_stat_def2 1, 1, 1, 1, 1, 1, 1, 48 
+  act_stat_def2 1, 1, 1, 1, 1, 1, 1, 90 
   act_skill_def_empty
   act_inventory_empty
   act_equipment_empty
index 503fd80c0eb891f6835dcbf6b31650b646aa5c70..3a9149595dd08879fce71e73ab66046445420b69 100644 (file)
@@ -305,18 +305,35 @@ unit_collides_with_any_other:
   ld a, CF_COLLISION
   ret
 #undefine scratch_loop_i
-  
-  ; transitions a unit
-  ; to a movement animation
-  ; the animation time is dependant on movement
-  ; speed (100-stat_calc_speed)
-  ; the end-state of the move_animation should
-  ; clear rt_sub_tile to 0 
+  ; sets st_unit_delay_to_active speed to unit's move speed
+  ; (STAT_MAX-stat_calc_speed)
   ; inputs:
   ;   de: actor
   ;    a: mask for rt_sub_tile
   ;       rt_subtile |= a
-unit_transition_to_move_animation:
+unit_set_delay_to_active_speed:
+  push de
+  ld hl, act_rt_sub_tile 
+  add hl, de ; hl = ptr to sub_tile
+
+  ld b, a
+  ld a, [hl]
+  or a, b ; apply the mask
+  ld [hl], a ; store new value
+
+  ; get movement speed 
+  pop de
+  call stat_calc_speed
+  ld de, st_unit_delay_to_active ; de == st_time
+  ; this overwrites the wait time 
+  ; by calculating the movement speed
+  ; and subtracting it from STAT_MAX
+  ld b, a ; b = current speed
+  ld a, STAT_MAX
+  ; a - b
+  sub a, b
+  ld [de], a ; delay timer is now speed
   ret
 
   ; moves a unit up
@@ -348,7 +365,7 @@ unit_try_move_up:
   
   ; not bits set
   ld a, 0b00000000
-  call unit_transition_to_move_animation
+  call unit_set_delay_to_active_speed
 
   ret
 
@@ -368,7 +385,7 @@ unit_try_move_down:
 
   ; set down bit
   ld a, 0b10000000
-  call unit_transition_to_move_animation
+  call unit_set_delay_to_active_speed
   
   ret
 
@@ -388,7 +405,7 @@ unit_try_move_left:
   
   ; no bits set
   ld a, 0b00000000
-  call unit_transition_to_move_animation
+  call unit_set_delay_to_active_speed
   
   ret
 
@@ -409,7 +426,7 @@ unit_try_move_right:
 
   ; set right bit
   ld a, 0b01000000
-  call unit_transition_to_move_animation
+  call unit_set_delay_to_active_speed
 
   ret 
 
@@ -583,7 +600,9 @@ unit_get_equipment:
 st_unit_idle:
   st_def 0x00, unit_idle, st_unit_idle
 
-st_unit_delay_to_active:
+  ; template for st_unit_delay_to_active 
+  ; in wram
+st_unit_delay_to_active_template:
   st_def 8, unit_delay_to_active, st_unit_switch_to_active
 
 st_unit_delay_to_active_fast:
index a160117c65f0a16af97df61a01e2139c23c823c8..30ed67fb9b83c4b11ec6610b41ded7e1862cc4bb 100644 (file)
@@ -40,6 +40,16 @@ empty_oam: .adv oamsize
   ; ptr to actor for next UI update
 ui_draw_actor: .adv 2
 
+  ; can be used for custom state transtions
+  ; simple write state info to this location 
+  ; and return st_custom
+st_custom: .adv st_size
+  ; it is common for delay to active to have a custom
+  ; timer to simulate movement speed
+  ; this is a valid delay state and should only be written to 
+  ; to change its timer
+st_unit_delay_to_active: .adv st_size
+
   ; game state
   ; this region holds the entire game state
   ; everything thats needed for a savme game should be