feat: add audio library and visual verification
This commit is contained in:
+27
-1
@@ -6,11 +6,11 @@ const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
const ActorStateType = preload("res://scripts/domain/actor_state.gd")
|
||||
const GameStateType = preload("res://scripts/domain/game_state.gd")
|
||||
const ActorViewType = preload("res://scripts/presentation/actor_view.gd")
|
||||
|
||||
@onready var map_renderer: MapRenderer = $World/MapRenderer
|
||||
@onready var player_view: ActorView = $World/Player
|
||||
@onready var enemies: Node2D = $World/Enemies
|
||||
@onready var hud: GameHud = $HUD
|
||||
@onready var audio_library = $AudioLibrary
|
||||
var game_state: GameState
|
||||
var enemy_views: Dictionary[StringName, ActorView] = {}
|
||||
|
||||
@@ -21,6 +21,19 @@ func _ready() -> void:
|
||||
player_view.set_actor(game_state.player)
|
||||
_sync_enemy_views()
|
||||
hud.refresh(game_state)
|
||||
audio_library.set_ambience_context("exploration")
|
||||
if "--capture" in OS.get_cmdline_user_args():
|
||||
_capture_visual_check()
|
||||
|
||||
func _capture_visual_check() -> void:
|
||||
await get_tree().process_frame
|
||||
await get_tree().process_frame
|
||||
await RenderingServer.frame_post_draw
|
||||
var capture_path := ProjectSettings.globalize_path("res://artifacts/visual-check.png")
|
||||
DirAccess.make_dir_recursive_absolute(capture_path.get_base_dir())
|
||||
var image := get_viewport().get_texture().get_image()
|
||||
var result := image.save_png(capture_path)
|
||||
get_tree().quit(0 if result == OK else 1)
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed(&"move_north"):
|
||||
@@ -52,9 +65,22 @@ func _process_action(direction: Vector2i) -> Array[TurnResult]:
|
||||
player_view.refresh()
|
||||
_sync_enemy_views()
|
||||
_show_damage_feedback(results)
|
||||
_dispatch_audio(results)
|
||||
hud.refresh(game_state, results)
|
||||
return results
|
||||
|
||||
func _dispatch_audio(results: Array[TurnResult]) -> void:
|
||||
for result in results:
|
||||
match result.kind:
|
||||
&"melee":
|
||||
audio_library.set_ambience_context("combat")
|
||||
audio_library.play_sfx("attack_given")
|
||||
&"enemy_melee":
|
||||
audio_library.set_ambience_context("combat")
|
||||
audio_library.play_sfx("attack_received")
|
||||
&"enemy_defeated":
|
||||
audio_library.play_sfx("death")
|
||||
|
||||
func _show_damage_feedback(results: Array[TurnResult]) -> void:
|
||||
for result in results:
|
||||
if result.kind == &"melee" and enemy_views.has(result.target_id):
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
class_name AudioLibrary
|
||||
extends Node
|
||||
|
||||
const AMBIENCE_CONTEXTS := ["exploration", "combat", "danger"]
|
||||
const SFX_EVENTS := ["attack_given", "attack_received", "menu_select", "menu_cancel", "item_use", "death"]
|
||||
const ADMITTED_EXTENSIONS := ["mp3", "ogg", "oga", "wav", "flac", "m4a", "opus"]
|
||||
const DEFAULT_AUDIO_HOME := ".local/share/dcwascii"
|
||||
|
||||
var current_ambience_context := ""
|
||||
var _audio_home_override := ""
|
||||
var _last_ambience_path := ""
|
||||
var _last_sfx_path := ""
|
||||
var _ambience_player: AudioStreamPlayer
|
||||
var _sfx_player: AudioStreamPlayer
|
||||
|
||||
func _init(audio_home_override: String = "") -> void:
|
||||
_audio_home_override = audio_home_override
|
||||
|
||||
func _ready() -> void:
|
||||
_ambience_player = AudioStreamPlayer.new()
|
||||
_sfx_player = AudioStreamPlayer.new()
|
||||
add_child(_ambience_player)
|
||||
add_child(_sfx_player)
|
||||
|
||||
func set_ambience_context(context: String) -> void:
|
||||
if not context in AMBIENCE_CONTEXTS:
|
||||
return
|
||||
current_ambience_context = context
|
||||
_last_ambience_path = ""
|
||||
var path := _find_first_audio_file("audio", current_ambience_context)
|
||||
if path.is_empty():
|
||||
_stop_ambience()
|
||||
return
|
||||
_last_ambience_path = path
|
||||
_play_ambience(path)
|
||||
|
||||
func play_sfx(event: String) -> void:
|
||||
_last_sfx_path = ""
|
||||
if not event in SFX_EVENTS:
|
||||
return
|
||||
var path := _find_first_audio_file("sfx", event)
|
||||
if path.is_empty():
|
||||
return
|
||||
_last_sfx_path = path
|
||||
_play_sfx(path)
|
||||
|
||||
func get_last_ambience_path() -> String:
|
||||
return _last_ambience_path
|
||||
|
||||
func get_last_sfx_path() -> String:
|
||||
return _last_sfx_path
|
||||
|
||||
func _audio_roots() -> Array[String]:
|
||||
if not _audio_home_override.is_empty():
|
||||
return [_audio_home_override]
|
||||
var roots: Array[String] = []
|
||||
var environment_root := OS.get_environment("DCW_AUDIO_HOME")
|
||||
if not environment_root.is_empty():
|
||||
roots.append(environment_root)
|
||||
roots.append(OS.get_environment("HOME").path_join(DEFAULT_AUDIO_HOME))
|
||||
return roots
|
||||
|
||||
func _find_first_audio_file(kind: String, name: String) -> String:
|
||||
for root in _audio_roots():
|
||||
var directory_path := root.path_join(kind).path_join(name)
|
||||
var directory := DirAccess.open(directory_path)
|
||||
if directory == null:
|
||||
continue
|
||||
directory.list_dir_begin()
|
||||
var entry := directory.get_next()
|
||||
while not entry.is_empty():
|
||||
if not directory.current_is_dir() and entry.get_extension().to_lower() in ADMITTED_EXTENSIONS:
|
||||
directory.list_dir_end()
|
||||
return directory_path.path_join(entry)
|
||||
entry = directory.get_next()
|
||||
directory.list_dir_end()
|
||||
return ""
|
||||
|
||||
func _play_ambience(path: String) -> void:
|
||||
var stream := _load_audio_stream(path)
|
||||
if stream == null or _ambience_player == null:
|
||||
return
|
||||
_ambience_player.stream = stream
|
||||
_ambience_player.play()
|
||||
|
||||
func _play_sfx(path: String) -> void:
|
||||
var stream := _load_audio_stream(path)
|
||||
if stream == null or _sfx_player == null:
|
||||
return
|
||||
_sfx_player.stream = stream
|
||||
_sfx_player.play()
|
||||
|
||||
func _load_audio_stream(path: String) -> AudioStream:
|
||||
# External roots are not imported Godot resources. Only use Godot-native loaders
|
||||
# that can accept a filesystem path; unavailable codecs fail closed.
|
||||
match path.get_extension().to_lower():
|
||||
"wav":
|
||||
return AudioStreamWAV.load_from_file(path)
|
||||
"ogg", "oga", "opus":
|
||||
return AudioStreamOggVorbis.load_from_file(path)
|
||||
"mp3":
|
||||
return AudioStreamMP3.load_from_file(path)
|
||||
_:
|
||||
return null
|
||||
|
||||
func _stop_ambience() -> void:
|
||||
if _ambience_player != null:
|
||||
_ambience_player.stop()
|
||||
@@ -0,0 +1 @@
|
||||
uid://c1s4audiolibrary
|
||||
Executable
+66
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$root_dir"
|
||||
GODOT_BIN="${GODOT_BIN:-/snap/bin/godot4}"
|
||||
if [[ ! -x "$GODOT_BIN" ]]; then
|
||||
GODOT_BIN="$(command -v godot4 || command -v godot || true)"
|
||||
fi
|
||||
if [[ -z "$GODOT_BIN" ]]; then
|
||||
echo "visual check unavailable: set GODOT_BIN or install godot4/godot" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
mkdir -p artifacts
|
||||
log_path="artifacts/visual-check.log"
|
||||
capture_path="artifacts/visual-check.png"
|
||||
rm -f "$capture_path"
|
||||
|
||||
runner=()
|
||||
if [[ -z "${DISPLAY:-}" ]]; then
|
||||
if command -v xvfb-run >/dev/null 2>&1; then
|
||||
runner=(xvfb-run --auto-servernum --server-args="-screen 0 640x480x24")
|
||||
else
|
||||
echo "visual check unavailable: DISPLAY is unset and xvfb-run is not installed" | tee "$log_path" >&2
|
||||
exit 2
|
||||
fi
|
||||
fi
|
||||
|
||||
set +e
|
||||
timeout --foreground 20s "${runner[@]}" "$GODOT_BIN" --path . -- --capture >"$log_path" 2>&1
|
||||
status=$?
|
||||
set -e
|
||||
if (( status != 0 )); then
|
||||
echo "visual check failed: Godot exited with status $status; see $log_path" >&2
|
||||
exit "$status"
|
||||
fi
|
||||
if grep -Eq 'SCRIPT ERROR|Parse Error|^ERROR:' "$log_path"; then
|
||||
echo "visual check failed: Godot reported errors; see $log_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ! -s "$capture_path" ]]; then
|
||||
echo "visual check failed: missing capture $capture_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! python3 - "$capture_path" <<'PY'
|
||||
import struct
|
||||
import sys
|
||||
|
||||
path = sys.argv[1]
|
||||
with open(path, "rb") as capture:
|
||||
signature = capture.read(8)
|
||||
length = struct.unpack(">I", capture.read(4))[0]
|
||||
kind = capture.read(4)
|
||||
data = capture.read(length)
|
||||
if signature != b"\x89PNG\r\n\x1a\n" or kind != b"IHDR" or len(data) != 13:
|
||||
raise SystemExit("invalid PNG")
|
||||
width, height = struct.unpack(">II", data[:8])
|
||||
if width == 0 or height == 0:
|
||||
raise SystemExit("empty PNG")
|
||||
print(f"visual capture: {path} ({width}x{height})")
|
||||
PY
|
||||
then
|
||||
echo "visual check failed: capture is not a nonempty valid PNG" >&2
|
||||
exit 1
|
||||
fi
|
||||
+21
-2
@@ -1,4 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
"$(dirname "$0")/test-unit.sh"
|
||||
"$(dirname "$0")/test-e2e.sh"
|
||||
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
"$script_dir/test-unit.sh"
|
||||
"$script_dir/test-e2e.sh"
|
||||
|
||||
set +e
|
||||
"$script_dir/run-visual-check.sh"
|
||||
visual_status=$?
|
||||
set -e
|
||||
case "$visual_status" in
|
||||
0)
|
||||
echo "verify: unit, E2E, and visual checks passed"
|
||||
;;
|
||||
2)
|
||||
echo "verify: unit and E2E checks passed; visual check unavailable" >&2
|
||||
;;
|
||||
*)
|
||||
echo "verify: visual check failed after unit and E2E checks passed" >&2
|
||||
exit "$visual_status"
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user