using System;
using System.Collections;
using UnityEngine;
namespace TKS
{
/// 간단한 연출(이동 트윈·돌진·화살·피해 숫자). 코루틴 실행자는 BattleManager. 가상 실행(Sim.Active · 적 AI 예측) 중 = 이동·대기 즉시 · 팝업·터짐 없음.
public static class Fx
{
public static MonoBehaviour Runner;
public static Coroutine Run(IEnumerator routine)
{
if (Sim.Active)
{
Sim.Run(routine); // 가상 실행 = 그 자리에서 끝까지(병렬 밀기 등)
return null;
}
if (Runner == null) return null;
return Runner.StartCoroutine(routine);
}
public static IEnumerator Wait(float seconds)
{
if (Sim.Active) yield break;
float t = 0f;
while (t < seconds)
{
t += Time.deltaTime;
yield return null;
}
}
///
/// 이동 물리(전투 시작 때 BalanceConfig 에서 받음 · PD 2026-11-16 「힘과 거리에 비례한 자연스러운 물리적 움직임 · 알까기·당구처럼」):
/// 모든 이동 = 마찰로 멈추는 등감속 운동 · 감속도 a = 2 × speed² / RefDistance(= RefDistance 를 갈 때 평균 속도가 speed) →
/// 거리 D 를 가는 초기 속도 = √(2aD)(멀리 = 세게 = 빠르게 출발) · 걸리는 시간 = √(D × RefDistance) / speed.
/// RefDistance 0 = 예전 방식(시간 = 거리 / 속도). ImpactFalloff = 부딪히러 가는 동안 감속이 걸리는 비율(BalanceConfig.impactFalloff).
///
public static float RefDistance;
public static float ImpactFalloff = 1f;
public static void Configure(BalanceConfig cfg)
{
RefDistance = cfg != null ? cfg.moveRefDistance : 0f;
ImpactFalloff = cfg != null ? Mathf.Clamp01(cfg.impactFalloff) : 1f;
}
/// MoveAlong 이 거리 total 을 가서 멈출 때까지 걸리는 시간(초) — 닿기 직전 이펙트(prehit)가 닿는 시각을 미리 셈.
public static float MoveDuration(float total, float speed)
{
speed = Mathf.Max(0.1f, speed);
return Mathf.Max(0.12f, RefDistance > 0f ? Mathf.Sqrt(Mathf.Max(0f, total) * RefDistance) / speed : total / speed);
}
///
/// 힘 launch(쏜 거리)로 출발해 total 만큼 간 곳에서 부딪힐 때까지 걸리는 시간 · v0 = launch 를 다 갈 때의 초기 속도 · fa = 가는 동안의 감속(마찰 × ImpactFalloff).
/// 부딪히는 순간 속도 = √(v0² − 2 fa total) = 힘 (launch − ImpactFalloff × total) 로 밀려나는 유닛의 초기 속도와 같다(속도가 그대로 이어짐).
///
public static float ApproachDuration(float total, float launch, float speed)
{
float v0, fa;
if (!Approach(launch, speed, out v0, out fa)) return Mathf.Max(0.12f, total / Mathf.Max(0.1f, speed));
float t = fa < 1e-4f ? total / v0 : (v0 - Mathf.Sqrt(Mathf.Max(0f, v0 * v0 - 2f * fa * total))) / fa;
return Mathf.Max(0.03f, t);
}
static bool Approach(float launch, float speed, out float v0, out float fa)
{
v0 = fa = 0f;
if (RefDistance <= 0f || launch <= 0f) return false;
speed = Mathf.Max(0.1f, speed);
float a = 2f * speed * speed / RefDistance;
v0 = Mathf.Sqrt(2f * a * launch);
fa = a * ImpactFalloff;
return true;
}
///
/// from → to 이동. 기본 = 마찰로 멈추는 감속 · launch > 0 = 힘 launch 로 쏴서 to 에서 부딪히는 직진(속도를 가진 채 닿는다 · ApproachDuration) ·
/// easeOut false(launch 없음) = 등속. onDistance 에는 지금까지 지나온 거리를 넘긴다.
///
public static IEnumerator MoveAlong(Transform tr, Vector2 from, Vector2 to, float speed, Action onDistance, bool easeOut = true, float launch = 0f)
{
float total = Vector2.Distance(from, to);
if (total < 1e-4f) yield break;
if (Sim.Active)
{
tr.position = new Vector3(to.x, to.y, 0f);
if (onDistance != null) onDistance(total);
yield break;
}
float v0, fa;
bool approach = Approach(Mathf.Max(launch, launch > 0f ? total : 0f), speed, out v0, out fa);
float duration = approach ? ApproachDuration(total, Mathf.Max(launch, total), speed) : MoveDuration(total, speed);
float el = 0f;
while (el < duration)
{
el += Time.deltaTime;
float k = Mathf.Clamp01(el / duration);
float tt = k * duration;
float e = approach ? Mathf.Clamp01((v0 * tt - 0.5f * fa * tt * tt) / total) : easeOut ? 1f - (1f - k) * (1f - k) : k;
var p = Vector2.Lerp(from, to, e);
tr.position = new Vector3(p.x, p.y, 0f);
if (onDistance != null) onDistance(e * total);
yield return null;
}
tr.position = new Vector3(to.x, to.y, 0f);
if (onDistance != null) onDistance(total);
}
/// 근접 공격 돌진(짧게 찌르고 복귀).
public static IEnumerator Lunge(Unit a, Vector2 toward)
{
if (Sim.Active) yield break;
Vector2 start = a.Position;
Vector2 dir = (toward - start);
if (dir.sqrMagnitude < 1e-4f) yield break;
dir.Normalize();
Vector2 peak = start + dir * 0.3f;
yield return MoveAlong(a.transform, start, peak, 6f, null);
yield return MoveAlong(a.transform, peak, start, 4f, null);
a.Position = start;
}
public static readonly Color ArrowColor = new Color(1f, 0.95f, 0.75f);
public static readonly Color StrategistShotColor = new Color(0.75f, 0.55f, 1f);
public static readonly Color SplashColor = new Color(0.7f, 0.45f, 1f, 0.45f);
public static readonly Color FriendlyFireColor = new Color(1f, 0.3f, 0.3f);
/// 화살(가늘고 긴 사각형) 날리기.
public static IEnumerator Arrow(Vector2 from, Vector2 to)
{
return Arrow(from, to, ArrowColor);
}
/// 투사체 비행 시간(초) — 선 화살과 스킬 투사체 이펙트(SkillVfx.Shot)가 같은 시간에 도착(피해 시점 동일).
public static float ArrowDuration(float dist)
{
return Mathf.Max(0.12f, dist / 14f);
}
/// 투사체(가늘고 긴 사각형) 날리기 — 궁병 화살 · 책사 계략(색만 다름).
public static IEnumerator Arrow(Vector2 from, Vector2 to, Color color)
{
if (Sim.Active) yield break;
var go = new GameObject("Arrow");
var sr = go.AddComponent();
sr.sprite = ProceduralSprites.Square;
sr.color = color;
sr.sortingOrder = 8;
Vector2 d = to - from;
float ang = Mathf.Atan2(d.y, d.x) * Mathf.Rad2Deg;
go.transform.rotation = Quaternion.Euler(0f, 0f, ang);
go.transform.localScale = new Vector3(0.5f, 0.06f, 1f);
float dur = ArrowDuration(d.magnitude);
float t = 0f;
while (t < dur)
{
t += Time.deltaTime;
var p = Vector2.Lerp(from, to, Mathf.Clamp01(t / dur));
go.transform.position = new Vector3(p.x, p.y, 0f);
yield return null;
}
UnityEngine.Object.Destroy(go);
}
/// 광역 터짐(반지름 radius 원이 짧게 커지며 사라짐 · 책사 광역).
public static void Burst(Vector2 center, float radius, Color color)
{
if (Sim.Active) return;
Run(BurstRoutine(center, radius, color));
}
static IEnumerator BurstRoutine(Vector2 center, float radius, Color color)
{
var sr = ProceduralSprites.NewSprite(null, "Burst", ProceduralSprites.Circle, color, radius * 2f, 7);
sr.transform.position = new Vector3(center.x, center.y, 0f);
float t = 0f;
const float dur = 0.35f;
while (t < dur)
{
t += Time.deltaTime;
float k = Mathf.Clamp01(t / dur);
float s = radius * 2f * (0.6f + 0.4f * k);
sr.transform.localScale = new Vector3(s, s, 1f);
var c = color;
c.a = color.a * (1f - k);
sr.color = c;
yield return null;
}
UnityEngine.Object.Destroy(sr.gameObject);
}
public static void Popup(Vector2 pos, string text, Color color)
{
if (Sim.Active) return;
Run(PopupRoutine(pos, text, color));
}
static IEnumerator PopupRoutine(Vector2 pos, string text, Color color)
{
var tm = TextUtil.CreateWorldText(null, text, 0.42f, color, 20);
tm.gameObject.name = "Popup";
var tr = tm.transform;
float t = 0f;
const float dur = 0.75f;
while (t < dur)
{
t += Time.deltaTime;
float k = Mathf.Clamp01(t / dur);
tr.position = new Vector3(pos.x, pos.y + 0.55f + 0.7f * k, 0f);
var c = color;
c.a = 1f - k * k;
tm.color = c;
yield return null;
}
UnityEngine.Object.Destroy(tm.gameObject);
}
}
}