using System.Collections.Generic;
using UnityEngine;
namespace TKS
{
///
/// 조준 중 상성 표시(개발 요청서 C5 · 1-4 부터 모든 판 · 2026-11-12) — 당기고 있는 유닛 기준으로 적 머리 위에
/// 유리(위 화살표 · 초록) / 불리(아래 화살표 · 붉은색)를 띄운다(상성 배율 = BalanceConfig.matchup · 같으면 표시 없음).
/// 켜는 판 = tutorial_config matchup_mark_chapter·stage 부터(TutorialRules.MatchupMarksOn). SlingshotInput 이 누를 때 Show · 놓을 때 Hide.
///
public class MatchupMarks : MonoBehaviour
{
static readonly Color GoodColor = new Color(0.35f, 0.95f, 0.4f);
static readonly Color BadColor = new Color(1f, 0.38f, 0.32f);
const int Order = 13;
const float Size = 0.9f;
static Sprite arrow;
readonly List pool = new List();
int used;
/// 지금 떠 있는 표시 수.
public int Count
{
get { return used; }
}
/// 표시 i 가 유리(위 화살표)인가.
public bool IsGood(int i)
{
return i >= 0 && i < used && pool[i].color == GoodColor;
}
public void Show(BattleManager battle, Unit actor)
{
Hide();
if (battle == null || battle.Config == null || actor == null || !TutorialRules.MatchupMarksOn(battle.Stage)) return;
foreach (var e in battle.Units)
{
if (e == null || !e.IsAlive || e.Faction == actor.Faction) continue;
float m = battle.Config.Matchup(actor.Type, e.Type);
if (Mathf.Abs(m - 1f) < 0.001f) continue;
bool good = m > 1f;
var sr = Next();
sr.transform.SetParent(e.transform, false);
bool art = e.View != null && e.View.HasArt;
float top = art ? -e.Radius * 0.55f + e.ArtHeight + 0.12f : e.Radius + 0.22f; // UnitView 의 HP 바 높이와 같은 식
sr.transform.localPosition = new Vector3(0f, top + 0.62f, 0f);
sr.transform.localRotation = Quaternion.Euler(0f, 0f, good ? 0f : 180f);
sr.transform.localScale = new Vector3(Size, Size, 1f);
sr.color = good ? GoodColor : BadColor;
sr.gameObject.SetActive(true);
}
}
public void Hide()
{
for (int i = 0; i < pool.Count; i++)
{
var sr = pool[i];
if (sr == null) continue; // 붙어 있던 유닛이 제거됨
sr.gameObject.SetActive(false);
sr.transform.SetParent(transform, false);
}
pool.RemoveAll(x => x == null);
used = 0;
}
SpriteRenderer Next()
{
if (used < pool.Count) return pool[used++];
var go = new GameObject("MatchupMark");
var sr = go.AddComponent();
sr.sprite = Arrow;
sr.sortingOrder = Order;
pool.Add(sr);
used++;
return sr;
}
void OnDestroy()
{
foreach (var sr in pool)
if (sr != null) Destroy(sr.gameObject);
pool.Clear();
}
/// 위쪽 화살표(흰 세모 + 짙은 테두리 · 1 월드 단위 크기) — 색은 SpriteRenderer.color.
static Sprite Arrow
{
get
{
if (arrow != null) return arrow;
const int n = 64;
var tex = new Texture2D(n, n, TextureFormat.RGBA32, false);
tex.wrapMode = TextureWrapMode.Clamp;
tex.filterMode = FilterMode.Bilinear;
var px = new Color[n * n];
for (int y = 0; y < n; y++)
{
for (int x = 0; x < n; x++)
{
// 세모: 아래 변 y = 8 · 꼭짓점 (32, 58) — 가장자리까지의 거리로 흰 면·짙은 테두리·바깥 투명
float fy = y + 0.5f, fx = x + 0.5f;
float half = (58f - fy) * (26f / 50f); // 그 높이의 반 폭
float dSide = (half - Mathf.Abs(fx - 32f)) * 0.887f; // 빗변까지 수직 거리
float d = Mathf.Min(dSide, fy - 8f);
Color c;
if (d < 0f) c = new Color(0f, 0f, 0f, 0f);
else if (d < 4f) c = new Color(0.12f, 0.1f, 0.1f, Mathf.Clamp01(d + 0.5f));
else c = Color.white;
px[y * n + x] = c;
}
}
tex.SetPixels(px);
tex.Apply();
tex.hideFlags = HideFlags.DontSave;
arrow = Sprite.Create(tex, new Rect(0, 0, n, n), new Vector2(0.5f, 0.5f), n);
arrow.hideFlags = HideFlags.DontSave;
return arrow;
}
}
}
}