Smoothly wrap around ceiling lips and wall tops #2
1 changed files with 29 additions and 12 deletions
|
|
@ -44,6 +44,8 @@ var wrapFromRotation := 0.0
|
||||||
var wrapToRotation := 0.0
|
var wrapToRotation := 0.0
|
||||||
var wrapDuration := 0.0
|
var wrapDuration := 0.0
|
||||||
var wrapProgress := 0.0
|
var wrapProgress := 0.0
|
||||||
|
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 wrapOntoWallNormalX := 0.0 # 0 means we end up on a floor instead of a wall.
|
||||||
var timeSinceUpPressed := 1.0
|
var timeSinceUpPressed := 1.0
|
||||||
|
|
||||||
|
|
@ -195,7 +197,8 @@ func _start_lip_wrap() -> bool:
|
||||||
var faceHit := _cast_ray(Vector2(pos.x + dir * radius, ceilingY - 2), Vector2(pos.x - dir * radius * 2, ceilingY - 2))
|
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
|
if not faceHit or faceHit.normal.x * dir < 0.5: return false
|
||||||
var corner := Vector2(faceHit.position.x, ceilingY)
|
var corner := Vector2(faceHit.position.x, ceilingY)
|
||||||
return _begin_wrap(corner, Vector2(dir, 0).angle(), PI/2 * dir, dir)
|
# 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), PI/2 * dir, dir)
|
||||||
|
|
||||||
# Just climbed past the top of a wall: find the corner and swing up on top.
|
# Just climbed past the top of a wall: find the corner and swing up on top.
|
||||||
func _start_wall_top_wrap() -> bool:
|
func _start_wall_top_wrap() -> bool:
|
||||||
|
|
@ -210,13 +213,14 @@ func _start_wall_top_wrap() -> bool:
|
||||||
var faceHit := _cast_ray(Vector2(pos.x + n * 2, topY + 2), Vector2(pos.x - n * radius * 2, topY + 2))
|
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
|
if not faceHit or faceHit.normal.x * n < 0.5: return false
|
||||||
var corner := Vector2(faceHit.position.x, topY)
|
var corner := Vector2(faceHit.position.x, topY)
|
||||||
return _begin_wrap(corner, -PI/2, 0.0, 0.0)
|
# 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)
|
||||||
|
|
||||||
# Starts swinging around corner, ending at toAngle from it. Returns false if
|
# Starts swinging around corner to toAngle from it, then moving by exitOffset.
|
||||||
# there's no room to end up there.
|
# Returns false if there's no room to end up there.
|
||||||
func _begin_wrap(corner: Vector2, toAngle: float, toRotation: float, ontoWallNormalX: float) -> bool:
|
func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotation: float, ontoWallNormalX: float) -> bool:
|
||||||
var radius := _get_radius() + WRAP_MARGIN
|
var radius := _get_radius() + WRAP_MARGIN
|
||||||
if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius): return false
|
if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius + exitOffset): return false
|
||||||
var offset := global_position - corner
|
var offset := global_position - corner
|
||||||
wrapCorner = corner
|
wrapCorner = corner
|
||||||
wrapFromAngle = offset.angle()
|
wrapFromAngle = offset.angle()
|
||||||
|
|
@ -225,10 +229,11 @@ func _begin_wrap(corner: Vector2, toAngle: float, toRotation: float, ontoWallNor
|
||||||
wrapToDistance = radius
|
wrapToDistance = radius
|
||||||
wrapFromRotation = sprite.rotation
|
wrapFromRotation = sprite.rotation
|
||||||
wrapToRotation = toRotation
|
wrapToRotation = toRotation
|
||||||
|
wrapExitOffset = exitOffset
|
||||||
wrapOntoWallNormalX = ontoWallNormalX
|
wrapOntoWallNormalX = ontoWallNormalX
|
||||||
# Keep the same speed as crawling along a surface.
|
# Keep the same speed as crawling along a surface.
|
||||||
var arcLength := absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius
|
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius
|
||||||
wrapDuration = maxf(arcLength / GROUND_SPEED, 0.05)
|
wrapDuration = maxf((wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05)
|
||||||
wrapProgress = 0.0
|
wrapProgress = 0.0
|
||||||
velocity = Vector2.ZERO
|
velocity = Vector2.ZERO
|
||||||
isWrapping = true
|
isWrapping = true
|
||||||
|
|
@ -236,12 +241,24 @@ func _begin_wrap(corner: Vector2, toAngle: float, toRotation: float, ontoWallNor
|
||||||
|
|
||||||
func _update_wrap(delta: float) -> void:
|
func _update_wrap(delta: float) -> void:
|
||||||
wrapProgress = minf(wrapProgress + delta / wrapDuration, 1.0)
|
wrapProgress = minf(wrapProgress + delta / wrapDuration, 1.0)
|
||||||
var angle := lerp_angle(wrapFromAngle, wrapToAngle, wrapProgress)
|
# Progress is split between the arc and the straight exit by their lengths.
|
||||||
var distance := lerpf(wrapFromDistance, wrapToDistance, wrapProgress)
|
var travelled := wrapProgress * (wrapArcLength + wrapExitOffset.length())
|
||||||
global_position = wrapCorner + Vector2.from_angle(angle) * distance
|
var arcProgress := 1.0 if wrapArcLength <= 0.0 else minf(travelled / wrapArcLength, 1.0)
|
||||||
sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, wrapProgress)
|
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
|
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.
|
||||||
|
if wrapOntoWallNormalX != 0.0:
|
||||||
|
move_and_collide(Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0))
|
||||||
|
else:
|
||||||
|
move_and_collide(Vector2(0, WRAP_MARGIN + 1))
|
||||||
|
|
||||||
isWrapping = false
|
isWrapping = false
|
||||||
isClimbingLip = false
|
isClimbingLip = false
|
||||||
isStickingToCeiling = false
|
isStickingToCeiling = false
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue