SlimePlatformer/Characters/player.gd
SnekInAScarf 485553df3e Fix climbing from a ceiling lip onto the wall above
- Pressing up while hanging on a lip now moves the slime up and back
  towards the platform so it wraps onto the wall instead of staying frozen.
- Only walking off a ceiling edge starts a lip hang (not landing on a
  ceiling or dropping off it with down).
- Don't count the held direction towards unsticking from the wall right
  after climbing on from a lip, until it's released.
- Remove leftover debug print.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 12:30:08 -07:00

128 lines
5 KiB
GDScript
Executable file

extends CharacterBody2D
const GROUND_SPEED = 150.0
const AIR_SPEED = 75.0
const CLIMB_SPEED = -150.0
const ROTATE_TO_WALL_SPEED = 25 # How quickly to rotate the sprite when clinging to a wall.
const CEILING_STICK_SPEED = -20.0 # Small upward push that keeps the slime pressed against the ceiling.
const WALL_STICK_SPEED = 20.0 # Small push into the wall that keeps the slime pressed against it.
# Temp for now, need to use aniamtedspite2d later on
@onready var sprite = $Sprite2D
# How long to press opposite direction before unsticking from a wall/ceiling
const MAX_SECS_TO_UNSTICK = 0.5
var isStickingToWall := false
var timeSpentUnstickingWall = 0.0
var wallNormalX := 0.0
var isStickingToCeiling := false
var timeSpentUnstickingCeiling = 0.0
var isClimbingLip = false
var timeSpentUnstickingLip= 0.0
var lipDirection := 0.0 # Which way we were moving when we went over the lip.
# After climbing onto a wall from a lip we're usually still holding away from it,
# so don't count that towards unsticking until the direction is released.
var ignoreWallUnstickUntilRelease := false
func _process(delta: float) -> void:
# Update sprite to be on the wall or ceiling. In a corner only one of them
# is ever true (that gets decided at the end of _physics_process).
if not is_on_floor() and isStickingToWall:
sprite.rotation = lerp_angle(sprite.rotation, PI/2 * wallNormalX, ROTATE_TO_WALL_SPEED * delta)
elif isStickingToCeiling:
sprite.rotation = lerp_angle(sprite.rotation, PI, ROTATE_TO_WALL_SPEED * delta)
else:
sprite.rotation = lerp_angle(sprite.rotation, 0.0, ROTATE_TO_WALL_SPEED * delta)
func _physics_process(delta: float) -> void:
# Get the input direction and handle the movement/deceleration.
var horizontalInput := Input.get_axis("moveLeft", "moveRight")
var verticalInput := Input.get_axis("moveDown", "moveUp")
# Quick flip sprite thing.
if horizontalInput:
if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0)
else: sprite.flip_h = (horizontalInput < 0)
# Vertical Input Controls
if isStickingToCeiling:
# If on ceiling, pressing down long enough unsticks from ceiling.
if verticalInput < 0: timeSpentUnstickingCeiling += delta
else: timeSpentUnstickingCeiling = 0.0
if timeSpentUnstickingCeiling >= MAX_SECS_TO_UNSTICK:
isStickingToCeiling = false
velocity.y = get_gravity().y * delta
else: velocity.y = CEILING_STICK_SPEED
# Move up and down along a wall when sticking to it.
elif isStickingToWall:
velocity.y = CLIMB_SPEED * verticalInput
# Don't fall if climbing lip. Pressing up climbs onto the wall above.
elif isClimbingLip:
velocity.y = CLIMB_SPEED if verticalInput > 0 else 0.0
# Else... just fall.
elif not is_on_floor():
velocity.y += get_gravity().y * delta
# Horizontal Input Controls
# If sticking to a wall, pressing away from it long enough gets us unstuck.
if isStickingToWall and not is_on_floor():
var pressingAwayFromWall := horizontalInput * wallNormalX > 0
if not pressingAwayFromWall: ignoreWallUnstickUntilRelease = false
if pressingAwayFromWall and not ignoreWallUnstickUntilRelease: timeSpentUnstickingWall += delta
else: timeSpentUnstickingWall = 0.0
if timeSpentUnstickingWall >= MAX_SECS_TO_UNSTICK:
velocity.x = horizontalInput * GROUND_SPEED
else:
velocity.x = -wallNormalX * WALL_STICK_SPEED
# Otherwise move horizontally based on what we're on.
else:
timeSpentUnstickingWall = 0.0
# Hang on the lip. Pressing up pushes back towards the platform so we
# slide around the corner and touch the wall.
if isClimbingLip:
velocity.x = -lipDirection * WALL_STICK_SPEED if verticalInput > 0 else 0.0
elif horizontalInput and (is_on_floor() or isStickingToCeiling):
velocity.x = horizontalInput * GROUND_SPEED
elif horizontalInput and not (is_on_floor() or isStickingToCeiling):
velocity.x = horizontalInput * AIR_SPEED
else:
velocity.x = move_toward(velocity.x, 0, GROUND_SPEED)
move_and_slide()
# Check if we should be sticking to walls/ceiling after moving our player.
isStickingToWall = is_on_wall()
if isStickingToWall:
wallNormalX = get_wall_normal().x
# Set if we're sticking to a ceiling.
# Also check if we started climbing a lip.
if isClimbingLip:
timeSpentUnstickingLip += delta
if timeSpentUnstickingLip >= MAX_SECS_TO_UNSTICK:
isClimbingLip = false
else: timeSpentUnstickingLip = 0.0
var wasJustOnCeiling = isStickingToCeiling
isStickingToCeiling = is_on_ceiling()
# Only walking off the ceiling's edge counts as a lip, not landing on
# a ceiling or letting go of it by pressing down.
if wasJustOnCeiling and not isStickingToCeiling and not isStickingToWall and velocity.x:
isClimbingLip = true
lipDirection = signf(velocity.x)
# Made it onto the wall from the lip.
if isClimbingLip and isStickingToWall:
isClimbingLip = false
ignoreWallUnstickUntilRelease = true
# In a corner touching both wall and ceiling, only stick to one
# surface depending on if we're pressing into the ceiling or not.
if isStickingToWall and isStickingToCeiling:
if verticalInput > 0:
isStickingToWall = false
else:
isStickingToCeiling = false