Minecart refactoring, break up collision handling into multiple fns

This commit is contained in:
scawful
2024-12-02 11:48:02 -05:00
parent 4145ea8e4b
commit 0a8e86b501

View File

@@ -121,6 +121,7 @@ Sprite_Minecart_Prep:
STZ.w SprBump, X ; No bump damage
STZ.w SprTileDie, X ; Set interactive hitbox
STZ.w !MinecartDirection
STZ.w !SpriteDirection, X
LDA.w SprSubtype, X : CMP.b #$00 : BEQ .north
CMP.b #$01 : BEQ .east
@@ -452,37 +453,26 @@ Sprite_Minecart_Main:
; =========================================================
HandleTileDirections:
CheckForOutOfBounds:
{
LDA.w SprTimerA, X : BEQ +
RTS
+
; If the cart got disconnected from the player, release them.
JSR CheckIfPlayerIsOn : BCS .player_on_cart
%GotoAction(6) ; Minecart_Release
RTS
.player_on_cart
; Setup Minecart position to look for tile IDs
; We use AND #$F8 to clamp to a 16x16 grid, however this needs work.
LDA.w SprY, X : AND #$F8 : STA.b $00 : LDA.w SprYH, X : STA.b $01
LDA.w SprX, X : AND #$F8 : STA.b $02 : LDA.w SprXH, X : STA.b $03
; Fetch tile attributes based on current coordinates
LDA.b #$00 : JSL Sprite_GetTileAttr : LDA $0FA5
LDA $0FA5
CMP.b #$02 : BNE .not_out_of_bounds
; If the tile is out of bounds, release the cart
LDA #$40 : STA.w SprTimerD, X
%GotoAction(6) ; Minecart_Release
RTS
.not_out_of_bounds
RTS
}
CheckForStopTiles:
{
; Check if the tile is a stop tile
CMP.b #$B7 : BEQ .stop_north
CMP.b #$B8 : BEQ .stop_south
CMP.b #$B9 : BEQ .stop_west
CMP.b #$BA : BEQ .stop_east
JMP .check_for_movement ; if none of the above, continue with normal logic
JMP +
.stop_north
; Set the new direction to north and flip the cart's orientation
@@ -493,7 +483,6 @@ HandleTileDirections:
; Set the new direction to south and flip the cart's orientation
LDA.b #North : STA.w SprSubtype, X : STZ.w !MinecartDirection
LDA.b #Up : STA !SpriteDirection, X
; -----------------------------------------------
.go_vert
%SetTimerA($40)
%StopCart()
@@ -509,44 +498,23 @@ HandleTileDirections:
; Set the new direction to west and flip the cart's orientation
LDA.b #East : STA.w SprSubtype, X : STA.w !MinecartDirection
LDA.b #Right : STA !SpriteDirection, X
; -----------------------------------------------
.go_horiz
%SetTimerA($40)
%StopCart()
%GotoAction(0) ; Minecart_WaitHoriz
JSL Link_ResetProperties_A
+
RTS
}
; -----------------------------------------------------
.check_for_movement
CheckForCornerTiles:
{
CMP.b #$B2 : BEQ .check_direction
CMP.b #$B3 : BEQ .check_direction
CMP.b #$B4 : BEQ .check_direction
CMP.b #$B5 : BEQ .check_direction
CMP.b #$B0 : BEQ .horiz
CMP.b #$B1 : BEQ .vert
CMP.b #$BB : BEQ .horiz
CMP.b #$BC : BEQ .vert
JMP .done
.horiz
; Are we moving left or right?
LDA.w SprSubtype, X : CMP.b #$03 : BEQ .inverse_horiz_velocity
LDA.b #!MinecartSpeed : STA.w SprXSpeed, X
LDA.b #East : STA !MinecartDirection
JMP .done
.inverse_horiz_velocity
LDA.b #-!MinecartSpeed : STA.w SprXSpeed, X
LDA.b #West : STA !MinecartDirection
JMP .done
.vert
; Are we moving up or down?
LDA.w SprSubtype, X : CMP.b #$00 : BEQ .inverse_vert_velocity
LDA.b #!MinecartSpeed : STA.w SprYSpeed, X
JMP .done
.inverse_vert_velocity
LDA.b #-!MinecartSpeed : STA.w SprYSpeed, X
JMP .done
SEC
RTS
.check_direction
LDA.w SprSubtype, X
ASL #2 ; Multiply by 4 to offset rows in the lookup table
@@ -589,7 +557,7 @@ HandleTileDirections:
LDA.w SprY, X : AND #$F8 : STA.w SprY, X
%GotoAction(5) ; Minecart_MoveWest
.done
LDA #$0F : STA.w SprTimerA, X
CLC
RTS
; Direction to move on tile collision
@@ -608,6 +576,63 @@ HandleTileDirections:
}
}
CheckForTrackTiles:
{
CMP.b #$B0 : BEQ .horiz
CMP.b #$B1 : BEQ .vert
CMP.b #$BB : BEQ .horiz
CMP.b #$BC : BEQ .vert
JMP .done
.horiz
; Are we moving left or right?
LDA.w SprSubtype, X : CMP.b #$03 : BEQ .inverse_horiz_velocity
LDA.b #!MinecartSpeed : STA.w SprXSpeed, X
LDA.b #East : STA !MinecartDirection
JMP .done
.inverse_horiz_velocity
LDA.b #-!MinecartSpeed : STA.w SprXSpeed, X
LDA.b #West : STA !MinecartDirection
JMP .done
.vert
; Are we moving up or down?
LDA.w SprSubtype, X : CMP.b #$00 : BEQ .inverse_vert_velocity
LDA.b #!MinecartSpeed : STA.w SprYSpeed, X
JMP .done
.inverse_vert_velocity
LDA.b #-!MinecartSpeed : STA.w SprYSpeed, X
.done
RTS
}
HandleTileDirections:
{
LDA.w SprTimerA, X : BEQ +
RTS
+
; If the cart got disconnected from the player, release them.
JSR CheckIfPlayerIsOn : BCS .player_on_cart
%GotoAction(6) ; Minecart_Release
RTS
.player_on_cart
; Setup Minecart position to look for tile IDs
; We use AND #$F8 to clamp to a 16x16 grid, however this needs work.
LDA.w SprY, X : AND #$F8 : STA.b $00 : LDA.w SprYH, X : STA.b $01
LDA.w SprX, X : AND #$F8 : STA.b $02 : LDA.w SprXH, X : STA.b $03
; Fetch tile attributes based on current coordinates
LDA.b #$00 : JSL Sprite_GetTileAttr
JSR CheckForOutOfBounds
JSR CheckForStopTiles
JSR CheckForCornerTiles : BCC .done
JSR CheckForTrackTiles
.done
LDA #$0F : STA.w SprTimerA, X
RTS
}
; =========================================================
; Check for the switch_track sprite and move based on the
; state of that sprite.