wip: collision with tiles
authorLukas Krickl <lukas@krickl.dev>
Fri, 18 Oct 2024 18:25:43 +0000 (20:25 +0200)
committerLukas Krickl <lukas@krickl.dev>
Fri, 18 Oct 2024 18:25:43 +0000 (20:25 +0200)
src/collision.s
src/player.s

index 7d1920c535bed037f3efdeeedfbc80ebeb7e4819..93192b894c6cfe6bc3575a0caac579327131b786 100644 (file)
@@ -22,7 +22,7 @@
   .db $1, $2
 #endmacro
 
-collision_player:
+collision_player_top:
   col_head 1
   ; center left
   col_point 0, 0
@@ -49,33 +49,45 @@ collision_tile_table_check:
   ld a, [de] ; load loop counter 
   inc de ; de + 1 = first point y coordinate
 @loop:
-  push af
-  push de
-
-  ld a, [de]
-  ld l, a
-  ld a, [ct_poy]
-  add a, l ; a = y + collision y offset 
-  inc de
-  
-  ld a, [de]
-  ld h, a
-  ld a, [ct_pox] 
-  add a, h ; a = x + collision x offset 
+    push af
+
+    ld a, [de]
+    ld l, a
+    ld a, [ct_poy]
+    add a, l ; a = y + collision y offset
+    ld l, a ; store y result in l for now
+    inc de ; de++ == x pos
+    
+    ld a, [de]
+    ld h, a
+    ld a, [ct_pox] 
+    add a, h ; a = x + collision x offset 
+    inc de ; de++ == next y
+    push de ; save de for next iteratin 
+    
+    ld d, l ; d = y
+    ld e, a ; e = x
+
+    call collision_tile
+    cp a, 0
+    jr nz, @done REL
   
-  ld d, l
-  ld e, h
-
-  call collision_tile
+    pop de
+    pop af
 
-  pop af
-  pop de
-
-  dec a ; a--
-  cp a, 0 ; again if not 0
+    dec a ; a--
+    cp a, 0 ; again if not 0
   jp nz, @loop
 
   ret
+@done:
+  ; we need to pop before ret!
+  ; the register values
+  ; do not matter too much at this point
+  ; but a needs to be preserved 
+  pop de 
+  pop de 
+  ret
 
   ; checks a point
   ; with a map's meta tile
index 7a1ed33c65ccff1ea7290d7a4b1880496065538c..30eb25f705dac726b5deea74db7ccc659741f31b 100644 (file)
@@ -73,6 +73,27 @@ player_update:
   ld a, [hl]
   dec a
   ld [hl], a
+
+  ; check collision
+  ; 1) save registers 
+  push hl 
+  push de
+
+  ; 2) hl = player_y already 
+  ; 3) load correct collision points 
+  ld de, collision_player_top
+  call collision_tile_table_check
+  pop de 
+  pop hl
+
+  cp a, 0
+  jr z, @no_collision_up REL
+    ld a, [hl]
+    inc a
+    ld [hl], a
+@no_collision_up:
+
+
 @notup:
   
   ; store y in d