diff --git a/Characters/player.gd b/Characters/player.gd index 9cd1dbc..f23e09f 100755 --- a/Characters/player.gd +++ b/Characters/player.gd @@ -50,6 +50,8 @@ 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 +# Same for down after wrapping onto a ceiling (e.g. crawling down a wall and under). +var ignoreCeilingUnstickUntilRelease := 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. @@ -79,6 +81,10 @@ var wrapEntryLength := 0.0 @export var edgeSensorRange := 24.0 var wallFacingY := -1.0 # Which way we last moved along a wall (-1 is up). +# Holding at an edge: moving along a surface towards its edge stops at the edge, +# and only lets go after holding towards it for MAX_SECS_TO_UNSTICK. +var timeSpentHoldingEdge := 0.0 + func _ready() -> void: floor_snap_length = SURFACE_SNAP_DISTANCE var sensorDepth := _get_radius() + 4 @@ -131,7 +137,8 @@ func _physics_process(delta: float) -> void: # Vertical Input Controls if isStickingToCeiling: # If on ceiling, pressing down long enough unsticks from ceiling. - if verticalInput < 0: timeSpentUnstickingCeiling += delta + if verticalInput >= 0: ignoreCeilingUnstickUntilRelease = false + if verticalInput < 0 and not ignoreCeilingUnstickUntilRelease: timeSpentUnstickingCeiling += delta else: timeSpentUnstickingCeiling = 0.0 if timeSpentUnstickingCeiling >= MAX_SECS_TO_UNSTICK: isStickingToCeiling = false @@ -170,6 +177,8 @@ func _physics_process(delta: float) -> void: else: velocity.x = _air_steer(velocity.x, horizontalInput) + _hold_at_edge(delta) + var wasOnWall := isStickingToWall and not is_on_floor() var velocityBeforeMove := velocity @@ -197,8 +206,10 @@ func _physics_process(delta: float) -> void: var wasJustOnCeiling = isStickingToCeiling isStickingToCeiling = is_on_ceiling() or (wasJustOnCeiling and _snap_to_surface(Vector2.DOWN)) # 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: + # a ceiling, letting go of it by pressing down, or after already holding + # at the edge long enough to let go. + if wasJustOnCeiling and not isStickingToCeiling and not isStickingToWall and velocity.x \ + and timeSpentHoldingEdge < MAX_SECS_TO_UNSTICK: isClimbingLip = true lipDirection = signf(velocity.x) # Made it onto the wall from the lip. @@ -351,20 +362,51 @@ func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool: # 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 corner = _find_edge_corner(normal, dir, edgeSensorRange + 2) + if corner == null: return false + # 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 * _get_radius(), dir, normal.angle()) + +# Finds the corner where the surface we're on (facing normal) ends, up to reach +# ahead of us in dir. Returns null if there's no edge that close. +func _find_edge_corner(normal: Vector2, dir: Vector2, reach: float) -> Variant: 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. + if not surfaceHit or surfaceHit.normal.dot(normal) < 0.5: return null + # Side of the platform, just inside its surface, looking back from reach + # ahead. If the surface carries on past reach, the ray starts inside it + # and finds nothing. 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()) + var faceHit := _cast_ray(line + dir * reach, line - dir * radius) + if not faceHit or faceHit.normal.dot(dir) < 0.5: return null + return faceHit.position + normal * 2 + +# If this frame's movement along the surface would take us past its edge, stop +# at the edge instead, until we've held towards it for MAX_SECS_TO_UNSTICK. +func _hold_at_edge(delta: float) -> void: + var normal := _get_surface_normal() + var tangent := normal.orthogonal() + var speed := velocity.dot(tangent) + if normal == Vector2.ZERO or is_zero_approx(speed): + timeSpentHoldingEdge = 0.0 + return + var dir := tangent * signf(speed) + # Climbing up past the top of a wall swings over onto it instead. + if absf(normal.x) > 0.5 and dir.y < 0: return + var step := absf(speed) * delta + var corner = _find_edge_corner(normal, dir, step + 1) + var distanceToEdge: float = INF if corner == null else (corner - global_position).dot(dir) + if distanceToEdge > step: + timeSpentHoldingEdge = 0.0 + return + # Held long enough: let go and carry on over the edge. + if timeSpentHoldingEdge >= MAX_SECS_TO_UNSTICK: return + timeSpentHoldingEdge += delta + velocity += dir * (maxf(distanceToEdge, 0.0) / delta - absf(speed)) # Starts swinging around corner to toAngle from it, then moving by exitOffset, # ending up stuck to the surface facing ontoNormal. @@ -441,3 +483,4 @@ func _update_wrap(delta: float) -> void: if isStickingToWall: wallNormalX = wrapOntoNormal.x ignoreWallUnstickUntilRelease = true + ignoreCeilingUnstickUntilRelease = isStickingToCeiling diff --git a/project.godot b/project.godot index 71be85a..f43494f 100755 --- a/project.godot +++ b/project.godot @@ -69,16 +69,14 @@ pause={ [physics] -common/physics_interpolation=true - 3d/physics_engine="Jolt Physics" 2d/default_gravity=700.0 +common/physics_interpolation=true [rendering] -2d/snap/snap_2d_transforms_to_pixel=true - textures/canvas_textures/default_texture_filter=0 rendering_device/driver.windows="d3d12" renderer/rendering_method="gl_compatibility" renderer/rendering_method.mobile="gl_compatibility" +2d/snap/snap_2d_transforms_to_pixel=true