From 225ef5c50cb6956cf0a6218ea42a33ecff6525b5 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sat, 20 Sep 2025 13:40:45 +0200 Subject: [PATCH] map: wip map scrolling --- src/defs.s | 9 +++++++++ src/player.s | 28 ++++++++++++++++++++++++++++ src/video.s | 24 ++++++++++++++++++++++++ src/wram.s | 7 +++++++ 4 files changed, 68 insertions(+) diff --git a/src/defs.s b/src/defs.s index ae493a7..319a96b 100644 --- a/src/defs.s +++ b/src/defs.s @@ -17,6 +17,8 @@ #define MAP_OBJ_MAX 32 #define RECT_MAX 32 +#define MAP_ROW_H 16 ; pixels per row + #define STACK_BEGIN 0xDFFF ; seed for the rng @@ -124,3 +126,10 @@ .de r_h, 1 .de r_w, 1 .de r_size, 0 + + + ; gameplay control flags +.se 1 +.de GPF_NO_SCROLL, 1 + ; scroll a row up next vblank +.de GPF_SCROLL, 2 diff --git a/src/player.s b/src/player.s index 31e966c..e0eb4f5 100644 --- a/src/player.s +++ b/src/player.s @@ -10,6 +10,9 @@ player_init: ld a, 0 ld [player+act_pos_y_hi], a + + ld a, 0xA0 ; initial next scroll + ld [player_next_scroll_y], a ret ; updates the special player actor @@ -21,6 +24,7 @@ player_update: ld c, 0 call player_stage_move_n call player_try_move + call player_try_scroll_up @not_up: ld b, BTNDOWN @@ -56,6 +60,30 @@ player_update: #define PLAYER_SPRITE_IDLE1 0x8D + ; if the player's real y position is over + ; a certain value we should scroll the map + ; if scrolling is allowed + ; inputs: + ; player setp up counter +player_try_scroll_up: + ld a, [game_flags] + and a, GPF_NO_SCROLL + ret nz ; no scroll allowed for now + + ld a, [player_next_scroll_y] + ld b, a + ld a, [player+act_pos_y] + cp a, b + jp nc, @no_scroll + + ld a, [game_flags] + or a, GPF_SCROLL + ld [game_flags], a + + +@no_scroll: + ret + ; stages a move in a positive directon ; inputs: ; b/c: y/x movement diff --git a/src/video.s b/src/video.s index 82a8cda..7a22390 100644 --- a/src/video.s +++ b/src/video.s @@ -14,6 +14,8 @@ vblank: ; dma previous frame's oam call OAMDMAFN + call video_map_perform_scroll + call scroll_write ; get inputs @@ -23,6 +25,28 @@ vblank: ld [frame_ready], a pop_all ret + + ; scrolls if the flag is set +video_map_perform_scroll: + ld a, [game_flags] + and a, GPF_SCROLL + ret z + + ld a, [scroll_y] + sub a, MAP_ROW_H + ld [scroll_y], a + + ld a, [player_next_scroll_y] + sub a, MAP_ROW_H + ld [player_next_scroll_y], a + + call map_advance_row + + ; unset flag + ld a, [game_flags] + and a, ~GPF_SCROLL & 0xFF + ld [game_flags], a + ret ; writes scroll to scroll registers diff --git a/src/wram.s b/src/wram.s index c26d6dd..adb6125 100644 --- a/src/wram.s +++ b/src/wram.s @@ -39,6 +39,9 @@ vblank_jp: .adv 4 ; of the input list ; if set to NULL do not read from this address demo_inputs: .adv 2 + + ; gameplay control flags +game_flags: .adv 1 ; dummy oam ; same memory as empty_unit @@ -53,6 +56,10 @@ scroll_x: .adv 1 ; 16 bit srand seed ; seed must never be 0 srand: .adv 2 + + ; if player y < next_scroll + ; advance if possible +player_next_scroll_y: .adv 1 player: .adv act_size -- 2.30.2