Skip to content

Commit

Permalink
Implement reverse gravity for ShadowDrill room
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsteele committed Jun 10, 2024
1 parent a965c4c commit 89a1270
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 112 deletions.
89 changes: 79 additions & 10 deletions src/avatar.asm
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ Zp_AvatarVelX_i16: .res 2
.EXPORTZP Zp_AvatarVelY_i16
Zp_AvatarVelY_i16: .res 2

;;; The object flags to apply for the player avatar. In particular, if
;;; bObj::FlipH is set, then the avatar will face left instead of right.
;;; The object flags to apply for the player avatar. If bObj::FlipH is set,
;;; then the avatar will face left instead of right. If bObj::FlipV is set,
;;; the avatar is acting under reverse gravity.
.EXPORTZP Zp_AvatarFlags_bObj
Zp_AvatarFlags_bObj: .res 1

Expand Down Expand Up @@ -228,6 +229,13 @@ _SetAvatarPose:
_SetPoseInAir:
;; The player avatar is airborne; set its pose based on its Y-velocity.
lda Zp_AvatarVelY_i16 + 1
bit Zp_AvatarFlags_bObj
.assert bObj::FlipV = bProc::Negative, error
bpl @normalGravity
@reverseGravity:
rsub #0
@normalGravity:
tay
bmi @jumping
cmp #2
blt @hovering
Expand Down Expand Up @@ -255,6 +263,7 @@ _SetPoseInWater:
sta Zp_AvatarPose_eAvatar
rts
_SetPoseOnGround:
;; TODO: Invert Up and Down buttons if gravity is reversed.
lda Zp_AvatarState_bAvatar
and #bAvatar::LandMask
beq @standOrRun ; landing timer is zero
Expand Down Expand Up @@ -314,27 +323,42 @@ _SetPoseOnGround:
;;; collisions. Sets Zp_AvatarExit_ePassage if the avatar hits a vertical
;;; passage.
.PROC FuncA_Avatar_ApplyVelY
;; Determine whether the player avatar is moving against gravity.
bit Zp_AvatarFlags_bObj
.assert bObj::FlipV = bProc::Negative, error
bpl @normalGravity
@reverseGravity:
lda #0
sub Zp_AvatarVelY_i16 + 0
lda #0
sbc Zp_AvatarVelY_i16 + 1
jmp @setOldVel
@normalGravity:
lda Zp_AvatarVelY_i16 + 1
@setOldVel:
pha ; old gravity-relative Y-vel (hi); negative if moving against gravity
_UpdatePosition:
lda Zp_AvatarVelY_i16 + 0
add Zp_AvatarSubY_u8
sta Zp_AvatarSubY_u8
lda Zp_AvatarVelY_i16 + 1
pha ; old Y-velocity (hi)
adc #0
sta Zp_AvatarPushDelta_i8
jsr Func_TryPushAvatarVert
pla ; old Y-velocity (hi)
bmi _NowAirborne ; avatar is moving up
sta T0 ; old Y-velocity (hi)
_CheckIfAirborne:
pla ; old gravity-relative Y-vel (hi); negative if moving against gravity
bmi _NowAirborne ; avatar is moving against gravity
sta T0 ; old vertical speed (hi)
lda Zp_AvatarCollided_ePlatform
.assert ePlatform::None = 0, error
beq _NowAirborne ; no downward collision
beq _NowAirborne ; no floor collision
_NowGrounded:
bit Zp_AvatarState_bAvatar
.assert bAvatar::Airborne = bProc::Negative, error
bpl @done ; avatar was already grounded
@wasAirborne:
lda #0
ldy T0 ; old Y-velocity (hi)
ldy T0 ; old vertical speed (hi)
bmi @setState
lda DataA_Avatar_LandingFrames_u8_arr, y
@setState:
Expand Down Expand Up @@ -425,7 +449,8 @@ _Stop:
;;; button is held.
.PROC FuncA_Avatar_ApplyDpadLeft
;; Face the player avatar to the left.
lda #bObj::FlipH | kPaletteObjAvatarNormal
lda Zp_AvatarFlags_bObj
ora #bObj::FlipH
sta Zp_AvatarFlags_bObj
_DetermineLimit:
;; Determine the (negative) X-velocity limit in pixels/frame, storing it in
Expand Down Expand Up @@ -489,7 +514,8 @@ _AccelerateTowardsLimit:
;;; button is held.
.PROC FuncA_Avatar_ApplyDpadRight
;; Face the player avatar to the right.
lda #kPaletteObjAvatarNormal
lda Zp_AvatarFlags_bObj
and #<~bObj::FlipH
sta Zp_AvatarFlags_bObj
_DetermineLimit:
;; Determine the (positive) X-velocity limit in pixels/frame, storing it in
Expand Down Expand Up @@ -569,7 +595,15 @@ _Grounded:
bpl @noJump
lda #eSample::JumpAnna ; param: eSample to play
jsr Func_PlaySfxSample
bit Zp_AvatarFlags_bObj
.assert bObj::FlipV = bProc::Negative, error
bpl @normalGravity
@reverseGravity:
ldax #$ffff & -kAvatarJumpVelocity
bpl @setVelocity ; unconditional
@normalGravity:
ldax #kAvatarJumpVelocity
@setVelocity:
stax Zp_AvatarVelY_i16
lda #bAvatar::Airborne | bAvatar::Jumping
sta Zp_AvatarState_bAvatar
Expand All @@ -585,12 +619,25 @@ _Airborne:
bit Zp_P1ButtonsHeld_bJoypad
.assert bJoypad::AButton = bProc::Negative, error
bmi _Return ; A button is still held, so don't slow the jump
bit Zp_AvatarFlags_bObj
.assert bObj::FlipV = bProc::Negative, error
bpl @normalGravity
@reverseGravity:
lda Zp_AvatarVelY_i16 + 1
bmi _Return ; avatar is moving upward, not downward
cmp #kAvatarStopJumpSpeed
blt _Return ; avatar is already below the downward speed cap
lda #kAvatarStopJumpSpeed
sta Zp_AvatarVelY_i16 + 1
bne @zeroVelLo ; unconditional
@normalGravity:
lda Zp_AvatarVelY_i16 + 1
bpl _Return ; avatar is moving downward, not upward
cmp #$ff & -kAvatarStopJumpSpeed
bge _Return ; avatar is already at or below the upward speed cap
lda #$ff & -kAvatarStopJumpSpeed
sta Zp_AvatarVelY_i16 + 1
@zeroVelLo:
lda #$00
sta Zp_AvatarVelY_i16 + 0
_Return:
Expand Down Expand Up @@ -650,6 +697,10 @@ _InWater:
@done:
rts
_InAir:
bit Zp_AvatarFlags_bObj
.assert bObj::FlipV = bProc::Negative, error
bmi _InAirReverseGravity
_InAirNormalGravity:
;; Accelerate the player avatar downwards.
lda #kAvatarGravity
add Zp_AvatarVelY_i16 + 0
Expand All @@ -668,6 +719,24 @@ _InAir:
sta Zp_AvatarVelY_i16 + 1
_Done:
rts
_InAirReverseGravity:
;; Accelerate the player avatar upwards.
lda Zp_AvatarVelY_i16 + 0
sub #kAvatarGravity
sta Zp_AvatarVelY_i16 + 0
lda Zp_AvatarVelY_i16 + 1
sbc #0
;; If moving upward, check for terminal velocity:
bpl @setVelYHi
.assert <-kAvatarMaxAirSpeedVert = 0, error
cmp #>-kAvatarMaxAirSpeedVert
bge @setVelYHi
lda #0
sta Zp_AvatarVelY_i16 + 0
lda #>-kAvatarMaxAirSpeedVert
@setVelYHi:
sta Zp_AvatarVelY_i16 + 1
rts
.ENDPROC

;;;=========================================================================;;;
Expand Down
5 changes: 4 additions & 1 deletion src/death.asm
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ _InitNumRollingDeathDigits:
iny
tya ; num consecutive 9's, plus 1
.assert kNumDeathDigits + 1 = 4, error
and #$03 ; A = A mod 4
mod #4
sta Zp_NumRollingDeathDigits_u8
_InitTimers:
lda #kDeathTotalAvatarAnimationFrames
Expand All @@ -191,6 +191,9 @@ _FadeOut:

;;; Performs per-frame updates during the player avatar death animation.
.PROC FuncA_Death_AnimateAvatar
lda Zp_AvatarFlags_bObj
and #<~bObj::FlipV
sta Zp_AvatarFlags_bObj
_DecrementTimer:
dec Zp_DeathTimer_u8
bne @done
Expand Down
15 changes: 12 additions & 3 deletions src/explore.asm
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
.IMPORT FuncA_Objects_DrawFloatingHud
.IMPORT FuncA_Objects_DrawPlayerAvatar
.IMPORT FuncA_Objects_MoveShapeLeftHalfTile
.IMPORT FuncA_Objects_MoveShapeUpByA
.IMPORT FuncA_Objects_MoveShapeVert
.IMPORT FuncA_Objects_SetShapePosToAvatarCenter
.IMPORT FuncA_Room_CallRoomTick
.IMPORT FuncA_Room_InitAllMachinesAndCallRoomEnter
Expand Down Expand Up @@ -85,6 +85,7 @@
.IMPORT Sram_CarryingFlower_eFlag
.IMPORT Sram_LastSafe_eRoom
.IMPORTZP Zp_AvatarExit_ePassage
.IMPORTZP Zp_AvatarFlags_bObj
.IMPORTZP Zp_AvatarHarmTimer_u8
.IMPORTZP Zp_AvatarPosX_i16
.IMPORTZP Zp_AvatarPosY_i16
Expand Down Expand Up @@ -550,8 +551,16 @@ _DrawObject:
bne @noZigZag
lda #$01
@noZigZag:
add #3 + kTileWidthPx * 2 ; param: offset
jsr FuncA_Objects_MoveShapeUpByA
bit Zp_AvatarFlags_bObj
.assert bObj::FlipV = bProc::Negative, error
bpl @normalGravity
@reverseGravity:
add #3 + kTileHeightPx
bne @moveShape ; unconditional
@normalGravity:
sub #5 + kTileHeightPx * 2 ; param: signed offset
@moveShape:
jsr FuncA_Objects_MoveShapeVert
;; Draw the object:
ldy #kPaletteObjDevicePrompt ; param: object flags
lda #kTileIdObjDevicePrompt ; param: tile ID
Expand Down
Loading

0 comments on commit 89a1270

Please sign in to comment.