Project_WL/AgentScripts/WL_StartWakeAnimSetup.cs

164 lines
7.9 KiB
C#

// PD 지시 #750 — pcanim_lobby.controller 에 기상 연출 상태 2개(lying / getup)를 추가한다.
// 2단계 실행: unity command run_script --file AgentScripts/WL_StartWakeAnimSetup.cs --entry WL_StartWakeAnimSetup.Run
// CLAUDE.md §1 준수: using 금지 · 네임스페이스 풀어 쓰기
//
// 전제
// · 아래 2개 FBX 를 Assets/Res_Addr/Animations/Animation/Common_PlatformAnim/ 에 복사해 둘 것
// C:\WL\Project_WL\Assets\Shinabro\Platform_Animation\Animation\98_Damage\Stander@LyingFront.FBX
// C:\WL\Project_WL\Assets\Shinabro\Platform_Animation\Animation\98_Damage\Stander@LyingFront_WakeUp.FBX
// (.meta 도 함께 — animationType: 3 = Humanoid 이고 힘민지 플레이어 클립과 같은 Shinabro Stander 팩이라
// Humanoid 재타게팅으로 호환된다. GUID: 092d5d11… / 6c185e22…)
// · 2026-09-06 #757 확장: 시작 맵을 e_MapType == Stage 로 바꾸면 PCActor.Set_Obj (PCActor.cs:267-286) 가
// pcanim_lobby 가 아니라 ClassConfig 의 s_AnimationController(클래스별 컨트롤러)를 로드한다.
// StartWakeSequence 는 lying/getup 상태를 가진 컨트롤러가 붙을 때까지 기다리므로
// 클래스 컨트롤러 전부에 같은 상태 2개를 추가해야 연출이 스킵되지 않는다.
// 대상 목록은 코드 상수가 아니라 ClassConfig.json(SOT)에서 읽는다 (C45).
//
// 되돌리기: 실행 전 *.controller 를 git 으로 커밋해 두면 checkout 로 복구된다 (상태 추가일 뿐이라 남겨도 무해).
public static class WL_StartWakeAnimSetup
{
const bool kDryRun = false; // 🔴 먼저 true 로 실행해 로그를 검토할 것
const string kLobbyController = "Assets/Res_Addr/Animations/pcanim_lobby.controller";
const string kClassConfig = "Assets/ResWork/Table/Export/ClassConfig.json";
const string kClipDir = "Assets/Res_Addr/Animations/Animation/Common_PlatformAnim";
const string kLyingFbx = kClipDir + "/Stander@LyingFront.FBX";
const string kWakeFbx = kClipDir + "/Stander@LyingFront_WakeUp.FBX";
const string kLyingState = "lying";
const string kGetUpState = "getup";
static System.Text.StringBuilder s_log = new System.Text.StringBuilder();
const string kLogPath = "AgentScripts/staging/WL_Monsters/WAKEANIM_LOG.txt";
public static void Run()
{
s_log.Length = 0;
Log("===== WL_StartWakeAnimSetup dryRun=" + kDryRun + " =====");
// 대상 컨트롤러 = 로비 + ClassConfig.json 의 s_AnimationController 전부 (C45 — SOT 직접 읽기)
var targets = new System.Collections.Generic.List<string>();
targets.Add(kLobbyController);
if (!System.IO.File.Exists(kClassConfig)) { Log("[오류] 테이블 없음: " + kClassConfig); Flush(); return; }
var json = System.IO.File.ReadAllText(kClassConfig);
var rx = new System.Text.RegularExpressions.Regex("\"s_AnimationController\"\\s*:\\s*\"([^\"]+)\"");
foreach (System.Text.RegularExpressions.Match m in rx.Matches(json))
{
var p = m.Groups[1].Value;
if (!targets.Contains(p)) targets.Add(p);
}
Log("대상 컨트롤러 " + targets.Count + "개 (로비 1 + ClassConfig " + (targets.Count - 1) + ")");
var lyingClip = FirstClip(kLyingFbx);
var wakeClip = FirstClip(kWakeFbx);
if (lyingClip == null || wakeClip == null)
{
Log("[오류] 클립을 못 찾았다. FBX 2개를 " + kClipDir + " 에 복사하고 임포트했는지 확인할 것.");
Flush(); return;
}
Log("lying 클립 = " + lyingClip.name + " length=" + lyingClip.length.ToString("F3")
+ " loop=" + lyingClip.isLooping + " humanMotion=" + lyingClip.humanMotion);
Log("getup 클립 = " + wakeClip.name + " length=" + wakeClip.length.ToString("F3")
+ " loop=" + wakeClip.isLooping + " humanMotion=" + wakeClip.humanMotion);
if (!lyingClip.humanMotion || !wakeClip.humanMotion)
Log("[경고] Humanoid 클립이 아니다 — 재타게팅이 되지 않는다.");
int changed = 0, already = 0;
for (int c = 0; c < targets.Count; c++)
{
var path = targets[c];
var ctrl = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEditor.Animations.AnimatorController>(path);
if (ctrl == null) { Log("[오류] 컨트롤러 없음: " + path); continue; }
if (ctrl.layers == null || ctrl.layers.Length == 0) { Log("[오류] 레이어 없음: " + path); continue; }
var sm = ctrl.layers[0].stateMachine;
bool hasLying = HasState(sm, kLyingState);
bool hasGetUp = HasState(sm, kGetUpState);
if (hasLying && hasGetUp)
{
already++;
Log(" [유지] " + System.IO.Path.GetFileName(path) + " 상태 " + sm.states.Length + "개 · lying/getup 이미 있음");
continue;
}
if (kDryRun)
{
Log(" [예정] " + System.IO.Path.GetFileName(path) + " 상태 " + sm.states.Length + "개 → 추가: "
+ (hasLying ? "" : kLyingState + " ") + (hasGetUp ? "" : kGetUpState));
continue;
}
if (!hasLying)
{
var s = sm.AddState(kLyingState);
s.motion = lyingClip; s.speed = 1f; s.writeDefaultValues = false;
}
if (!hasGetUp)
{
var s = sm.AddState(kGetUpState);
s.motion = wakeClip; s.speed = 1f; s.writeDefaultValues = false;
}
// 전이(transition)는 만들지 않는다 — 힘민지는 Animator.Play(stateName) 직접 호출 방식이라
// 전이가 있으면 오히려 의도치 않은 자동 이동이 생긴다 (Actor.AnimationPlay, Actor.cs:2020).
UnityEditor.EditorUtility.SetDirty(ctrl);
changed++;
Log(" [추가] " + System.IO.Path.GetFileName(path) + " 최종 상태 " + sm.states.Length + "개 (기본상태 = "
+ (sm.defaultState == null ? "null" : sm.defaultState.name) + " · 변경 안 함)");
}
if (!kDryRun)
{
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
}
Log("완료 — 변경 " + changed + "개 · 이미 보유 " + already + "개 · 대상 " + targets.Count + "개 (dryRun=" + kDryRun + ")");
Flush();
}
static void Log(string s) { s_log.AppendLine(s); }
static void Flush()
{
var dir = System.IO.Path.GetDirectoryName(kLogPath);
if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);
System.IO.File.WriteAllText(kLogPath, s_log.ToString());
UnityEngine.Debug.Log("[WakeAnim] 로그: " + kLogPath);
}
static UnityEngine.AnimationClip FirstClip(string fbxPath)
{
var all = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(fbxPath);
if (all == null) return null;
UnityEngine.AnimationClip best = null;
for (int i = 0; i < all.Length; i++)
{
var c = all[i] as UnityEngine.AnimationClip;
if (c == null) continue;
if (c.name.StartsWith("__preview__")) continue;
if (best == null) best = c;
}
return best;
}
static bool HasState(UnityEditor.Animations.AnimatorStateMachine sm, string name)
{
for (int i = 0; i < sm.states.Length; i++)
if (sm.states[i].state != null && sm.states[i].state.name == name) return true;
return false;
}
static string StateNames(UnityEditor.Animations.AnimatorStateMachine sm)
{
var sb = new System.Text.StringBuilder();
for (int i = 0; i < sm.states.Length; i++)
{
if (sm.states[i].state == null) continue;
if (sb.Length > 0) sb.Append(", ");
sb.Append(sm.states[i].state.name);
}
return sb.ToString();
}
}