diff --git a/inc/util.inc b/inc/util.inc index 21cdfc0..8c4a726 100644 --- a/inc/util.inc +++ b/inc/util.inc @@ -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 ld [hl], HIGH(\1) inc hl diff --git a/src/bg.s b/src/bg.s index 0a4d878..a768d9b 100644 --- a/src/bg.s +++ b/src/bg.s @@ -5,18 +5,22 @@ SECTION "BG Data", WRAM0 BG_COLLISION_DATA:: dw BG_MAP_WIDTH:: db +BG_MAP_HEIGHT:: db SECTION "BG Code", ROM0 MACRO update_map_info ld hl, BG_COLLISION_DATA ld a, HIGH(\1) - ld [hli], a + ld [hl+], a ld a, LOW(\1) - ld [hli], a + ld [hl+], a ld a, \2 - ld [hli], a + ld [hl+], a + + ld a, \3 + ld [hl+], a ENDM BG_Init:: @@ -47,6 +51,6 @@ BG_Init:: ld d, intro_NUM_TILES call memcpy - update_map_info intro_COLLISION, intro_WIDTH + update_map_info intro_COLLISION, intro_WIDTH, intro_HEIGHT ret diff --git a/src/collision.s b/src/collision.s index fb4f7fb..ad791c2 100644 --- a/src/collision.s +++ b/src/collision.s @@ -35,18 +35,18 @@ player_bg_collides:: ; top left call can_move_to - ret z + ret nz ; top right inc c call can_move_to - ret z + ret nz ; bottom left dec c inc b call can_move_to - ret z + ret nz ; bottom right inc c @@ -58,7 +58,7 @@ player_bg_collides:: ; @param b y-coordinate ; @param c x-coordinate ; @destroy a, d, e -; @note Z = false, NZ = true +; @note NZ = false, Z = true can_move_to: ; todo: should be aware of scx/scy push bc @@ -86,7 +86,7 @@ can_move_to: ; check [hl] = 1 ld a, [hl] - cp 0 + cp 1 pop hl pop bc diff --git a/src/player.s b/src/player.s index 9946141..161af50 100644 --- a/src/player.s +++ b/src/player.s @@ -15,7 +15,6 @@ PLAYER_DIR:: db PLAYER_JUMPING:: db PLAYER_VY:: dw -PLAYER_OLD_Y: db 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_WIDTH EQU 2 DEF SPRITE_HEIGHT EQU 2 +DEF PLAYER_SPEED EQU 2 DEF GRAVITY EQU (0 << 8) | 8 DEF INIT_VY EQU (2 << 8) | 40 @@ -63,13 +63,13 @@ Player_Update:: ; check for jump ld a, b and BTN_UP - jr z, .update_jump + jr z, .jump_update_check ; initialize jump state if not already jumping ld hl, PLAYER_JUMPING ld a, [hl] or a - jr nz, .update_jump + jr nz, .jump_update ld [hl], 1 @@ -78,12 +78,14 @@ Player_Update:: inc hl ld [hl], LOW(INIT_VY) -.update_jump: +; todo: deduplicate +.jump_update_check: ld hl, PLAYER_JUMPING ld a, [hl] or a jr z, .right +.jump_update: ; load y velocity into bc ld hl, PLAYER_VY ld b, [hl] @@ -101,12 +103,6 @@ Player_Update:: ld hl, PLAYER_VY STORE16 bc - ; old_y = y - ld hl, PLAYER_Y - ld a, [hl] - ld hl, PLAYER_OLD_Y - ld [hl], a - ; y -= floor(vy) ld hl, PLAYER_Y ld a, [hl] @@ -115,24 +111,39 @@ Player_Update:: ; roll back jump if there was a collision 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 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 + bit 7, [hl] + + ; either way, set VY = 0 + ld [hl], 0 + ld hl, PLAYER_VY + 1 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], 0 -.update_jump_oam: +.jump_update_oam: call update_oam ; check for move right @@ -153,7 +164,7 @@ Player_Update:: inc [hl] call player_bg_collides - jr z, .right_rollback + jr nz, .right_rollback ld hl, PLAYER_DIR ld [hl], 0 @@ -180,11 +191,11 @@ Player_Update:: or a ret z - dec [hl] - dec [hl] + sub PLAYER_SPEED + ld [hl], a call player_bg_collides - jr z, .left_rollback + jr nz, .left_rollback ld hl, PLAYER_DIR ld [hl], $ff @@ -194,7 +205,7 @@ Player_Update:: .left_rollback: ld hl, PLAYER_X - dec [hl] + inc [hl] inc [hl] ret