unit: Added basic movement and state transitions
authorLukas Krickl <lukas@krickl.dev>
Mon, 12 May 2025 15:06:44 +0000 (17:06 +0200)
committerLukas Krickl <lukas@krickl.dev>
Mon, 12 May 2025 15:06:44 +0000 (17:06 +0200)
src/unit.s

index eaed64798cebebe2bb1589f61ad37ee2ec2e7915..f5be36bd2e601102c2fb7e6fd4b7639d5d8ac087 100644 (file)
@@ -54,6 +54,11 @@ unit_demo_1_update:
   push de
   call unit_handle_inputs
   pop de
+  
+  push bc
+  call unit_demo_1_draw
+  pop bc
+  ret
 
   ; inputs
   ;   de: actor
@@ -122,8 +127,34 @@ unit_handle_inputs:
   jr z, @notup REL
 
     call unit_try_move_up
+    ld bc, st_unit_delay
+    ret
 @notup:
 
+  input_held BTNDOWN
+  jr z, @notdown REL
+    
+    call unit_try_move_down
+    ld bc, st_unit_delay
+    ret
+@notdown:
+
+  input_held BTNLEFT
+  jr z, @notleft REL
+
+    call unit_try_move_left
+    ld bc, st_unit_delay
+    ret
+@notleft:
+
+  input_held BTNRIGHT
+  jr z, @notright REL
+
+    call unit_try_move_right
+    ld bc, st_unit_delay
+    ret
+@notright:
+
   ldnull bc
   ret
 
@@ -138,6 +169,40 @@ unit_handle_inputs:
   ;   actor initiative based on tile flags
   ;   actor position
 unit_try_move_up:
+  ld hl, act_pos_y
+  add hl, de
+  ; hl = actor y
+  ld a, [hl]
+  dec a
+  ld [hl], a
+
+  ret
+
+unit_try_move_down:
+  ld hl, act_pos_y
+  add hl, de
+  ; hl = actor y
+  ld a, [hl]
+  inc a
+  ld [hl], a
+  ret
+
+unit_try_move_left:
+  ld hl, act_pos_x
+  add hl, de
+  ; hl = actor x
+  ld a, [hl]
+  dec a
+  ld [hl], a
+  ret
+
+unit_try_move_right:
+  ld hl, act_pos_x
+  add hl, de
+  ; hl = actor x
+  ld a, [hl]
+  inc a
+  ld [hl], a
   ret
 
   ; centers the current scroll on the selected unit
@@ -146,11 +211,32 @@ unit_try_move_up:
   ;   de: actor
 unit_scroll_center:
   ret
+  
+  ; switches a unit to active state
+  ; inputs:
+  ;   de: unit
+unit_switch_to_active:
+  ld hl, act_st_active
+  add hl, de ; hl = st_active ptr
+  ld a, [hl+]
+  ld c, a
+  ld a, [hl]
+  ld b, a 
+
+  ; bc = next active state
+
+  ret
 
 unit_demo_1:
   st_def 0x00, unit_demo_1_init, st_unit_demo_1_update
   act_def ACT_T_DEMO_1, 0, 1, 1, 1, 1, 1, 2, 2, 0 
-  act_st_def NULL, st_unit_demo_1_update, NULL, NULL
+  act_st_def NULL, NULL, st_unit_demo_1_update, NULL
   
 st_unit_demo_1_update:
   st_def 0x00, unit_demo_1_update, st_unit_demo_1_update 
+
+st_unit_delay:
+  st_def CURSOR_MOVE_TIMER, st_null_fn, st_unit_switch_to_active
+
+st_unit_switch_to_active:
+  st_def 0, unit_switch_to_active, st_unit_switch_to_active