W6: Water and swimming

The simulation has a global sea level (Simulation.sea_level, changed with
set_sea_level and read every tick, so the tide can move it mid-match).
Walkers wade more slowly in water over WADE_DEPTH; in water SWIM_DEPTH deep
they swim: floating head-out, sprint-swimming, diving with crouch, kicking
out with jump, and standing again in the shallows. Falling into deep water
does no fall damage. Water tracks breath, stamina and drowning in whole
ticks: swimming spends stamina, an exhausted swimmer slips under, and with
no breath left the tide phase's drown damage lands once a second. All of
it, plus the sea level, is in the state hash.

WaterView draws the sea surface at the simulation's level with a
procedural shader: depth-tinted colour, foam at the waterline, ripples.

Closes #13

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 12:24:50 -05:00
co-authored by Claude Opus 5.5
parent 5db53de4e8
commit be987b6f82
18 changed files with 641 additions and 10 deletions
+97 -7
View File
@@ -1,6 +1,6 @@
## Command-driven player movement: walk, sprint, crouch, jump, gravity, slopes,
## step-up, sliding and fall damage. Deterministic: it depends only on the entity, the
## command and the ground.
## step-up, sliding, fall damage, wading and swimming. Deterministic: it depends only on
## the entity, the command, the ground and the sea level.
class_name Movement
extends RefCounted
@@ -9,7 +9,9 @@ const DT: float = 1.0 / Tuning.TICK_RATE_HZ
## Advances one player by one tick. `command` may be null, meaning no input this tick:
## the player keeps looking the same way and stops trying to move, but still falls.
static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -> void:
## `sea_level` is the water surface; the default means no water at all. Breath, stamina
## and drowning are Water's job, after this.
static func step(player: SimEntity, command: PlayerCommand, ground: SimGround, sea_level: float = -INF) -> void:
var wish: Vector3 = Vector3.ZERO
var wants_jump: bool = false
player.crouching = false
@@ -20,6 +22,20 @@ static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -
wants_jump = command.is_pressed(PlayerCommand.BUTTON_JUMP) and not player.crouching
wish = _wish_velocity(command, player.crouching)
if player.swimming:
if wants_jump and player.stamina_ticks > 0 and _at_surface(player, sea_level):
# Kick up out of the water. Airborne from here; back in if it lands in deep water.
player.swimming = false
player.on_ground = false
player.velocity.y = Tuning.JUMP_VELOCITY
else:
_swim(player, command, ground, sea_level)
return
# Wading: water over the feet slows walkers and stops sprinting.
if player.on_ground and sea_level - player.position.y > Tuning.WADE_DEPTH:
wish = wish.limit_length(Tuning.WADE_SPEED)
var velocity: Vector3 = player.velocity
if player.on_ground:
velocity.x = wish.x
@@ -33,7 +49,10 @@ static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -
velocity.z += (wish.z - velocity.z) * Tuning.AIR_CONTROL
velocity = _move_horizontally(player, velocity, ground)
_move_vertically(player, velocity, ground)
_move_vertically(player, velocity, ground, sea_level)
# Walked, or the tide rose, into water deep enough to swim.
if not player.swimming and player.on_ground and Water.is_deep(sea_level, player.position.y):
_enter_water(player)
## Height of the player's body, which shrinks while crouching.
@@ -82,12 +101,13 @@ static func _move_horizontally(player: SimEntity, velocity: Vector3, ground: Sim
static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround) -> bool:
var step: float = Tuning.STEP_HEIGHT if player.on_ground else 0.0
var supported: bool = player.on_ground or player.swimming
var step: float = Tuning.STEP_HEIGHT if supported else 0.0
# On foot, the body is tested where it will stand: on whatever it steps up onto at
# the target. Otherwise the next riser of a stair narrower than the body would block
# a player who hasn't been lifted onto the current one yet.
var body: Vector3 = target
if player.on_ground:
if supported:
body.y = maxf(target.y, ground.support_height(target.x, target.z, target.y + step))
if ground.body_blocked(body, Tuning.PLAYER_RADIUS, body_height(player), step):
return false
@@ -100,7 +120,7 @@ static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround)
return true
static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGround) -> void:
static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGround, sea_level: float) -> void:
var feet: Vector3 = player.position
if player.on_ground:
var reach: float = feet.y + Tuning.STEP_HEIGHT
@@ -125,6 +145,12 @@ static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGr
player.position.y = new_y
var support: float = ground.support_height(feet.x, feet.z, maxf(feet.y, new_y))
# Falling into deep water: the water catches the player, with no fall damage.
if velocity.y <= 0.0 and Water.is_deep(sea_level, support) and new_y <= sea_level - Tuning.SWIM_DEPTH:
player.position.y = maxf(new_y, support)
player.velocity = velocity
_enter_water(player)
return
if new_y > support:
player.velocity = velocity
return
@@ -157,3 +183,67 @@ static func _land(player: SimEntity, impact_speed: float) -> void:
var excess: float = drop - Tuning.FALL_DAMAGE_SAFE_METRES
if excess > 0.0:
player.health = maxi(player.health - roundi(excess * Tuning.FALL_DAMAGE_PER_METRE), 0)
static func _enter_water(player: SimEntity) -> void:
player.swimming = true
player.on_ground = false
player.velocity.y *= Tuning.WATER_ENTRY_DAMP
## True when a swimmer is up at the surface, head out, and so can kick out of the water.
static func _at_surface(player: SimEntity, sea_level: float) -> bool:
return player.position.y >= sea_level - Tuning.SWIM_FLOAT_DEPTH - 0.2
## One tick in deep water. Swimmers move at SWIM_SPEED (faster sprinting while stamina
## lasts), float with the head out, dive while crouch is held, and rest on the sea floor
## rather than sinking through it. Out of stamina, they slip under. Where the water
## over the floor gets shallow they stand up and walk.
static func _swim(player: SimEntity, command: PlayerCommand, ground: SimGround, sea_level: float) -> void:
var has_stamina: bool = player.stamina_ticks > 0
var speed: float = Tuning.SWIM_SPEED
var diving: bool = false
var move: Vector3 = Vector3.ZERO
if command != null:
var input: Vector2 = command.move_vector()
if input.length() > 1.0:
input = input.normalized()
var angle: float = command.yaw_radians()
var forward: Vector3 = Vector3(sin(angle), 0.0, cos(angle))
var right: Vector3 = Vector3(-cos(angle), 0.0, sin(angle))
move = right * input.x + forward * input.y
diving = command.is_pressed(PlayerCommand.BUTTON_CROUCH)
if command.is_pressed(PlayerCommand.BUTTON_SPRINT) and has_stamina and input.y > 0.0:
speed = Tuning.SWIM_SPRINT_SPEED
if not has_stamina:
speed *= Tuning.SWIM_EXHAUSTED_FACTOR
var velocity: Vector3 = move * speed
var float_depth: float = Tuning.SWIM_FLOAT_DEPTH if has_stamina else Tuning.SWIM_EXHAUSTED_DEPTH
var wanted: float = -Tuning.SWIM_VERTICAL_SPEED
if not diving:
wanted = clampf((sea_level - float_depth - player.position.y) * 4.0, -Tuning.SWIM_VERTICAL_SPEED, Tuning.SWIM_VERTICAL_SPEED)
velocity.y = move_toward(player.velocity.y, wanted, Tuning.SWIM_VERTICAL_ACCEL * DT)
velocity = _move_horizontally(player, velocity, ground)
var new_y: float = player.position.y + velocity.y * DT
var floor_y: float = ground.support_height(player.position.x, player.position.z, maxf(player.position.y, new_y) + Tuning.STEP_HEIGHT)
if new_y <= floor_y:
new_y = floor_y
velocity.y = maxf(velocity.y, 0.0)
if velocity.y > 0.0 and ground.body_blocked(Vector3(player.position.x, new_y, player.position.z), Tuning.PLAYER_RADIUS, body_height(player), 0.0):
new_y = player.position.y
velocity.y = 0.0
player.position.y = new_y
player.velocity = velocity
# Shallow enough to stand: back on foot, without fall damage.
if sea_level - floor_y < Tuning.SWIM_EXIT_DEPTH:
player.swimming = false
if floor_y >= player.position.y - Tuning.GROUND_SNAP:
player.position.y = floor_y
player.velocity.y = 0.0
player.on_ground = _walkable_support(ground, player.position)
else:
player.on_ground = false