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

179 lines
7.3 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;
namespace TKS.OutGame
{
/// <summary>
/// 외형(코스튬) 규칙(2026-11-20 개발요청_코스튬 §2 · §3) — 보유 = 계정 공용(SaveData.costumes) · 착용 = 영웅마다 부위마다 1개(HeroState.costume) ·
/// 미보유도 미리보기 · 적용 = 보유한 것만 · 외형(skin) 부위 = 잠김(costume_slots e_state Later) · 능력치 없음.
/// 기준점 = costume_anchors(그림 비율 · 왼쪽 위 0) · 없으면 알파 경계 자동(머리 = 위 중앙 · 얼굴 = 위에서 38 % · 무기 = 숨김) · 다른 프레임 = 윗선 이동만큼 · 뒷모습 = 얼굴 숨김.
/// </summary>
public static class CostumeRules
{
/// <summary>자동 기준점 얼굴 높이(경계 위에서 비율 · costume_config anchor_fallback 메모 「얼굴 = 위에서 38 %」).</summary>
const float AutoFaceRatio = 0.38f;
public static bool Owns(SaveData d, string id)
{
return d != null && d.costumes != null && d.costumes.Contains(id);
}
/// <summary>꾸미기 얻기(테스트 옵션 지급 · 얻는 곳은 PD 결정 대기) → 빨간 점.</summary>
public static bool Grant(CostumeDb db, SaveData d, string id)
{
if (db == null || d == null || db.ItemOf(id) == null || Owns(d, id)) return false;
if (d.costumes == null) d.costumes = new List<string>();
d.costumes.Add(id);
d.costumeNew = true;
return true;
}
public static int GrantAll(CostumeDb db, SaveData d)
{
int n = 0;
foreach (var it in db.items)
if (Grant(db, d, it.id)) n++;
return n;
}
/// <summary>아직 없는 꾸미기 하나(표 순서) — 없으면 null.</summary>
public static string GrantNext(CostumeDb db, SaveData d)
{
foreach (var it in db.items)
if (Grant(db, d, it.id)) return it.id;
return null;
}
/// <summary>영웅의 착용(부위 → id · 열린 부위만).</summary>
public static Dictionary<string, string> Worn(CostumeDb db, HeroState hs)
{
var m = new Dictionary<string, string>();
if (db == null || hs == null || hs.costume == null) return m;
foreach (var id in hs.costume)
{
var it = db.ItemOf(id);
var sl = it != null ? db.SlotOf(it.slot) : null;
if (sl != null && sl.open) m[it.slot] = id;
}
return m;
}
/// <summary>적용할 수 있는가(null = 됨 · 「unowned」 = 미보유가 입혀져 있음).</summary>
public static string CanApply(CostumeDb db, SaveData d, Dictionary<string, string> map)
{
foreach (var kv in map)
{
if (string.IsNullOrEmpty(kv.Value)) continue;
var it = db.ItemOf(kv.Value);
var sl = it != null ? db.SlotOf(it.slot) : null;
if (it == null || sl == null || !sl.open || it.slot != kv.Key) return "bad";
if (!Owns(d, kv.Value)) return "unowned";
}
return null;
}
/// <summary>착용 저장(부위마다 1개 · 빈 값 = 벗기) — 실패 = 사유.</summary>
public static string Apply(CostumeDb db, SaveData d, HeroState hs, Dictionary<string, string> map)
{
if (hs == null) return "영웅 없음";
var why = CanApply(db, d, map);
if (why != null) return why;
hs.costume = new List<string>();
foreach (var s in db.slots)
{
string id;
if (s.open && map.TryGetValue(s.slot, out id) && !string.IsNullOrEmpty(id)) hs.costume.Add(id);
}
return null;
}
/// <summary>그리는 순서(n_layer 오름차순 = 아래부터) 부위 목록(열린 부위만).</summary>
public static List<CostumeDb.Slot> DrawOrder(CostumeDb db)
{
var l = new List<CostumeDb.Slot>();
foreach (var s in db.slots)
if (s.open) l.Add(s);
l.Sort((a, b) => a.layer.CompareTo(b.layer));
return l;
}
// ---------- 그림 ----------
static readonly Dictionary<string, Sprite> sprites = new Dictionary<string, Sprite>();
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
sprites.Clear();
}
/// <summary>꾸미기 그림(Resources/s_art · 가운데 = 기준점) — 텍스처로 읽어 가운데 피벗 스프라이트(임포트 종류와 무관).</summary>
public static Sprite Art(CostumeDb.Item it)
{
if (it == null) return null;
Sprite sp;
if (sprites.TryGetValue(it.art, out sp) && sp != null) return sp;
var tex = Resources.Load<Texture2D>(it.art);
sp = tex != null ? Sprite.Create(tex, new Rect(0f, 0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100f) : null;
sprites[it.art] = sp;
return sp;
}
// ---------- 기준점 ----------
public struct Place
{
public bool show;
/// <summary>그 프레임 그림 안 자리(px · 왼쪽 위 0).</summary>
public float x, y;
/// <summary>꾸미기 폭(px) = 유닛 그림 폭 × f_scale.</summary>
public float width;
public float rot;
public int frameW, frameH;
}
/// <summary>그 부위 기준점(unit = 영웅 id · art = 그림 id · clip/index = 지금 프레임).</summary>
public static Place Resolve(CostumeDb db, string unit, string art, CostumeDb.Slot slot, string clip, int index)
{
var p = new Place();
var baseF = db.FrameOf(art, "front_idle", 0);
if (baseF == null) return p;
var cur = db.FrameOf(art, clip ?? "front_idle", index) ?? baseF;
bool back = clip != null && clip.StartsWith("back");
if (back && slot.anchor == "face") return p; // 뒷모습 = 얼굴 숨김
p.frameW = baseF.w;
p.frameH = baseF.h;
var a = db.AnchorOf(unit, slot.anchor);
float scale = 1f;
if (a != null)
{
p.x = a.x * baseF.w;
p.y = a.y * baseF.h;
scale = a.scale;
p.rot = a.rot;
}
else
{
float cx = (baseF.left + baseF.right) * 0.5f;
switch (slot.anchor)
{
case "head":
p.x = cx;
p.y = baseF.top;
break;
case "face":
p.x = cx;
p.y = baseF.top + (baseF.bottom - baseF.top) * AutoFaceRatio;
break;
default:
return p; // 무기 등 = 표에 기준점이 없으면 숨김(anchor_fallback)
}
}
p.y += cur.top - baseF.top; // 다른 프레임 · 동작 = 그림 윗선 이동만큼
p.width = baseF.w * scale;
p.show = true;
return p;
}
}
}