Browse Source

Fix resolution of vertical collisions

master
Forest Belton 3 years ago
parent
commit
ee9923aa73
4 changed files with 49 additions and 34 deletions
  1. +2
    -2
      inc/util.inc
  2. +8
    -4
      src/bg.s
  3. +5
    -5
      src/collision.s
  4. +34
    -23
      src/player.s

+ 2
- 2
inc/util.inc View File

@ -1,5 +1,5 @@
; Stores a 16-bit register into the address stored in HL
; \1 The register to store
; Stores a 16-bit value into the address stored in HL
; \1 The value to store (immediate or register)
MACRO STORE16 MACRO STORE16
ld [hl], HIGH(\1) ld [hl], HIGH(\1)
inc hl inc hl

+ 8
- 4
src/bg.s View File

@ -5,18 +5,22 @@ SECTION "BG Data", WRAM0
BG_COLLISION_DATA:: dw BG_COLLISION_DATA:: dw
BG_MAP_WIDTH:: db BG_MAP_WIDTH:: db
BG_MAP_HEIGHT:: db
SECTION "BG Code", ROM0 SECTION "BG Code", ROM0
MACRO update_map_info MACRO update_map_info
ld hl, BG_COLLISION_DATA ld hl, BG_COLLISION_DATA
ld a, HIGH(\1) ld a, HIGH(\1)
ld [hli], a
ld [hl+], a
ld a, LOW(\1) ld a, LOW(\1)
ld [hli], a
ld [hl+], a
ld a, \2 ld a, \2
ld [hli], a
ld [hl+], a
ld a, \3
ld [hl+], a
ENDM ENDM
BG_Init:: BG_Init::
@ -47,6 +51,6 @@ BG_Init::
ld d, intro_NUM_TILES ld d, intro_NUM_TILES
call memcpy call memcpy
update_map_info intro_COLLISION, intro_WIDTH
update_map_info intro_COLLISION, intro_WIDTH, intro_HEIGHT
ret ret

+ 5
- 5
src/collision.s View File

@ -35,18 +35,18 @@ player_bg_collides::
; top left ; top left
call can_move_to call can_move_to
ret z
ret nz
; top right ; top right
inc c inc c
call can_move_to call can_move_to
ret z
ret nz
; bottom left ; bottom left
dec c dec c
inc b inc b
call can_move_to call can_move_to
ret z
ret nz
; bottom right ; bottom right
inc c inc c
@ -58,7 +58,7 @@ player_bg_collides::
; @param b y-coordinate ; @param b y-coordinate
; @param c x-coordinate ; @param c x-coordinate
; @destroy a, d, e ; @destroy a, d, e
; @note Z = false, NZ = true
; @note NZ = false, Z = true
can_move_to: can_move_to:
; todo: should be aware of scx/scy ; todo: should be aware of scx/scy
push bc push bc
@ -86,7 +86,7 @@ can_move_to:
; check [hl] = 1 ; check [hl] = 1
ld a, [hl] ld a, [hl]
cp 0
cp 1
pop hl pop hl
pop bc pop bc

+ 34
- 23
src/player.s View File

@ -15,7 +15,6 @@ PLAYER_DIR:: db
PLAYER_JUMPING:: db PLAYER_JUMPING:: db
PLAYER_VY:: dw PLAYER_VY:: dw
PLAYER_OLD_Y: db
Section "Player Code", ROM0 Section "Player Code", ROM0
@ -26,6 +25,7 @@ DEF SPRITE_OAM_IDX EQU 0
DEF SPRITE_IDX EQU 32 ; tile index, rename later DEF SPRITE_IDX EQU 32 ; tile index, rename later
DEF SPRITE_WIDTH EQU 2 DEF SPRITE_WIDTH EQU 2
DEF SPRITE_HEIGHT EQU 2 DEF SPRITE_HEIGHT EQU 2
DEF PLAYER_SPEED EQU 2
DEF GRAVITY EQU (0 << 8) | 8 DEF GRAVITY EQU (0 << 8) | 8
DEF INIT_VY EQU (2 << 8) | 40 DEF INIT_VY EQU (2 << 8) | 40
@ -63,13 +63,13 @@ Player_Update::
; check for jump ; check for jump
ld a, b ld a, b
and BTN_UP and BTN_UP
jr z, .update_jump
jr z, .jump_update_check
; initialize jump state if not already jumping ; initialize jump state if not already jumping
ld hl, PLAYER_JUMPING ld hl, PLAYER_JUMPING
ld a, [hl] ld a, [hl]
or a or a
jr nz, .update_jump
jr nz, .jump_update
ld [hl], 1 ld [hl], 1
@ -78,12 +78,14 @@ Player_Update::
inc hl inc hl
ld [hl], LOW(INIT_VY) ld [hl], LOW(INIT_VY)
.update_jump:
; todo: deduplicate
.jump_update_check:
ld hl, PLAYER_JUMPING ld hl, PLAYER_JUMPING
ld a, [hl] ld a, [hl]
or a or a
jr z, .right jr z, .right
.jump_update:
; load y velocity into bc ; load y velocity into bc
ld hl, PLAYER_VY ld hl, PLAYER_VY
ld b, [hl] ld b, [hl]
@ -101,12 +103,6 @@ Player_Update::
ld hl, PLAYER_VY ld hl, PLAYER_VY
STORE16 bc STORE16 bc
; old_y = y
ld hl, PLAYER_Y
ld a, [hl]
ld hl, PLAYER_OLD_Y
ld [hl], a
; y -= floor(vy) ; y -= floor(vy)
ld hl, PLAYER_Y ld hl, PLAYER_Y
ld a, [hl] ld a, [hl]
@ -115,24 +111,39 @@ Player_Update::
; roll back jump if there was a collision ; roll back jump if there was a collision
call player_bg_collides call player_bg_collides
jr nz, .update_jump_oam
jr z, .jump_update_oam
.jump_rollback:
ld hl, PLAYER_VY
bit 7, [hl]
ld hl, PLAYER_OLD_Y
ld a, [hl]
ld hl, PLAYER_Y ld hl, PLAYER_Y
ld b, [hl]
jr z, .jump_rollback_down
dec [hl]
jr .jump_rollback_check
.jump_rollback_down:
inc [hl]
.jump_rollback_check:
call player_bg_collides
jr nz, .jump_rollback
; set PLAYER_JUMPING = 0 if VY < 0 (fell into ground)
ld hl, PLAYER_VY ld hl, PLAYER_VY
bit 7, [hl]
; either way, set VY = 0
ld [hl], 0
ld hl, PLAYER_VY + 1
ld [hl], 0 ld [hl], 0
; if colliding below, stop jump state
cp b
jr nc, .right
jr z, .jump_update_oam
ld hl, PLAYER_JUMPING ld hl, PLAYER_JUMPING
ld [hl], 0 ld [hl], 0
.update_jump_oam:
.jump_update_oam:
call update_oam call update_oam
; check for move right ; check for move right
@ -153,7 +164,7 @@ Player_Update::
inc [hl] inc [hl]
call player_bg_collides call player_bg_collides
jr z, .right_rollback
jr nz, .right_rollback
ld hl, PLAYER_DIR ld hl, PLAYER_DIR
ld [hl], 0 ld [hl], 0
@ -180,11 +191,11 @@ Player_Update::
or a or a
ret z ret z
dec [hl]
dec [hl]
sub PLAYER_SPEED
ld [hl], a
call player_bg_collides call player_bg_collides
jr z, .left_rollback
jr nz, .left_rollback
ld hl, PLAYER_DIR ld hl, PLAYER_DIR
ld [hl], $ff ld [hl], $ff
@ -194,7 +205,7 @@ Player_Update::
.left_rollback: .left_rollback:
ld hl, PLAYER_X ld hl, PLAYER_X
dec [hl]
inc [hl]
inc [hl] inc [hl]
ret ret

Loading…
Cancel
Save