From: Lukas Krickl Date: Tue, 20 May 2025 16:03:11 +0000 (+0200) Subject: unit: improved unit switching X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=fbfdcaf6200f259e9bdbde37928909fff5acb157;p=gbrg%2F.git unit: improved unit switching 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. --- diff --git a/src/defs.s b/src/defs.s index cf84ffd..232845a 100644 --- a/src/defs.s +++ b/src/defs.s @@ -39,6 +39,10 @@ .se 1 .de DRAWF_UPDATE_UI, 1 + ; gameplay flags +.se 1 +.de GPF_UNIT_NEXT, 1 + ; cell flags .se 1 .de CF_COLLISION, 1 diff --git a/src/map.s b/src/map.s index 493d685..780a5d3 100644 --- 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 diff --git a/src/unit.s b/src/unit.s index 66bc157..73f9a85 100644 --- a/src/unit.s +++ b/src/unit.s @@ -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: diff --git a/src/update.s b/src/update.s index 2c83a23..ea6accd 100644 --- a/src/update.s +++ b/src/update.s @@ -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 diff --git a/src/wram.s b/src/wram.s index 51e1fe8..9ccd82f 100644 --- a/src/wram.s +++ b/src/wram.s @@ -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