Trim player.gd comments: shorter, and none that restate the code
Only comments and blank lines changed (the code with comments stripped is identical). Shortened the overview, section headers and longer explanations, and removed comments that just describe the next line. Kept the ones explaining why or non-obvious maths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
804a7a76c9
commit
cb0ce427aa
1 changed files with 126 additions and 301 deletions
|
|
@ -1,162 +1,102 @@
|
||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
# =============================================================================
|
# The slime crawls on floors, walls and ceilings and sticks to them.
|
||||||
# THE SLIME PLAYER
|
# - 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.
|
||||||
#
|
#
|
||||||
# The slime crawls along floors, walls and ceilings and sticks to them. The
|
# Terms:
|
||||||
# main things it can do:
|
# - Godot's y axis points DOWN, so up is negative y (Vector2.UP is (0, -1)).
|
||||||
#
|
# - Normal: arrow pointing straight out of a surface.
|
||||||
# - Crawl: left/right on floors and ceilings, up/down on walls.
|
# - Tangent: arrow pointing along a surface.
|
||||||
# - Let go: hold the direction away from a wall or ceiling for HOLD_TIME.
|
# - a.dot(b): how much two arrows point the same way (1 same, 0 sideways, -1 opposite).
|
||||||
# - Edge hold: crawling towards the edge of a surface stops at the edge, and
|
|
||||||
# only carries on over it after holding towards it for HOLD_TIME.
|
|
||||||
# - Edge wrap: near an edge, press INTO the surface (down on a floor, up on a
|
|
||||||
# ceiling, towards the wall on a wall) to swing around the corner onto the
|
|
||||||
# side of the platform.
|
|
||||||
# - Hop: standing still, hold away from the surface for HOLD_TIME to hop
|
|
||||||
# across to a surface within one tile.
|
|
||||||
#
|
|
||||||
# How one frame works (see _physics_process):
|
|
||||||
# 1. Read the input.
|
|
||||||
# 2. If we're in the middle of an edge wrap, keep animating it and stop there.
|
|
||||||
# 3. Maybe start an edge wrap.
|
|
||||||
# 4. Work out our velocity (how fast and which way we want to move).
|
|
||||||
# 5. Move with move_and_slide().
|
|
||||||
# 6. Check what we're touching now (floor, wall, ceiling) and update the sprite.
|
|
||||||
#
|
|
||||||
# A few terms used in the comments:
|
|
||||||
#
|
|
||||||
# - Godot's y axis points DOWN. So "up" is negative y: Vector2.UP is (0, -1).
|
|
||||||
# - delta: how many seconds since the last physics frame (1/60 of a second).
|
|
||||||
# Multiplying a speed by delta gives how far to move this frame.
|
|
||||||
# - Normal: an arrow pointing straight out of a surface. A floor's normal
|
|
||||||
# points up, a ceiling's points down, a wall's points away from the wall.
|
|
||||||
# - Tangent: an arrow pointing ALONG a surface, the way you'd crawl on it.
|
|
||||||
# - Dot product (a.dot(b)): how much two arrows point the same way. 1 means
|
|
||||||
# the same direction, 0 means sideways to each other, -1 means opposite.
|
|
||||||
# For example, velocity.dot(tangent) is how fast we're moving along a surface.
|
|
||||||
# - Raycast: an invisible line we shoot out to see what it hits first.
|
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Settings ----
|
||||||
# SETTINGS
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# ---- Speeds ----
|
# Speeds are positive; the direction is applied where they're used.
|
||||||
# All speeds are positive numbers. Which way to go (+ or -) is decided where
|
const GROUND_SPEED = 150.0 # Crawling on floors, walls and ceilings (pixels per second).
|
||||||
# they're used.
|
const AIR_SPEED = 75.0
|
||||||
const GROUND_SPEED = 150.0 # Crawling speed on floors, walls and ceilings, in pixels per second.
|
const CLIMB_SPEED = 150.0
|
||||||
const AIR_SPEED = 75.0 # Steering speed in the air.
|
|
||||||
const CLIMB_SPEED = 150.0 # Speed up and down a wall.
|
|
||||||
|
|
||||||
# How quickly we speed up and slow down on a surface, in pixels per second,
|
# Full speed in 4 frames, stopped in 6.
|
||||||
# per second. We reach full speed in 4 frames and stop in 6.
|
|
||||||
const GROUND_ACCEL = GROUND_SPEED / (4.0 / 60.0)
|
const GROUND_ACCEL = GROUND_SPEED / (4.0 / 60.0)
|
||||||
const GROUND_FRICTION = GROUND_SPEED / (6.0 / 60.0)
|
const GROUND_FRICTION = GROUND_SPEED / (6.0 / 60.0)
|
||||||
|
|
||||||
# In the air we steer more gently, and keep any extra speed we had when we
|
# Gentler steering in the air. Extra speed from leaving a surface fades slowly.
|
||||||
# left a surface, letting it fade slowly instead of snapping down to AIR_SPEED.
|
|
||||||
const AIR_ACCEL = 600.0
|
const AIR_ACCEL = 600.0
|
||||||
const AIR_DRAG = 300.0
|
const AIR_DRAG = 300.0
|
||||||
const MAX_FALL_SPEED = 400.0
|
const MAX_FALL_SPEED = 400.0
|
||||||
|
|
||||||
# ---- Sticking to walls and ceilings ----
|
# A small push into the wall/ceiling each frame keeps Godot reporting that we're touching it.
|
||||||
# Every frame we push gently into the wall or ceiling we're on, so Godot keeps
|
|
||||||
# telling us we're touching it.
|
|
||||||
const CEILING_STICK_SPEED = 20.0
|
const CEILING_STICK_SPEED = 20.0
|
||||||
const WALL_STICK_SPEED = 20.0
|
const WALL_STICK_SPEED = 20.0
|
||||||
# If we drift off a wall or ceiling by up to this many pixels, pull back onto it.
|
const SURFACE_SNAP_DISTANCE = 4.0 # Drift off a wall/ceiling by up to this and we pull back on.
|
||||||
const SURFACE_SNAP_DISTANCE = 4.0
|
|
||||||
|
|
||||||
# How many seconds to hold a direction before something happens: letting go of
|
# How long to hold before letting go, going over an edge, or hopping.
|
||||||
# a wall or ceiling, carrying on over an edge we're holding at, and hopping.
|
|
||||||
const HOLD_TIME = 0.5
|
const HOLD_TIME = 0.5
|
||||||
|
|
||||||
# ---- Edges ----
|
# How close an edge must be (from our center) for an edge wrap.
|
||||||
# How close (in pixels, from our center) an edge must be for pressing into the
|
|
||||||
# surface to wrap around it. @export means you can change it in the Inspector.
|
|
||||||
@export var edgeSensorRange := 12.0
|
@export var edgeSensorRange := 12.0
|
||||||
# How far past our radius the edge sensors reach into the surface.
|
const EDGE_SENSOR_DEPTH = 4.0 # How far past our radius the sensors reach into the surface.
|
||||||
const EDGE_SENSOR_DEPTH = 4.0
|
const WRAP_MARGIN = 0.5 # Extra gap from the corner so we don't clip into it.
|
||||||
# When wrapping around a corner, keep this much extra gap so we don't clip into it.
|
|
||||||
const WRAP_MARGIN = 0.5
|
|
||||||
|
|
||||||
# ---- Hops ----
|
const HOP_REACH = 32.0 # One tile.
|
||||||
# How far a hop can reach to find a surface (one tile).
|
const HOP_NO_CEILING_HEIGHT = 16.0 # Floor hop with nothing in reach, so you can tell it worked.
|
||||||
const HOP_REACH = 32.0
|
const HOP_OVERSHOOT = 2.0 # Aim slightly past the surface so we arrive with some speed and stick.
|
||||||
# On the floor with no ceiling in reach, hop this high anyway, so you can tell it worked.
|
|
||||||
const HOP_NO_CEILING_HEIGHT = 16.0
|
|
||||||
# Aim this far past the surface, so we arrive with a little speed and stick to it.
|
|
||||||
const HOP_OVERSHOOT = 2.0
|
|
||||||
|
|
||||||
# ---- Looks and sound ----
|
const ROTATE_TO_SURFACE_SPEED = 25.0
|
||||||
const ROTATE_TO_SURFACE_SPEED = 25.0 # How quickly the sprite turns to sit on a wall or ceiling.
|
const MIN_LAND_SPEED = 60.0 # Slower landings don't splat.
|
||||||
const MIN_LAND_SPEED = 60.0 # Landing slower than this doesn't splat.
|
const LAND_PITCH_MIN = 0.87
|
||||||
const LAND_PITCH_MIN = 0.87 # Each landing sound plays at a random pitch between these.
|
|
||||||
const LAND_PITCH_MAX = 1.15
|
const LAND_PITCH_MAX = 1.15
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- State ----
|
||||||
# STATE (things that change while playing)
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# ---- What we're stuck to ----
|
# For the floor we use Godot's is_on_floor().
|
||||||
# (For the floor we just ask Godot with is_on_floor().)
|
|
||||||
var isStickingToWall := false
|
var isStickingToWall := false
|
||||||
var wallNormalX := 0.0 # Which way the wall we're on faces: -1 is left, 1 is right.
|
var wallNormalX := 0.0 # -1 if the wall faces left, 1 if it faces right.
|
||||||
var isStickingToCeiling := false
|
var isStickingToCeiling := false
|
||||||
|
|
||||||
# ---- Timers (in seconds) ----
|
var timeSpentUnstickingWall := 0.0
|
||||||
var timeSpentUnstickingWall := 0.0 # Holding away from the wall.
|
var timeSpentUnstickingCeiling := 0.0
|
||||||
var timeSpentUnstickingCeiling := 0.0 # Holding down on the ceiling.
|
var timeSpentHoldingEdge := 0.0
|
||||||
var timeSpentHoldingEdge := 0.0 # Stopped at an edge, holding towards it.
|
var timeSpentHoldingUpOnFloor := 0.0
|
||||||
var timeSpentHoldingUpOnFloor := 0.0 # Standing still holding up, for the floor hop.
|
|
||||||
|
|
||||||
# Right after wrapping onto a wall we're usually still holding away from it.
|
# Right after wrapping onto a wall or ceiling we're usually still holding away
|
||||||
# Don't count that towards letting go until the player releases the direction.
|
# from it. That shouldn't count towards letting go until it's released.
|
||||||
var ignoreWallUnstickUntilRelease := false
|
var ignoreWallUnstickUntilRelease := false
|
||||||
# Same for holding down after wrapping onto a ceiling.
|
|
||||||
var ignoreCeilingUnstickUntilRelease := false
|
var ignoreCeilingUnstickUntilRelease := false
|
||||||
|
|
||||||
# Which way we last moved along a wall: -1 is up, 1 is down. Used as "facing"
|
var wallFacingY := -1.0 # Last way we moved along a wall (-1 up). Our "facing" on walls.
|
||||||
# on walls when picking which edge to wrap around.
|
var isHoppingAcross := false # No air steering mid-hop, so it lands where it was aimed.
|
||||||
var wallFacingY := -1.0
|
|
||||||
|
|
||||||
# True while hopping from one wall to the one opposite. We don't steer in the
|
# A wrap is a straight "entry" to the arc, a quarter-circle "arc" around the
|
||||||
# air until we land, so the hop goes exactly where it was aimed.
|
# corner, then a straight "exit" onto the new surface.
|
||||||
var isHoppingAcross := false
|
|
||||||
|
|
||||||
# ---- Edge wrap ----
|
|
||||||
# A wrap moves us in three parts: a straight "entry" to the start of the arc,
|
|
||||||
# a quarter-circle "arc" around the corner, and a straight "exit" onto the new
|
|
||||||
# surface. These remember the wrap we're in the middle of.
|
|
||||||
var isWrapping := false
|
var isWrapping := false
|
||||||
var wrapCorner := Vector2.ZERO # The corner we swing around.
|
var wrapCorner := Vector2.ZERO
|
||||||
var wrapEntryStart := Vector2.ZERO
|
var wrapEntryStart := Vector2.ZERO
|
||||||
var wrapEntryLength := 0.0
|
var wrapEntryLength := 0.0
|
||||||
var wrapFromAngle := 0.0 # Where the arc starts and ends, as angles around the corner.
|
var wrapFromAngle := 0.0 # Angles around the corner.
|
||||||
var wrapToAngle := 0.0
|
var wrapToAngle := 0.0
|
||||||
var wrapFromDistance := 0.0 # How far from the corner we are at the start and end of the arc.
|
var wrapFromDistance := 0.0 # Distances from the corner.
|
||||||
var wrapToDistance := 0.0
|
var wrapToDistance := 0.0
|
||||||
var wrapArcLength := 0.0
|
var wrapArcLength := 0.0
|
||||||
var wrapExitOffset := Vector2.ZERO
|
var wrapExitOffset := Vector2.ZERO
|
||||||
var wrapFromRotation := 0.0 # The sprite turns from this to this during the arc.
|
var wrapFromRotation := 0.0 # Sprite rotation.
|
||||||
var wrapToRotation := 0.0
|
var wrapToRotation := 0.0
|
||||||
var wrapOntoNormal := Vector2.UP # The normal of the surface we'll end up on.
|
var wrapOntoNormal := Vector2.UP # The surface we'll end up on.
|
||||||
var wrapSpeed := 0.0
|
var wrapSpeed := 0.0
|
||||||
var wrapDistanceTravelled := 0.0
|
var wrapDistanceTravelled := 0.0
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Nodes ----
|
||||||
# NODES (children of the Player in player.tscn)
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Temp for now, need to use AnimatedSprite2D later on.
|
# Temp for now, need to use AnimatedSprite2D later on.
|
||||||
@onready var sprite: Sprite2D = $Sprite2D
|
@onready var sprite: Sprite2D = $Sprite2D
|
||||||
@onready var collisionShape: CollisionShape2D = $CollisionShape2D
|
@onready var collisionShape: CollisionShape2D = $CollisionShape2D
|
||||||
# Two raycasts, one each side of us, that look for edges. They sit under a
|
# Edge-finding raycasts, one each side, under a pivot we turn to face the surface we're on.
|
||||||
# pivot (EdgeSensors) that we turn to point them into whatever surface we're on.
|
|
||||||
@onready var edgeSensors: Node2D = $EdgeSensors
|
@onready var edgeSensors: Node2D = $EdgeSensors
|
||||||
@onready var leftEdgeSensor: RayCast2D = $EdgeSensors/LeftEdgeSensor
|
@onready var leftEdgeSensor: RayCast2D = $EdgeSensors/LeftEdgeSensor
|
||||||
@onready var rightEdgeSensor: RayCast2D = $EdgeSensors/RightEdgeSensor
|
@onready var rightEdgeSensor: RayCast2D = $EdgeSensors/RightEdgeSensor
|
||||||
|
|
@ -165,78 +105,55 @@ var wrapDistanceTravelled := 0.0
|
||||||
@onready var splatParticles: CPUParticles2D = $SplatParticles
|
@onready var splatParticles: CPUParticles2D = $SplatParticles
|
||||||
@onready var landSound: AudioStreamPlayer = $LandSound
|
@onready var landSound: AudioStreamPlayer = $LandSound
|
||||||
@onready var wrapSound: AudioStreamPlayer = $WrapSound
|
@onready var wrapSound: AudioStreamPlayer = $WrapSound
|
||||||
# Placeholder sounds, see Assets/Sounds/CREDITS.txt. One is picked at random
|
# Placeholder sounds, see Assets/Sounds/CREDITS.txt. One plays at random per landing.
|
||||||
# for each landing, at a slightly random pitch, so they don't all sound the same.
|
|
||||||
@export var landSounds: Array[AudioStream] = []
|
@export var landSounds: Array[AudioStream] = []
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# SETUP
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
# Godot's own "stay stuck to the floor" distance, the same as ours for walls and ceilings.
|
|
||||||
floor_snap_length = SURFACE_SNAP_DISTANCE
|
floor_snap_length = SURFACE_SNAP_DISTANCE
|
||||||
|
|
||||||
# Place the edge sensors edgeSensorRange to each side, pointing down
|
|
||||||
# (they get turned to face other surfaces later).
|
|
||||||
var sensorLength := _get_radius() + EDGE_SENSOR_DEPTH
|
var sensorLength := _get_radius() + EDGE_SENSOR_DEPTH
|
||||||
leftEdgeSensor.position = Vector2(-edgeSensorRange, 0)
|
leftEdgeSensor.position = Vector2(-edgeSensorRange, 0)
|
||||||
leftEdgeSensor.target_position = Vector2(0, sensorLength)
|
leftEdgeSensor.target_position = Vector2(0, sensorLength)
|
||||||
rightEdgeSensor.position = Vector2(edgeSensorRange, 0)
|
rightEdgeSensor.position = Vector2(edgeSensorRange, 0)
|
||||||
rightEdgeSensor.target_position = Vector2(0, sensorLength)
|
rightEdgeSensor.target_position = Vector2(0, sensorLength)
|
||||||
|
|
||||||
# Raycasts normally ignore their parent's collider, but the sensors' parent
|
# Raycasts only ignore their parent, which is the EdgeSensors pivot, not us.
|
||||||
# is the EdgeSensors pivot, not us. So tell them to ignore us explicitly,
|
|
||||||
# otherwise they can hit the slime itself.
|
|
||||||
leftEdgeSensor.add_exception(self)
|
leftEdgeSensor.add_exception(self)
|
||||||
rightEdgeSensor.add_exception(self)
|
rightEdgeSensor.add_exception(self)
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# EVERY FRAME
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
# get_axis gives -1, 0 or 1 (or in between on a controller stick).
|
# -1 is left/down, 1 is right/up.
|
||||||
# horizontalInput: -1 is left, 1 is right. verticalInput: -1 is down, 1 is up.
|
|
||||||
var horizontalInput := Input.get_axis("moveLeft", "moveRight")
|
var horizontalInput := Input.get_axis("moveLeft", "moveRight")
|
||||||
var verticalInput := Input.get_axis("moveDown", "moveUp")
|
var verticalInput := Input.get_axis("moveDown", "moveUp")
|
||||||
|
|
||||||
# In the middle of swinging around a corner: keep going and skip everything else.
|
|
||||||
if isWrapping:
|
if isWrapping:
|
||||||
_update_wrap(delta)
|
_update_wrap(delta)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Remember if we were in the air before moving, to know if we just landed.
|
|
||||||
var wasInAir := _get_surface_normal() == Vector2.ZERO
|
var wasInAir := _get_surface_normal() == Vector2.ZERO
|
||||||
if not wasInAir:
|
if not wasInAir:
|
||||||
isHoppingAcross = false
|
isHoppingAcross = false
|
||||||
|
|
||||||
# Near an edge and pressing into the surface: start swinging around it.
|
|
||||||
if isStickingToWall and verticalInput != 0.0:
|
if isStickingToWall and verticalInput != 0.0:
|
||||||
wallFacingY = -signf(verticalInput)
|
wallFacingY = -signf(verticalInput)
|
||||||
if _start_edge_wrap(horizontalInput, verticalInput):
|
if _start_edge_wrap(horizontalInput, verticalInput):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Work out our velocity for this frame.
|
|
||||||
_update_facing(horizontalInput)
|
_update_facing(horizontalInput)
|
||||||
_update_vertical_movement(delta, horizontalInput, verticalInput)
|
_update_vertical_movement(delta, horizontalInput, verticalInput)
|
||||||
_update_floor_hop(delta, horizontalInput, verticalInput)
|
_update_floor_hop(delta, horizontalInput, verticalInput)
|
||||||
_update_horizontal_movement(delta, horizontalInput, verticalInput)
|
_update_horizontal_movement(delta, horizontalInput, verticalInput)
|
||||||
_hold_at_edge(delta)
|
_hold_at_edge(delta)
|
||||||
|
|
||||||
# Remember a couple of things from before we move.
|
|
||||||
var wasOnWall := isStickingToWall and not is_on_floor()
|
var wasOnWall := isStickingToWall and not is_on_floor()
|
||||||
var velocityBeforeMove := velocity
|
var velocityBeforeMove := velocity
|
||||||
|
|
||||||
# Move! Godot slides us along anything we bump into.
|
|
||||||
move_and_slide()
|
move_and_slide()
|
||||||
|
|
||||||
# Now check what we're touching.
|
|
||||||
_update_wall_sticking(wasOnWall)
|
_update_wall_sticking(wasOnWall)
|
||||||
|
|
||||||
# Climbed up past the top of a wall: swing over onto the top of it.
|
|
||||||
var climbedPastWallTop := wasOnWall and not isStickingToWall and verticalInput > 0
|
var climbedPastWallTop := wasOnWall and not isStickingToWall and verticalInput > 0
|
||||||
var tryingToLetGo := timeSpentUnstickingWall >= HOLD_TIME
|
var tryingToLetGo := timeSpentUnstickingWall >= HOLD_TIME
|
||||||
if climbedPastWallTop and not tryingToLetGo and _start_wall_top_wrap():
|
if climbedPastWallTop and not tryingToLetGo and _start_wall_top_wrap():
|
||||||
|
|
@ -244,19 +161,16 @@ func _physics_process(delta: float) -> void:
|
||||||
|
|
||||||
_update_ceiling_sticking()
|
_update_ceiling_sticking()
|
||||||
|
|
||||||
# In a corner, touching a wall and a ceiling at once, only stick to one:
|
# In a corner, stick to the ceiling if pressing up, otherwise the wall.
|
||||||
# the ceiling if we're pressing up, otherwise the wall.
|
|
||||||
if isStickingToWall and isStickingToCeiling:
|
if isStickingToWall and isStickingToCeiling:
|
||||||
if verticalInput > 0:
|
if verticalInput > 0:
|
||||||
isStickingToWall = false
|
isStickingToWall = false
|
||||||
else:
|
else:
|
||||||
isStickingToCeiling = false
|
isStickingToCeiling = false
|
||||||
|
|
||||||
# Just landed on something hard enough: squish.
|
|
||||||
var normal := _get_surface_normal()
|
var normal := _get_surface_normal()
|
||||||
var landedOnSomething := wasInAir and normal != Vector2.ZERO
|
var landedOnSomething := wasInAir and normal != Vector2.ZERO
|
||||||
if landedOnSomething:
|
if landedOnSomething:
|
||||||
# How fast we were moving into the surface (against its normal).
|
|
||||||
var impactSpeed := velocityBeforeMove.dot(-normal)
|
var impactSpeed := velocityBeforeMove.dot(-normal)
|
||||||
if impactSpeed >= MIN_LAND_SPEED:
|
if impactSpeed >= MIN_LAND_SPEED:
|
||||||
_splat(normal)
|
_splat(normal)
|
||||||
|
|
@ -264,36 +178,28 @@ func _physics_process(delta: float) -> void:
|
||||||
_update_sprite_rotation(delta)
|
_update_sprite_rotation(delta)
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Movement steps (in the order _physics_process runs them) ----
|
||||||
# MOVEMENT STEPS (in the order _physics_process runs them)
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Flips the sprite to face the way we're moving.
|
|
||||||
func _update_facing(horizontalInput: float) -> void:
|
func _update_facing(horizontalInput: float) -> void:
|
||||||
if horizontalInput == 0.0:
|
if horizontalInput == 0.0:
|
||||||
return
|
return
|
||||||
if isStickingToCeiling:
|
if isStickingToCeiling:
|
||||||
# Upside down on the ceiling, so flip the other way.
|
sprite.flip_h = horizontalInput > 0 # Upside down, so flipped the other way.
|
||||||
sprite.flip_h = horizontalInput > 0
|
|
||||||
else:
|
else:
|
||||||
sprite.flip_h = horizontalInput < 0
|
sprite.flip_h = horizontalInput < 0
|
||||||
|
|
||||||
|
|
||||||
# Works out velocity.y (up and down).
|
|
||||||
func _update_vertical_movement(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
func _update_vertical_movement(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
||||||
if isStickingToCeiling:
|
if isStickingToCeiling:
|
||||||
_update_ceiling_letting_go(delta, horizontalInput, verticalInput)
|
_update_ceiling_letting_go(delta, horizontalInput, verticalInput)
|
||||||
elif isStickingToWall:
|
elif isStickingToWall:
|
||||||
# Climb up and down the wall. (Up is negative y.)
|
|
||||||
var targetSpeed := -CLIMB_SPEED * verticalInput
|
var targetSpeed := -CLIMB_SPEED * verticalInput
|
||||||
velocity.y = _accelerate(velocity.y, targetSpeed, delta)
|
velocity.y = _accelerate(velocity.y, targetSpeed, delta)
|
||||||
elif not is_on_floor():
|
elif not is_on_floor():
|
||||||
# Falling: gravity speeds us up, but no faster than MAX_FALL_SPEED.
|
|
||||||
velocity.y = minf(velocity.y + get_gravity().y * delta, MAX_FALL_SPEED)
|
velocity.y = minf(velocity.y + get_gravity().y * delta, MAX_FALL_SPEED)
|
||||||
|
|
||||||
|
|
||||||
# On the ceiling: holding down (while not moving left/right) for HOLD_TIME lets
|
# Holding down (not moving) for HOLD_TIME lets go, hopping onto a floor if one's in reach.
|
||||||
# go of it, hopping down onto a floor if one's in reach.
|
|
||||||
func _update_ceiling_letting_go(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
func _update_ceiling_letting_go(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
||||||
if verticalInput >= 0:
|
if verticalInput >= 0:
|
||||||
ignoreCeilingUnstickUntilRelease = false
|
ignoreCeilingUnstickUntilRelease = false
|
||||||
|
|
@ -306,22 +212,19 @@ func _update_ceiling_letting_go(delta: float, horizontalInput: float, verticalIn
|
||||||
timeSpentUnstickingCeiling = 0.0
|
timeSpentUnstickingCeiling = 0.0
|
||||||
|
|
||||||
if timeSpentUnstickingCeiling < HOLD_TIME:
|
if timeSpentUnstickingCeiling < HOLD_TIME:
|
||||||
# Not letting go yet: push up to stay stuck.
|
|
||||||
velocity.y = -CEILING_STICK_SPEED
|
velocity.y = -CEILING_STICK_SPEED
|
||||||
return
|
return
|
||||||
|
|
||||||
isStickingToCeiling = false
|
isStickingToCeiling = false
|
||||||
var floorDistance := _get_hop_distance(Vector2.DOWN)
|
var floorDistance := _get_hop_distance(Vector2.DOWN)
|
||||||
if floorDistance >= 0.0:
|
if floorDistance >= 0.0:
|
||||||
# Floor in reach: hop down onto it (see _get_hop_speed for the maths).
|
|
||||||
velocity.y = _get_hop_speed(floorDistance)
|
velocity.y = _get_hop_speed(floorDistance)
|
||||||
else:
|
else:
|
||||||
# Nothing in reach: just start falling.
|
|
||||||
velocity.y = get_gravity().y * delta
|
velocity.y = get_gravity().y * delta
|
||||||
|
|
||||||
|
|
||||||
# On the floor and standing still: holding up for HOLD_TIME hops up to the
|
# Holding up (not moving) for HOLD_TIME hops to the ceiling. Not against a wall,
|
||||||
# ceiling above. Not when against a wall though, where up climbs the wall.
|
# where up climbs it instead.
|
||||||
func _update_floor_hop(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
func _update_floor_hop(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
||||||
var holdingUp := verticalInput > 0
|
var holdingUp := verticalInput > 0
|
||||||
var standingStill := horizontalInput == 0.0
|
var standingStill := horizontalInput == 0.0
|
||||||
|
|
@ -332,10 +235,9 @@ func _update_floor_hop(delta: float, horizontalInput: float, verticalInput: floa
|
||||||
|
|
||||||
if timeSpentHoldingUpOnFloor >= HOLD_TIME:
|
if timeSpentHoldingUpOnFloor >= HOLD_TIME:
|
||||||
timeSpentHoldingUpOnFloor = 0.0
|
timeSpentHoldingUpOnFloor = 0.0
|
||||||
velocity.y = -_get_hop_speed(_get_hop_height()) # Negative, because up is negative y.
|
velocity.y = -_get_hop_speed(_get_hop_height())
|
||||||
|
|
||||||
|
|
||||||
# Works out velocity.x (left and right).
|
|
||||||
func _update_horizontal_movement(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
func _update_horizontal_movement(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
||||||
if isStickingToWall and not is_on_floor():
|
if isStickingToWall and not is_on_floor():
|
||||||
_update_wall_letting_go(delta, horizontalInput, verticalInput)
|
_update_wall_letting_go(delta, horizontalInput, verticalInput)
|
||||||
|
|
@ -348,11 +250,9 @@ func _update_horizontal_movement(delta: float, horizontalInput: float, verticalI
|
||||||
velocity.x = _air_steer(velocity.x, horizontalInput, delta)
|
velocity.x = _air_steer(velocity.x, horizontalInput, delta)
|
||||||
|
|
||||||
|
|
||||||
# On a wall: holding away from it (while not climbing) for HOLD_TIME hops across
|
# Holding away (not climbing) for HOLD_TIME hops to the opposite wall if one's
|
||||||
# to the wall opposite if there's one in reach, otherwise lets go.
|
# in reach, otherwise lets go.
|
||||||
func _update_wall_letting_go(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
func _update_wall_letting_go(delta: float, horizontalInput: float, verticalInput: float) -> void:
|
||||||
# The wall's normal points away from it, so pressing the same way as the
|
|
||||||
# normal means pressing away from the wall.
|
|
||||||
var pressingAwayFromWall := horizontalInput * wallNormalX > 0
|
var pressingAwayFromWall := horizontalInput * wallNormalX > 0
|
||||||
if not pressingAwayFromWall:
|
if not pressingAwayFromWall:
|
||||||
ignoreWallUnstickUntilRelease = false
|
ignoreWallUnstickUntilRelease = false
|
||||||
|
|
@ -364,7 +264,6 @@ func _update_wall_letting_go(delta: float, horizontalInput: float, verticalInput
|
||||||
timeSpentUnstickingWall = 0.0
|
timeSpentUnstickingWall = 0.0
|
||||||
|
|
||||||
if timeSpentUnstickingWall < HOLD_TIME:
|
if timeSpentUnstickingWall < HOLD_TIME:
|
||||||
# Not letting go yet: push into the wall to stay stuck.
|
|
||||||
velocity.x = -wallNormalX * WALL_STICK_SPEED
|
velocity.x = -wallNormalX * WALL_STICK_SPEED
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -375,9 +274,7 @@ func _update_wall_letting_go(delta: float, horizontalInput: float, verticalInput
|
||||||
velocity.x = horizontalInput * GROUND_SPEED
|
velocity.x = horizontalInput * GROUND_SPEED
|
||||||
|
|
||||||
|
|
||||||
# After moving: are we still on the wall?
|
|
||||||
func _update_wall_sticking(wasOnWall: bool) -> void:
|
func _update_wall_sticking(wasOnWall: bool) -> void:
|
||||||
# If we drifted slightly off the wall without meaning to let go, pull back onto it.
|
|
||||||
var meantToLetGo := timeSpentUnstickingWall >= HOLD_TIME
|
var meantToLetGo := timeSpentUnstickingWall >= HOLD_TIME
|
||||||
var driftedBackOn := false
|
var driftedBackOn := false
|
||||||
if wasOnWall and not meantToLetGo and not is_on_wall():
|
if wasOnWall and not meantToLetGo and not is_on_wall():
|
||||||
|
|
@ -388,8 +285,6 @@ func _update_wall_sticking(wasOnWall: bool) -> void:
|
||||||
wallNormalX = get_wall_normal().x
|
wallNormalX = get_wall_normal().x
|
||||||
|
|
||||||
|
|
||||||
# After moving: are we still on the ceiling? If we drifted slightly off it,
|
|
||||||
# pull back onto it.
|
|
||||||
func _update_ceiling_sticking() -> void:
|
func _update_ceiling_sticking() -> void:
|
||||||
var wasOnCeiling := isStickingToCeiling
|
var wasOnCeiling := isStickingToCeiling
|
||||||
var driftedBackOn := false
|
var driftedBackOn := false
|
||||||
|
|
@ -398,26 +293,21 @@ func _update_ceiling_sticking() -> void:
|
||||||
isStickingToCeiling = is_on_ceiling() or driftedBackOn
|
isStickingToCeiling = is_on_ceiling() or driftedBackOn
|
||||||
|
|
||||||
|
|
||||||
# Turns the sprite to sit on the wall or ceiling we're on.
|
|
||||||
func _update_sprite_rotation(delta: float) -> void:
|
func _update_sprite_rotation(delta: float) -> void:
|
||||||
var targetRotation := 0.0 # Upright, for the floor and the air.
|
var targetRotation := 0.0
|
||||||
if isStickingToWall and not is_on_floor():
|
if isStickingToWall and not is_on_floor():
|
||||||
targetRotation = PI / 2 * wallNormalX # A quarter turn (PI/2) towards the wall.
|
targetRotation = PI / 2 * wallNormalX
|
||||||
elif isStickingToCeiling:
|
elif isStickingToCeiling:
|
||||||
targetRotation = PI # A half turn: upside down.
|
targetRotation = PI
|
||||||
|
|
||||||
# Turn part of the way there each frame, so it's smooth. Using exp() makes
|
# Smooth, and the same speed at any frame rate thanks to exp().
|
||||||
# it turn at the same rate however fast the game is running.
|
|
||||||
var turnAmount := 1.0 - exp(-ROTATE_TO_SURFACE_SPEED * delta)
|
var turnAmount := 1.0 - exp(-ROTATE_TO_SURFACE_SPEED * delta)
|
||||||
sprite.rotation = lerp_angle(sprite.rotation, targetRotation, turnAmount)
|
sprite.rotation = lerp_angle(sprite.rotation, targetRotation, turnAmount)
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Movement helpers ----
|
||||||
# MOVEMENT HELPERS
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Moves speed towards targetSpeed, at GROUND_ACCEL when speeding up and at
|
# GROUND_ACCEL when speeding up, GROUND_FRICTION when slowing, stopping or turning.
|
||||||
# GROUND_FRICTION when slowing down, stopping or turning around.
|
|
||||||
func _accelerate(speed: float, targetSpeed: float, delta: float) -> float:
|
func _accelerate(speed: float, targetSpeed: float, delta: float) -> float:
|
||||||
var goingSameWay := speed == 0.0 or signf(targetSpeed) == signf(speed)
|
var goingSameWay := speed == 0.0 or signf(targetSpeed) == signf(speed)
|
||||||
var speedingUp := targetSpeed != 0.0 and goingSameWay and absf(targetSpeed) > absf(speed)
|
var speedingUp := targetSpeed != 0.0 and goingSameWay and absf(targetSpeed) > absf(speed)
|
||||||
|
|
@ -425,8 +315,7 @@ func _accelerate(speed: float, targetSpeed: float, delta: float) -> float:
|
||||||
return move_toward(speed, targetSpeed, rate * delta)
|
return move_toward(speed, targetSpeed, rate * delta)
|
||||||
|
|
||||||
|
|
||||||
# Steering in the air. Extra speed left over from a surface fades slowly,
|
# Extra speed from leaving a surface fades slowly unless we steer against it.
|
||||||
# unless we steer against it.
|
|
||||||
func _air_steer(speed: float, horizontalInput: float, delta: float) -> float:
|
func _air_steer(speed: float, horizontalInput: float, delta: float) -> float:
|
||||||
var targetSpeed := horizontalInput * AIR_SPEED
|
var targetSpeed := horizontalInput * AIR_SPEED
|
||||||
var steeringSameWay := horizontalInput == 0.0 or signf(horizontalInput) == signf(speed)
|
var steeringSameWay := horizontalInput == 0.0 or signf(horizontalInput) == signf(speed)
|
||||||
|
|
@ -436,15 +325,12 @@ func _air_steer(speed: float, horizontalInput: float, delta: float) -> float:
|
||||||
return move_toward(speed, targetSpeed, AIR_ACCEL * delta)
|
return move_toward(speed, targetSpeed, AIR_ACCEL * delta)
|
||||||
|
|
||||||
|
|
||||||
# If the surface with this normal is within SURFACE_SNAP_DISTANCE, move flush
|
# Moves flush against the surface with this normal if it's within SURFACE_SNAP_DISTANCE.
|
||||||
# against it and return true.
|
|
||||||
func _snap_to_surface(normal: Vector2) -> bool:
|
func _snap_to_surface(normal: Vector2) -> bool:
|
||||||
# Test-move towards the surface (the "true" means don't actually move yet).
|
var hit := move_and_collide(-normal * SURFACE_SNAP_DISTANCE, true) # true = test only, don't move.
|
||||||
var hit := move_and_collide(-normal * SURFACE_SNAP_DISTANCE, true)
|
|
||||||
if hit == null:
|
if hit == null:
|
||||||
return false
|
return false
|
||||||
# Only count it if we'd hit the surface head on, so catching the corner of
|
# Only head-on, so catching the corner of an edge we're leaving doesn't pull us around it.
|
||||||
# an edge we're crawling off doesn't pull us back around it.
|
|
||||||
var hitHeadOn := hit.get_normal().dot(normal) >= 0.9
|
var hitHeadOn := hit.get_normal().dot(normal) >= 0.9
|
||||||
if not hitHeadOn:
|
if not hitHeadOn:
|
||||||
return false
|
return false
|
||||||
|
|
@ -452,7 +338,7 @@ func _snap_to_surface(normal: Vector2) -> bool:
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
|
||||||
# The normal of the surface we're stuck to, or (0, 0) if we're in the air.
|
# The normal of the surface we're stuck to, or zero in the air.
|
||||||
func _get_surface_normal() -> Vector2:
|
func _get_surface_normal() -> Vector2:
|
||||||
if is_on_floor():
|
if is_on_floor():
|
||||||
return Vector2.UP
|
return Vector2.UP
|
||||||
|
|
@ -463,25 +349,20 @@ func _get_surface_normal() -> Vector2:
|
||||||
return Vector2.ZERO
|
return Vector2.ZERO
|
||||||
|
|
||||||
|
|
||||||
# Which way the slime is facing, as an arrow.
|
|
||||||
func _get_facing() -> Vector2:
|
func _get_facing() -> Vector2:
|
||||||
if is_on_floor():
|
if is_on_floor():
|
||||||
if sprite.flip_h:
|
if sprite.flip_h:
|
||||||
return Vector2.LEFT
|
return Vector2.LEFT
|
||||||
return Vector2.RIGHT
|
return Vector2.RIGHT
|
||||||
if isStickingToCeiling:
|
if isStickingToCeiling:
|
||||||
# Upside down, so flip_h means the opposite.
|
|
||||||
if sprite.flip_h:
|
if sprite.flip_h:
|
||||||
return Vector2.RIGHT
|
return Vector2.RIGHT
|
||||||
return Vector2.LEFT
|
return Vector2.LEFT
|
||||||
return Vector2(0, wallFacingY)
|
return Vector2(0, wallFacingY)
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Hops ----
|
||||||
# HOPS
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# How high to hop from the floor: up to the ceiling if one's in reach.
|
|
||||||
func _get_hop_height() -> float:
|
func _get_hop_height() -> float:
|
||||||
var ceilingDistance := _get_hop_distance(Vector2.UP)
|
var ceilingDistance := _get_hop_distance(Vector2.UP)
|
||||||
if ceilingDistance >= 0.0:
|
if ceilingDistance >= 0.0:
|
||||||
|
|
@ -489,65 +370,54 @@ func _get_hop_height() -> float:
|
||||||
return HOP_NO_CEILING_HEIGHT
|
return HOP_NO_CEILING_HEIGHT
|
||||||
|
|
||||||
|
|
||||||
# How far to hop in this direction to reach a surface facing us within
|
# Distance to a surface facing us within HOP_REACH (plus HOP_OVERSHOOT), or -1 if none.
|
||||||
# HOP_REACH (plus HOP_OVERSHOOT). Returns -1 if there isn't one.
|
|
||||||
func _get_hop_distance(direction: Vector2) -> float:
|
func _get_hop_distance(direction: Vector2) -> float:
|
||||||
# Test-move our whole shape that way, to see if it hits anything.
|
|
||||||
var hit := move_and_collide(direction * HOP_REACH, true)
|
var hit := move_and_collide(direction * HOP_REACH, true)
|
||||||
if hit == null:
|
if hit == null:
|
||||||
return -1.0
|
return -1.0
|
||||||
# Only count surfaces facing back at us, not ones we'd just brush past.
|
|
||||||
var facingUs := hit.get_normal().dot(-direction) > 0.5
|
var facingUs := hit.get_normal().dot(-direction) > 0.5
|
||||||
if not facingUs:
|
if not facingUs:
|
||||||
return -1.0
|
return -1.0
|
||||||
return hit.get_travel().length() + HOP_OVERSHOOT
|
return hit.get_travel().length() + HOP_OVERSHOOT
|
||||||
|
|
||||||
|
|
||||||
# The speed to launch at so gravity slows us to a stop after `distance` pixels.
|
# Launch speed for gravity to stop us after `distance`: sqrt(2 x gravity x distance).
|
||||||
# This is the physics formula speed = square root of (2 x gravity x distance).
|
|
||||||
func _get_hop_speed(distance: float) -> float:
|
func _get_hop_speed(distance: float) -> float:
|
||||||
return sqrt(2.0 * get_gravity().y * distance)
|
return sqrt(2.0 * get_gravity().y * distance)
|
||||||
|
|
||||||
|
|
||||||
# Hops from the wall we're on to the one `distance` pixels away opposite. We go
|
# Sideways at crawling speed, with enough upward speed that gravity brings us
|
||||||
# sideways at crawling speed, and start with enough upward speed that gravity
|
# back to the same height as we arrive.
|
||||||
# brings us back to the same height just as we arrive.
|
|
||||||
func _hop_across(distance: float) -> void:
|
func _hop_across(distance: float) -> void:
|
||||||
var flightTime := distance / GROUND_SPEED
|
var flightTime := distance / GROUND_SPEED
|
||||||
velocity.x = wallNormalX * GROUND_SPEED
|
velocity.x = wallNormalX * GROUND_SPEED
|
||||||
# Gravity takes half the flight time to stop our rise and half to bring us back down.
|
|
||||||
velocity.y = -0.5 * get_gravity().y * flightTime
|
velocity.y = -0.5 * get_gravity().y * flightTime
|
||||||
isHoppingAcross = true
|
isHoppingAcross = true
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Edges ----
|
||||||
# EDGES
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# If we're pressing into the surface near its edge, start wrapping around it.
|
# Pressing into the surface near an edge starts wrapping around it. A direction
|
||||||
# Holding a direction along the surface picks that side. Otherwise we go the
|
# along the surface picks the side, otherwise facing does (or the other side if
|
||||||
# way we're facing, or the other way if only that side has an edge.
|
# only it has an edge).
|
||||||
func _start_edge_wrap(horizontalInput: float, verticalInput: float) -> bool:
|
func _start_edge_wrap(horizontalInput: float, verticalInput: float) -> bool:
|
||||||
var normal := _get_surface_normal()
|
var normal := _get_surface_normal()
|
||||||
if normal == Vector2.ZERO:
|
if normal == Vector2.ZERO:
|
||||||
return false # In the air.
|
return false
|
||||||
|
|
||||||
# The input as an arrow. Up is negative y, so flip verticalInput.
|
var inputDirection := Vector2(horizontalInput, -verticalInput) # Up is negative y.
|
||||||
var inputDirection := Vector2(horizontalInput, -verticalInput)
|
|
||||||
var pressingIntoSurface := inputDirection.dot(-normal) >= 0.5
|
var pressingIntoSurface := inputDirection.dot(-normal) >= 0.5
|
||||||
if not pressingIntoSurface:
|
if not pressingIntoSurface:
|
||||||
return false
|
return false
|
||||||
|
|
||||||
# Turn the sensors to point into the surface. Their "right" now runs along it.
|
# Point the sensors into the surface. Their local +x now runs along it.
|
||||||
edgeSensors.rotation = normal.angle() + PI / 2
|
edgeSensors.rotation = normal.angle() + PI / 2
|
||||||
var tangent := Vector2.from_angle(edgeSensors.rotation)
|
var tangent := Vector2.from_angle(edgeSensors.rotation)
|
||||||
|
|
||||||
# Holding a direction along the surface too? Then go that way.
|
|
||||||
var inputAlongSurface := inputDirection.dot(tangent)
|
var inputAlongSurface := inputDirection.dot(tangent)
|
||||||
if absf(inputAlongSurface) >= 0.5:
|
if absf(inputAlongSurface) >= 0.5:
|
||||||
return _try_edge_wrap(normal, tangent, signf(inputAlongSurface))
|
return _try_edge_wrap(normal, tangent, signf(inputAlongSurface))
|
||||||
|
|
||||||
# Otherwise try the side we're facing first, then the other side.
|
|
||||||
var facingSide := 1.0
|
var facingSide := 1.0
|
||||||
if _get_facing().dot(tangent) < 0:
|
if _get_facing().dot(tangent) < 0:
|
||||||
facingSide = -1.0
|
facingSide = -1.0
|
||||||
|
|
@ -556,52 +426,44 @@ func _start_edge_wrap(horizontalInput: float, verticalInput: float) -> bool:
|
||||||
return _try_edge_wrap(normal, tangent, -facingSide)
|
return _try_edge_wrap(normal, tangent, -facingSide)
|
||||||
|
|
||||||
|
|
||||||
# Tries to wrap around an edge on one side. side is 1 for the right-hand sensor
|
# side: 1 for the sensor along the tangent, -1 for the other.
|
||||||
# (along the tangent) or -1 for the left-hand one.
|
|
||||||
func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool:
|
func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool:
|
||||||
var sensor := leftEdgeSensor
|
var sensor := leftEdgeSensor
|
||||||
if side > 0:
|
if side > 0:
|
||||||
sensor = rightEdgeSensor
|
sensor = rightEdgeSensor
|
||||||
|
|
||||||
# If the sensor still touches the surface, there's no edge on that side.
|
|
||||||
sensor.force_raycast_update()
|
sensor.force_raycast_update()
|
||||||
if sensor.is_colliding():
|
if sensor.is_colliding():
|
||||||
return false
|
return false # Still over the surface: no edge.
|
||||||
# If the sensor starts inside something, it's an inside corner, not an edge.
|
|
||||||
if not _is_point_free(sensor.global_position):
|
if not _is_point_free(sensor.global_position):
|
||||||
return false
|
return false # Starts inside something: an inside corner, not an edge.
|
||||||
|
|
||||||
var direction := tangent * side
|
var direction := tangent * side
|
||||||
var corner = _find_edge_corner(normal, direction, edgeSensorRange + 2)
|
var corner = _find_edge_corner(normal, direction, edgeSensorRange + 2)
|
||||||
if corner == null:
|
if corner == null:
|
||||||
return false
|
return false
|
||||||
|
|
||||||
# Slide to just past the corner, swing around it, then carry on along the
|
# Slide to the corner, swing around it, then carry on down the side so we end flat against it.
|
||||||
# side of the platform so we end up flat against it.
|
|
||||||
var arcEndAngle := direction.angle()
|
var arcEndAngle := direction.angle()
|
||||||
var exitOffset := -normal * _get_radius()
|
var exitOffset := -normal * _get_radius()
|
||||||
var arcStartAngle := normal.angle()
|
var arcStartAngle := normal.angle()
|
||||||
return _begin_wrap(corner, arcEndAngle, exitOffset, direction, arcStartAngle)
|
return _begin_wrap(corner, arcEndAngle, exitOffset, direction, arcStartAngle)
|
||||||
|
|
||||||
|
|
||||||
# Finds the corner where the surface we're on ends, if it's within `reach`
|
# The corner where our surface ends, if within `reach` ahead in `direction`, else null.
|
||||||
# pixels ahead of us in `direction`. Returns null if there's no edge that close.
|
|
||||||
# (It returns either a Vector2 or null, which is why the return type is Variant.)
|
|
||||||
func _find_edge_corner(normal: Vector2, direction: Vector2, reach: float) -> Variant:
|
func _find_edge_corner(normal: Vector2, direction: Vector2, reach: float) -> Variant:
|
||||||
var radius := _get_radius()
|
var radius := _get_radius()
|
||||||
var currentPosition := global_position
|
var currentPosition := global_position
|
||||||
|
|
||||||
# 1. Find the surface we're on: shoot a ray into it from a little way
|
# 1. Find our surface with a ray from slightly behind us.
|
||||||
# behind us (away from the edge).
|
|
||||||
var rayStart := currentPosition - direction * radius * 0.5
|
var rayStart := currentPosition - direction * radius * 0.5
|
||||||
var surfaceHit := _cast_ray(rayStart, rayStart - normal * radius * 2)
|
var surfaceHit := _cast_ray(rayStart, rayStart - normal * radius * 2)
|
||||||
if surfaceHit.is_empty() or surfaceHit.normal.dot(normal) < 0.5:
|
if surfaceHit.is_empty() or surfaceHit.normal.dot(normal) < 0.5:
|
||||||
return null
|
return null
|
||||||
|
|
||||||
# 2. Find the side of the platform: shoot a ray from `reach` ahead of us
|
# 2. Find the platform's side with a ray from `reach` ahead back towards us,
|
||||||
# back towards us, just inside the surface (2 pixels deep). If the
|
# 2px inside the surface. If the surface goes on past `reach`, the ray
|
||||||
# surface carries on further than `reach`, the ray starts inside it and
|
# starts inside it and hits nothing.
|
||||||
# hits nothing.
|
|
||||||
var surfacePoint: Vector2 = surfaceHit.position
|
var surfacePoint: Vector2 = surfaceHit.position
|
||||||
var howFarAhead := (currentPosition - surfacePoint).dot(direction)
|
var howFarAhead := (currentPosition - surfacePoint).dot(direction)
|
||||||
var justInsideSurface := surfacePoint - normal * 2 + direction * howFarAhead
|
var justInsideSurface := surfacePoint - normal * 2 + direction * howFarAhead
|
||||||
|
|
@ -610,89 +472,74 @@ func _find_edge_corner(normal: Vector2, direction: Vector2, reach: float) -> Var
|
||||||
return null
|
return null
|
||||||
var corner: Vector2 = sideHit.position + normal * 2
|
var corner: Vector2 = sideHit.position + normal * 2
|
||||||
|
|
||||||
# 3. Make sure it's a real edge: there must be empty space just past the
|
# 3. A real edge has empty space past it. Tile seams and the foot of a wall don't.
|
||||||
# corner, behind the surface. Seams between tiles (and the bottom of a
|
|
||||||
# wall) have something solid there.
|
|
||||||
if not _is_point_free(corner + direction * 2 - normal * 2):
|
if not _is_point_free(corner + direction * 2 - normal * 2):
|
||||||
return null
|
return null
|
||||||
return corner
|
return corner
|
||||||
|
|
||||||
|
|
||||||
# If this frame's movement along the surface would take us past its edge, stop
|
# Stops us at an edge we'd cross this frame, until we've held towards it for HOLD_TIME.
|
||||||
# at the edge instead. Once we've held towards the edge for HOLD_TIME, let go.
|
|
||||||
func _hold_at_edge(delta: float) -> void:
|
func _hold_at_edge(delta: float) -> void:
|
||||||
var normal := _get_surface_normal()
|
var normal := _get_surface_normal()
|
||||||
var tangent := normal.orthogonal() # orthogonal() turns an arrow a quarter turn.
|
var tangent := normal.orthogonal()
|
||||||
var speedAlongSurface := velocity.dot(tangent)
|
var speedAlongSurface := velocity.dot(tangent)
|
||||||
if normal == Vector2.ZERO or is_zero_approx(speedAlongSurface):
|
if normal == Vector2.ZERO or is_zero_approx(speedAlongSurface):
|
||||||
timeSpentHoldingEdge = 0.0
|
timeSpentHoldingEdge = 0.0
|
||||||
return # In the air, or not moving along the surface.
|
return
|
||||||
|
|
||||||
var direction := tangent * signf(speedAlongSurface)
|
var direction := tangent * signf(speedAlongSurface)
|
||||||
# Climbing up past the top of a wall swings over onto it instead (see _start_wall_top_wrap).
|
# Climbing past a wall's top swings over it instead (_start_wall_top_wrap).
|
||||||
var onWall := absf(normal.x) > 0.5
|
var onWall := absf(normal.x) > 0.5
|
||||||
if onWall and direction.y < 0:
|
if onWall and direction.y < 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
var distanceThisFrame := absf(speedAlongSurface) * delta
|
var distanceThisFrame := absf(speedAlongSurface) * delta
|
||||||
var corner = _find_edge_corner(normal, direction, distanceThisFrame + 1)
|
var corner = _find_edge_corner(normal, direction, distanceThisFrame + 1)
|
||||||
var distanceToEdge := INF # INF means "infinitely far": no edge nearby.
|
var distanceToEdge := INF
|
||||||
if corner != null:
|
if corner != null:
|
||||||
distanceToEdge = (corner - global_position).dot(direction)
|
distanceToEdge = (corner - global_position).dot(direction)
|
||||||
if distanceToEdge > distanceThisFrame:
|
if distanceToEdge > distanceThisFrame:
|
||||||
timeSpentHoldingEdge = 0.0
|
timeSpentHoldingEdge = 0.0
|
||||||
return # We won't reach the edge this frame.
|
return
|
||||||
|
|
||||||
if timeSpentHoldingEdge >= HOLD_TIME:
|
if timeSpentHoldingEdge >= HOLD_TIME:
|
||||||
return # Held long enough: carry on over the edge.
|
return
|
||||||
timeSpentHoldingEdge += delta
|
timeSpentHoldingEdge += delta
|
||||||
|
|
||||||
# Change our speed along the surface so we stop exactly at the edge.
|
# Set our speed along the surface to stop exactly at the edge.
|
||||||
var speedToReachEdge := maxf(distanceToEdge, 0.0) / delta
|
var speedToReachEdge := maxf(distanceToEdge, 0.0) / delta
|
||||||
velocity += direction * (speedToReachEdge - absf(speedAlongSurface))
|
velocity += direction * (speedToReachEdge - absf(speedAlongSurface))
|
||||||
|
|
||||||
|
|
||||||
# We just climbed past the top of a wall: find its top corner and swing over
|
# We've just climbed past a wall's top corner, so _find_edge_corner (which looks
|
||||||
# onto the top. (By now we've already gone past the corner, so
|
# from the surface we're on) can't see it. Find it with our own raycasts.
|
||||||
# _find_edge_corner, which looks from the surface we're on, can't see it.
|
|
||||||
# That's why this has its own raycasts.)
|
|
||||||
func _start_wall_top_wrap() -> bool:
|
func _start_wall_top_wrap() -> bool:
|
||||||
var wallNormal := wallNormalX
|
var wallNormal := wallNormalX
|
||||||
var radius := _get_radius()
|
var radius := _get_radius()
|
||||||
var currentPosition := global_position
|
var currentPosition := global_position
|
||||||
|
|
||||||
# Find the top of the platform: shoot a ray down just past the wall.
|
|
||||||
var aboveTop := currentPosition + Vector2(-wallNormal * (radius + 2), -radius)
|
var aboveTop := currentPosition + Vector2(-wallNormal * (radius + 2), -radius)
|
||||||
var topHit := _cast_ray(aboveTop, aboveTop + Vector2(0, radius * 3))
|
var topHit := _cast_ray(aboveTop, aboveTop + Vector2(0, radius * 3))
|
||||||
if topHit.is_empty() or topHit.normal.y > -0.5:
|
if topHit.is_empty() or topHit.normal.y > -0.5:
|
||||||
return false
|
return false
|
||||||
var topY: float = topHit.position.y
|
var topY: float = topHit.position.y
|
||||||
|
|
||||||
# Find the side of the platform, just below its top.
|
|
||||||
var besideWall := Vector2(currentPosition.x + wallNormal * 2, topY + 2)
|
var besideWall := Vector2(currentPosition.x + wallNormal * 2, topY + 2)
|
||||||
var sideHit := _cast_ray(besideWall, Vector2(currentPosition.x - wallNormal * radius * 2, topY + 2))
|
var sideHit := _cast_ray(besideWall, Vector2(currentPosition.x - wallNormal * radius * 2, topY + 2))
|
||||||
if sideHit.is_empty() or sideHit.normal.x * wallNormal < 0.5:
|
if sideHit.is_empty() or sideHit.normal.x * wallNormal < 0.5:
|
||||||
return false
|
return false
|
||||||
|
|
||||||
var corner := Vector2(sideHit.position.x, topY)
|
var corner := Vector2(sideHit.position.x, topY)
|
||||||
# After the arc, carry on onto the platform instead of balancing on its corner.
|
var exitOffset := Vector2(-wallNormal * radius, 0) # Onto the platform, not balanced on the corner.
|
||||||
var exitOffset := Vector2(-wallNormal * radius, 0)
|
|
||||||
return _begin_wrap(corner, Vector2.UP.angle(), exitOffset, Vector2.UP)
|
return _begin_wrap(corner, Vector2.UP.angle(), exitOffset, Vector2.UP)
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Edge wrap ----
|
||||||
# EDGE WRAP (swinging around a corner)
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Starts swinging around `corner`:
|
# Swings around `corner` to arcEndAngle, then moves by exitOffset, ending stuck
|
||||||
# - arcStartAngle (optional): first move in a straight line to this point on
|
# to the surface facing ontoNormal. With arcStartAngle, first moves straight to
|
||||||
# the arc. If left out, the arc starts from where we are now.
|
# that point on the arc. Returns false if there's no room at the end.
|
||||||
# - arcEndAngle: where the arc ends, as an angle around the corner.
|
|
||||||
# - exitOffset: then move this much in a straight line, onto the new surface.
|
|
||||||
# - ontoNormal: the normal of the surface we'll end up stuck to.
|
|
||||||
# Returns false (and doesn't start) if there's no room where we'd end up.
|
|
||||||
func _begin_wrap(corner: Vector2, arcEndAngle: float, exitOffset: Vector2, ontoNormal: Vector2, arcStartAngle := NAN) -> bool:
|
func _begin_wrap(corner: Vector2, arcEndAngle: float, exitOffset: Vector2, ontoNormal: Vector2, arcStartAngle := NAN) -> bool:
|
||||||
# How far from the corner the arc keeps us: our radius, plus a little gap.
|
|
||||||
var arcRadius := _get_radius() + WRAP_MARGIN
|
var arcRadius := _get_radius() + WRAP_MARGIN
|
||||||
var endPosition := corner + Vector2.from_angle(arcEndAngle) * arcRadius + exitOffset
|
var endPosition := corner + Vector2.from_angle(arcEndAngle) * arcRadius + exitOffset
|
||||||
if not _is_spot_free(endPosition):
|
if not _is_spot_free(endPosition):
|
||||||
|
|
@ -700,46 +547,39 @@ func _begin_wrap(corner: Vector2, arcEndAngle: float, exitOffset: Vector2, ontoN
|
||||||
|
|
||||||
wrapCorner = corner
|
wrapCorner = corner
|
||||||
wrapEntryStart = global_position
|
wrapEntryStart = global_position
|
||||||
# NAN means "not a number". It's the default when no arcStartAngle is given.
|
if is_nan(arcStartAngle): # NAN ("not a number") means it wasn't given.
|
||||||
if is_nan(arcStartAngle):
|
|
||||||
# Start the arc right where we are.
|
|
||||||
var fromCorner := global_position - corner
|
var fromCorner := global_position - corner
|
||||||
wrapFromAngle = fromCorner.angle()
|
wrapFromAngle = fromCorner.angle()
|
||||||
wrapFromDistance = fromCorner.length()
|
wrapFromDistance = fromCorner.length()
|
||||||
wrapEntryLength = 0.0
|
wrapEntryLength = 0.0
|
||||||
else:
|
else:
|
||||||
# Move straight to the start of the arc first.
|
|
||||||
wrapFromAngle = arcStartAngle
|
wrapFromAngle = arcStartAngle
|
||||||
wrapFromDistance = arcRadius
|
wrapFromDistance = arcRadius
|
||||||
var arcStart := corner + Vector2.from_angle(arcStartAngle) * arcRadius
|
var arcStart := corner + Vector2.from_angle(arcStartAngle) * arcRadius
|
||||||
wrapEntryLength = global_position.distance_to(arcStart)
|
wrapEntryLength = global_position.distance_to(arcStart)
|
||||||
wrapToAngle = arcEndAngle
|
wrapToAngle = arcEndAngle
|
||||||
wrapToDistance = arcRadius
|
wrapToDistance = arcRadius
|
||||||
# The length of a circle's arc is its angle (in radians) times its radius.
|
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * arcRadius # Angle x radius.
|
||||||
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * arcRadius
|
|
||||||
wrapExitOffset = exitOffset
|
wrapExitOffset = exitOffset
|
||||||
|
|
||||||
wrapFromRotation = sprite.rotation
|
wrapFromRotation = sprite.rotation
|
||||||
wrapToRotation = ontoNormal.angle() + PI / 2 # Turned to stand on the new surface.
|
wrapToRotation = ontoNormal.angle() + PI / 2
|
||||||
wrapOntoNormal = ontoNormal
|
wrapOntoNormal = ontoNormal
|
||||||
|
|
||||||
wrapDistanceTravelled = 0.0
|
wrapDistanceTravelled = 0.0
|
||||||
# Start at the speed we were already going, so crawling into a wrap is seamless.
|
wrapSpeed = minf(velocity.length(), GROUND_SPEED) # Keep our speed going in.
|
||||||
wrapSpeed = minf(velocity.length(), GROUND_SPEED)
|
|
||||||
velocity = Vector2.ZERO
|
velocity = Vector2.ZERO
|
||||||
isWrapping = true
|
isWrapping = true
|
||||||
wrapSound.play()
|
wrapSound.play()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
|
||||||
# Moves us along the wrap a little each frame.
|
|
||||||
func _update_wrap(delta: float) -> void:
|
func _update_wrap(delta: float) -> void:
|
||||||
# Speed up to crawling speed, like we would on a surface.
|
|
||||||
wrapSpeed = move_toward(wrapSpeed, GROUND_SPEED, GROUND_ACCEL * delta)
|
wrapSpeed = move_toward(wrapSpeed, GROUND_SPEED, GROUND_ACCEL * delta)
|
||||||
var totalLength := wrapEntryLength + wrapArcLength + wrapExitOffset.length()
|
var totalLength := wrapEntryLength + wrapArcLength + wrapExitOffset.length()
|
||||||
wrapDistanceTravelled = minf(wrapDistanceTravelled + wrapSpeed * delta, totalLength)
|
wrapDistanceTravelled = minf(wrapDistanceTravelled + wrapSpeed * delta, totalLength)
|
||||||
|
|
||||||
# Part 1: the straight entry, to the start of the arc.
|
# Entry.
|
||||||
var distance := wrapDistanceTravelled
|
var distance := wrapDistanceTravelled
|
||||||
if distance < wrapEntryLength:
|
if distance < wrapEntryLength:
|
||||||
var arcStart := wrapCorner + Vector2.from_angle(wrapFromAngle) * wrapFromDistance
|
var arcStart := wrapCorner + Vector2.from_angle(wrapFromAngle) * wrapFromDistance
|
||||||
|
|
@ -747,7 +587,7 @@ func _update_wrap(delta: float) -> void:
|
||||||
return
|
return
|
||||||
distance -= wrapEntryLength
|
distance -= wrapEntryLength
|
||||||
|
|
||||||
# Part 2: the arc. arcProgress goes from 0 (start) to 1 (end).
|
# Arc. Progress goes from 0 to 1.
|
||||||
var arcProgress := 1.0
|
var arcProgress := 1.0
|
||||||
if wrapArcLength > 0.0:
|
if wrapArcLength > 0.0:
|
||||||
arcProgress = minf(distance / wrapArcLength, 1.0)
|
arcProgress = minf(distance / wrapArcLength, 1.0)
|
||||||
|
|
@ -756,8 +596,7 @@ func _update_wrap(delta: float) -> void:
|
||||||
var positionOnArc := wrapCorner + Vector2.from_angle(angle) * distanceFromCorner
|
var positionOnArc := wrapCorner + Vector2.from_angle(angle) * distanceFromCorner
|
||||||
sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, arcProgress)
|
sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, arcProgress)
|
||||||
|
|
||||||
# Part 3: the straight exit, once we're past the end of the arc.
|
# Exit.
|
||||||
# exitProgress goes from 0 to 1 along it.
|
|
||||||
var exitProgress := 0.0
|
var exitProgress := 0.0
|
||||||
if wrapExitOffset != Vector2.ZERO:
|
if wrapExitOffset != Vector2.ZERO:
|
||||||
exitProgress = maxf(distance - wrapArcLength, 0.0) / wrapExitOffset.length()
|
exitProgress = maxf(distance - wrapArcLength, 0.0) / wrapExitOffset.length()
|
||||||
|
|
@ -767,17 +606,14 @@ func _update_wrap(delta: float) -> void:
|
||||||
_finish_wrap(delta)
|
_finish_wrap(delta)
|
||||||
|
|
||||||
|
|
||||||
# The wrap is done: settle onto the new surface.
|
|
||||||
func _finish_wrap(delta: float) -> void:
|
func _finish_wrap(delta: float) -> void:
|
||||||
# Close the small gap left by WRAP_MARGIN so we're touching the new surface.
|
# Close the WRAP_MARGIN gap with move_and_slide() so is_on_floor()/is_on_wall()
|
||||||
# We use move_and_slide() for this so Godot's is_on_floor()/is_on_wall()
|
|
||||||
# describe where we are now, not where we were before the wrap.
|
# describe where we are now, not where we were before the wrap.
|
||||||
velocity = -wrapOntoNormal * (WRAP_MARGIN + 1) / delta
|
velocity = -wrapOntoNormal * (WRAP_MARGIN + 1) / delta
|
||||||
|
|
||||||
# Godot still thinks we're on whatever floor we were on before the wrap. So
|
# Godot still thinks we're on the floor we started from, so it would snap us
|
||||||
# it would try to snap us down onto it (floor_snap_length), and count the
|
# down to it and count the wall as floor (floor_block_on_wall). Turn both off
|
||||||
# wall we're pushing into as floor (floor_block_on_wall). Switch both off
|
# for this move unless we're ending on a floor.
|
||||||
# for this one move, unless we're actually ending on a floor.
|
|
||||||
var endingOnFloor := wrapOntoNormal == Vector2.UP
|
var endingOnFloor := wrapOntoNormal == Vector2.UP
|
||||||
var savedFloorSnap := floor_snap_length
|
var savedFloorSnap := floor_snap_length
|
||||||
var savedBlockOnWall := floor_block_on_wall
|
var savedBlockOnWall := floor_block_on_wall
|
||||||
|
|
@ -788,8 +624,7 @@ func _finish_wrap(delta: float) -> void:
|
||||||
floor_snap_length = savedFloorSnap
|
floor_snap_length = savedFloorSnap
|
||||||
floor_block_on_wall = savedBlockOnWall
|
floor_block_on_wall = savedBlockOnWall
|
||||||
|
|
||||||
# Keep moving at the same speed along the new surface, so leaving the wrap
|
# Keep our speed along the new surface so leaving the wrap isn't a sudden stop.
|
||||||
# isn't a sudden stop. Normal movement speeds up or slows down from here.
|
|
||||||
var exitDirection: Vector2
|
var exitDirection: Vector2
|
||||||
if wrapExitOffset != Vector2.ZERO:
|
if wrapExitOffset != Vector2.ZERO:
|
||||||
exitDirection = wrapExitOffset.normalized()
|
exitDirection = wrapExitOffset.normalized()
|
||||||
|
|
@ -797,7 +632,6 @@ func _finish_wrap(delta: float) -> void:
|
||||||
exitDirection = Vector2.from_angle(wrapToAngle).orthogonal()
|
exitDirection = Vector2.from_angle(wrapToAngle).orthogonal()
|
||||||
velocity = exitDirection * wrapSpeed
|
velocity = exitDirection * wrapSpeed
|
||||||
|
|
||||||
# Now stuck to the new surface.
|
|
||||||
isWrapping = false
|
isWrapping = false
|
||||||
isStickingToCeiling = wrapOntoNormal.y > 0.5
|
isStickingToCeiling = wrapOntoNormal.y > 0.5
|
||||||
isStickingToWall = absf(wrapOntoNormal.x) > 0.5
|
isStickingToWall = absf(wrapOntoNormal.x) > 0.5
|
||||||
|
|
@ -807,16 +641,11 @@ func _finish_wrap(delta: float) -> void:
|
||||||
ignoreCeilingUnstickUntilRelease = isStickingToCeiling
|
ignoreCeilingUnstickUntilRelease = isStickingToCeiling
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Effects ----
|
||||||
# EFFECTS
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Squish sound and a little burst of slime where we hit the surface.
|
|
||||||
func _splat(normal: Vector2) -> void:
|
func _splat(normal: Vector2) -> void:
|
||||||
splatParticles.global_position = global_position - normal * _get_radius()
|
splatParticles.global_position = global_position - normal * _get_radius()
|
||||||
# The particles jump to a new spot rather than moving there, so don't
|
splatParticles.reset_physics_interpolation() # A jump, not movement: don't smooth from the last spot.
|
||||||
# smooth (interpolate) from where they were last time.
|
|
||||||
splatParticles.reset_physics_interpolation()
|
|
||||||
splatParticles.direction = normal
|
splatParticles.direction = normal
|
||||||
splatParticles.restart()
|
splatParticles.restart()
|
||||||
|
|
||||||
|
|
@ -826,22 +655,18 @@ func _splat(normal: Vector2) -> void:
|
||||||
landSound.play()
|
landSound.play()
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# ---- Physics queries (all ignore the slime itself) ----
|
||||||
# PHYSICS QUERIES (asking Godot what's around us)
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
func _get_radius() -> float:
|
func _get_radius() -> float:
|
||||||
return (collisionShape.shape as CircleShape2D).radius
|
return (collisionShape.shape as CircleShape2D).radius
|
||||||
|
|
||||||
|
|
||||||
# Shoots a ray from `from` to `to`, ignoring the slime itself. Returns what it
|
# Returns the hit (.position, .normal), or an empty Dictionary if nothing.
|
||||||
# hit as a Dictionary (with .position and .normal), or an empty one if nothing.
|
|
||||||
func _cast_ray(from: Vector2, to: Vector2) -> Dictionary:
|
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)
|
||||||
|
|
||||||
|
|
||||||
# True if nothing solid (other than us) is at this exact point.
|
|
||||||
func _is_point_free(point: Vector2) -> bool:
|
func _is_point_free(point: Vector2) -> bool:
|
||||||
var query := PhysicsPointQueryParameters2D.new()
|
var query := PhysicsPointQueryParameters2D.new()
|
||||||
query.position = point
|
query.position = point
|
||||||
|
|
@ -850,7 +675,7 @@ func _is_point_free(point: Vector2) -> bool:
|
||||||
return get_world_2d().direct_space_state.intersect_point(query, 1).is_empty()
|
return get_world_2d().direct_space_state.intersect_point(query, 1).is_empty()
|
||||||
|
|
||||||
|
|
||||||
# True if the slime's whole shape would fit at this spot without touching anything.
|
# Whether our whole shape fits at this spot.
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue