## Captions for the sounds that matter in play (A13), for players who can't hear them ## or play with the sound down: what was heard and, for placed sounds, which way it ## came from against where the camera looks. It reads the cues SoundDirector decided ## each frame, so a caption is shown exactly when its sound is played. ## ## Repeats of a caption refresh it rather than stacking ("Footsteps, behind" stays one ## line while they go on), the newest line is last, and lines fade after SECONDS. ## Sounds the player makes themselves (their own steps, shots and pieces) aren't ## captioned: they know. class_name Captions extends RefCounted const SECONDS: float = 3.0 const MAX_LINES: int = 4 ## Placed sounds closer than this to the player are their own. const OWN_METRES: float = 2.5 ## Footsteps further than this aren't worth a line. const STEP_METRES: float = 20.0 ## Each sound's words. Footsteps, shots and pieces go by prefix (see words_for()). const WORDS: Dictionary = { "foghorn": "Foghorn: the sea is rising", "bell": "Ship's bell", "surge": "A surge breaks", "lighthouse": "Lighthouse tone: high water, last land", "hurt": "You're hit", "eliminated": "Someone is out", "keg_blast": "Powder keg blast", "hit_player": "Shot strikes someone", "splash": "Splash", "swim": "Swimming", "swing_boathook": "Boathook swing", "chest_open": "Chest opened", } ## Live lines: words -> {"words", "side", "left" (seconds)}, newest last. var _lines: Array[Dictionary] = [] ## The words for a sound, or "" for one that isn't captioned. static func words_for(sound: String) -> String: if sound.begins_with("step_"): return "Footsteps" if sound.begins_with("shot_"): return "Gunfire" if sound.begins_with("break_"): return "A piece breaks" if sound.begins_with("build_"): return "Building" return String(WORDS.get(sound, "")) ## Which way `at` is from a listener at `eye` looking along `yaw` (simulation ## convention): "ahead", "right", "behind" or "left". static func side_of(eye: Vector3, yaw: float, at: Vector3) -> String: var to: Vector2 = Vector2(at.x - eye.x, at.z - eye.z) if to.length() < 0.01: return "" var turn: float = angle_difference(yaw, atan2(to.x, to.y)) # Positive turn is to the left: yaw grows anticlockwise seen from above. if absf(turn) <= PI / 4.0: return "ahead" if absf(turn) >= PI * 3.0 / 4.0: return "behind" return "left" if turn > 0.0 else "right" ## Takes a frame's cues. `eye` and `yaw` are the camera's; `own` is where the player ## is, or Vector3.INF with no player. func hear(cues: Array[Dictionary], eye: Vector3, yaw: float, own: Vector3, delta: float) -> void: for line: Dictionary in _lines: line["left"] = float(line["left"]) - delta _lines = _lines.filter(func(line: Dictionary) -> bool: return float(line["left"]) > 0.0) for cue: Dictionary in cues: var words: String = words_for(String(cue["sound"])) if words.is_empty(): continue var side: String = "" if bool(cue["spatial"]): var at: Vector3 = cue["at"] if own != Vector3.INF and at.distance_to(own) < OWN_METRES: continue if words == "Footsteps" and own != Vector3.INF and at.distance_to(own) > STEP_METRES: continue side = side_of(eye, yaw, at) _show(words, side) func _show(words: String, side: String) -> void: var text: String = words if side.is_empty() else "%s, %s" % [words, side] for index: int in _lines.size(): if _lines[index]["text"] == text: _lines.remove_at(index) break _lines.append({"text": text, "left": SECONDS}) while _lines.size() > MAX_LINES: _lines.remove_at(0) ## The lines to show now, oldest first. func lines() -> Array[String]: var shown: Array[String] = [] for line: Dictionary in _lines: shown.append(String(line["text"])) return shown func clear() -> void: _lines.clear()