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
Showing only changes of commit 935ee797dc - Show all commits

View file

@ -37,6 +37,14 @@ 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 # How long to press opposite direction before unsticking from a wall/ceiling
const MAX_SECS_TO_UNSTICK = 0.5 const MAX_SECS_TO_UNSTICK = 0.5
# Hop: holding up 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.
const HOP_MAX_HEIGHT = 64.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 isStickingToWall := false var isStickingToWall := false
var timeSpentUnstickingWall = 0.0 var timeSpentUnstickingWall = 0.0
@ -158,6 +166,14 @@ func _physics_process(delta: float) -> void:
elif not is_on_floor(): elif not is_on_floor():
velocity.y = minf(velocity.y + get_gravity().y * delta, MAX_FALL_SPEED) velocity.y = minf(velocity.y + get_gravity().y * delta, MAX_FALL_SPEED)
# On the floor, 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: 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 # 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 gets us unstuck.
if isStickingToWall and not is_on_floor(): if isStickingToWall and not is_on_floor():
@ -236,6 +252,13 @@ func _physics_process(delta: float) -> void:
_update_sprite_rotation(delta) _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 hit := move_and_collide(Vector2(0, -HOP_MAX_HEIGHT), true)
if hit and hit.get_normal().y > 0.5:
return hit.get_travel().length() + HOP_OVERSHOOT
return HOP_NO_CEILING_HEIGHT
# Moves speed towards target: GROUND_ACCEL when speeding up, GROUND_FRICTION # Moves speed towards target: GROUND_ACCEL when speeding up, GROUND_FRICTION
# when slowing down or stopping. # when slowing down or stopping.
func _accelerate(speed: float, target: float) -> float: func _accelerate(speed: float, target: float) -> float: