## The tide's schedule: Tuning.TIDE_PHASES played from the tick the tide started. Each ## phase holds the water still for `hold` seconds, then raises it over `rise` seconds to ## its `level`. After the last rise comes sudden death, and when that has run its course ## the match must end. Everything here is a pure function of elapsed ticks, so any tick's ## water level, forecast and phase can be asked for at any time. class_name Tide extends RefCounted const HOLD: int = 0 const RISE: int = 1 const SUDDEN_DEATH: int = 2 const OVER: int = 3 const TICKS: int = Tuning.TICK_RATE_HZ var started_tick: int = 0 ## Each phase's level, from SafeGround.plan_levels() on an island, or the table's. var levels: PackedFloat32Array = PackedFloat32Array() ## Tick offsets from the start: where each phase's hold and rise begin. var _hold_starts: PackedInt32Array = PackedInt32Array() var _rise_starts: PackedInt32Array = PackedInt32Array() var _rise_ends: PackedInt32Array = PackedInt32Array() func _init(start_tick: int, phase_levels: PackedFloat32Array = PackedFloat32Array()) -> void: started_tick = start_tick levels = phase_levels if levels.is_empty(): for phase: Dictionary in Tuning.TIDE_PHASES: levels.append(float(phase["level"])) var at: int = 0 for phase: Dictionary in Tuning.TIDE_PHASES: _hold_starts.append(at) at += roundi(float(phase["hold"]) * TICKS) _rise_starts.append(at) at += maxi(roundi(float(phase["rise"]) * TICKS), 1) _rise_ends.append(at) static func phase_count() -> int: return Tuning.TIDE_PHASES.size() ## Ticks from the start until sudden death begins. func sudden_death_offset() -> int: return _rise_ends[_rise_ends.size() - 1] ## Ticks from the start until the match must end. func time_limit_offset() -> int: return sudden_death_offset() + roundi(Tuning.TIDE_SUDDEN_DEATH_SECONDS * TICKS) ## The phase index in effect at `tick`: the one whose hold or rise is under way, or the ## last one from sudden death on. func phase_at(tick: int) -> int: var elapsed: int = tick - started_tick for index: int in phase_count(): if elapsed < _rise_ends[index]: return index return phase_count() - 1 ## HOLD, RISE, SUDDEN_DEATH or OVER at `tick`. func stage_at(tick: int) -> int: var elapsed: int = tick - started_tick if elapsed >= time_limit_offset(): return OVER if elapsed >= sudden_death_offset(): return SUDDEN_DEATH var index: int = phase_at(tick) return RISE if elapsed >= _rise_starts[index] else HOLD ## The sea level at `tick`. Before the tide starts it's the starting level. func level_at(tick: int) -> float: var elapsed: int = tick - started_tick if elapsed <= 0: return Tuning.SEA_LEVEL_START var index: int = phase_at(tick) var from: float = Tuning.SEA_LEVEL_START if index == 0 else levels[index - 1] var to: float = levels[index] if elapsed >= _rise_ends[index]: return to if elapsed < _rise_starts[index]: return from var fraction: float = float(elapsed - _rise_starts[index]) / (_rise_ends[index] - _rise_starts[index]) return lerpf(from, to, fraction) ## The level the water is heading for: the current phase's level, announced from the ## start of its hold. From sudden death on, the final level. func forecast_at(tick: int) -> float: return levels[phase_at(tick)] ## Ticks until the current hold ends and the water starts rising, or 0 while it rises. func ticks_until_rise(tick: int) -> int: if stage_at(tick) != HOLD: return 0 return _rise_starts[phase_at(tick)] - (tick - started_tick) func drown_dps_at(tick: int) -> int: return int(Tuning.TIDE_PHASES[phase_at(tick)]["drown_dps"]) ## True on the first tick of a phase's rise: when that phase's storm surge strikes. func surge_starts(tick: int) -> bool: var elapsed: int = tick - started_tick return _rise_starts.has(elapsed) func surge_damage_at(tick: int) -> int: return int(Tuning.TIDE_PHASES[phase_at(tick)].get("surge", 0))