Added extra code for sticking to the wall or ceiling.

- Slight delay before unsticking from a wall/ceiling.
This commit is contained in:
SnekInAScarf 2026-10-29 14:25:36 -07:00
parent 4df996690e
commit f70cc0b97a

View file

@ -4,23 +4,30 @@ const MOVE_SPEED = 150.0
const CLIMB_SPEED = -150.0 const CLIMB_SPEED = -150.0
const JUMP_VELOCITY = -250.0 const JUMP_VELOCITY = -250.0
const WALL_SLIDE_MODIFIER = 0.1
const ROTATE_TO_WALL_SPEED = 25 # How quickly to rotate the sprite when clinging to a wall. 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 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 # Temp for now, need to use aniamtedspite2d later on
@onready var sprite = $Sprite2D @onready var sprite = $Sprite2D
# is_on_ceiling() is only true on frames where we actually bumped the ceiling, # How long to press opposite direction before unsticking from a wall/ceiling
# so we remember that we're stuck to it here. const MAX_SECS_TO_UNSTICK = 0.5
var isClingingToCeiling := false
var isStickingToWall := false
var timeSpentUnstickingWall = 0.0
var wallNormalX := 0.0
var isStickingToCeiling := false
var timeSpentUnstickingCeiling = 0.0
func _process(delta: float) -> void: func _process(delta: float) -> void:
# Update sprite to be on the ceiling or wall # Update sprite to be on the wall or ceiling. In a corner only one of them
if isClingingToCeiling: # 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) sprite.rotation = lerp_angle(sprite.rotation, PI, ROTATE_TO_WALL_SPEED * delta)
elif not is_on_floor() and is_on_wall():
sprite.rotation = lerp_angle(sprite.rotation, PI/2 * get_wall_normal().x, ROTATE_TO_WALL_SPEED * delta)
else: else:
sprite.rotation = lerp_angle(sprite.rotation, 0.0, ROTATE_TO_WALL_SPEED * delta) sprite.rotation = lerp_angle(sprite.rotation, 0.0, ROTATE_TO_WALL_SPEED * delta)
@ -30,29 +37,53 @@ func _physics_process(delta: float) -> void:
var verticalInput := Input.get_axis("moveDown", "moveUp") var verticalInput := Input.get_axis("moveDown", "moveUp")
# Quick flip sprite thing. # Quick flip sprite thing.
if horizontalInput: sprite.flip_h = (horizontalInput < 0) if horizontalInput:
if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0)
else: sprite.flip_h = (horizontalInput < 0)
# Pressing down lets go of the ceiling. Zeroing velocity.y means we start # Vertical Input Controls
# falling right away, instead of drifting up and sticking again. if isStickingToCeiling:
if isClingingToCeiling and verticalInput < 0: # If on ceiling, pressing down long enough unsticks from ceiling.
isClingingToCeiling = false if verticalInput < 0: timeSpentUnstickingCeiling += delta
velocity.y = 0.0 else: timeSpentUnstickingCeiling = 0.0
# If on the ceiling, stay pressed against it. if timeSpentUnstickingCeiling >= MAX_SECS_TO_UNSTICK:
# If touching the wall, stick to it or move up/down. isStickingToCeiling = false
# Else, fall normally if not on floor. velocity.y = get_gravity().y * delta
if isClingingToCeiling: else: velocity.y = CEILING_STICK_SPEED
velocity.y = CEILING_STICK_SPEED # Move up and down along a wall when sticking to it.
elif is_on_wall(): elif isStickingToWall:
velocity.y = CLIMB_SPEED * verticalInput velocity.y = CLIMB_SPEED * verticalInput
elif not is_on_floor(): elif not is_on_floor():
velocity.y += get_gravity().y * delta velocity.y += get_gravity().y * delta
# 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 pressingAwayFromWall: timeSpentUnstickingWall += delta
else: timeSpentUnstickingWall = 0.0
if timeSpentUnstickingWall >= MAX_SECS_TO_UNSTICK:
velocity.x = horizontalInput * MOVE_SPEED
else:
velocity.x = -wallNormalX * WALL_STICK_SPEED
else:
timeSpentUnstickingWall = 0.0
if horizontalInput: velocity.x = horizontalInput * MOVE_SPEED if horizontalInput: velocity.x = horizontalInput * MOVE_SPEED
else: velocity.x = move_toward(velocity.x, 0, MOVE_SPEED) else: velocity.x = move_toward(velocity.x, 0, MOVE_SPEED)
move_and_slide() move_and_slide()
# Touching the ceiling sticks us to it. Crawling off the ceiling's edge # Check if we should be sticking to walls/ceiling after moving our player.
# means we're not touching it anymore, so we start falling. isStickingToWall = is_on_wall()
isClingingToCeiling = is_on_ceiling() if isStickingToWall:
wallNormalX = get_wall_normal().x
isStickingToCeiling = is_on_ceiling()
# 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