Slide over floor edges onto the wall below when pressing down
Two RayCast2D edge sensors point down into the floor edgeSensorRange to each side of the slime. When one stops touching floor there's an edge in range on that side, and pressing down slides to it, swings over the corner and carries on down the wall so the slime ends flat against it. - Down + a direction picks that side; down alone goes the way the slime is facing, or the other way if only that side has an edge in range. - Down in the middle of a platform does nothing. - edgeSensorRange is exported (default 24, tuned with a headless sweep). - Wraps can now start with a straight move to the arc, and end with a move_and_slide so is_on_floor()/is_on_wall() aren't stale afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b098b78934
commit
815444f3af
2 changed files with 83 additions and 10 deletions
|
|
@ -12,6 +12,8 @@ const WALL_STICK_SPEED = 20.0 # Small push into the wall that keeps the slime pr
|
|||
# Temp for now, need to use aniamtedspite2d later on
|
||||
@onready var sprite = $Sprite2D
|
||||
@onready var collisionShape: CollisionShape2D = $CollisionShape2D
|
||||
@onready var leftEdgeSensor: RayCast2D = $LeftEdgeSensor
|
||||
@onready var rightEdgeSensor: RayCast2D = $RightEdgeSensor
|
||||
|
||||
# How long to press opposite direction before unsticking from a wall/ceiling
|
||||
const MAX_SECS_TO_UNSTICK = 0.5
|
||||
|
|
@ -48,6 +50,20 @@ var wrapArcLength := 0.0
|
|||
var wrapExitOffset := Vector2.ZERO # Straight move after the arc, onto the new surface.
|
||||
var wrapOntoWallNormalX := 0.0 # 0 means we end up on a floor instead of a wall.
|
||||
var timeSinceUpPressed := 1.0
|
||||
var wrapEntryStart := Vector2.ZERO # Where a straight move before the arc starts from.
|
||||
var wrapEntryLength := 0.0
|
||||
|
||||
# Floor edges: the edge sensors point down into the floor a little way to each
|
||||
# side. If one stops touching floor, there's an edge within edgeSensorRange on
|
||||
# that side and pressing down will slide over it onto the wall below.
|
||||
@export var edgeSensorRange := 24.0
|
||||
|
||||
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.
|
||||
|
|
@ -80,6 +96,10 @@ func _physics_process(delta: float) -> void:
|
|||
if isClimbingLip and wantsToClimb and _start_lip_wrap():
|
||||
return
|
||||
|
||||
# On a floor near its edge and pressing down: slide over onto the wall below.
|
||||
if is_on_floor() and verticalInput < 0 and _start_floor_edge_wrap(horizontalInput):
|
||||
return
|
||||
|
||||
# Quick flip sprite thing.
|
||||
if horizontalInput:
|
||||
if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0)
|
||||
|
|
@ -216,16 +236,52 @@ func _start_wall_top_wrap() -> bool:
|
|||
# After the arc, carry on onto the platform instead of perching on its corner.
|
||||
return _begin_wrap(corner, -PI/2, Vector2(-n * radius, 0), 0.0, 0.0)
|
||||
|
||||
# On a floor and pressing down: if there's an edge in range, slide to it and
|
||||
# over onto the wall below. Holding a direction picks that side, otherwise we
|
||||
# go the way we're facing, or the other way if only that side has an edge.
|
||||
func _start_floor_edge_wrap(horizontalInput: float) -> bool:
|
||||
if horizontalInput:
|
||||
return _try_floor_edge_wrap(signf(horizontalInput))
|
||||
var facing := -1.0 if sprite.flip_h else 1.0
|
||||
return _try_floor_edge_wrap(facing) or _try_floor_edge_wrap(-facing)
|
||||
|
||||
func _try_floor_edge_wrap(dir: float) -> bool:
|
||||
var sensor := rightEdgeSensor if dir > 0 else leftEdgeSensor
|
||||
sensor.force_raycast_update()
|
||||
if sensor.is_colliding(): return false
|
||||
var radius := _get_radius()
|
||||
var pos := global_position
|
||||
# Top of the floor we're on, checked from the side away from the edge.
|
||||
var floorHit := _cast_ray(pos - Vector2(dir * radius * 0.5, 0), pos + Vector2(-dir * radius * 0.5, radius * 2))
|
||||
if not floorHit or floorHit.normal.y > -0.5: return false
|
||||
var floorY: float = floorHit.position.y
|
||||
# Side of the platform, just below its top, looking back from past the sensor.
|
||||
var faceHit := _cast_ray(Vector2(pos.x + dir * (edgeSensorRange + 2), floorY + 2), Vector2(pos.x - dir * radius, floorY + 2))
|
||||
if not faceHit or faceHit.normal.x * dir < 0.5: return false
|
||||
var corner := Vector2(faceHit.position.x, floorY)
|
||||
# Slide to just above the corner, swing down beside the wall, then carry on
|
||||
# down it so our side ends up flat against it.
|
||||
return _begin_wrap(corner, Vector2(dir, 0).angle(), Vector2(0, radius), PI/2 * dir, dir, -PI/2)
|
||||
|
||||
# Starts swinging around corner to toAngle from it, then moving by exitOffset.
|
||||
# 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, toRotation: float, ontoWallNormalX: float) -> bool:
|
||||
func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotation: float, ontoWallNormalX: float, 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
|
||||
wrapFromAngle = offset.angle()
|
||||
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
|
||||
wrapFromDistance = offset.length()
|
||||
wrapToDistance = radius
|
||||
wrapFromRotation = sprite.rotation
|
||||
wrapToRotation = toRotation
|
||||
|
|
@ -233,7 +289,7 @@ func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotatio
|
|||
wrapOntoWallNormalX = ontoWallNormalX
|
||||
# Keep the same speed as crawling along a surface.
|
||||
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius
|
||||
wrapDuration = maxf((wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05)
|
||||
wrapDuration = maxf((wrapEntryLength + wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05)
|
||||
wrapProgress = 0.0
|
||||
velocity = Vector2.ZERO
|
||||
isWrapping = true
|
||||
|
|
@ -241,8 +297,14 @@ func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotatio
|
|||
|
||||
func _update_wrap(delta: float) -> void:
|
||||
wrapProgress = minf(wrapProgress + delta / wrapDuration, 1.0)
|
||||
# Progress is split between the arc and the straight exit by their lengths.
|
||||
var travelled := wrapProgress * (wrapArcLength + wrapExitOffset.length())
|
||||
# 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()
|
||||
|
|
@ -252,12 +314,15 @@ func _update_wrap(delta: float) -> void:
|
|||
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
|
||||
# and the sticking checks after the next move_and_slide pick it up.
|
||||
# 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.
|
||||
if wrapOntoWallNormalX != 0.0:
|
||||
move_and_collide(Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0))
|
||||
velocity = Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0) / delta
|
||||
else:
|
||||
move_and_collide(Vector2(0, WRAP_MARGIN + 1))
|
||||
velocity = Vector2(0, WRAP_MARGIN + 1) / delta
|
||||
move_and_slide()
|
||||
velocity = Vector2.ZERO
|
||||
|
||||
isWrapping = false
|
||||
isClimbingLip = false
|
||||
|
|
|
|||
|
|
@ -14,3 +14,11 @@ offset = Vector2(0, -6)
|
|||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1379572123]
|
||||
shape = SubResource("CircleShape2D_eovys")
|
||||
|
||||
[node name="LeftEdgeSensor" type="RayCast2D" parent="."]
|
||||
position = Vector2(-24, 0)
|
||||
target_position = Vector2(0, 14)
|
||||
|
||||
[node name="RightEdgeSensor" type="RayCast2D" parent="."]
|
||||
position = Vector2(24, 0)
|
||||
target_position = Vector2(0, 14)
|
||||
|
|
|
|||
Loading…
Reference in a new issue