diff --git a/Characters/player.gd b/Characters/player.gd index 5c15d25..687f38b 100755 --- a/Characters/player.gd +++ b/Characters/player.gd @@ -12,8 +12,9 @@ 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 +@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 @@ -48,15 +49,17 @@ 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 wrapOntoWallNormalX := 0.0 # 0 means we end up on a floor instead of a wall. +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 -# 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. +# 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 @@ -96,8 +99,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): + # 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. @@ -196,6 +201,13 @@ 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 @@ -218,7 +230,7 @@ func _start_lip_wrap() -> bool: 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), PI/2 * dir, dir) + 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: @@ -234,40 +246,67 @@ func _start_wall_top_wrap() -> bool: 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), 0.0, 0.0) + return _begin_wrap(corner, -PI/2, Vector2(-n * radius, 0), Vector2.UP) -# 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) +# 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 -func _try_floor_edge_wrap(dir: float) -> bool: - var sensor := rightEdgeSensor if dir > 0 else leftEdgeSensor +# 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 - # 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) + # 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. +# 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, toRotation: float, ontoWallNormalX: float, fromAngle := NAN) -> bool: +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 @@ -284,9 +323,9 @@ func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotatio wrapToAngle = toAngle wrapToDistance = radius wrapFromRotation = sprite.rotation - wrapToRotation = toRotation + wrapToRotation = ontoNormal.angle() + PI/2 wrapExitOffset = exitOffset - wrapOntoWallNormalX = ontoWallNormalX + 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) @@ -317,17 +356,14 @@ func _update_wrap(delta: float) -> void: # 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: - velocity = Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0) / delta - else: - velocity = Vector2(0, WRAP_MARGIN + 1) / delta + velocity = -wrapOntoNormal * (WRAP_MARGIN + 1) / delta move_and_slide() velocity = Vector2.ZERO isWrapping = false isClimbingLip = false - isStickingToCeiling = false - isStickingToWall = wrapOntoWallNormalX != 0.0 + isStickingToCeiling = wrapOntoNormal.y > 0.5 + isStickingToWall = absf(wrapOntoNormal.x) > 0.5 if isStickingToWall: - wallNormalX = wrapOntoWallNormalX + wallNormalX = wrapOntoNormal.x ignoreWallUnstickUntilRelease = true diff --git a/Characters/player.tscn b/Characters/player.tscn index 124552c..7796463 100755 --- a/Characters/player.tscn +++ b/Characters/player.tscn @@ -15,10 +15,12 @@ offset = Vector2(0, -6) [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1379572123] shape = SubResource("CircleShape2D_eovys") -[node name="LeftEdgeSensor" type="RayCast2D" parent="."] +[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="."] +[node name="RightEdgeSensor" type="RayCast2D" parent="EdgeSensors"] position = Vector2(24, 0) target_position = Vector2(0, 14)