- SFX.gd autoload: fire-and-forget CDN audio player with 8-slot pool,
session-level stream cache, OGG/MP3 support via HTTPRequest download,
uses Settings BUS_SFX so volume/mute work automatically
- Register SFX in project.godot autoloads
- game_screen.gd:
- _sfx_for_unit(iid, key) helper — looks up CardDefinition SFX URL
from ContentCache using the unit's def ID
- Attack sound: played when player initiates attack + counter-attack
- Damaged sound: played at all damage animation sites (immediate,
delayed bystander, post-attack callback, counter-chain)
- Death sound: played when dying animation fires
- Move sound: played when movement animation starts
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
85 lines
2.8 KiB
GDScript
85 lines
2.8 KiB
GDScript
extends Node
|
|
## SFX autoload — fire-and-forget sound effects from CDN URLs.
|
|
##
|
|
## Usage:
|
|
## SFX.play("https://cdn.sor.gg/sfx/attack.ogg") # fire-and-forget
|
|
##
|
|
## Volume and mute are applied globally through the "SFX" AudioServer bus
|
|
## (managed by Settings.sfx_volume / Settings.sfx_muted).
|
|
## Downloaded streams are cached in memory for the session so each URL
|
|
## only hits the network once.
|
|
|
|
const POOL_SIZE := 8 # max simultaneous SFX
|
|
|
|
var _pool : Array[AudioStreamPlayer] = []
|
|
var _cache : Dictionary = {} # url → AudioStream
|
|
|
|
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
|
|
|
func _ready() -> void:
|
|
for _i in POOL_SIZE:
|
|
var p := AudioStreamPlayer.new()
|
|
p.bus = Settings.BUS_SFX
|
|
add_child(p)
|
|
_pool.append(p)
|
|
|
|
# ── Public API ────────────────────────────────────────────────────────────────
|
|
|
|
## Play [param url] asynchronously (fire-and-forget).
|
|
## No-ops if url is empty or the download fails.
|
|
func play(url: String) -> void:
|
|
if url.is_empty():
|
|
return
|
|
_play_async(url)
|
|
|
|
# ── Internal ──────────────────────────────────────────────────────────────────
|
|
|
|
func _play_async(url: String) -> void:
|
|
var stream : AudioStream = _cache.get(url, null)
|
|
if stream == null:
|
|
stream = await _load_stream(url)
|
|
if stream == null:
|
|
return
|
|
_cache[url] = stream
|
|
|
|
var player := _get_free_player()
|
|
player.stream = stream
|
|
player.play()
|
|
|
|
|
|
func _get_free_player() -> AudioStreamPlayer:
|
|
for p : AudioStreamPlayer in _pool:
|
|
if not p.playing:
|
|
return p
|
|
# All slots busy — steal the oldest (index 0, round-robin would be nicer
|
|
# but overkill for 8-slot game SFX).
|
|
return _pool[0]
|
|
|
|
|
|
func _load_stream(url: String) -> AudioStream:
|
|
if url.begins_with("res://") or url.begins_with("user://"):
|
|
return load(url) as AudioStream
|
|
|
|
var http := HTTPRequest.new()
|
|
add_child(http)
|
|
http.request(url)
|
|
var result : Array = await http.request_completed
|
|
http.queue_free()
|
|
|
|
var response_code : int = result[1]
|
|
var body : PackedByteArray = result[3]
|
|
if response_code != 200 or body.is_empty():
|
|
push_warning("SFX: failed to download %s (HTTP %d)" % [url, response_code])
|
|
return null
|
|
|
|
var ext := url.get_extension().to_lower()
|
|
if ext == "ogg":
|
|
return AudioStreamOggVorbis.load_from_buffer(body)
|
|
elif ext == "mp3":
|
|
var s := AudioStreamMP3.new()
|
|
s.data = body
|
|
return s
|
|
|
|
push_warning("SFX: unsupported format '%s' — use OGG or MP3." % ext)
|
|
return null
|