The edge sensors now live under an EdgeSensors pivot that's rotated to point into whatever surface the slime is on. Pressing into the surface (down on floors, up on ceilings, towards the wall on walls) near an edge slides over it onto the side of the platform. The other axis picks the side; otherwise facing decides (last direction moved along a wall). - Ceiling edge -> wall above, wall top -> floor, wall bottom -> ceiling underneath, floor edge -> wall below, all through one code path. - Skip sensors that start inside something (inside corners). - _begin_wrap takes the normal of the surface to end up on and works out the sprite rotation from it; wraps can now end on a ceiling. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
369 lines
16 KiB
GDScript
Executable file
369 lines
16 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
|
|
@onready var collisionShape: CollisionShape2D = $CollisionShape2D
|
|
@onready var edgeSensors: Node2D = $EdgeSensors
|
|
@onready var leftEdgeSensor: RayCast2D = $EdgeSensors/LeftEdgeSensor
|
|
@onready var rightEdgeSensor: RayCast2D = $EdgeSensors/RightEdgeSensor
|
|
|
|
# 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
|
|
|
|
# Edge wrapping: instead of relying on physics to slide around a corner, the
|
|
# slime swings around it on a quarter circle so it always feels the same.
|
|
const WRAP_MARGIN = 0.5 # Extra gap from the corner so we don't clip into it.
|
|
const CLIMB_INPUT_BUFFER = 0.15 # Tapping up this long before reaching a lip still counts.
|
|
var isWrapping := false
|
|
var wrapCorner := Vector2.ZERO
|
|
var wrapFromAngle := 0.0
|
|
var wrapToAngle := 0.0
|
|
var wrapFromDistance := 0.0
|
|
var wrapToDistance := 0.0
|
|
var wrapFromRotation := 0.0
|
|
var wrapToRotation := 0.0
|
|
var wrapDuration := 0.0
|
|
var wrapProgress := 0.0
|
|
var wrapArcLength := 0.0
|
|
var wrapExitOffset := Vector2.ZERO # Straight move after the arc, onto the new surface.
|
|
var wrapOntoNormal := Vector2.UP # Normal of the surface we end up on.
|
|
var timeSinceUpPressed := 1.0
|
|
var wrapEntryStart := Vector2.ZERO # Where a straight move before the arc starts from.
|
|
var wrapEntryLength := 0.0
|
|
|
|
# Surface edges: the edge sensors are rotated to point into whatever surface
|
|
# we're on, a little way to each side of us. If one stops touching the surface,
|
|
# there's an edge within edgeSensorRange on that side, and pressing into the
|
|
# surface will slide over it onto the side of the platform.
|
|
@export var edgeSensorRange := 24.0
|
|
var wallFacingY := -1.0 # Which way we last moved along a wall (-1 is up).
|
|
|
|
func _ready() -> void:
|
|
var sensorDepth := _get_radius() + 4
|
|
leftEdgeSensor.position = Vector2(-edgeSensorRange, 0)
|
|
leftEdgeSensor.target_position = Vector2(0, sensorDepth)
|
|
rightEdgeSensor.position = Vector2(edgeSensorRange, 0)
|
|
rightEdgeSensor.target_position = Vector2(0, sensorDepth)
|
|
|
|
func _process(delta: float) -> void:
|
|
# The wrap sets the sprite rotation itself.
|
|
if isWrapping: return
|
|
|
|
# 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")
|
|
|
|
if Input.is_action_just_pressed("moveUp"): timeSinceUpPressed = 0.0
|
|
else: timeSinceUpPressed += delta
|
|
|
|
# Swinging around a corner, ignore everything else until we're done.
|
|
if isWrapping:
|
|
_update_wrap(delta)
|
|
return
|
|
|
|
# Hanging on a lip and pressing up: swing around onto the wall above.
|
|
var wantsToClimb := verticalInput > 0 or timeSinceUpPressed <= CLIMB_INPUT_BUFFER
|
|
if isClimbingLip and wantsToClimb and _start_lip_wrap():
|
|
return
|
|
|
|
# Near the edge of a surface and pressing into it: slide over onto the
|
|
# side of the platform.
|
|
if isStickingToWall and verticalInput: wallFacingY = -signf(verticalInput)
|
|
if _start_edge_wrap(horizontalInput, verticalInput):
|
|
return
|
|
|
|
# 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)
|
|
|
|
var wasOnWall := isStickingToWall and not is_on_floor()
|
|
|
|
move_and_slide()
|
|
|
|
# Check if we should be sticking to walls/ceiling after moving our player.
|
|
isStickingToWall = is_on_wall()
|
|
# Climbed past the top of a wall: swing over onto the floor on top.
|
|
if wasOnWall and not isStickingToWall and verticalInput > 0 \
|
|
and timeSpentUnstickingWall < MAX_SECS_TO_UNSTICK and _start_wall_top_wrap():
|
|
return
|
|
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
|
|
|
|
func _get_radius() -> float:
|
|
return (collisionShape.shape as CircleShape2D).radius
|
|
|
|
func _cast_ray(from: Vector2, to: Vector2) -> Dictionary:
|
|
var query := PhysicsRayQueryParameters2D.create(from, to, collision_mask, [get_rid()])
|
|
return get_world_2d().direct_space_state.intersect_ray(query)
|
|
|
|
func _is_point_free(point: Vector2) -> bool:
|
|
var query := PhysicsPointQueryParameters2D.new()
|
|
query.position = point
|
|
query.collision_mask = collision_mask
|
|
query.exclude = [get_rid()]
|
|
return get_world_2d().direct_space_state.intersect_point(query, 1).is_empty()
|
|
|
|
func _is_spot_free(spot: Vector2) -> bool:
|
|
var query := PhysicsShapeQueryParameters2D.new()
|
|
query.shape = collisionShape.shape
|
|
query.transform = Transform2D(0.0, spot)
|
|
query.collision_mask = collision_mask
|
|
query.exclude = [get_rid()]
|
|
return get_world_2d().direct_space_state.intersect_shape(query, 1).is_empty()
|
|
|
|
# Just went over a ceiling edge: find the corner and swing up beside the wall.
|
|
func _start_lip_wrap() -> bool:
|
|
var dir := lipDirection
|
|
var radius := _get_radius()
|
|
var pos := global_position
|
|
# Bottom of the platform we just came off of.
|
|
var ceilingHit := _cast_ray(pos - Vector2(dir * (radius + 2), 0), pos - Vector2(dir * (radius + 2), radius * 2))
|
|
if not ceilingHit or ceilingHit.normal.y < 0.5: return false
|
|
var ceilingY: float = ceilingHit.position.y
|
|
# Side of the platform, just above its bottom.
|
|
var faceHit := _cast_ray(Vector2(pos.x + dir * radius, ceilingY - 2), Vector2(pos.x - dir * radius * 2, ceilingY - 2))
|
|
if not faceHit or faceHit.normal.x * dir < 0.5: return false
|
|
var corner := Vector2(faceHit.position.x, ceilingY)
|
|
# After the arc, carry on up the wall so our side is flat against it.
|
|
return _begin_wrap(corner, Vector2(dir, 0).angle(), Vector2(0, -radius), Vector2(dir, 0))
|
|
|
|
# Just climbed past the top of a wall: find the corner and swing up on top.
|
|
func _start_wall_top_wrap() -> bool:
|
|
var n := wallNormalX
|
|
var radius := _get_radius()
|
|
var pos := global_position
|
|
# Top of the platform, just past the wall.
|
|
var topHit := _cast_ray(pos + Vector2(-n * (radius + 2), -radius), pos + Vector2(-n * (radius + 2), radius * 2))
|
|
if not topHit or topHit.normal.y > -0.5: return false
|
|
var topY: float = topHit.position.y
|
|
# Side of the platform, just below its top.
|
|
var faceHit := _cast_ray(Vector2(pos.x + n * 2, topY + 2), Vector2(pos.x - n * radius * 2, topY + 2))
|
|
if not faceHit or faceHit.normal.x * n < 0.5: return false
|
|
var corner := Vector2(faceHit.position.x, topY)
|
|
# After the arc, carry on onto the platform instead of perching on its corner.
|
|
return _begin_wrap(corner, -PI/2, Vector2(-n * radius, 0), Vector2.UP)
|
|
|
|
# Normal of the surface we're stuck to, or zero if we're in the air.
|
|
func _get_surface_normal() -> Vector2:
|
|
if is_on_floor(): return Vector2.UP
|
|
if isStickingToCeiling: return Vector2.DOWN
|
|
if isStickingToWall: return Vector2(wallNormalX, 0)
|
|
return Vector2.ZERO
|
|
|
|
# Which way the slime is facing, in world space.
|
|
func _get_facing() -> Vector2:
|
|
if is_on_floor(): return Vector2(-1 if sprite.flip_h else 1, 0)
|
|
if isStickingToCeiling: return Vector2(1 if sprite.flip_h else -1, 0)
|
|
return Vector2(0, wallFacingY)
|
|
|
|
# Pressing into the surface we're on: if there's an edge in range, slide to it
|
|
# and over onto the side of the platform. Holding a direction along the surface
|
|
# picks that side, otherwise we go the way we're facing, or the other way if
|
|
# only that side has an edge.
|
|
func _start_edge_wrap(horizontalInput: float, verticalInput: float) -> bool:
|
|
var normal := _get_surface_normal()
|
|
if normal == Vector2.ZERO: return false
|
|
var input := Vector2(horizontalInput, -verticalInput)
|
|
if input.dot(-normal) < 0.5: return false
|
|
# Point the sensors into the surface. Their local +x runs along it.
|
|
edgeSensors.rotation = normal.angle() + PI/2
|
|
var tangent := Vector2.from_angle(edgeSensors.rotation)
|
|
var along := input.dot(tangent)
|
|
if absf(along) >= 0.5:
|
|
return _try_edge_wrap(normal, tangent, signf(along))
|
|
var facing := 1.0 if _get_facing().dot(tangent) >= 0 else -1.0
|
|
return _try_edge_wrap(normal, tangent, facing) or _try_edge_wrap(normal, tangent, -facing)
|
|
|
|
func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool:
|
|
var sensor := rightEdgeSensor if side > 0 else leftEdgeSensor
|
|
sensor.force_raycast_update()
|
|
if sensor.is_colliding(): return false
|
|
# A sensor starting inside something is an inside corner, not an edge.
|
|
if not _is_point_free(sensor.global_position): return false
|
|
var dir := tangent * side
|
|
var radius := _get_radius()
|
|
var pos := global_position
|
|
# The surface we're on, checked from the side away from the edge.
|
|
var surfaceFrom := pos - dir * radius * 0.5
|
|
var surfaceHit := _cast_ray(surfaceFrom, surfaceFrom - normal * radius * 2)
|
|
if not surfaceHit or surfaceHit.normal.dot(normal) < 0.5: return false
|
|
# Side of the platform, just inside its surface, looking back from past the sensor.
|
|
var line: Vector2 = surfaceHit.position - normal * 2 + dir * (pos - surfaceHit.position).dot(dir)
|
|
var faceHit := _cast_ray(line + dir * (edgeSensorRange + 2), line - dir * radius)
|
|
if not faceHit or faceHit.normal.dot(dir) < 0.5: return false
|
|
var corner: Vector2 = faceHit.position + normal * 2
|
|
# Slide to just past the corner, swing around it, then carry on along the
|
|
# side of the platform so we end up flat against it.
|
|
return _begin_wrap(corner, dir.angle(), -normal * radius, dir, normal.angle())
|
|
|
|
# Starts swinging around corner to toAngle from it, then moving by exitOffset,
|
|
# ending up stuck to the surface facing ontoNormal.
|
|
# If fromAngle is given, first moves straight to that point on the arc,
|
|
# otherwise the arc starts from where we are now.
|
|
# Returns false if there's no room to end up there.
|
|
func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, ontoNormal: Vector2, fromAngle := NAN) -> bool:
|
|
var radius := _get_radius() + WRAP_MARGIN
|
|
if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius + exitOffset): return false
|
|
var offset := global_position - corner
|
|
wrapCorner = corner
|
|
wrapEntryStart = global_position
|
|
if is_nan(fromAngle):
|
|
wrapFromAngle = offset.angle()
|
|
wrapFromDistance = offset.length()
|
|
wrapEntryLength = 0.0
|
|
else:
|
|
wrapFromAngle = fromAngle
|
|
wrapFromDistance = radius
|
|
wrapEntryLength = global_position.distance_to(corner + Vector2.from_angle(fromAngle) * radius)
|
|
wrapToAngle = toAngle
|
|
wrapToDistance = radius
|
|
wrapFromRotation = sprite.rotation
|
|
wrapToRotation = ontoNormal.angle() + PI/2
|
|
wrapExitOffset = exitOffset
|
|
wrapOntoNormal = ontoNormal
|
|
# Keep the same speed as crawling along a surface.
|
|
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius
|
|
wrapDuration = maxf((wrapEntryLength + wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05)
|
|
wrapProgress = 0.0
|
|
velocity = Vector2.ZERO
|
|
isWrapping = true
|
|
return true
|
|
|
|
func _update_wrap(delta: float) -> void:
|
|
wrapProgress = minf(wrapProgress + delta / wrapDuration, 1.0)
|
|
# Progress is split between the straight entry, the arc and the straight
|
|
# exit by their lengths.
|
|
var travelled := wrapProgress * (wrapEntryLength + wrapArcLength + wrapExitOffset.length())
|
|
var arcStart := wrapCorner + Vector2.from_angle(wrapFromAngle) * wrapFromDistance
|
|
if travelled < wrapEntryLength:
|
|
global_position = wrapEntryStart.lerp(arcStart, travelled / wrapEntryLength)
|
|
return
|
|
travelled -= wrapEntryLength
|
|
var arcProgress := 1.0 if wrapArcLength <= 0.0 else minf(travelled / wrapArcLength, 1.0)
|
|
var exitProgress := 0.0 if wrapExitOffset == Vector2.ZERO \
|
|
else maxf(travelled - wrapArcLength, 0.0) / wrapExitOffset.length()
|
|
var angle := lerp_angle(wrapFromAngle, wrapToAngle, arcProgress)
|
|
var distance := lerpf(wrapFromDistance, wrapToDistance, arcProgress)
|
|
global_position = wrapCorner + Vector2.from_angle(angle) * distance + wrapExitOffset * exitProgress
|
|
sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, arcProgress)
|
|
if wrapProgress < 1.0: return
|
|
|
|
# Close the small gap left by WRAP_MARGIN so we're touching the new surface.
|
|
# 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
|
|
move_and_slide()
|
|
velocity = Vector2.ZERO
|
|
|
|
isWrapping = false
|
|
isClimbingLip = false
|
|
isStickingToCeiling = wrapOntoNormal.y > 0.5
|
|
isStickingToWall = absf(wrapOntoNormal.x) > 0.5
|
|
if isStickingToWall:
|
|
wallNormalX = wrapOntoNormal.x
|
|
ignoreWallUnstickUntilRelease = true
|