Added collision check for new movement
authorLukas Krickl <lukas@krickl.dev>
Mon, 18 Nov 2024 13:41:05 +0000 (14:41 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 18 Nov 2024 13:41:05 +0000 (14:41 +0100)
src/collision.s
src/player.s

index 56f18692a951587b42ff74bdef0f42c7598b3ae4..5d84f71722e5a328b9f3911ba6bd7005efcaa24c 100644 (file)
   .db $1, $2
 #endmacro
 
-collision_player_top:
-  col_head 2
-  col_point 2, 0
-  col_point 2, 8 
-
-collision_player_bot:
-  col_head 2
-  col_point 12, 0
-  col_point 12, 8 
-
-collision_player_right:
-  col_head 2
-  col_point 2, 8
-  col_point 12, 8 
-
-collision_player_left:
-  col_head 2
-  col_point 2, 0
-  col_point 12, 0 
-
-tile_collision_rec8x8:
+collision_player:
+  col_head 1
   col_point 0, 0
-  col_point 0, 8
-  col_point 8, 0
-  col_point 8, 8
 
 collision_tile_lut:
   .rep cti, ROOM_H, 1, .db cti * ROOM_W 
index 8dedd63c0b8bc31dd632ad87a35290236b8fde27..78bba70d4e15a1e0e9a457f76f2eb39bf27b463e 100644 (file)
@@ -54,7 +54,7 @@ player_init:
   ; 1) save registers 
   push hl 
   push de
-
+   
   ; 2) hl = player_y already 
   ; 3) load correct collision points 
   ld de, $1 
@@ -71,7 +71,7 @@ player_init:
   ; actor table
   ; inputs:
   ;   hl: pointer to player memory
-player_update:
+player_update: 
   ; update
   ld a, [who]
   cp a, WHO_PLAYER
@@ -88,6 +88,15 @@ player_update:
   ld a, RF_WALL 
   ld [ct_mask], a
   
+  ; tmp = y movement offset 
+  ; tmp+1 = x movement offset
+  ; load tmp and +1 with the current position by 
+  ; default
+  ld a, [hl+]
+  ld [tmp], a
+  ld a, [hl]
+  dec hl
+  ld [tmp+1], a
 
   ; input handling
   input_held BTNDOWN
@@ -98,6 +107,11 @@ player_update:
     ld [anim_move_y], a
     ld a, ANIM_STEP_DOWN
     ld [anim_step_y], a
+  
+    ; set expected offset 
+    ld a, ANIM_MOVE_TILE_SIZE
+    add a, [hl]
+    ld [tmp], a
 @notdown:
   
   input_held BTNUP
@@ -108,6 +122,11 @@ player_update:
     ld [anim_move_y], a
     ld a, ANIM_STEP_UP
     ld [anim_step_y], a
+
+    ; set expected offset 
+    ld a, 0xFF - ANIM_MOVE_TILE_SIZE + 1
+    add a, [hl]
+    ld [tmp], a
 @notup:
   
 
@@ -119,6 +138,13 @@ player_update:
     ld [anim_move_x], a
     ld a, ANIM_STEP_LEFT
     ld [anim_step_x], a
+
+    ; set expected offset 
+    ld a, 0xFF - ANIM_MOVE_TILE_SIZE + 1
+    inc hl
+    add a, [hl]
+    dec hl
+    ld [tmp+1], a
 @notleft:
 
   input_held BTNRIGHT
@@ -129,6 +155,13 @@ player_update:
     ld [anim_move_x], a
     ld a, ANIM_STEP_RIGHT
     ld [anim_step_x], a
+
+    ; set expected offset 
+    ld a, ANIM_MOVE_TILE_SIZE
+    inc hl
+    add a, [hl]
+    dec hl
+    ld [tmp+1], a
 @notright:
 
 @action_buttons:
@@ -172,6 +205,26 @@ player_update:
   ld a, [hl]
   ld e, a
 
+  ; if any collision happens now 
+  ; we stop the animation
+  push hl ; need to save hl
+  ld a, [tmp] 
+  add a, 8
+  ld d, a ; d = target y + 8 to center on tile 
+  ld a, [tmp+1]
+  add a, 8
+  ld e, a ; e = target x + 8 to center on tile 
+  call collision_tile 
+  pop hl
+  jr z, @no_collision REL
+    ; clear anim memory
+    xor a, a
+    ld [anim_move_y], a
+    ld [anim_move_x], a
+    ld [anim_step_y], a
+    ld [anim_step_x], a
+@no_collision:
+
 @skip_input:
   ; hl should be player_y here