## The start of a match: a sky barge drifts across the island on a seeded straight route, ## carrying every player. Each one leaps when they choose, falls, opens a wing-sail ## (by hand, or automatically near the ground) and steers down. Anyone still aboard ## when the barge reaches the end of its route is thrown off. ## ## The route is drawn from the "drop" stream: a heading, and a point near the island's ## middle that the route passes through, so it always crosses land. Movement handles ## falling and gliding; this owns the barge, boarding and leaping. class_name Drop extends RefCounted ## Player drop states, in SimEntity.drop_state. const NONE: int = 0 const ABOARD: int = 1 const FREEFALL: int = 2 const GLIDING: int = 3 ## Where the route starts and ends, at barge altitude. var start: Vector3 = Vector3.ZERO var finish: Vector3 = Vector3.ZERO ## Ticks the barge takes from start to finish. var duration_ticks: int = 0 ## Simulation tick the drop began on. var began_tick: int = 0 ## True once the barge has reached the end and thrown everyone off. var finished: bool = false ## Plans the route for `island` from the simulation's "drop" stream. static func plan(island_ground: SimGround, rng: SimRng, tick: int) -> Drop: var drop: Drop = Drop.new() var stream: RandomNumberGenerator = rng.stream("drop") var size: float = Tuning.WORLD_SIZE_METRES var centre: Vector2 = Vector2(size, size) * 0.5 var heading: float = stream.randf() * TAU var jitter_angle: float = stream.randf() * TAU var jitter: float = sqrt(stream.randf()) * Tuning.BARGE_CENTRE_JITTER var through: Vector2 = centre + Vector2(cos(jitter_angle), sin(jitter_angle)) * jitter # Pull the crossing point onto land if the jitter put it over water. var tries: int = 0 while island_ground.terrain_height(through.x, through.y) <= Tuning.ISLAND_BEACH_TOP and tries < 16: through = through.lerp(centre, 0.5) tries += 1 var direction: Vector2 = Vector2(cos(heading), sin(heading)) var margin: float = Tuning.BARGE_ROUTE_MARGIN var back: float = _distance_to_edge(through, -direction, margin, size) var ahead: float = _distance_to_edge(through, direction, margin, size) var a: Vector2 = through - direction * back var b: Vector2 = through + direction * ahead drop.start = Vector3(a.x, Tuning.BARGE_ALTITUDE, a.y) drop.finish = Vector3(b.x, Tuning.BARGE_ALTITUDE, b.y) drop.duration_ticks = maxi(ceili(a.distance_to(b) / Tuning.BARGE_SPEED * Tuning.TICK_RATE_HZ), 1) drop.began_tick = tick return drop ## How far along the route the barge is at `tick`, from 0 to 1. func progress(tick: int) -> float: return clampf(float(tick - began_tick) / duration_ticks, 0.0, 1.0) func barge_position(tick: int) -> Vector3: return start.lerp(finish, progress(tick)) func barge_velocity() -> Vector3: return (finish - start) / (float(duration_ticks) / Tuning.TICK_RATE_HZ) ## True once the barge is at the end of its route. func is_over(tick: int) -> bool: return tick - began_tick >= duration_ticks ## Points along the route every `step` metres, at ground level, for tests and maps. func route_samples(step: float) -> PackedVector2Array: var points: PackedVector2Array = PackedVector2Array() var a: Vector2 = Vector2(start.x, start.z) var b: Vector2 = Vector2(finish.x, finish.z) var count: int = maxi(ceili(a.distance_to(b) / step), 1) for index: int in count + 1: points.append(a.lerp(b, float(index) / count)) return points ## Puts a player aboard: carried with the barge, not moving on their own. static func board(player: SimEntity, at: Vector3) -> void: player.drop_state = ABOARD player.position = at player.velocity = Vector3.ZERO player.on_ground = false player.swimming = false ## Throws a player off the barge into freefall, keeping some of the barge's speed. func leap(player: SimEntity, tick: int) -> void: player.drop_state = FREEFALL player.position = barge_position(tick) var carried: Vector3 = barge_velocity() * Tuning.LEAP_CARRY player.velocity = Vector3(carried.x, 0.0, carried.z) player.on_ground = false ## Distance from `from` along unit `direction` to the square [margin, size - margin]. static func _distance_to_edge(from: Vector2, direction: Vector2, margin: float, size: float) -> float: var best: float = INF for axis: int in 2: var d: float = direction[axis] if absf(d) < 0.000001: continue var edge: float = (size - margin) if d > 0.0 else margin best = minf(best, (edge - from[axis]) / d) return maxf(best, 0.0)