mapobj: Added basic mapobject handling.
authorLukas Krickl <lukas@krickl.dev>
Sat, 20 Sep 2025 11:13:11 +0000 (13:13 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sat, 20 Sep 2025 11:13:11 +0000 (13:13 +0200)
Map objects get loaded as new map rows are drawn.

src/defs.s
src/macros.inc
src/map.s
src/mapobj.s
src/rowpatterns.s
src/wram.s

index 48ac0fe71cc5b0f1ccd7c6f91e872a4e5b50f557..ae493a7e2f468c8c696b4d1154e3ff476d252612 100644 (file)
 .de pat_tilemap, 10
 .de pat_size, 0
 
+       ; map object types
+.se 0
+.de MOT_SET_PAT, 1
+.de MOT_DISABLE_SCROLL, 1
+.de MOT_ENABLE_SCROLL, 1
+.de MOT_RECT, 1
+.de MOT_ACTOR_SPAWNER, 1
+
        ; map object struct
 .se 0
 .de mo_type, 1
index f7a3427209f69bf8272b850301619e4ed9f3991f..f687d226f9bcd23268b96b91eef068dc08b56dc8 100644 (file)
@@ -189,10 +189,10 @@ $1:
        ;               $1: type
        ;               $2: flags
        ;               $3: row
-       ;               $4: dat1
-       ;               $5: dat2
+       ;               $4: dat1/dat2 (word)
 #macro modef
-       .db $1, $2, $3, $4, $5
+       .db $1, $2, $3
+       dw $4
 #endmacro
        
        ; adjusts an input position 
index 3e19a6786f75d45d7fa186f25146a0385797c209..5c593843f86b04873fab48e29bd125d402788311 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -35,6 +35,14 @@ map_load:
        ld a, SCRN0_END HI
        ld [map_scrn_ptr+1], a
 
+       ; load mo ptr
+       ld hl, map_objs_ptr
+       add hl, de
+       ld a, [hl+]
+       ld [map_curr_obj], a
+       ld a, [hl+]
+       ld [map_curr_obj+1], a
+
 
        call lcd_on
        call vblank_wait
@@ -176,15 +184,11 @@ map_advance_row:
        ld a, [map_curr_row]
        inc a
        ld [map_curr_row], a
-       call map_check_obj
+
+       call mo_exec
 
        ret
        
-       ; checks the newly drawn row for objects
-       ; that can execute code
-map_check_obj:
-       ; TODO
-       ret
        
        ; draws a full page of the currently selected map
        ; waits for blank between each row
@@ -218,4 +222,8 @@ map_page_full_draw:
        ret
 
 l1_map:
-       mapdef 0, pat_empty, NULL, bank8000, bank8800, bank8C00, bank9000       
+       mapdef 0, pat_empty, l1_objs, bank8000, bank8800, bank8C00, bank9000    
+
+l1_objs:
+       modef MOT_SET_PAT, 0, 3, pat_center_grass 
+       modef MOT_SET_PAT, 0, 5, pat_empty
index f9803c4ce4f5dbe9f485414ae9f641b3b96d3a1f..fbd0c33edcb905fc209266680ab20ad8af36b033 100644 (file)
@@ -4,3 +4,106 @@
 ; is checked
 ; if the row matches it is loaded into ram and executed
 ; when an object is off-screen it is unloded
+
+mo_routines:
+        dw mo_set_pat
+        dw mo_disable_scroll
+        dw mo_enable_scroll
+        dw mo_rect
+        dw mo_actor_spawner
+       
+       ; executes all map objects in the map object list 
+       ; until the mo's row != current row
+       ; advances mo ptr
+mo_exec:
+       ld a, [map_curr_obj]
+       ld e, a
+       ld a, [map_curr_obj+1]
+       ld d, a
+
+       ld a, [map_curr_row]
+       ld b, a ; b = current row
+@loop:
+               ; execute map object by type
+               ld hl, mo_row
+               add hl, de
+               ld a, [hl]
+               ; if rows do not match exit
+               cp a, b
+               jr nz, @done REL
+
+               ; otherwise call object routine
+               push_all
+               ld a, [de] ; type
+               add a, a ; * 2 for offset
+               ld hl, mo_routines
+               ld b, 0
+               ld c, a 
+               add hl, bc
+
+               ; hl = rotuine ptr
+               ld a, [hl+]
+               ld b, a
+               ld a, [hl+]
+               ld h, a
+               ld l, b
+
+               ; call routine
+               call_hl
+
+
+               pop_all
+               ; go to next object
+               ld hl, mo_size
+               add hl, de
+               push hl
+               pop de ; de = next obj
+               jr @loop REL
+
+@done:
+
+       ; write back next ptr
+       ld a, e 
+       ld [map_curr_obj], a
+       ld a, d 
+       ld [map_curr_obj+1], a
+
+       ret
+       
+       ; sets the next draw pattern
+       ; inputs:
+       ;               de: map object ptr
+mo_set_pat:
+       ; new pattern is in dat1 and dat2
+       ld hl, mo_dat
+       add hl, de
+
+       ld a, [hl+]
+       ld [map_curr_pat], a
+       ld a, [hl]
+       ld [map_curr_pat+1], a
+       ret
+       
+       ; disables map scrolling
+       ; inputs:
+       ;               de: map object ptr
+mo_disable_scroll:
+       ret
+       
+       ; enables map scrolling
+       ; inputs:
+       ;               de: map object ptr
+mo_enable_scroll:
+       ret
+
+       ; spawns a collision rectangle
+       ; inputs:
+       ;               de: map object ptr
+mo_rect:
+       ret
+       
+       ; spawns an actor spawner
+       ; inputs:
+       ;               de: map object ptr
+mo_actor_spawner:
+       ret
index 3ca43c606e86af827f64f760a73d7ffc00c56539..36bc135d04d3e5f40665f269a94ee50a3ea41819 100644 (file)
@@ -1,9 +1,14 @@
 
        ; tile space
 .def int TS = 0x40
+.def int GS = 0x62
 
 pat_empty:
 .db TS, TS, TS, TS, TS, TS, TS, TS, TS, TS 
+
+pat_center_grass:
+.db TS, TS, TS, GS, GS, GS, TS, TS, TS, TS
+
        
        ; enf of level row pattern
 pat_eol:
index 94c87280a310403b644add8b265f72ab53fb45ab..c26d6ddd11996f3183ac326e6dd4c8e07fc06f88 100644 (file)
@@ -69,7 +69,7 @@ map_curr_pat: .adv 2
 map: .adv 2
        ; ptr to the next map object
        ; that may be loaded
-map_obj_ptr: .adv 2
+map_curr_obj: .adv 2
        
        ; current bg pointer to write to
 map_scrn_ptr: .adv 2