A8: Synthesized sound effects and an adaptive score
Every sound is made by scripts in tools/audio/ and committed as a WAV in assets/audio/ with a manifest of hashes and levels: - dsp.gd: seeded noise, wavetable oscillators, filters, envelopes and struck modes. recipes.gd: 50 effects (footsteps on five surfaces, a report per weapon, impacts, carpentry building and breaking, the tide's bell, foghorn, surge and lighthouse, player, ambience, menus). - score.gd: an original eight-bar tune and chords in D Dorian, 6/8, rendered as four looping layers (drone, frame drum, concertina, fiddle). - loudness.gd and check.gd: per-category loudness targets, a -1 dBFS peak ceiling and the mix order (the tide loudest). make audio and make audio-check. In the client, SoundDirector turns FX records, footsteps, building, the tide and the player's own hurts into sounds, placed on 3D voices at the event so gunfire and footsteps come from their direction. MusicDirector fades the score's layers with the tide on an AudioStreamSynchronized, full in menus. MenuSounds knocks for buttons. Closes #48 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
## Wooden knocks for the menus: a soft tick when a button is pointed at or focused, a
|
||||
## peg knocked home when it's pressed, a two-step knock for going back. Attached to a
|
||||
## Control, it hooks every button under it, including ones made later (the server list).
|
||||
class_name MenuSounds
|
||||
extends Node
|
||||
|
||||
const BACK_WORDS: Array[String] = ["Back", "Resume", "Leave", "Cancel"]
|
||||
|
||||
## Sounds played since attached, newest last: for tests.
|
||||
var heard: Array[String] = []
|
||||
var audible: bool = true
|
||||
|
||||
var _root: Control
|
||||
var _voice: AudioStreamPlayer
|
||||
|
||||
|
||||
func attach(root: Control) -> void:
|
||||
_root = root
|
||||
SoundBank.ensure_bus(SoundBank.EFFECTS_BUS)
|
||||
_voice = AudioStreamPlayer.new()
|
||||
_voice.bus = SoundBank.EFFECTS_BUS
|
||||
_voice.max_polyphony = 4
|
||||
add_child(_voice)
|
||||
_hook_all(root)
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
get_tree().node_added.connect(_hook_later)
|
||||
|
||||
|
||||
func _hook_later(node: Node) -> void:
|
||||
if _root != null and (node == _root or _root.is_ancestor_of(node)):
|
||||
_hook(node)
|
||||
|
||||
|
||||
func _hook_all(node: Node) -> void:
|
||||
_hook(node)
|
||||
for child: Node in node.get_children():
|
||||
_hook_all(child)
|
||||
|
||||
|
||||
func _hook(node: Node) -> void:
|
||||
var button: BaseButton = node as BaseButton
|
||||
if button == null or button.has_meta("menu_sounds"):
|
||||
return
|
||||
button.set_meta("menu_sounds", true)
|
||||
var text: String = (button as Button).text if button is Button else ""
|
||||
var press: String = "ui_back" if BACK_WORDS.has(text) else "ui_click"
|
||||
button.pressed.connect(play.bind(press))
|
||||
button.mouse_entered.connect(play.bind("ui_hover"))
|
||||
button.focus_entered.connect(play.bind("ui_hover"))
|
||||
|
||||
|
||||
func play(sound: String) -> void:
|
||||
heard.append(sound)
|
||||
if heard.size() > 64:
|
||||
heard.pop_front()
|
||||
if not audible or _voice == null:
|
||||
return
|
||||
_voice.stream = SoundBank.stream(sound)
|
||||
_voice.play()
|
||||
@@ -0,0 +1 @@
|
||||
uid://ducdhhaku6pd
|
||||
@@ -0,0 +1,89 @@
|
||||
## The adaptive score (tools/audio/score.gd): four layers of one tune that loop in step
|
||||
## on an AudioStreamSynchronized, each faded in or out by how much the moment calls for.
|
||||
##
|
||||
## intensity 0 drone holding tide, early in a match
|
||||
## intensity 1 drone, drum lobby, drop, the sea rising
|
||||
## intensity 2 drone, drum, concertina the middle phases
|
||||
## intensity 3 drone, drum, concertina, fiddle menus, results, the last land
|
||||
##
|
||||
## Quiet in play and fuller in menus and results, as docs/STYLE.md asks. Plays on
|
||||
## the Music bus, so the music volume setting covers it.
|
||||
class_name MusicDirector
|
||||
extends Node
|
||||
|
||||
const LAYERS: Array[String] = ["music_drone", "music_drum", "music_concertina", "music_fiddle"]
|
||||
## Seconds for a layer to fade fully in or out.
|
||||
const FADE_SECONDS: float = 3.0
|
||||
## Gain of the whole score in a match, under the effects, and in menus.
|
||||
const MATCH_GAIN_DB: float = -6.0
|
||||
const MENU_GAIN_DB: float = 0.0
|
||||
|
||||
## How full the music is now, 0 to 3, and each layer's current gain, 0 to 1.
|
||||
var intensity: int = 3
|
||||
var gains: PackedFloat32Array = PackedFloat32Array([0.0, 0.0, 0.0, 0.0])
|
||||
var in_match: bool = false
|
||||
var player: AudioStreamPlayer
|
||||
var audible: bool = true
|
||||
|
||||
var _sync: AudioStreamSynchronized
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
SoundBank.ensure_bus(SoundBank.MUSIC_BUS)
|
||||
_sync = AudioStreamSynchronized.new()
|
||||
_sync.stream_count = LAYERS.size()
|
||||
for index: int in LAYERS.size():
|
||||
_sync.set_sync_stream(index, SoundBank.stream(LAYERS[index]))
|
||||
_sync.set_sync_stream_volume(index, -80.0)
|
||||
player = AudioStreamPlayer.new()
|
||||
player.bus = SoundBank.MUSIC_BUS
|
||||
player.stream = _sync
|
||||
add_child(player)
|
||||
if audible:
|
||||
player.play()
|
||||
|
||||
|
||||
## The intensity for a moment: no game (menus) is 3; otherwise by match phase and tide.
|
||||
static func intensity_for(game: ClientGame) -> int:
|
||||
if game == null or game.world == null:
|
||||
return 3
|
||||
match game.match_phase:
|
||||
Match.LOBBY, Match.COUNTDOWN, Match.DROP:
|
||||
return 1
|
||||
Match.RESULTS:
|
||||
return 3
|
||||
var world: Simulation = game.world
|
||||
if world.tide == null:
|
||||
return 0
|
||||
var stage: int = world.tide.stage_at(world.tick)
|
||||
if stage == Tide.SUDDEN_DEATH or stage == Tide.OVER:
|
||||
return 3
|
||||
var phase: int = world.tide.phase_at(world.tick)
|
||||
var last: int = Tide.phase_count() - 1
|
||||
if phase >= last:
|
||||
return 3
|
||||
if phase >= 2:
|
||||
return 2
|
||||
if phase == 1 or stage == Tide.RISE:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
## One frame: aims the layers at `game`'s intensity (null for the menus) and fades.
|
||||
func update(game: ClientGame, delta: float) -> void:
|
||||
intensity = intensity_for(game)
|
||||
in_match = game != null and game.world != null and game.match_phase != Match.RESULTS
|
||||
var step: float = delta / FADE_SECONDS
|
||||
for index: int in LAYERS.size():
|
||||
var aim: float = 1.0 if index <= intensity else 0.0
|
||||
gains[index] = move_toward(gains[index], aim, step)
|
||||
if _sync == null:
|
||||
return
|
||||
var overall: float = MATCH_GAIN_DB if in_match else MENU_GAIN_DB
|
||||
for index: int in LAYERS.size():
|
||||
_sync.set_sync_stream_volume(index, linear_to_db(maxf(gains[index], 0.0001)) + overall)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
var shell: AppShell = get_parent() as AppShell
|
||||
update(shell.session.game if shell != null and shell.session != null else null, delta)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dli4je20nwxp4
|
||||
@@ -0,0 +1,56 @@
|
||||
## The game's sounds, as the generator in tools/audio/ wrote them to assets/audio/
|
||||
## (see assets/PROVENANCE.md). Streams are loaded once and kept; the looped ones
|
||||
## (ambience and the score's layers) are set to loop whole.
|
||||
class_name SoundBank
|
||||
extends RefCounted
|
||||
|
||||
const DIR: String = "res://assets/audio"
|
||||
const MUSIC_BUS: String = "Music"
|
||||
const EFFECTS_BUS: String = "Effects"
|
||||
## Sounds that loop: ambience and the score's layers.
|
||||
const LOOPED_PREFIXES: Array[String] = ["amb_", "music_"]
|
||||
|
||||
static var _cache: Dictionary = {}
|
||||
|
||||
|
||||
## The stream for a sound by name, or null if there's no such file.
|
||||
static func stream(sound: String) -> AudioStream:
|
||||
if _cache.has(sound):
|
||||
return _cache[sound]
|
||||
var path: String = "%s/%s.wav" % [DIR, sound]
|
||||
var loaded: AudioStreamWAV = null
|
||||
if ResourceLoader.exists(path):
|
||||
loaded = load(path) as AudioStreamWAV
|
||||
if loaded != null and loops(sound):
|
||||
loaded.loop_mode = AudioStreamWAV.LOOP_FORWARD
|
||||
loaded.loop_begin = 0
|
||||
loaded.loop_end = int(round(loaded.get_length() * loaded.mix_rate))
|
||||
_cache[sound] = loaded
|
||||
return loaded
|
||||
|
||||
|
||||
## Lets go of every loaded stream (tests, and leaving the game).
|
||||
static func clear() -> void:
|
||||
_cache.clear()
|
||||
|
||||
|
||||
static func has(sound: String) -> bool:
|
||||
return stream(sound) != null
|
||||
|
||||
|
||||
static func loops(sound: String) -> bool:
|
||||
for prefix: String in LOOPED_PREFIXES:
|
||||
if sound.begins_with(prefix):
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
## Makes the Music and Effects buses if they aren't there yet, sending to Master.
|
||||
static func ensure_bus(bus: String) -> int:
|
||||
var index: int = AudioServer.get_bus_index(bus)
|
||||
if index < 0:
|
||||
AudioServer.add_bus()
|
||||
index = AudioServer.bus_count - 1
|
||||
AudioServer.set_bus_name(index, bus)
|
||||
AudioServer.set_bus_send(index, "Master")
|
||||
return index
|
||||
@@ -0,0 +1 @@
|
||||
uid://2tji48nqpqc4
|
||||
@@ -0,0 +1,386 @@
|
||||
## The sound of a match, heard from the camera: everything the client's world and the
|
||||
## server's FX messages say happened, turned into sounds placed where they happened.
|
||||
##
|
||||
## - Shots, swings, where shots land and harvesting, from ClientGame.fx (NetFx).
|
||||
## - Footsteps for every player walking near the listener, by what they walk on: grass,
|
||||
## sand, rock, built pieces or the shallows. Landings and splashes.
|
||||
## - Pieces built and broken, by material, from the world's structures.
|
||||
## - The tide: a ship's bell when a phase turns, the foghorn when the sea starts to
|
||||
## rise, the surge's crash, and the lighthouse tone for the last land.
|
||||
## - The client's own player: hurt, and the feed's eliminations.
|
||||
## - Ambience under it all: surf that grows near the water, and wind that grows with
|
||||
## height and in the glide.
|
||||
##
|
||||
## Positional sounds play on AudioStreamPlayer3D voices at the event's position, so
|
||||
## the engine pans and attenuates them against the camera's listener: gunfire and
|
||||
## footsteps are heard from their direction. The rest play flat. update() fills `cues`
|
||||
## with what it decided this frame, which is what the tests look at; it reads the
|
||||
## world and never changes it.
|
||||
class_name SoundDirector
|
||||
extends Node3D
|
||||
|
||||
## How many sounds can play at once, placed and flat. The oldest is cut off past it.
|
||||
const VOICES_3D: int = 24
|
||||
const VOICES_2D: int = 8
|
||||
## Metres a player walks between footfalls, and the slowest speed that counts as
|
||||
## walking, in metres a second.
|
||||
const STRIDE_METRES: float = 1.6
|
||||
const WALK_SPEED: float = 0.8
|
||||
## A player who moves further than this in a frame was placed, not walking.
|
||||
const TELEPORT_METRES: float = 6.0
|
||||
## Footsteps further than this from the listener aren't played at all.
|
||||
const STEP_HEARING_METRES: float = 45.0
|
||||
## Falling faster than this (m/s) before touching ground plays a landing.
|
||||
const LANDING_SPEED: float = 6.0
|
||||
## How far each kind of sound carries, in metres: where it's at half volume
|
||||
## (unit_size) and where it stops (max_distance).
|
||||
const CARRY: Dictionary = {
|
||||
"footstep": [3.0, 45.0],
|
||||
"weapon": [18.0, 600.0],
|
||||
"impact": [6.0, 120.0],
|
||||
"build": [6.0, 120.0],
|
||||
"water": [5.0, 80.0],
|
||||
"player": [4.0, 60.0],
|
||||
}
|
||||
## Height above the sea where the surf has faded to its quietest, in metres.
|
||||
const SURF_FADE_METRES: float = 70.0
|
||||
|
||||
## What update() decided this frame: {sound, category, spatial, at, volume_db, pitch}.
|
||||
var cues: Array[Dictionary] = []
|
||||
## Sounds started since the director was made.
|
||||
var played: int = 0
|
||||
## Whether to actually start voices. Off, update() only fills `cues`.
|
||||
var audible: bool = true
|
||||
## Rain under the ambience, for weather (A6) to turn on: 0 to 1.
|
||||
var rain: float = 0.0
|
||||
|
||||
var _world: Simulation = null
|
||||
var _fx_seen: int = 0
|
||||
var _feed_seen: int = 0
|
||||
var _pieces: Dictionary = {}
|
||||
var _players: Dictionary = {}
|
||||
var _tide_phase: int = -1
|
||||
var _tide_stage: int = -1
|
||||
var _tide_tick: int = -1
|
||||
var _match_phase: int = -1
|
||||
var _own_health: int = -1
|
||||
var _voices_3d: Array[AudioStreamPlayer3D] = []
|
||||
var _voices_2d: Array[AudioStreamPlayer] = []
|
||||
var _next_3d: int = 0
|
||||
var _next_2d: int = 0
|
||||
var _ambience: Dictionary = {}
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
SoundBank.ensure_bus(SoundBank.EFFECTS_BUS)
|
||||
for index: int in VOICES_3D:
|
||||
var voice: AudioStreamPlayer3D = AudioStreamPlayer3D.new()
|
||||
voice.bus = SoundBank.EFFECTS_BUS
|
||||
voice.attenuation_model = AudioStreamPlayer3D.ATTENUATION_INVERSE_DISTANCE
|
||||
voice.panning_strength = 1.0
|
||||
add_child(voice)
|
||||
_voices_3d.append(voice)
|
||||
for index: int in VOICES_2D:
|
||||
var flat: AudioStreamPlayer = AudioStreamPlayer.new()
|
||||
flat.bus = SoundBank.EFFECTS_BUS
|
||||
add_child(flat)
|
||||
_voices_2d.append(flat)
|
||||
for loop: String in ["amb_surf", "amb_wind", "amb_rain"]:
|
||||
var bed: AudioStreamPlayer = AudioStreamPlayer.new()
|
||||
bed.bus = SoundBank.EFFECTS_BUS
|
||||
bed.stream = SoundBank.stream(loop)
|
||||
bed.volume_db = -80.0
|
||||
add_child(bed)
|
||||
_ambience[loop] = bed
|
||||
|
||||
|
||||
## One frame. `own_id` is the client's own player or 0; `listener` is where the camera is.
|
||||
func update(game: ClientGame, own_id: int, listener: Vector3, delta: float) -> void:
|
||||
cues.clear()
|
||||
var world: Simulation = game.world
|
||||
if world != _world:
|
||||
_begin(world, game)
|
||||
if world == null:
|
||||
_set_ambience(0.0, 0.0, 0.0)
|
||||
return
|
||||
_hear_fx(game, world)
|
||||
_hear_players(world, own_id, listener, delta)
|
||||
_hear_building(world)
|
||||
_hear_tide(world)
|
||||
_hear_match(game, own_id, world)
|
||||
_hear_ambience(world, own_id, listener, delta)
|
||||
if audible:
|
||||
for cue: Dictionary in cues:
|
||||
_play(cue)
|
||||
played += cues.size()
|
||||
|
||||
|
||||
## The surface a player at `feet` is walking on: "shallows", "wood", "sand", "rock" or
|
||||
## "grass".
|
||||
static func surface_at(world: Simulation, feet: Vector3) -> String:
|
||||
if world.water_level_at(feet) > feet.y + 0.05:
|
||||
return "shallows"
|
||||
var terrain: float = world.ground.terrain_height(feet.x, feet.z)
|
||||
if feet.y > terrain + 0.3:
|
||||
return "wood"
|
||||
if world.island == null:
|
||||
return "grass"
|
||||
match world.island.band_at(feet.x, feet.z):
|
||||
Island.Band.SEA, Island.Band.BEACH:
|
||||
return "sand"
|
||||
Island.Band.HIGHLAND, Island.Band.SUMMIT:
|
||||
return "rock"
|
||||
return "grass"
|
||||
|
||||
|
||||
## The report for a shot by item code: each weapon has its own. The boathook has no
|
||||
## code (it is always carried), so a negative one is its swing.
|
||||
static func shot_sound(item: int) -> String:
|
||||
if item < 0:
|
||||
return "swing_boathook"
|
||||
var def: ItemDef = ItemCatalog.get_def(item)
|
||||
if def == null:
|
||||
return "shot_dock_carbine"
|
||||
if def.id == "powder_keg":
|
||||
return "swing_boathook"
|
||||
var sound: String = "shot_" + def.id
|
||||
return sound if SoundBank.has(sound) else "shot_dock_carbine"
|
||||
|
||||
|
||||
func _begin(world: Simulation, game: ClientGame) -> void:
|
||||
_world = world
|
||||
_fx_seen = game.fx_received
|
||||
_feed_seen = game.feed.size()
|
||||
_players.clear()
|
||||
_pieces.clear()
|
||||
_own_health = -1
|
||||
_tide_phase = -1
|
||||
_tide_stage = -1
|
||||
_tide_tick = -1
|
||||
_match_phase = game.match_phase
|
||||
if world != null:
|
||||
for piece_id: int in world.structures.pieces:
|
||||
_pieces[piece_id] = _piece_record(world.structures.pieces[piece_id])
|
||||
|
||||
|
||||
func _cue(sound: String, category: String, at: Variant = null, volume_db: float = 0.0, pitch: float = 1.0) -> void:
|
||||
cues.append({
|
||||
"sound": sound, "category": category, "spatial": at != null,
|
||||
"at": at if at != null else Vector3.ZERO, "volume_db": volume_db, "pitch": pitch,
|
||||
})
|
||||
|
||||
|
||||
func _hear_fx(game: ClientGame, world: Simulation) -> void:
|
||||
var fresh: int = mini(game.fx_received - _fx_seen, game.fx.size())
|
||||
_fx_seen = game.fx_received
|
||||
for index: int in range(game.fx.size() - fresh, game.fx.size()):
|
||||
var record: Dictionary = game.fx[index]
|
||||
var at: Vector3 = record["position"]
|
||||
match int(record["kind"]):
|
||||
NetFx.SHOT:
|
||||
_cue(shot_sound(int(record["item"])), "weapon", at, 0.0, _wobble(record, 0.04))
|
||||
NetFx.IMPACT:
|
||||
_cue(_impact_sound(record, world), "impact", at, -2.0, _wobble(record, 0.08))
|
||||
NetFx.HARVEST:
|
||||
_cue("chop", "impact", at + Vector3(0.0, 1.0, 0.0), -3.0, _wobble(record, 0.08))
|
||||
|
||||
|
||||
func _impact_sound(record: Dictionary, world: Simulation) -> String:
|
||||
var item: ItemDef = ItemCatalog.get_def(int(record["item"]))
|
||||
if item != null and item.id == "powder_keg":
|
||||
return "keg_blast"
|
||||
match int(record.get("hit", Weapons.HIT_NONE)):
|
||||
Weapons.HIT_PLAYER:
|
||||
return "hit_player"
|
||||
Weapons.HIT_PIECE:
|
||||
var piece: StructureSet.Piece = world.structures.find(int(record.get("target", 0)))
|
||||
if piece != null and piece.material == Materials.STONE:
|
||||
return "hit_stone"
|
||||
if piece != null and piece.material == Materials.SALVAGE_IRON:
|
||||
return "hit_iron"
|
||||
return "hit_wood"
|
||||
Weapons.HIT_PROP:
|
||||
return "hit_wood"
|
||||
return "hit_ground"
|
||||
|
||||
|
||||
## A small, repeatable pitch change per event, so a burst of the same sound doesn't
|
||||
## sound like one sample.
|
||||
static func _wobble(record: Dictionary, spread: float) -> float:
|
||||
var h: int = hash([record.get("tick", 0), record.get("player", 0), record.get("kind", 0)])
|
||||
return 1.0 + spread * (float(h % 1000) / 500.0 - 1.0)
|
||||
|
||||
|
||||
func _hear_players(world: Simulation, own_id: int, listener: Vector3, delta: float) -> void:
|
||||
var seen: Dictionary = {}
|
||||
for entity: SimEntity in world.entities.entities():
|
||||
if entity.archetype != SimEntity.ARCHETYPE_PLAYER:
|
||||
continue
|
||||
seen[entity.id] = true
|
||||
var was: Dictionary = _players.get(entity.id, {})
|
||||
_players[entity.id] = {
|
||||
"at": entity.position, "on_ground": entity.on_ground, "swimming": entity.swimming,
|
||||
"fall": was.get("fall", 0.0), "walked": was.get("walked", 0.0), "foot": was.get("foot", 1),
|
||||
}
|
||||
var now: Dictionary = _players[entity.id]
|
||||
if was.is_empty() or entity.eliminated or entity.drop_state == Drop.ABOARD:
|
||||
continue
|
||||
var moved: Vector3 = entity.position - Vector3(was["at"])
|
||||
if moved.length() > TELEPORT_METRES:
|
||||
continue
|
||||
var near: bool = entity.position.distance_to(listener) <= STEP_HEARING_METRES or entity.id == own_id
|
||||
var dt: float = maxf(delta, 0.0001)
|
||||
# How fast they were falling, kept while in the air, for the landing.
|
||||
if not entity.on_ground:
|
||||
now["fall"] = maxf(float(was.get("fall", 0.0)), -moved.y / dt)
|
||||
if entity.swimming and not bool(was["swimming"]):
|
||||
if near:
|
||||
_cue("splash", "water", entity.position, 0.0)
|
||||
now["walked"] = 0.0
|
||||
continue
|
||||
if entity.on_ground and not bool(was["on_ground"]) and not entity.swimming:
|
||||
if near and float(was.get("fall", 0.0)) >= LANDING_SPEED:
|
||||
_cue("land", "footstep", entity.position, 0.0)
|
||||
now["fall"] = 0.0
|
||||
now["walked"] = 0.0
|
||||
continue
|
||||
var horizontal: float = Vector2(moved.x, moved.z).length()
|
||||
if horizontal / dt < WALK_SPEED or not (entity.on_ground or entity.swimming):
|
||||
continue
|
||||
now["walked"] = float(was["walked"]) + horizontal
|
||||
var stride: float = STRIDE_METRES * (1.6 if entity.swimming else 1.0) * (0.75 if entity.crouching else 1.0)
|
||||
if float(now["walked"]) < stride:
|
||||
continue
|
||||
now["walked"] = float(now["walked"]) - stride
|
||||
now["foot"] = 3 - int(was["foot"])
|
||||
if not near:
|
||||
continue
|
||||
if entity.swimming:
|
||||
_cue("swim", "water", entity.position, -4.0)
|
||||
else:
|
||||
var sound: String = "step_%s_%d" % [surface_at(world, entity.position), now["foot"]]
|
||||
_cue(sound, "footstep", entity.position, -4.0 if entity.crouching else 0.0)
|
||||
for entity_id: int in _players.keys():
|
||||
if not seen.has(entity_id):
|
||||
_players.erase(entity_id)
|
||||
|
||||
|
||||
func _piece_record(piece: StructureSet.Piece) -> Array:
|
||||
var centre: Vector3 = piece.boxes[0].get_center() if not piece.boxes.is_empty() else Vector3.ZERO
|
||||
return [piece.material, centre]
|
||||
|
||||
|
||||
func _hear_building(world: Simulation) -> void:
|
||||
var pieces: Dictionary = world.structures.pieces
|
||||
for piece_id: int in pieces:
|
||||
if not _pieces.has(piece_id):
|
||||
var record: Array = _piece_record(pieces[piece_id])
|
||||
_pieces[piece_id] = record
|
||||
_cue("build_" + Materials.NAMES[clampi(int(record[0]), 0, 2)], "build", record[1])
|
||||
for piece_id: int in _pieces.keys():
|
||||
if not pieces.has(piece_id):
|
||||
var gone: Array = _pieces[piece_id]
|
||||
_pieces.erase(piece_id)
|
||||
_cue("break_" + Materials.NAMES[clampi(int(gone[0]), 0, 2)], "build", gone[1])
|
||||
|
||||
|
||||
func _hear_tide(world: Simulation) -> void:
|
||||
if world.tide == null:
|
||||
return
|
||||
var tick: int = world.tick
|
||||
var phase: int = world.tide.phase_at(tick)
|
||||
var stage: int = world.tide.stage_at(tick)
|
||||
if _tide_tick >= 0 and tick > _tide_tick:
|
||||
if stage == Tide.SUDDEN_DEATH and _tide_stage != Tide.SUDDEN_DEATH:
|
||||
_cue("lighthouse", "tide")
|
||||
elif stage == Tide.RISE and _tide_stage == Tide.HOLD:
|
||||
_cue("foghorn", "tide")
|
||||
elif phase != _tide_phase and stage == Tide.HOLD:
|
||||
_cue("bell", "tide")
|
||||
# A surge can start on any tick since the last frame.
|
||||
for t: int in range(maxi(_tide_tick + 1, tick - 64), tick + 1):
|
||||
if world.tide.surge_starts(t):
|
||||
_cue("surge", "tide")
|
||||
break
|
||||
_tide_tick = tick
|
||||
_tide_phase = phase
|
||||
_tide_stage = stage
|
||||
|
||||
|
||||
func _hear_match(game: ClientGame, own_id: int, world: Simulation) -> void:
|
||||
if game.match_phase != _match_phase:
|
||||
if game.match_phase == Match.DROP or game.match_phase == Match.RESULTS:
|
||||
_cue("bell", "tide", null, -4.0)
|
||||
_match_phase = game.match_phase
|
||||
if game.feed.size() > _feed_seen:
|
||||
_cue("eliminated", "player", null, -2.0)
|
||||
_feed_seen = game.feed.size()
|
||||
var me: SimEntity = world.entities.find(own_id) if own_id != 0 else null
|
||||
if me == null:
|
||||
_own_health = -1
|
||||
return
|
||||
if _own_health >= 0 and me.health < _own_health and not me.eliminated:
|
||||
_cue("hurt", "player", null, 0.0)
|
||||
_own_health = me.health
|
||||
|
||||
|
||||
func _hear_ambience(world: Simulation, own_id: int, listener: Vector3, _delta: float) -> void:
|
||||
var above_sea: float = maxf(listener.y - world.sea_level, 0.0)
|
||||
var surf: float = clampf(1.0 - above_sea / SURF_FADE_METRES, 0.15, 1.0)
|
||||
var wind: float = clampf(0.25 + above_sea / 120.0, 0.25, 1.0)
|
||||
var me: SimEntity = world.entities.find(own_id) if own_id != 0 else null
|
||||
if me != null and (me.drop_state == Drop.FREEFALL or me.drop_state == Drop.GLIDING):
|
||||
wind = 1.0
|
||||
_set_ambience(surf, wind, rain)
|
||||
|
||||
|
||||
func _set_ambience(surf: float, wind: float, wet: float) -> void:
|
||||
var levels: Dictionary = {"amb_surf": surf, "amb_wind": wind, "amb_rain": wet}
|
||||
for loop: String in _ambience:
|
||||
var bed: AudioStreamPlayer = _ambience[loop]
|
||||
var level: float = float(levels[loop])
|
||||
bed.volume_db = linear_to_db(maxf(level, 0.0001))
|
||||
if not audible or bed.stream == null:
|
||||
continue
|
||||
if level > 0.001 and not bed.playing:
|
||||
bed.play()
|
||||
elif level <= 0.001 and bed.playing:
|
||||
bed.stop()
|
||||
|
||||
|
||||
## The ambience's volume per loop, 0 to 1, as last set.
|
||||
func ambience_level(loop: String) -> float:
|
||||
var bed: AudioStreamPlayer = _ambience.get(loop)
|
||||
return db_to_linear(bed.volume_db) if bed != null else 0.0
|
||||
|
||||
|
||||
func _play(cue: Dictionary) -> void:
|
||||
var stream: AudioStream = SoundBank.stream(String(cue["sound"]))
|
||||
if stream == null:
|
||||
return
|
||||
if bool(cue["spatial"]):
|
||||
var voice: AudioStreamPlayer3D = _voices_3d[_next_3d]
|
||||
_next_3d = (_next_3d + 1) % _voices_3d.size()
|
||||
var carry: Array = CARRY.get(String(cue["category"]), [6.0, 120.0])
|
||||
voice.unit_size = float(carry[0])
|
||||
voice.max_distance = float(carry[1])
|
||||
voice.global_position = cue["at"]
|
||||
voice.stream = stream
|
||||
voice.volume_db = float(cue["volume_db"])
|
||||
voice.pitch_scale = float(cue["pitch"])
|
||||
voice.play()
|
||||
else:
|
||||
var flat: AudioStreamPlayer = _voices_2d[_next_2d]
|
||||
_next_2d = (_next_2d + 1) % _voices_2d.size()
|
||||
flat.stream = stream
|
||||
flat.volume_db = float(cue["volume_db"])
|
||||
flat.pitch_scale = float(cue["pitch"])
|
||||
flat.play()
|
||||
|
||||
|
||||
## The placed voice that last played `sound`, or null: for tests.
|
||||
func voice_playing(sound: String) -> AudioStreamPlayer3D:
|
||||
for voice: AudioStreamPlayer3D in _voices_3d:
|
||||
if voice.stream != null and voice.stream == SoundBank.stream(sound):
|
||||
return voice
|
||||
return null
|
||||
@@ -0,0 +1 @@
|
||||
uid://dryqmjgtmtbud
|
||||
Reference in New Issue
Block a user