Compare commits
No commits in common. "f9c20f4de919e5d216a5bdbcba6cfe1b5b3d4603" and "0f24145f49450e15b11f4573c88086dadb6fa164" have entirely different histories.
f9c20f4de9
...
0f24145f49
2 changed files with 21 additions and 132 deletions
|
|
@ -12,9 +12,6 @@ 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
|
# Temp for now, need to use aniamtedspite2d later on
|
||||||
@onready var sprite = $Sprite2D
|
@onready var sprite = $Sprite2D
|
||||||
@onready var collisionShape: CollisionShape2D = $CollisionShape2D
|
@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
|
# 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
|
||||||
|
|
@ -49,24 +46,8 @@ var wrapDuration := 0.0
|
||||||
var wrapProgress := 0.0
|
var wrapProgress := 0.0
|
||||||
var wrapArcLength := 0.0
|
var wrapArcLength := 0.0
|
||||||
var wrapExitOffset := Vector2.ZERO # Straight move after the arc, onto the new surface.
|
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 wrapOntoWallNormalX := 0.0 # 0 means we end up on a floor instead of a wall.
|
||||||
var timeSinceUpPressed := 1.0
|
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:
|
func _process(delta: float) -> void:
|
||||||
# The wrap sets the sprite rotation itself.
|
# The wrap sets the sprite rotation itself.
|
||||||
|
|
@ -99,12 +80,6 @@ func _physics_process(delta: float) -> void:
|
||||||
if isClimbingLip and wantsToClimb and _start_lip_wrap():
|
if isClimbingLip and wantsToClimb and _start_lip_wrap():
|
||||||
return
|
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.
|
# Quick flip sprite thing.
|
||||||
if horizontalInput:
|
if horizontalInput:
|
||||||
if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0)
|
if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0)
|
||||||
|
|
@ -201,13 +176,6 @@ func _cast_ray(from: Vector2, to: Vector2) -> Dictionary:
|
||||||
var query := PhysicsRayQueryParameters2D.create(from, to, collision_mask, [get_rid()])
|
var query := PhysicsRayQueryParameters2D.create(from, to, collision_mask, [get_rid()])
|
||||||
return get_world_2d().direct_space_state.intersect_ray(query)
|
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:
|
func _is_spot_free(spot: Vector2) -> bool:
|
||||||
var query := PhysicsShapeQueryParameters2D.new()
|
var query := PhysicsShapeQueryParameters2D.new()
|
||||||
query.shape = collisionShape.shape
|
query.shape = collisionShape.shape
|
||||||
|
|
@ -230,7 +198,7 @@ func _start_lip_wrap() -> bool:
|
||||||
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)
|
||||||
# After the arc, carry on up the wall so our side is flat against it.
|
# 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))
|
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:
|
||||||
|
|
@ -246,89 +214,26 @@ func _start_wall_top_wrap() -> bool:
|
||||||
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)
|
||||||
# After the arc, carry on onto the platform instead of perching on its corner.
|
# 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)
|
return _begin_wrap(corner, -PI/2, Vector2(-n * radius, 0), 0.0, 0.0)
|
||||||
|
|
||||||
# Normal of the surface we're stuck to, or zero if we're in the air.
|
# Starts swinging around corner to toAngle from it, then moving by exitOffset.
|
||||||
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.
|
# 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:
|
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 + exitOffset): 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
|
||||||
wrapEntryStart = global_position
|
wrapFromAngle = offset.angle()
|
||||||
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
|
wrapToAngle = toAngle
|
||||||
|
wrapFromDistance = offset.length()
|
||||||
wrapToDistance = radius
|
wrapToDistance = radius
|
||||||
wrapFromRotation = sprite.rotation
|
wrapFromRotation = sprite.rotation
|
||||||
wrapToRotation = ontoNormal.angle() + PI/2
|
wrapToRotation = toRotation
|
||||||
wrapExitOffset = exitOffset
|
wrapExitOffset = exitOffset
|
||||||
wrapOntoNormal = ontoNormal
|
wrapOntoWallNormalX = ontoWallNormalX
|
||||||
# Keep the same speed as crawling along a surface.
|
# Keep the same speed as crawling along a surface.
|
||||||
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius
|
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius
|
||||||
wrapDuration = maxf((wrapEntryLength + wrapArcLength + exitOffset.length()) / 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
|
||||||
|
|
@ -336,14 +241,8 @@ func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, ontoNorma
|
||||||
|
|
||||||
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)
|
||||||
# Progress is split between the straight entry, the arc and the straight
|
# Progress is split between the arc and the straight exit by their lengths.
|
||||||
# exit by their lengths.
|
var travelled := wrapProgress * (wrapArcLength + wrapExitOffset.length())
|
||||||
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 arcProgress := 1.0 if wrapArcLength <= 0.0 else minf(travelled / wrapArcLength, 1.0)
|
||||||
var exitProgress := 0.0 if wrapExitOffset == Vector2.ZERO \
|
var exitProgress := 0.0 if wrapExitOffset == Vector2.ZERO \
|
||||||
else maxf(travelled - wrapArcLength, 0.0) / wrapExitOffset.length()
|
else maxf(travelled - wrapArcLength, 0.0) / wrapExitOffset.length()
|
||||||
|
|
@ -353,17 +252,17 @@ func _update_wrap(delta: float) -> void:
|
||||||
sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, arcProgress)
|
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.
|
# 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
|
# and the sticking checks after the next move_and_slide pick it up.
|
||||||
# instead of still describing where we were before the wrap.
|
if wrapOntoWallNormalX != 0.0:
|
||||||
velocity = -wrapOntoNormal * (WRAP_MARGIN + 1) / delta
|
move_and_collide(Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0))
|
||||||
move_and_slide()
|
else:
|
||||||
velocity = Vector2.ZERO
|
move_and_collide(Vector2(0, WRAP_MARGIN + 1))
|
||||||
|
|
||||||
isWrapping = false
|
isWrapping = false
|
||||||
isClimbingLip = false
|
isClimbingLip = false
|
||||||
isStickingToCeiling = wrapOntoNormal.y > 0.5
|
isStickingToCeiling = false
|
||||||
isStickingToWall = absf(wrapOntoNormal.x) > 0.5
|
isStickingToWall = wrapOntoWallNormalX != 0.0
|
||||||
if isStickingToWall:
|
if isStickingToWall:
|
||||||
wallNormalX = wrapOntoNormal.x
|
wallNormalX = wrapOntoWallNormalX
|
||||||
ignoreWallUnstickUntilRelease = true
|
ignoreWallUnstickUntilRelease = true
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,3 @@ offset = Vector2(0, -6)
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1379572123]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1379572123]
|
||||||
shape = SubResource("CircleShape2D_eovys")
|
shape = SubResource("CircleShape2D_eovys")
|
||||||
|
|
||||||
[node name="EdgeSensors" type="Node2D" parent="."]
|
|
||||||
|
|
||||||
[node name="LeftEdgeSensor" type="RayCast2D" parent="EdgeSensors"]
|
|
||||||
position = Vector2(-24, 0)
|
|
||||||
target_position = Vector2(0, 14)
|
|
||||||
|
|
||||||
[node name="RightEdgeSensor" type="RayCast2D" parent="EdgeSensors"]
|
|
||||||
position = Vector2(24, 0)
|
|
||||||
target_position = Vector2(0, 14)
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue