ThreeKingdomsSlingshot/Assets/Scripts/Runtime/SkillTest/SkillVfxShots.cs

253 lines
12 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using UnityEngine;
namespace TKS
{
/// <summary>
/// 스킬 연출 자동 촬영(2026-11-11 · 액티브 스킬 연출 본작업의 자체 품질 확인용 · PD 보고용 아님): 스킬 테스트 씬에서 영웅을 차례로 골라
/// 액티브를 발동(즉시형 = 버튼 · 다음 행동형 = 버튼 → 적 1 쪽으로 슬링샷)하고 정해진 시각마다 주 카메라를 RenderTexture 에 직접 그려 저장한다
/// (게임 창 캡처는 에디터가 뒤에 있으면 갱신되지 않음 · 실측). 시간 = <c>Time.captureFramerate</c> 로 프레임마다 고정 간격(실제 시간과 무관 · 재현 가능).
/// 영웅 id 앞에 - 를 붙이면(-guanyu) 액티브를 누르지 않고 일반 슬링샷(돌진)만 찍는다(파일 이름 = id + dash).
/// ~ 를 붙이면(~liubei) 예약만 하고 쏘지 않은 채 예약 기운(aura)을 시각마다 찍는다(파일 이름 = id + aura · 유닛 중심).
/// + 를 붙이면(+liubei) 쏜 뒤 화면 중심이 유닛을 따라간다(파일 이름 = id + unit · 재행동 이펙트 확인용).
/// 시작 = Play 중 <c>SkillVfxShots.Begin(폴더, "guanyu,zhangfei", "0.1,0.3,…", 한 변 픽셀, 화면 높이(월드))</c> · 끝 = 폴더에 done.txt.
/// </summary>
public class SkillVfxShots : MonoBehaviour
{
string outDir;
string[] heroIds;
float[] times;
int size;
float viewH;
readonly List<string> log = new List<string>();
public static string Begin(string outDir, string heroIdsCsv, string timesCsv, int size, float viewH)
{
if (!Application.isPlaying) return "Play 중이 아님";
if (SkillTestBootstrap.Instance == null) return "스킬 테스트 씬이 아님";
var go = new GameObject("SkillVfxShots");
var d = go.AddComponent<SkillVfxShots>();
d.outDir = outDir;
d.heroIds = heroIdsCsv.Split(',');
var ts = timesCsv.Split(',');
d.times = new float[ts.Length];
for (int i = 0; i < ts.Length; i++) d.times[i] = float.Parse(ts[i].Trim(), CultureInfo.InvariantCulture);
d.size = size;
d.viewH = viewH;
Directory.CreateDirectory(outDir);
if (File.Exists(Path.Combine(outDir, "done.txt"))) File.Delete(Path.Combine(outDir, "done.txt"));
d.StartCoroutine(d.Go());
return "시작 " + d.heroIds.Length + "명";
}
/// <summary>먼 적 시험(^)에서 적을 위로 옮기는 거리(월드).</summary>
const float FarShift = 6f;
IEnumerator Go()
{
var st = SkillTestBootstrap.Instance;
int prevRate = Time.captureFramerate;
Time.captureFramerate = 30;
st.EnemyCount = SkillTestBootstrap.MaxEnemies;
foreach (var raw in heroIds)
{
string id = raw.Trim();
bool plain = id.StartsWith("-");
bool auraOnly = id.StartsWith("~");
bool followUnit = id.StartsWith("+");
bool moveTest = id.StartsWith(">"); // 적이 없는 쪽으로 일반 슬링샷 = 이동만(궁병의 출발·도착 먼지 확인 · 파일 이름 <id>move)
bool farTest = id.StartsWith("^"); // 적을 사거리 밖 멀리(위로 FarShift) 옮긴 뒤 스킬 = 먼 적까지 날아가는지 확인(파일 이름 <id>far)
bool allyTest = id.StartsWith("@"); // 아군 더미를 향해 스킬(간옹 유세 = 아군 명중 회복 확인 · 파일 이름 <id>ally · 2026-11-13)
if (plain || auraOnly || followUnit || moveTest || farTest || allyTest) id = id.Substring(1);
if (allyTest) st.AllyDummy = true;
HeroDb_Hero(st, id, out var hero);
if (hero == null)
{
Log(id + " 영웅 없음");
continue;
}
st.SelectHero(hero);
yield return WaitPhase(st, 5f);
yield return Frames(12);
Unit u = null;
foreach (var p in st.Battle.AliveUnits(Faction.Player))
{
u = p;
break;
}
if (u == null)
{
Log(id + " 유닛 없음");
continue;
}
if (farTest)
foreach (var e in st.Battle.AliveUnits(Faction.Enemy))
{
var fp = st.Battle.Board.ClampInside(e.Position + new Vector2(0f, FarShift), e.Radius);
e.Position = fp;
e.transform.position = new Vector3(fp.x, fp.y, 0f);
}
Unit foe = null;
float best = float.MaxValue;
foreach (var e in st.Battle.AliveUnits(Faction.Enemy))
{
float dx = Mathf.Abs(e.Position.x - u.Position.x);
if (dx < best)
{
best = dx;
foe = e;
}
}
Vector2 target = foe != null ? foe.Position : u.Position + new Vector2(0f, 3.4f);
if (moveTest) target = u.Position + new Vector2(-2.4f, 0.5f);
if (allyTest)
foreach (var a in st.Battle.AliveUnits(Faction.Player))
if (a != u)
{
target = a.Position;
break;
}
Vector2 focus = (u.Position + target) * 0.5f + new Vector2(0f, 0.6f);
var us = st.Battle.Skills.Of(u);
bool instant = us != null && us.Active != null && us.Active.Trigger == SkillTrigger.Instant;
st.Sling.Select(u);
yield return null;
int n = 0;
float t0 = Time.time;
if (plain || moveTest)
{
bool ok = st.Battle.RequestPlayerAction(u, target);
Log(id + " 일반 슬링샷" + (ok ? "" : " · 행동 요청 실패"));
id += moveTest ? "move" : "dash";
t0 = Time.time;
}
else
{
string msg = st.Hud.PressSkill();
Log(id + " " + (us != null && us.Active != null ? us.Active.Id : "-") + (instant ? " 즉시" : " 예약") + " · " + msg);
}
if (auraOnly)
{
yield return new WaitForSecondsRealtime(0.35f);
id += "aura";
t0 = Time.time;
foreach (float t in times)
{
while (Time.time - t0 < t - 1e-4f) yield return null;
yield return Shot(u.Position + new Vector2(0f, 0.75f), id, n++, t.ToString("0.00", CultureInfo.InvariantCulture));
}
continue;
}
if (followUnit) id += "unit";
if (farTest) id += "far";
if (allyTest) id += "ally";
if (!plain && !moveTest && !instant)
{
yield return new WaitForSecondsRealtime(0.35f); // 기운(aura)은 실제 시간 0.15초마다 맞춰 뜸(SkillVfxLoops)
yield return Frames(24); // 예약 기운(aura) 확인용 0.8초
yield return Shot(focus, id, n++, "aura");
bool ok = st.Battle.RequestPlayerAction(u, target);
if (!ok) Log(id + " 행동 요청 실패");
t0 = Time.time;
}
foreach (float t in times)
{
while (Time.time - t0 < t - 1e-4f)
{
Tap(st, u, target);
yield return null;
}
Tap(st, u, target);
yield return Shot(followUnit ? u.Position + new Vector2(0f, 0.75f) : focus, id, n++, t.ToString("0.00", CultureInfo.InvariantCulture));
}
float w0 = Time.time;
while (st.Battle.Phase != BattlePhase.PlayerTurn && Time.time - w0 < 6f)
{
Tap(st, u, target);
yield return null;
}
}
Time.captureFramerate = prevRate;
File.WriteAllLines(Path.Combine(outDir, "done.txt"), log.ToArray());
Destroy(gameObject);
}
static void HeroDb_Hero(SkillTestBootstrap st, string id, out TKS.OutGame.HeroDb.Hero hero)
{
hero = null;
foreach (var h in st.Heroes)
if (h.id == id) hero = h;
}
/// <summary>행동 중 탭을 기다리는 스킬(조조 밀칠 방향 · 하후돈 복귀 · 장합 변진)에 자동으로 한 점을 넣는다.</summary>
static void Tap(SkillTestBootstrap st, Unit u, Vector2 target)
{
var sk = st.Battle.Skills;
if (sk == null || sk.PendingKind == SkillSystem.TapKind.None) return;
Vector2 w = sk.PendingKind == SkillSystem.TapKind.Teleport ? u.Position + new Vector2(1.2f, 2.2f) : sk.PendingCenter + new Vector2(1.5f, 1.5f);
string m;
sk.TryTeleportTap(w, out m);
}
static IEnumerator WaitPhase(SkillTestBootstrap st, float sec)
{
float t0 = Time.realtimeSinceStartup;
while (st.Battle.Phase != BattlePhase.PlayerTurn && Time.realtimeSinceStartup - t0 < sec) yield return null;
}
static IEnumerator Frames(int n)
{
for (int i = 0; i < n; i++) yield return null;
}
IEnumerator Shot(Vector2 focus, string id, int n, string tag)
{
yield return new WaitForEndOfFrame();
var cam = Camera.main;
if (cam == null) yield break;
var rt = RenderTexture.GetTemporary(size, size, 24);
var prevTarget = cam.targetTexture;
var prevPos = cam.transform.position;
float prevSize = cam.orthographicSize;
int prevMask = cam.cullingMask;
var canvases = FindObjectsByType<Canvas>(FindObjectsSortMode.None); // HUD 는 빼고 전장만
var wasOn = new bool[canvases.Length];
for (int i = 0; i < canvases.Length; i++)
{
wasOn[i] = canvases[i].enabled;
canvases[i].enabled = false;
}
cam.targetTexture = rt;
if (viewH > 0f) // 0 이하 = 게임 화면 카메라 그대로(옮겨 찍으면 길게 늘인 빛 기둥 · 레이저가 빠짐 · 2026-11-12 실측)
{
cam.transform.position = new Vector3(focus.x, focus.y, prevPos.z);
cam.orthographicSize = viewH * 0.5f;
}
cam.Render();
cam.cullingMask = prevMask;
for (int i = 0; i < canvases.Length; i++)
if (canvases[i] != null) canvases[i].enabled = wasOn[i];
cam.targetTexture = prevTarget;
cam.transform.position = prevPos;
cam.orthographicSize = prevSize;
var prevActive = RenderTexture.active;
RenderTexture.active = rt;
var tex = new Texture2D(size, size, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, size, size), 0, 0);
tex.Apply();
RenderTexture.active = prevActive;
RenderTexture.ReleaseTemporary(rt);
File.WriteAllBytes(Path.Combine(outDir, id + "_" + n.ToString("00") + "_" + tag + ".png"), tex.EncodeToPNG());
Destroy(tex);
}
void Log(string s)
{
log.Add(s);
}
}
}