ThreeKingdomsSlingshot/Assets/Scripts/OutGame/Costume/CostumeLayers.cs

234 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>꾸미기 레이어 공용 — 지금 그림이 어느 클립 몇 번째 프레임인지 · 착용 지도(세이브 · 미리보기).</summary>
public static class CostumeView
{
public static CostumeDb Db
{
get { return OutGameData.Default != null ? OutGameData.Default.Costume : null; }
}
/// <summary>내 세이브의 그 영웅 착용(없으면 빈 지도).</summary>
public static Dictionary<string, string> WornOf(string heroId)
{
var root = OutGameRoot.Instance;
var s = root != null ? root.Session : null;
var hs = s != null && s.Save != null && !string.IsNullOrEmpty(heroId) ? s.Save.Hero(heroId) : null;
return CostumeRules.Worn(Db, hs);
}
/// <summary>스프라이트 → 클립 · 프레임 번호(이미 읽힌 클립에서 · 못 찾으면 front_idle 0).</summary>
public static void FrameOf(UnitArtData data, Sprite sp, string hint, out string clip, out int index)
{
clip = "front_idle";
index = 0;
if (data == null || sp == null) return;
Sprite[] fr;
if (!string.IsNullOrEmpty(hint) && data.Clips.TryGetValue(hint, out fr))
{
int i = System.Array.IndexOf(fr, sp);
if (i >= 0)
{
clip = hint;
index = i;
return;
}
}
if (data.Clips.TryGetValue("front_idle", out fr))
{
int i = System.Array.IndexOf(fr, sp);
if (i >= 0) index = i;
}
}
}
/// <summary>
/// 전투 유닛 꾸미기(2026-11-20 개발요청_코스튬 §3) — 유닛 그림(FrameAnimator SpriteRenderer) 위에 n_layer 순으로 자식 SpriteRenderer · 기준점 · 폭 = 그림 폭 × f_scale ·
/// 좌우 뒤집힘 = 같이 · 아군 영웅(내 세이브의 착용)만 · 매 프레임 지금 프레임에 맞춤.
/// </summary>
public class CostumeLayers : MonoBehaviour
{
Unit unit;
FrameAnimator anim;
readonly Dictionary<string, SpriteRenderer> layers = new Dictionary<string, SpriteRenderer>();
public static CostumeLayers Attach(FrameAnimator anim, Unit u)
{
if (anim == null || u == null) return null;
var c = anim.gameObject.AddComponent<CostumeLayers>();
c.anim = anim;
c.unit = u;
return c;
}
/// <summary>시험 · 캡처 — 보이는 꾸미기 수.</summary>
public int Visible
{
get
{
int n = 0;
foreach (var kv in layers)
if (kv.Value != null && kv.Value.enabled) n++;
return n;
}
}
void LateUpdate()
{
var db = CostumeView.Db;
var sr = anim != null ? anim.Renderer : null;
bool on = db != null && sr != null && sr.enabled && unit != null && unit.Faction == Faction.Player && !unit.Npc && !string.IsNullOrEmpty(unit.HeroId);
var worn = on ? CostumeView.WornOf(unit.HeroId) : null;
var data = anim != null ? anim.Data : null;
string clip;
int index;
CostumeView.FrameOf(data, sr != null ? sr.sprite : null, anim != null ? anim.Current : null, out clip, out index);
if (db != null)
foreach (var slot in CostumeRules.DrawOrder(db))
{
SpriteRenderer lr;
layers.TryGetValue(slot.slot, out lr);
string id = null;
if (worn != null) worn.TryGetValue(slot.slot, out id);
var it = id != null ? db.ItemOf(id) : null;
var pl = it != null && data != null ? CostumeRules.Resolve(db, unit.HeroId, data.Id, slot, clip, index) : default(CostumeRules.Place);
var art = pl.show ? CostumeRules.Art(it) : null;
if (art == null)
{
if (lr != null) lr.enabled = false;
continue;
}
if (lr == null)
{
var go = new GameObject("Costume_" + slot.slot);
go.transform.SetParent(transform, false);
lr = go.AddComponent<SpriteRenderer>();
layers[slot.slot] = lr;
}
lr.enabled = true;
lr.sprite = art;
lr.sortingLayerID = sr.sortingLayerID;
lr.sortingOrder = sr.sortingOrder + slot.layer;
var c = lr.color;
c.a = sr.color.a;
lr.color = c;
float ppu = data.Meta.ppu > 0.01f ? data.Meta.ppu : 100f;
float px = data.Meta.pivot != null && data.Meta.pivot.Length >= 2 ? data.Meta.pivot[0] : 0.5f;
float py = data.Meta.pivot != null && data.Meta.pivot.Length >= 2 ? data.Meta.pivot[1] : 0f;
float lx = (pl.x - px * pl.frameW) / ppu;
float ly = ((pl.frameH - pl.y) - py * pl.frameH) / ppu;
float rot = -pl.rot;
if (sr.flipX)
{
lx = -lx;
rot = -rot;
}
lr.flipX = sr.flipX;
lr.transform.localPosition = new Vector3(lx, ly, 0f);
lr.transform.localEulerAngles = new Vector3(0f, 0f, rot);
float k = (pl.width / ppu) / Mathf.Max(0.0001f, art.bounds.size.x);
lr.transform.localScale = new Vector3(k, k, 1f);
}
}
}
/// <summary>
/// UI 유닛 그림 꾸미기(로비 · 영웅 화면 · 편성 · 외형 창) — 유닛 그림 Image 위 자식 Image(n_layer 순) · 그려진 칸(preserveAspect 맞춤) 기준 · Override = 외형 창 미리보기.
/// </summary>
public class UiCostume : MonoBehaviour
{
Image img;
UnitArtData data;
string heroId;
readonly Dictionary<string, Image> layers = new Dictionary<string, Image>();
/// <summary>외형 창 미리보기(부위 → id · null = 내 세이브 착용).</summary>
public Dictionary<string, string> Override;
public static UiCostume Attach(Image img, UnitArtData data, string heroId)
{
if (img == null || data == null || string.IsNullOrEmpty(heroId) || CostumeView.Db == null) return null;
var c = img.GetComponent<UiCostume>() ?? img.gameObject.AddComponent<UiCostume>();
c.img = img;
c.data = data;
c.heroId = heroId;
return c;
}
public int Visible
{
get
{
int n = 0;
foreach (var kv in layers)
if (kv.Value != null && kv.Value.enabled) n++;
return n;
}
}
void LateUpdate()
{
var db = CostumeView.Db;
if (db == null || img == null || data == null) return;
var worn = Override ?? CostumeView.WornOf(heroId);
string clip;
int index;
CostumeView.FrameOf(data, img.sprite, null, out clip, out index);
var r = img.rectTransform.rect;
// 그려진 칸 = preserveAspect 면 그림 비율로 맞춤(가운데) · 아니면 칸 전체
float dw = r.width, dh = r.height;
var sp = img.sprite;
if (img.preserveAspect && sp != null && sp.rect.width > 0f && sp.rect.height > 0f)
{
float s = Mathf.Min(r.width / sp.rect.width, r.height / sp.rect.height);
dw = sp.rect.width * s;
dh = sp.rect.height * s;
}
float ox = r.xMin + (r.width - dw) * 0.5f, oy = r.yMin + (r.height - dh) * 0.5f;
foreach (var slot in CostumeRules.DrawOrder(db))
{
Image li;
layers.TryGetValue(slot.slot, out li);
string id = null;
if (worn != null) worn.TryGetValue(slot.slot, out id);
var it = id != null ? db.ItemOf(id) : null;
var pl = it != null ? CostumeRules.Resolve(db, heroId, data.Id, slot, clip, index) : default(CostumeRules.Place);
var art = pl.show ? CostumeRules.Art(it) : null;
if (art == null || pl.frameW <= 0 || pl.frameH <= 0)
{
if (li != null) li.enabled = false;
continue;
}
if (li == null)
{
var go = new GameObject("Costume_" + slot.slot, typeof(RectTransform));
go.transform.SetParent(transform, false);
li = go.AddComponent<Image>();
li.raycastTarget = false;
layers[slot.slot] = li;
}
li.transform.SetAsLastSibling(); // n_layer 오름차순으로 돌므로 큰 층이 위(유닛 그림 = 부모 = 맨 아래)
li.enabled = true;
li.sprite = art;
var c = li.color;
c.a = img.color.a;
li.color = c;
var rt = li.rectTransform;
rt.anchorMin = rt.anchorMax = new Vector2(0.5f, 0.5f);
rt.pivot = new Vector2(0.5f, 0.5f);
float w = dw * pl.width / pl.frameW;
rt.sizeDelta = new Vector2(w, w * art.rect.height / Mathf.Max(1f, art.rect.width));
float lx = ox + pl.x / pl.frameW * dw;
float ly = oy + (1f - pl.y / pl.frameH) * dh;
rt.localPosition = new Vector3(lx, ly, 0f);
rt.localEulerAngles = new Vector3(0f, 0f, -pl.rot);
rt.localScale = Vector3.one;
}
}
}
}