unit: Added basic drawing rotuine for demo_1 unit
authorLukas Krickl <lukas@krickl.dev>
Sun, 11 May 2025 15:31:43 +0000 (17:31 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sun, 11 May 2025 15:31:43 +0000 (17:31 +0200)
src/player.s
src/sys.s
src/unit.s
src/update.s
src/wram.s

index 8a13571b76b6b6bb05f7339848cb469348cc5e9c..b7e87a761dbe53d0112bf82affa61f5467f5b410 100644 (file)
@@ -1,6 +1,3 @@
-; player gets 3 sprite slots in a row
-.def int PLAYER_SPRITE1 = shadow_oam + 0
-
 .def int CURSOR_TILE = 0x04
 
 #define CURSOR_MOVE_TIMER TILE_SIZE
@@ -33,13 +30,10 @@ cursor_player_update:
 cursor_player_draw_cursor:
 @draw_cursor:
   ; draw cursor
-  ld a, [scroll_y]
-  ld b, a ; b = scroll_y
-  ld a, [scroll_x]
-  ld c, a ; c = scroll_x
+  call load_scroll
 
   ; 8x8 small cursor
-  ld hl, PLAYER_SPRITE1
+  call load_unit_obj
 
   ld a, [cursor_y]
   add a, OBJ_OFF_Y
index 6030700936b04fec34d23cccbc50b0c166f8e577..500aac407a00e28096e92221ebc957f761fd358c 100644 (file)
--- a/src/sys.s
+++ b/src/sys.s
@@ -49,3 +49,33 @@ call_tbl:
   ; hl = function value 
   jp hl
 
+  ; loads scroll into bc
+  ; returns:
+  ;   b: scroll y
+  ;   c: scroll x
+load_scroll:
+  ld a, [scroll_y]
+  ld b, a ; b = scroll_y
+  ld a, [scroll_x]
+  ld c, a ; c = scroll_x
+  ret
+
+  ; loads unit sprite into hl
+  ; increments unit sprite
+  ; returns;
+  ;   hl: current unit sprite
+load_unit_obj:
+  ld a, [unit_sprite]
+  
+
+  ld d, 0
+  ld e, a
+  ld hl, shadow_oam 
+  
+  add hl, de ; hl = next oam 
+  
+  ; next object 
+  add a, oamsize 
+  ld [unit_sprite], a
+
+  ret
index fa4581570eea8fe4252d1bdc8e220259a42d3bde..365f572fb4c9a7f05fe5c49ee27e1b4815553122 100644 (file)
@@ -4,6 +4,7 @@
   ; inputs:
   ;   hl: unit table (p0/p1)
 units_update:
+  
   ; loop counter
   ld a, UNITS_MAX 
 
@@ -40,10 +41,79 @@ units_update:
 
   ret
 
+unit_demo_1_init:
+  ldnull bc
+  ret
+
 unit_demo_1_update:
+  ; TODO: call with correct values 
+  ldnull hl
+  ld a, 1 ; hard-coded action remaining 
+  call unit_handle_inputs
+
+unit_demo_1_draw:
+  ld b, 0x84 ; tile 
+  ld c, 0x00 ; flags
+  call unit_generic_draw
+  ldnull bc
+  ret
+
+  ; draws any unit
+  ; inputs:
+  ;   b: tile
+  ;   c: flags
+unit_generic_draw:
+  push bc
+
+  ld hl, act_pos_y
+  add hl, de ; hl = act_pos_y
+  push hl
+
+  call load_scroll
+  call load_unit_obj
+  
+  pop de ; de = act_pos_y
+  
+  ; set y pos
+  ld a, [de]
+  mul8 a
+  add a, OBJ_OFF_Y
+  add a, b
+  ld [hl+], a
+  
+  ; set x pos
+  inc de
+  ld a, [de]
+  mul8 a
+  add a, OBJ_OFF_X
+  add a, b
+  ld [hl+], a
+  
+  pop bc ; bc = inputs again
+
+  ; set tile 
+  ld a, b
+  ld [hl+], a
+
+
+  ; set flags
+  ld a, c
+  ld [hl+], a
+
+  ldnull bc
+  ret
+
+  ; generic unit input handler
+  ; inputs:
+  ;   hl: action table 
+  ;    a: remaining moves
+unit_handle_inputs:
   ldnull bc
   ret
 
 unit_demo_1:
-  st_def 0x00, unit_demo_1_update, 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 
+
+st_unit_demo_1_update:
+  st_def 0x00, unit_demo_1_update, st_unit_demo_1_update 
index 6e73f56132d110b794c15bcc971331ed7ba8c0c5..1edf380906b9c3f8002fcb7a87ba1f3a07a5ef65 100644 (file)
@@ -36,6 +36,10 @@ update:
   ld a, [frame_count]
   inc a
   ld [frame_count], a
+
+  ; reset objects
+  xor a, a
+  ld [unit_sprite], a
   
   ld de, game_mode
   jp st_update
index fb71539685ee1ac1a92d8a2fdc9742b1c0d39fbe..1fbe1b958b77643bf12947de0fad38e9a132d089 100644 (file)
@@ -6,6 +6,9 @@ shadow_oam: .adv OBJSMAX * oamsize
 frame_ready: .adv 1
 frame_count: .adv 1
 
+  ; current sprite
+unit_sprite: .adv 1
+
   ; current frame's inputs 
 curr_inputs: .adv 1
   ; previous frame's inputs