feat: gravity kind of works

main
Louis Pearson 2024-04-19 14:03:13 -06:00
parent 40c151a86b
commit 57b27449a4
1 changed files with 121 additions and 68 deletions

189
main.asm
View File

@ -43,23 +43,42 @@ WaitVBlank:
; Reset positions ; Reset positions
ld hl, wMetaspritePosition.x ld hl, wMetaspritePosition.x
ld a, 0 ld a, $10
ld [wMetaspritePosition.x], a ld [wMetaspritePosition.x], a
ld a, $80 ld a, $0
ld [wMetaspritePosition.x + 1], a ld [wMetaspritePosition.x + 1], a
ld hl, wMetaspritePosition.y ld hl, wMetaspritePosition.y
ld a, 0 ld a, $10
ld [wMetaspritePosition.y], a ld [wMetaspritePosition.y], a
ld a, $80 ld a, $01
ld [wMetaspritePosition.y + 1], a ld [wMetaspritePosition.y + 1], a
ld hl, wMetaspritePosition.z ld hl, wMetaspritePosition.z
ld a, 0 ld a, $00
ld [wMetaspritePosition.z], a ld [wMetaspritePosition.z], a
ld a, 0 ld a, $0
ld [wMetaspritePosition.z + 1], a ld [wMetaspritePosition.z + 1], a
; Reset velocity
ld hl, wMetaspriteVelocity.x
ld a, $0
ld [wMetaspriteVelocity.x], a
ld a, $0
ld [wMetaspriteVelocity.x + 1], a
ld hl, wMetaspriteVelocity.y
ld a, $0
ld [wMetaspriteVelocity.y], a
ld a, $0
ld [wMetaspriteVelocity.y + 1], a
ld hl, wMetaspriteVelocity.z
ld a, $0
ld [wMetaspriteVelocity.z], a
ld a, $0
ld [wMetaspriteVelocity.z + 1], a
; Copy the tile data ; Copy the tile data
ld de, Tiles ld de, Tiles
ld hl, $9000 ld hl, $9000
@ -91,10 +110,19 @@ WaitVBlank:
; Enable interrupts ; Enable interrupts
ei ei
Main: Main::
call ResetShadowOAM call ResetShadowOAM
call UpdateJoypadState call UpdateJoypadState
call HandleInput
call HandlePhysics
call HandleRender
halt
nop
jp Main
HandleInput::
ld a, [wJoypadState] ld a, [wJoypadState]
bit PADB_LEFT, a bit PADB_LEFT, a
jr nz, .leftend jr nz, .leftend
@ -151,46 +179,22 @@ Main:
ld [wMetaspritePosition.y+1], a ld [wMetaspritePosition.y+1], a
.downend: .downend:
ld a, [wJoypadState] ld a, [wJoypadPressed]
bit PADB_A, a bit PADB_A, a
jr nz, .jumpend jr z, .jumpend
ld a, [wMetaspritePosition.z] ld a, [wMetaspriteVelocity.z]
add a, 16 sub a, $F0
ld b, a ld b, a
ld [wMetaspritePosition.z], a ld [wMetaspriteVelocity.z], a
ld a, [wMetaspritePosition.z+1] ld a, [wMetaspriteVelocity.z+1]
adc 0 sbc a, $00
ld c, a ld c, a
ld [wMetaspritePosition.z+1], a ld [wMetaspriteVelocity.z+1], a
.jumpend: .jumpend:
ret
ld a, [wJoypadState] HandleRender::
bit PADB_B, a
jr nz, .fallend
ld a, [wMetaspritePosition.z + 1]
cp 0
jr nz, .fall
ld a, [wMetaspritePosition.z]
cp 16
jr nc, .fall
ld a, 0
ld [wMetaspritePosition.z], a
jr .fallend
.fall:
ld a, [wMetaspritePosition.z]
sub a, 16
ld b, a
ld [wMetaspritePosition.z], a
ld a, [wMetaspritePosition.z+1]
sbc 0
ld c, a
ld [wMetaspritePosition.z+1], a
.fallend:
; Render the player ; Render the player
; load de ; load de
ld a, [wMetaspritePosition.x] ld a, [wMetaspritePosition.x]
@ -202,12 +206,12 @@ Main:
ld a, [wMetaspritePosition.z] ld a, [wMetaspritePosition.z]
ld c, a ld c, a
ld a, [wMetaspritePosition.y] ld a, [wMetaspritePosition.y]
sub a, c add a, c
ld c, a ld c, a
ld a, [wMetaspritePosition.z + 1] ld a, [wMetaspritePosition.z + 1]
ld b, a ld b, a
ld a, [wMetaspritePosition.y + 1] ld a, [wMetaspritePosition.y + 1]
sbc a, b adc a, b
ld b, a ld b, a
ld hl, CatMetasprite ld hl, CatMetasprite
@ -228,31 +232,61 @@ Main:
ld hl, ShadowMetasprite ld hl, ShadowMetasprite
call RenderMetasprite call RenderMetasprite
ldh a, [hFrameCounter]
inc a
ldh [hFrameCounter], a
halt
nop
jp Main
Memcopy::
dec bc
inc b
inc c
.loop:
ld a, [de]
ld [hli], a
inc de
dec c
jr nz, .loop
dec b
jr nz, .loop
ret ret
HandlePhysics::
; Skip gravity if on ground
ld a, [wMetaspritePosition.z]
ld b, a
ld a, [wMetaspritePosition.z + 1]
or a, b
cp 0
jr z, .endGravity
; Apply gravity
ld a, [wMetaspriteVelocity.z]
add a, $02
ld [wMetaspriteVelocity.z], a
ld a, [wMetaspriteVelocity.z+1]
adc a, $00
ld [wMetaspriteVelocity.z+1], a
.endGravity:
; Calculate new z, place in bc
ld a, [wMetaspriteVelocity.z]
ld c, a
ld a, [wMetaspritePosition.z]
add c
ld a, [wMetaspriteVelocity.z + 1]
ld b, a
ld a, [wMetaspritePosition.z + 1]
adc b
; If z is greater than or equal to 0, pos/vel to 0
jr nz, .endLanding1
jr nc, .endLanding1
.startLanding
ld a, 0
ld [wMetaspriteVelocity.z], a
ld [wMetaspriteVelocity.z+1], a
ld c, 0
ld b, 0
jr .endLanding
.endLanding1
nop
.endLanding
; Update z position
ld a, c
ld [wMetaspritePosition.z], a
ld a, b
ld [wMetaspritePosition.z+1], a
ret
SECTION "Joypad Routine", ROM0
UpdateJoypadState:: UpdateJoypadState::
ld hl, rP1 ld hl, rP1
ld [hl], P1F_GET_BTN ld [hl], P1F_GET_BTN
@ -287,6 +321,20 @@ UpdateJoypadState::
ld [wJoypadState], a ld [wJoypadState], a
ret ret
SECTION "MemCopy Routine", ROM0
Memcopy::
dec bc
inc b
inc c
.loop:
ld a, [de]
ld [hli], a
inc de
dec c
jr nz, .loop
dec b
jr nz, .loop
ret
SECTION "VBlank Interrupt", ROM0[$0040] SECTION "VBlank Interrupt", ROM0[$0040]
VBlankInterrupt: VBlankInterrupt:
@ -443,7 +491,7 @@ ShadowMetasprite:
SECTION "Position Vars", WRAM0 SECTION "Position Vars", WRAM0
; Q12.4 fixed-point X posiition ; Q12.4 fixed-point X posiition
wMetaspritePosition: wMetaspritePosition::
.x: .x:
dw dw
.y: .y:
@ -453,7 +501,12 @@ wMetaspritePosition:
; Q4.4 fixed-point velocity ; Q4.4 fixed-point velocity
wMetaspriteVelocity:: wMetaspriteVelocity::
db .x:
dw
.y:
dw
.z:
dw
SECTION "Joypad Vars", WRAM0 SECTION "Joypad Vars", WRAM0
wJoypadState: wJoypadState: