From: Lukas Krickl Date: Mon, 12 May 2025 15:06:44 +0000 (+0200) Subject: unit: Added basic movement and state transitions X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=142971745096ba00644e44c2cbe714872da962df;p=gbrg%2F.git unit: Added basic movement and state transitions --- diff --git a/src/unit.s b/src/unit.s index eaed647..f5be36b 100644 --- a/src/unit.s +++ b/src/unit.s @@ -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