Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- X player coordinate is the middle of the player horizontally
- Y player coordinate is one pixel under the bottom of the player
- Constants:
- BlockSize : Width/Height of a map block
- Variables:
- PlayerRunning : Player is currently running
- PlayerOnGround : Player is on the ground this frame
- PlayerWasOnGround : Player was on the ground last frame
- MaxSpeed : Maximum allowed speed for walking/running
- PlayerPX : Player X position
- PlayerPY : Player Y position
- PlayerVX : Player X speed
- PlayerVY : Player Y speed
- GetSlopeHeight(X, Y):
- BlockID = Level[X / BlockSize][Y / BlockSize]
- return the Y position corresponding to the highest point on the block, using X to select a column within the block
- (If the column is the maximum height, check if the block above is a slope, and use that block's highest point if so)
- Player update():
- PlayerWasOnGround = PlayerOnGround
- PlayerOnGround = 0
- // Get the maximum horizontal speed
- If PlayerRunning:
- MaxSpeed = Run speed
- Else:
- MaxSpeed = Walk speed
- // Speed up if pressing left/right
- If pressing left:
- If PlayerVX > -MaxSpeed:
- PlayerVX -= Acceleration speed
- If pressing right:
- If PlayerVX < MaxSpeed:
- PlayerVX += Acceleration speed
- // Slow down if not pressing left/right
- Temp = Deceleration speed
- If pressing left:
- If PlayerVX < 0:
- goto SkipDeceleration
- Temp /= 2
- If pressing right:
- If PlayerVX > 0:
- goto SkipDeceleration
- Temp /= 2
- If PlayerVX > 0:
- PlayerVX -= Temp
- If PlayerVX < 0:
- PlayerVX = 0
- Else If PlayerVX < 0:
- PlayerVX += Temp
- If PlayerVX > 0:
- PlayerVX = 0
- SkipDeceleration:
- PlayerPX += PlayerVX
- // Going downhill requires extra help
- If PlayerWasOnGround && !JumpButtonPressed && PlayerVX != 0:
- If Level[PlayerPX / BlockSize][PlayerPY / BlockSize] is slope:
- PlayerPY = GetSlopeHeight(PlayerPX, PlayerPY)
- PlayerOnGround = True
- PlayerVY = 0
- Else If Level[PlayerPX / BlockSize][(PlayerPY+BlockSize) / BlockSize] is slope:
- PlayerPY = GetSlopeHeight(PlayerPX, PlayerPY + BlockSize)
- PlayerOnGround = True
- PlayerVY = 0
- // Interact with walls
- If PlayerVX >= 0:
- SideXPos = PlayerPX + 3 pixels
- If PlayerVX < 0:
- SideXPos = PlayerPX - 3 pixels
- TrySideInteraction(SideXPos, PlayerPY - 16 pixels)
- TrySideInteraction(SideXPos, PlayerPY - 23 pixels)
- // Vertical movement
- If PlayerVY < MaxGravity:
- PlayerVY += Gravity
- PlayerPY += PlayerVY
- // Original code allows canceling a jump here, for variable height jumps
- // Slope interaction
- If Level[PlayerPX / BlockSize][PlayerPY / BlockSize] is slope:
- Temp = GetSlopeHeight(PlayerPX, PlayerPY)
- If PlayerPY >= Temp:
- PlayerPY = Temp
- PlayerVY = 0
- PlayerOnGround = 1
- goto SkipGroundCheck
- // Collide with blocks on the bottom
- If PlayerVY >= 0:
- TryAboveInteraction(PlayerPX - 4 pixels, PlayerPY)
- TryAboveInteraction(PlayerPX + 3 pixels, PlayerPY)
- SkipGroundCheck:
- // Collide with blocks above the player
- TryBelowInteraction(PlayerPX, PlayerPY - 30 pixels)
- If PlayerOnGround:
- If JumpButtonPressed:
- PlayerVY = Jump velocity
- Update PlayerRunning for the current run button status
- TryBelowInteraction(X, Y):
- If Level[X / BlockSize][Y / BlockSize] is solid on the bottom:
- PlayerVY = 0
- Snap PlayerPY to the bottom of the block
- TrySideInteraction(X, Y):
- If Level[X / BlockSize][Y / BlockSize] is solid on the side:
- PlayerVX = 0
- Snap PlayerPX to the side of the block
- TryAboveInteraction(X, Y):
- If Level[X / BlockSize][Y / BlockSize] is solid on the top:
- PlayerVY = 0
- Snap PlayerPY to the top of the block
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement