using System.Collections.Generic;
using UnityEngine;
namespace TKS
{
///
/// 게임 코드의 한 줄 훅이 부르는 소리 판단(2026-11-20 사운드 1차 적용) — 병종별 소리 · 스킬별 소리(skill_sfx.csv) · 화면 · 전투별 배경음악(sound_bgm.csv).
/// 스킬 id · 화면 이름 · 파일 이름은 코드에 쓰지 않는다(표에서 읽음). 배경음악 표의 s_where = 「screen_」+화면 클래스 이름 · 「mode_」+컨텐츠 모드 kind · 「battle_」+스테이지 종류(소문자).
///
public static class SoundHooks
{
const string SkillFile = "skill_sfx", BgmFile = "sound_bgm";
const string WhereScreen = "screen_", WhereMode = "mode_", WhereBattle = "battle_";
public class SkillRow
{
public string cast = "", launch = "", hit = "", voice = "";
}
static Dictionary skills;
static Dictionary bgm;
static readonly Dictionary readyState = new Dictionary();
static int side; // 0 = 없음 · 1 = 아군 턴 · 2 = 적 턴
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
skills = null;
bgm = null;
readyState.Clear();
side = 0;
}
/// 소리 표 3개를 다음 사용 때 다시 읽는다.
public static void Reload()
{
Sound.Reload();
skills = null;
bgm = null;
}
public static Dictionary Skills
{
get
{
if (skills != null) return skills;
skills = new Dictionary();
foreach (var r in Sound.ReadTable(SkillFile))
{
string id;
if (!r.TryGetValue("s_skill", out id) || string.IsNullOrEmpty(id)) continue;
skills[id] = new SkillRow { cast = Col(r, "s_cast"), launch = Col(r, "s_launch"), hit = Col(r, "s_hit"), voice = Col(r, "s_voice") };
}
return skills;
}
}
public static Dictionary BgmTable
{
get
{
if (bgm != null) return bgm;
bgm = new Dictionary();
foreach (var r in Sound.ReadTable(BgmFile))
{
string w = Col(r, "s_where");
if (w.Length > 0) bgm[w] = Col(r, "s_bgm");
}
return bgm;
}
}
static string Col(Dictionary r, string k)
{
string v;
return r.TryGetValue(k, out v) && v != null ? v.Trim() : "";
}
static SkillRow RowOf(SkillDef def)
{
SkillRow row;
return def != null && def.Id != null && Skills.TryGetValue(def.Id, out row) ? row : null;
}
// ---------- 배경음악 ----------
static bool BgmWhere(string where)
{
string key;
if (!BgmTable.TryGetValue(where, out key) || key.Length == 0) return false;
Sound.Bgm(key);
return true;
}
/// 그 화면(클래스 이름)의 배경음악 — 표에 줄이 없으면 지금 음악 그대로.
public static void ScreenBgm(object screen)
{
if (screen != null) BgmWhere(WhereScreen + screen.GetType().Name);
}
/// 아웃게임 화면 전환 = ui_screen + 그 화면 배경음악.
public static void Screen(object screen)
{
Sound.PlayUi(SoundKeys.UiScreen);
ScreenBgm(screen);
}
/// 전투 시작: 소리 표를 다시 읽고(Play 켠 채 교체 확인) · 컨텐츠 모드 → 스테이지 종류 순서로 배경음악.
public static void BattleStart(BattleManager b, BalanceConfig.StageDef stage)
{
Reload();
side = 0;
readyState.Clear();
if (b != null && b.Mode != null && !string.IsNullOrEmpty(b.Mode.kind) && BgmWhere(WhereMode + b.Mode.kind)) return;
if (stage != null) BgmWhere(WhereBattle + stage.kind.ToString().ToLowerInvariant());
}
// ---------- 전투 ----------
static string DashKey(UnitType t)
{
return t == UnitType.Cavalry ? SoundKeys.BtDashCavalry : SoundKeys.BtDash;
}
static string HitKey(UnitType t)
{
switch (t)
{
case UnitType.Spear: return SoundKeys.BtHitSpear;
case UnitType.Cavalry: return SoundKeys.BtHitCavalry;
case UnitType.Archer: return SoundKeys.BtHitArrow;
case UnitType.Strategist: return SoundKeys.BtHitMagic;
default: return SoundKeys.BtHitInfantry;
}
}
/// 행동 시작(슬링 발사 직후 · ActionResolver.Resolve): 예약 스킬의 s_launch · 없으면 근접 병종 돌진 소리(원거리는 쏘는 순간 ).
public static void ActionStart(Unit u, bool moveOnly, ActionOutcome o)
{
if (u == null) return;
var row = o != null ? RowOf(o.SkillUsed) : null;
if (row != null && row.launch.Length > 0) Sound.Play(row.launch);
else if (!UnitTypeInfo.IsRanged(u.Type)) Sound.Play(DashKey(u.Type));
}
/// 궁병 · 책사 투사체 발사(예약 스킬의 s_launch 가 이미 났으면 생략).
public static void Shot(Unit u, ActionOutcome o)
{
if (u == null) return;
var row = o != null ? RowOf(o.SkillUsed) : null;
if (row != null && row.launch.Length > 0) return;
Sound.Play(u.Type == UnitType.Strategist ? SoundKeys.BtShotMagic : SoundKeys.BtShotArrow);
}
/// 복귀 등 같은 행동 안의 새 이동(기병 = 말 소리).
public static void Dash(Unit u)
{
if (u != null) Sound.Play(DashKey(u.Type));
}
public static void Bounce(Unit struck)
{
Sound.Play(struck != null ? SoundKeys.BtBounceUnit : SoundKeys.BtBounceWall);
}
/// 그 유닛이 지금 행동 중이면 그 행동의 스킬 s_hit(이펙트 키 = 예약 스킬 id 가 표에 있을 때) · 없으면 null.
static string SkillHit(Unit by)
{
var sys = SkillSystem.Current;
var keys = sys != null ? sys.VfxKeysOf(by) : null;
if (keys == null) return null;
foreach (var k in keys)
{
SkillRow row;
if (k != null && Skills.TryGetValue(k, out row) && row.hit.Length > 0) return row.hit;
}
return null;
}
/// 피해 한 곳(SkillSystem.Hit): 반격 = bt_counter · 스킬 행동 = 그 스킬 s_hit · 치명타 = bt_crit · 그 밖 = 공격자 병종 타격음 + 맞는 쪽 신음.
public static void Hit(Unit d, Unit by, HitKind kind, bool crit, int dealt)
{
if (d == null || dealt <= 0) return;
if (kind == HitKind.Counter) Sound.Play(SoundKeys.BtCounter);
else if (kind == HitKind.Attack || kind == HitKind.Skill || kind == HitKind.Guard)
{
string sk = SkillHit(by);
if (sk != null) Sound.Play(sk);
if (crit) Sound.Play(SoundKeys.BtCrit);
else if (sk == null && by != null) Sound.Play(HitKey(by.Type));
}
else return; // 지속 피해 · 지형 · 자기 소모 = 상태 이상 · 기믹 소리가 따로
Sound.Play(SoundKeys.BtHurt);
}
/// 회복: 다른 아군을 고친 회복 = 그 행동 스킬의 s_hit(없으면 bt_heal) · 흡혈 · 자기 회복 = bt_heal.
public static void Heal(Unit u, int got, Unit healer)
{
if (u == null || got <= 0) return;
string sk = healer != null && healer != u && healer.Faction == u.Faction ? SkillHit(healer) : null;
Sound.Play(sk ?? SoundKeys.BtHeal);
}
/// 시한 효과 등록: 상대 진영에게 = 약화 · 무적 · 호위 = 보호 · 그 밖 = 강화.
public static void Timed(Unit owner, string effectKey, HashSet targets)
{
if (owner == null) return;
bool debuff = false;
if (targets != null)
foreach (var t in targets)
if (t != null && t.Faction != owner.Faction)
{
debuff = true;
break;
}
if (debuff) Sound.Play(SoundKeys.BtDebuff);
else if (effectKey == SkillKeys.Invuln || effectKey == SkillKeys.Guard) Sound.Play(SoundKeys.BtShield);
else Sound.Play(SoundKeys.BtBuff);
}
/// 스킬 버튼으로 발동 · 예약 = 표 s_cast(없으면 bt_skill_btn) + 영웅 음성 s_voice(2026-11-21 PD 「음성은 스킬 시전할 때만」).
public static void SkillCast(SkillDef def)
{
var row = RowOf(def);
Sound.Play(row != null && row.cast.Length > 0 ? row.cast : SoundKeys.BtSkillBtn);
if (row != null && row.voice.Length > 0) Sound.Play(row.voice);
}
/// 발동 연출(SkillSystem.Announce): 아군의 다음 행동형은 예약할 때 이미 냈으므로 생략 · 즉시형 · 적 무장 발동 = s_cast.
public static void Announce(Unit u, SkillDef def)
{
if (u == null || def == null || !def.IsActive) return;
if (def.Trigger == SkillTrigger.NextAction && u.Faction == Faction.Player) return;
SkillCast(def);
}
/// 전투 단계: 턴 전환(진영이 바뀔 때만) · 승리 · 패배 징글 · 아군 턴마다 쿨이 끝난 스킬 = bt_skill_ready.
public static void Phase(BattleManager b, BattlePhase p)
{
switch (p)
{
case BattlePhase.PlayerTurn:
if (side != 1) Sound.Play(SoundKeys.BtTurnPlayer);
side = 1;
CheckReady(b);
break;
case BattlePhase.EnemyTurn:
if (side != 2) Sound.Play(SoundKeys.BtTurnEnemy);
side = 2;
break;
case BattlePhase.StageClear:
side = 0;
Sound.Jingle(SoundKeys.BtVictory);
break;
case BattlePhase.Defeat:
side = 0;
Sound.Jingle(SoundKeys.BtDefeat);
break;
case BattlePhase.Setup:
side = 0;
break;
}
}
static void CheckReady(BattleManager b)
{
if (b == null || b.Skills == null || Sim.Active) return;
bool ready = false;
foreach (var u in b.Units)
{
if (u == null || !u.IsAlive || u.Faction != Faction.Player) continue;
var st = b.Skills.StateOf(u);
SkillSystem.ActiveState prev;
if (readyState.TryGetValue(u, out prev) && prev == SkillSystem.ActiveState.Cooldown && st == SkillSystem.ActiveState.Ready) ready = true;
readyState[u] = st;
}
if (ready) Sound.Play(SoundKeys.BtSkillReady);
}
/// 조준 시작(화살표가 뜨는 순간) — 게이지 치명타 구간 진입 판정도 새로.
public static void AimStart()
{
Sound.Rise(SoundKeys.BtGaugeCrit, false);
Sound.Play(SoundKeys.BtAim);
}
}
}