Hop across to a surface within a tile (floor, walls and ceilings) #5

Merged
SnekInAScarf merged 5 commits from floor-hop into main 2026-10-31 20:47:40 -05:00

View file

@ -1,5 +1,8 @@
extends CharacterBody2D
# Note: should check if i want unsticking to occur while moving on ceiling or wall.
# so far it isn't the case with ground movement.
const GROUND_SPEED = 150.0
const AIR_SPEED = 75.0
@ -37,6 +40,18 @@ const MIN_LAND_SPEED = 60.0 # Slower impacts than this don't splat.
# How long to press opposite direction before unsticking from a wall/ceiling
const MAX_SECS_TO_UNSTICK = 0.5
# Hop: holding up (and not left/right) on the floor for MAX_SECS_TO_UNSTICK hops just high enough
# to reach a ceiling up to HOP_MAX_HEIGHT above us (measured from our top).
# With no ceiling in reach it's just a little HOP_NO_CEILING_HEIGHT hop.
# Walls and ceilings work the same way with the direction away from them (also
# only while not moving along them), but with nothing in reach we let go as
# usual instead of doing a little hop.
const HOP_MAX_HEIGHT = 32.0
const HOP_NO_CEILING_HEIGHT = 16.0
const HOP_OVERSHOOT = 2.0 # Aim slightly past the ceiling so we still hit it with some speed.
var timeSpentHoldingUpOnFloor := 0.0
var isHoppingAcross := false # Hopping from a wall to the one opposite: no air steering until we land.
var isStickingToWall := false
var timeSpentUnstickingWall = 0.0
@ -121,6 +136,7 @@ func _physics_process(delta: float) -> void:
return
var wasInAir := _get_surface_normal() == Vector2.ZERO and not isClimbingLip
if not wasInAir: isHoppingAcross = false
# Hanging on a lip and pressing up: swing around onto the wall above.
var wantsToClimb := verticalInput > 0 or timeSinceUpPressed <= CLIMB_INPUT_BUFFER
@ -140,13 +156,18 @@ func _physics_process(delta: float) -> void:
# Vertical Input Controls
if isStickingToCeiling:
# If on ceiling, pressing down long enough unsticks from ceiling.
# If on ceiling, pressing down long enough (while not moving along it)
# unsticks from ceiling.
if verticalInput >= 0: ignoreCeilingUnstickUntilRelease = false
if verticalInput < 0 and not ignoreCeilingUnstickUntilRelease: timeSpentUnstickingCeiling += delta
if verticalInput < 0 and not horizontalInput and not ignoreCeilingUnstickUntilRelease:
timeSpentUnstickingCeiling += delta
else: timeSpentUnstickingCeiling = 0.0
if timeSpentUnstickingCeiling >= MAX_SECS_TO_UNSTICK:
isStickingToCeiling = false
velocity.y = get_gravity().y * delta
# With a floor within reach below: hop down onto it. Otherwise just let go.
var floorDistance := _get_hop_distance(Vector2.DOWN)
if floorDistance >= 0.0: velocity.y = sqrt(2.0 * get_gravity().y * floorDistance)
else: 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:
@ -158,15 +179,29 @@ func _physics_process(delta: float) -> void:
elif not is_on_floor():
velocity.y = minf(velocity.y + get_gravity().y * delta, MAX_FALL_SPEED)
# On the floor and standing still, holding up long enough hops up to the
# ceiling above. Not against a wall though, where up climbs it instead.
if is_on_floor() and not isStickingToWall and verticalInput > 0 and not horizontalInput:
timeSpentHoldingUpOnFloor += delta
else: timeSpentHoldingUpOnFloor = 0.0
if timeSpentHoldingUpOnFloor >= MAX_SECS_TO_UNSTICK:
timeSpentHoldingUpOnFloor = 0.0
velocity.y = -sqrt(2.0 * get_gravity().y * _get_hop_height())
# Horizontal Input Controls
# If sticking to a wall, pressing away from it long enough gets us unstuck.
# If sticking to a wall, pressing away from it long enough (while not
# climbing) 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
if pressingAwayFromWall and not verticalInput and not ignoreWallUnstickUntilRelease:
timeSpentUnstickingWall += delta
else: timeSpentUnstickingWall = 0.0
if timeSpentUnstickingWall >= MAX_SECS_TO_UNSTICK:
velocity.x = horizontalInput * GROUND_SPEED
# With a wall within reach opposite: hop across to it. Otherwise just let go.
var wallDistance := _get_hop_distance(Vector2(wallNormalX, 0))
if wallDistance >= 0.0: _hop_across(wallDistance)
else: velocity.x = horizontalInput * GROUND_SPEED
else:
velocity.x = -wallNormalX * WALL_STICK_SPEED
# Otherwise move horizontally based on what we're on.
@ -178,7 +213,7 @@ func _physics_process(delta: float) -> void:
velocity.x = -lipDirection * WALL_STICK_SPEED if verticalInput > 0 else 0.0
elif is_on_floor() or isStickingToCeiling:
velocity.x = _accelerate(velocity.x, horizontalInput * GROUND_SPEED)
else:
elif not isHoppingAcross:
velocity.x = _air_steer(velocity.x, horizontalInput)
_hold_at_edge(delta)
@ -236,6 +271,27 @@ func _physics_process(delta: float) -> void:
_update_sprite_rotation(delta)
# How high to hop: up to the ceiling above us if there's one in reach.
func _get_hop_height() -> float:
var distance := _get_hop_distance(Vector2.UP)
return distance if distance >= 0.0 else HOP_NO_CEILING_HEIGHT
# How far to hop in direction to reach a surface facing us within
# HOP_MAX_HEIGHT (plus HOP_OVERSHOOT), or -1 if there isn't one.
func _get_hop_distance(direction: Vector2) -> float:
var hit := move_and_collide(direction * HOP_MAX_HEIGHT, true)
if hit and hit.get_normal().dot(-direction) > 0.5:
return hit.get_travel().length() + HOP_OVERSHOOT
return -1.0
# Hops from the wall we're on to the one distance away opposite, at crawling
# speed, on an arc that lands at the same height we left from.
func _hop_across(distance: float) -> void:
var flightTime := distance / GROUND_SPEED
velocity.x = wallNormalX * GROUND_SPEED
velocity.y = -0.5 * get_gravity().y * flightTime
isHoppingAcross = true
# Moves speed towards target: GROUND_ACCEL when speeding up, GROUND_FRICTION
# when slowing down or stopping.
func _accelerate(speed: float, target: float) -> float:
@ -477,7 +533,18 @@ func _update_wrap(delta: float) -> void:
# Done with move_and_slide so is_on_floor()/is_on_wall() are up to date
# instead of still describing where we were before the wrap.
velocity = -wrapOntoNormal * (WRAP_MARGIN + 1) / delta
# Godot still thinks we're on whatever floor we were on before the wrap, so
# it would try to snap us down onto it, and (floor_block_on_wall) count the
# wall we're pushing into as floor. Only allow that if we're ending on a floor.
var endingOnFloor := wrapOntoNormal == Vector2.UP
var floorSnap := floor_snap_length
var blockOnWall := floor_block_on_wall
if not endingOnFloor:
floor_snap_length = 0.0
floor_block_on_wall = false
move_and_slide()
floor_snap_length = floorSnap
floor_block_on_wall = blockOnWall
# Carry on at the same speed along the new surface, so leaving the wrap
# isn't a sudden stop. Normal movement then speeds up or slows down from here.
var exitDirection := wrapExitOffset.normalized() if wrapExitOffset != Vector2.ZERO \