Some adjustments to naming and comments to player.gd
This commit is contained in:
parent
8fe449099b
commit
cfea34861f
1 changed files with 16 additions and 60 deletions
|
|
@ -1,22 +1,12 @@
|
|||
extends CharacterBody2D
|
||||
|
||||
# The slime crawls on floors, walls and ceilings and sticks to them.
|
||||
# - Let go: hold away from a wall or ceiling for HOLD_TIME.
|
||||
# - Edge hold: crawling to an edge stops there until you've held towards it for HOLD_TIME.
|
||||
# - Edge wrap: near an edge, press into the surface to swing around the corner.
|
||||
# - Hop: standing still, hold away from the surface for HOLD_TIME to hop to one within a tile.
|
||||
#
|
||||
# Terms:
|
||||
# - Godot's y axis points DOWN, so up is negative y (Vector2.UP is (0, -1)).
|
||||
# - Normal: arrow pointing straight out of a surface.
|
||||
# - Tangent: arrow pointing along a surface.
|
||||
# - a.dot(b): how much two arrows point the same way (1 same, 0 sideways, -1 opposite).
|
||||
# Generally the player press away from a surface while holding still to unstick.
|
||||
|
||||
|
||||
# ---- Settings ----
|
||||
|
||||
# Speeds are positive; the direction is applied where they're used.
|
||||
const GROUND_SPEED = 150.0 # Crawling on floors, walls and ceilings (pixels per second).
|
||||
###############
|
||||
## Constants ##
|
||||
###############
|
||||
const GROUND_SPEED = 150.0
|
||||
const AIR_SPEED = 75.0
|
||||
const CLIMB_SPEED = 150.0
|
||||
|
||||
|
|
@ -38,11 +28,11 @@ const SURFACE_SNAP_DISTANCE = 4.0 # Drift off a wall/ceiling by up to this and w
|
|||
const HOLD_TIME = 0.5
|
||||
|
||||
# How close an edge must be (from our center) for an edge wrap.
|
||||
@export var edgeSensorRange := 12.0
|
||||
const EDGE_SENSOR_RANGE := 12.0
|
||||
const EDGE_SENSOR_DEPTH = 4.0 # How far past our radius the sensors reach into the surface.
|
||||
const WRAP_MARGIN = 0.5 # Extra gap from the corner so we don't clip into it.
|
||||
|
||||
const HOP_REACH = 32.0 # One tile.
|
||||
const HOP_REACH = 32.0
|
||||
const HOP_NO_CEILING_HEIGHT = 16.0 # Floor hop with nothing in reach, so you can tell it worked.
|
||||
const HOP_OVERSHOOT = 2.0 # Aim slightly past the surface so we arrive with some speed and stick.
|
||||
|
||||
|
|
@ -51,10 +41,7 @@ const MIN_LAND_SPEED = 60.0 # Slower landings don't splat.
|
|||
const LAND_PITCH_MIN = 0.87
|
||||
const LAND_PITCH_MAX = 1.15
|
||||
|
||||
|
||||
# ---- State ----
|
||||
|
||||
# For the floor we use Godot's is_on_floor().
|
||||
var isStickingToWall := false
|
||||
var wallNormalX := 0.0 # -1 if the wall faces left, 1 if it faces right.
|
||||
var isStickingToCeiling := false
|
||||
|
|
@ -112,16 +99,15 @@ func _ready() -> void:
|
|||
floor_snap_length = SURFACE_SNAP_DISTANCE
|
||||
|
||||
var sensorLength := _get_radius() + EDGE_SENSOR_DEPTH
|
||||
leftEdgeSensor.position = Vector2(-edgeSensorRange, 0)
|
||||
leftEdgeSensor.position = Vector2(-EDGE_SENSOR_RANGE, 0)
|
||||
leftEdgeSensor.target_position = Vector2(0, sensorLength)
|
||||
rightEdgeSensor.position = Vector2(edgeSensorRange, 0)
|
||||
rightEdgeSensor.position = Vector2(EDGE_SENSOR_RANGE, 0)
|
||||
rightEdgeSensor.target_position = Vector2(0, sensorLength)
|
||||
|
||||
# Raycasts only ignore their parent, which is the EdgeSensors pivot, not us.
|
||||
leftEdgeSensor.add_exception(self)
|
||||
rightEdgeSensor.add_exception(self)
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
var xInput := Input.get_axis("moveLeft", "moveRight")
|
||||
var yInput := Input.get_axis("moveDown", "moveUp")
|
||||
|
|
@ -178,7 +164,6 @@ func _physics_process(delta: float) -> void:
|
|||
|
||||
_update_sprite_rotation(delta)
|
||||
|
||||
|
||||
# ---- Movement steps (in the order _physics_process runs them) ----
|
||||
|
||||
func _update_facing(xInput: float, yInput: float) -> void:
|
||||
|
|
@ -213,7 +198,6 @@ func _update_vertical_movement(delta: float, xInput: float, yInput: float) -> vo
|
|||
elif not is_on_floor():
|
||||
velocity.y = minf(velocity.y + get_gravity().y * delta, MAX_FALL_SPEED)
|
||||
|
||||
|
||||
# Holding down (not moving) for HOLD_TIME lets go, hopping onto a floor if one's in reach.
|
||||
func _update_ceiling_letting_go(delta: float, xInput: float, yInput: float) -> void:
|
||||
if yInput >= 0:
|
||||
|
|
@ -237,13 +221,9 @@ func _update_ceiling_letting_go(delta: float, xInput: float, yInput: float) -> v
|
|||
else:
|
||||
velocity.y = get_gravity().y * delta
|
||||
|
||||
|
||||
# Holding up (not moving) for HOLD_TIME hops to the ceiling. Not against a wall,
|
||||
# where up climbs it instead.
|
||||
# Hopping from the floop while holding still.
|
||||
func _update_floor_hop(delta: float, xInput: float, yInput: float) -> void:
|
||||
var holdingUp := yInput > 0
|
||||
var standingStill := xInput == 0.0
|
||||
if is_on_floor() and not isStickingToWall and holdingUp and standingStill:
|
||||
if is_on_floor() and not isStickingToWall and xInput == 0.0 and yInput > 0:
|
||||
timeSpentHoldingUpOnFloor += delta
|
||||
else:
|
||||
timeSpentHoldingUpOnFloor = 0.0
|
||||
|
|
@ -252,7 +232,6 @@ func _update_floor_hop(delta: float, xInput: float, yInput: float) -> void:
|
|||
timeSpentHoldingUpOnFloor = 0.0
|
||||
velocity.y = -_get_hop_speed(_get_hop_height())
|
||||
|
||||
|
||||
func _update_horizontal_movement(delta: float, xInput: float, yInput: float) -> void:
|
||||
if isStickingToWall and not is_on_floor():
|
||||
_update_wall_letting_go(delta, xInput, yInput)
|
||||
|
|
@ -264,7 +243,6 @@ func _update_horizontal_movement(delta: float, xInput: float, yInput: float) ->
|
|||
elif not isHoppingAcross:
|
||||
velocity.x = _air_steer(velocity.x, xInput, delta)
|
||||
|
||||
|
||||
# Holding away (not climbing) for HOLD_TIME hops to the opposite wall if one's
|
||||
# in reach, otherwise lets go.
|
||||
func _update_wall_letting_go(delta: float, xInput: float, yInput: float) -> void:
|
||||
|
|
@ -288,7 +266,6 @@ func _update_wall_letting_go(delta: float, xInput: float, yInput: float) -> void
|
|||
else:
|
||||
velocity.x = xInput * GROUND_SPEED
|
||||
|
||||
|
||||
func _update_wall_sticking(wasOnWall: bool) -> void:
|
||||
var meantToLetGo := timeSpentUnstickingWall >= HOLD_TIME
|
||||
var driftedBackOn := false
|
||||
|
|
@ -299,7 +276,6 @@ func _update_wall_sticking(wasOnWall: bool) -> void:
|
|||
if isStickingToWall:
|
||||
wallNormalX = get_wall_normal().x
|
||||
|
||||
|
||||
func _update_ceiling_sticking() -> void:
|
||||
var wasOnCeiling := isStickingToCeiling
|
||||
var driftedBackOn := false
|
||||
|
|
@ -307,7 +283,6 @@ func _update_ceiling_sticking() -> void:
|
|||
driftedBackOn = _snap_to_surface(Vector2.DOWN)
|
||||
isStickingToCeiling = is_on_ceiling() or driftedBackOn
|
||||
|
||||
|
||||
func _update_sprite_rotation(delta: float) -> void:
|
||||
var targetRotation := 0.0
|
||||
if isStickingToWall and not is_on_floor():
|
||||
|
|
@ -319,7 +294,6 @@ func _update_sprite_rotation(delta: float) -> void:
|
|||
var turnAmount := 1.0 - exp(-ROTATE_TO_SURFACE_SPEED * delta)
|
||||
sprite.rotation = lerp_angle(sprite.rotation, targetRotation, turnAmount)
|
||||
|
||||
|
||||
# ---- Movement helpers ----
|
||||
|
||||
# GROUND_ACCEL when speeding up, GROUND_FRICTION when slowing, stopping or turning.
|
||||
|
|
@ -329,7 +303,6 @@ func _accelerate(speed: float, targetSpeed: float, delta: float) -> float:
|
|||
var rate := GROUND_ACCEL if speedingUp else GROUND_FRICTION
|
||||
return move_toward(speed, targetSpeed, rate * delta)
|
||||
|
||||
|
||||
# Extra speed from leaving a surface fades slowly unless we steer against it.
|
||||
func _air_steer(speed: float, xInput: float, delta: float) -> float:
|
||||
var targetSpeed := xInput * AIR_SPEED
|
||||
|
|
@ -339,7 +312,6 @@ func _air_steer(speed: float, xInput: float, delta: float) -> float:
|
|||
return move_toward(speed, targetSpeed, AIR_DRAG * delta)
|
||||
return move_toward(speed, targetSpeed, AIR_ACCEL * delta)
|
||||
|
||||
|
||||
# Moves flush against the surface with this normal if it's within SURFACE_SNAP_DISTANCE.
|
||||
func _snap_to_surface(normal: Vector2) -> bool:
|
||||
var hit := move_and_collide(-normal * SURFACE_SNAP_DISTANCE, true) # true = test only, don't move.
|
||||
|
|
@ -352,18 +324,12 @@ func _snap_to_surface(normal: Vector2) -> bool:
|
|||
global_position += hit.get_travel()
|
||||
return true
|
||||
|
||||
|
||||
# The normal of the surface we're stuck to, or zero 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)
|
||||
if is_on_floor(): return Vector2.UP
|
||||
if isStickingToCeiling: return Vector2.DOWN
|
||||
if isStickingToWall: return Vector2(wallNormalX, 0)
|
||||
return Vector2.ZERO
|
||||
|
||||
|
||||
func _get_facing() -> Vector2:
|
||||
if is_on_floor():
|
||||
if sprite.flip_h:
|
||||
|
|
@ -375,16 +341,13 @@ func _get_facing() -> Vector2:
|
|||
return Vector2.LEFT
|
||||
return Vector2(0, wallFacingY)
|
||||
|
||||
|
||||
# ---- Hops ----
|
||||
|
||||
func _get_hop_height() -> float:
|
||||
var ceilingDistance := _get_hop_distance(Vector2.UP)
|
||||
if ceilingDistance >= 0.0:
|
||||
return ceilingDistance
|
||||
return HOP_NO_CEILING_HEIGHT
|
||||
|
||||
|
||||
# Distance to a surface facing us within HOP_REACH (plus HOP_OVERSHOOT), or -1 if none.
|
||||
func _get_hop_distance(direction: Vector2) -> float:
|
||||
var hit := move_and_collide(direction * HOP_REACH, true)
|
||||
|
|
@ -454,7 +417,7 @@ func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool:
|
|||
return false # Starts inside something: an inside corner, not an edge.
|
||||
|
||||
var direction := tangent * side
|
||||
var corner = _find_edge_corner(normal, direction, edgeSensorRange + 2)
|
||||
var corner = _find_edge_corner(normal, direction, EDGE_SENSOR_RANGE + 2)
|
||||
if corner == null:
|
||||
return false
|
||||
|
||||
|
|
@ -525,7 +488,6 @@ func _hold_at_edge(delta: float) -> void:
|
|||
var speedToReachEdge := maxf(distanceToEdge, 0.0) / delta
|
||||
velocity += direction * (speedToReachEdge - absf(speedAlongSurface))
|
||||
|
||||
|
||||
# We've just climbed past a wall's top corner, so _find_edge_corner (which looks
|
||||
# from the surface we're on) can't see it. Find it with our own raycasts.
|
||||
func _start_wall_top_wrap() -> bool:
|
||||
|
|
@ -562,7 +524,7 @@ func _begin_wrap(corner: Vector2, arcEndAngle: float, exitOffset: Vector2, ontoN
|
|||
|
||||
wrapCorner = corner
|
||||
wrapEntryStart = global_position
|
||||
if is_nan(arcStartAngle): # NAN ("not a number") means it wasn't given.
|
||||
if is_nan(arcStartAngle):
|
||||
var fromCorner := global_position - corner
|
||||
wrapFromAngle = fromCorner.angle()
|
||||
wrapFromDistance = fromCorner.length()
|
||||
|
|
@ -620,7 +582,6 @@ func _update_wrap(delta: float) -> void:
|
|||
if wrapDistanceTravelled >= totalLength:
|
||||
_finish_wrap(delta)
|
||||
|
||||
|
||||
func _finish_wrap(delta: float) -> void:
|
||||
# Close the WRAP_MARGIN gap with move_and_slide() so is_on_floor()/is_on_wall()
|
||||
# describe where we are now, not where we were before the wrap.
|
||||
|
|
@ -655,7 +616,6 @@ func _finish_wrap(delta: float) -> void:
|
|||
ignoreWallUnstickUntilRelease = true
|
||||
ignoreCeilingUnstickUntilRelease = isStickingToCeiling
|
||||
|
||||
|
||||
# ---- Effects ----
|
||||
|
||||
func _splat(normal: Vector2) -> void:
|
||||
|
|
@ -669,19 +629,16 @@ func _splat(normal: Vector2) -> void:
|
|||
landSound.pitch_scale = randf_range(LAND_PITCH_MIN, LAND_PITCH_MAX)
|
||||
landSound.play()
|
||||
|
||||
|
||||
# ---- Physics queries (all ignore the slime itself) ----
|
||||
|
||||
func _get_radius() -> float:
|
||||
return (collisionShape.shape as CircleShape2D).radius
|
||||
|
||||
|
||||
# Returns the hit (.position, .normal), or an empty Dictionary if nothing.
|
||||
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
|
||||
|
|
@ -689,7 +646,6 @@ func _is_point_free(point: Vector2) -> bool:
|
|||
query.exclude = [get_rid()]
|
||||
return get_world_2d().direct_space_state.intersect_point(query, 1).is_empty()
|
||||
|
||||
|
||||
# Whether our whole shape fits at this spot.
|
||||
func _is_spot_free(spot: Vector2) -> bool:
|
||||
var query := PhysicsShapeQueryParameters2D.new()
|
||||
|
|
|
|||
Loading…
Reference in a new issue