ThreeKingdomsSlingshot/Assets/Scripts/OutGame/UI/ShopWindow.cs

320 lines
18 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;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>
/// 재화 상점 창(PD 2026-11-24 · 개발 요청서 「재화배치」 §2-1-5 · 6) — 메달 상점의 품목 목록 팝업 + 살 때 · 받을 때 고르는 창
/// (진영 비서 = 진영 · 불멸 장비 = 병종 → 부위 · 천기 보물 조각 = 보유 보물) · 떠돌이 상인(영웅 조각 · wander_shop) 창. 훈장 상점(맹장 토벌전 화면)도 고르는 창은 이것을 쓴다.
/// 품목 · 가격 · 한도 = 표(ShopRules) · 임시 화면(전용 시안 없음 · 공용 팝업 틀).
/// </summary>
public static class ShopWindow
{
const float W = 860f; // 공용 팝업 내용 칸 폭(OutGameRoot.ModalCustom)
const float Frame = 290f; // 팝업 틀의 위(제목 띠) + 아래(버튼 줄) 몫
const float HeadH = 76f;
const float RowH = 128f;
const float OptH = 96f;
const float OptGap = 14f;
const int OptPage = 15; // 고르는 창 한 쪽의 칸 수(3열 × 5줄)
static readonly Vector2 TL = new Vector2(0f, 1f);
/// <summary>마지막으로 연 상점 · 품목 줄 수 · 고르는 창의 칸 수(테스트 · 캡처).</summary>
public static string LastShop { get; private set; }
public static int LastRows { get; private set; }
public static int LastOptions { get; private set; }
public static string TitleOf(string shop)
{
return shop == ShopRules.Medal ? "메달 상점" : "훈장 상점";
}
/// <summary>상점 창 — 닫을 때 onClose(화면 새로 그리기).</summary>
public static void Open(OutGameRoot root, string shop, Action onClose = null)
{
var s = root.Session;
var db = s.Db;
var d = s.Save;
var rows = ShopRules.Rows(shop);
LastShop = shop;
LastRows = rows.Count;
var c = root.ModalCustom(TitleOf(shop), Frame + HeadH + rows.Count * RowH, new OutGameRoot.ModalButton("닫기", UiKit.Dark, onClose));
var head = UiKit.Label(c, "보유 " + ShopRules.CurrencyKo(shop) + " " + ShopRules.Have(db, d, shop).ToString("N0"), 36, UiKit.Ink, TextAnchor.MiddleLeft, TL, TL, new Vector2(8f, -6f), new Vector2(W - 16f, 56f));
head.name = "Have";
for (int i = 0; i < rows.Count; i++)
{
var r = rows[i];
string item = r.S("s_item");
int limit = r.I("n_weekly_limit"), bought = ShopRules.Bought(db, d, shop, item, s.Now), amount = r.I("n_amount");
bool left = limit <= 0 || bought < limit;
float y = HeadH + i * RowH;
var row = UiKit.Skin(UiKit.At(c, "Row_" + item, 0f, y, W, RowH - 12f), "panel12", UiKit.Col("row_on_field", UiKit.Dark)).rectTransform;
var name = UiKit.Title(UiKit.Label(row, r.S("s_name_ko") + (amount > 1 ? " ×" + amount : ""), 32, UiKit.White, TextAnchor.MiddleLeft, TL, TL, new Vector2(24f, -12f), new Vector2(470f, 48f)), true);
name.name = "Name";
name.horizontalOverflow = HorizontalWrapMode.Overflow;
string sub = PickNote(r.S("e_pick")) + (limit > 0 ? "이번 주 " + bought + " / " + limit : "한도 없음");
UiKit.Label(row, sub, 24, UiKit.Col("text_soft", UiKit.InkSoft), TextAnchor.MiddleLeft, TL, TL, new Vector2(24f, -62f), new Vector2(520f, 40f), false).name = "Sub";
var price = UiKit.Title(UiKit.Label(row, ShopRules.CurrencyKo(shop) + " " + r.I("n_price").ToString("N0"), 28, UiKit.Coin, TextAnchor.MiddleRight, TL, TL, new Vector2(W - 470f, -34f), new Vector2(240f, 48f)), true);
price.name = "Price";
var b = UiKit.SkinBtn(row, "Buy", left ? "btn_l_yellow" : "btn_s_gray", UiKit.White, left ? "구매" : "완료", 32, left ? UiKit.Col("text_on_yellow", UiKit.Ink) : UiKit.White,
W - 216f, 18f, 196f, RowH - 48f, () => Buy(root, shop, item, () => Open(root, shop, onClose)));
b.interactable = left;
}
}
static string PickNote(string pick)
{
switch (pick)
{
case ShopRules.PickFaction: return "진영 선택 · ";
case ShopRules.PickGear: return "병종 · 부위 선택 · ";
case ShopRules.PickTreasure: return "보유 보물 선택 · ";
}
return "";
}
/// <summary>품목 1개 구매 — 고르는 품목이면 고르는 창을 거친다 · 끝나면(성공 · 실패 · 취소) after.</summary>
public static void Buy(OutGameRoot root, string shop, string item, Action after)
{
var s = root.Session;
var r = ShopRules.Row(shop, item);
if (r == null) return;
var pre = ShopRules.CanPay(s.Db, s.Save, shop, item, s.Now);
if (pre != null)
{
root.Toast(pre);
return;
}
Action<string> go = pick => s.ShopBuy(shop, item, pick, (why, gains) =>
{
root.Toast(why ?? (gains != null && gains.Count > 0 ? GoodsRules.Lines(gains) : "구매 완료"));
if (after != null) after();
});
Pick(root, r.S("s_name_ko"), r.S("e_pick"), item, go, after);
}
/// <summary>고르는 창(없으면 바로 go(""))) — kind = ShopRules.Pick* · key = 보상 낱말(비서 등급 · 장비 등급을 읽는다).</summary>
public static void Pick(OutGameRoot root, string title, string kind, string key, Action<string> go, Action cancel)
{
var s = root.Session;
var db = s.Db;
var d = s.Save;
var opts = new List<KeyValuePair<string, Action>>();
switch (kind)
{
case ShopRules.PickFaction:
int tier = GoodsRules.TomeTier(key);
foreach (var f in db.Awaken.Factions)
{
var it = db.Awaken.ItemOf(f, tier);
if (it == null) continue;
string fac = f;
opts.Add(new KeyValuePair<string, Action>(it.nameKo + "\n<size=22>보유 " + AwakenRules.Goods(d, it.key) + "</size>", () => go(fac)));
}
Choose(root, title + " — 진영 고르기", opts, cancel, 2, 0);
return;
case ShopRules.PickGear:
var seen = new List<string>();
foreach (var k in db.Gear.kinds)
{
if (seen.Contains(k.unitType)) continue;
seen.Add(k.unitType);
string ut = k.unitType, ko = k.unitKo;
opts.Add(new KeyValuePair<string, Action>(ko, () => PickGearKind(root, title, key, ut, ko, go, cancel)));
}
Choose(root, title + " — 병종 고르기", opts, cancel, 3, 0);
return;
case ShopRules.PickTreasure:
foreach (var id in ShopRules.PickableTreasures(db, d))
{
var def = db.Treasures.Get(id);
var st = d.inv.Treasure(id);
string tid = id;
opts.Add(new KeyValuePair<string, Action>(def.nameKo + "\n<size=22>★" + st.star + " · 조각 " + st.shards + "</size>", () => go(tid)));
}
if (opts.Count == 0)
{
root.Toast("고를 수 있는 보유 보물이 없습니다");
if (cancel != null) cancel();
return;
}
Choose(root, title + " — 보물 고르기", opts, cancel, 2, 0);
return;
}
go("");
}
static void PickGearKind(OutGameRoot root, string title, string key, string unitType, string unitKo, Action<string> go, Action cancel)
{
var db = root.Session.Db;
string grade = GoodsRules.GearGrade(db, key);
var opts = new List<KeyValuePair<string, Action>>();
foreach (var k in db.Gear.kinds)
{
if (k.unitType != unitType) continue;
var g = db.Gear.Find(unitType, k.kind, grade);
if (g == null) continue;
string pick = unitType + ":" + k.kind;
opts.Add(new KeyValuePair<string, Action>(k.kindKo + " · " + g.nameKo + "\n<size=22>보유 " + root.Session.Save.inv.GearCount(g.id) + "</size>", () => go(pick)));
}
Choose(root, title + " — " + unitKo + " 부위 고르기", opts, cancel, 2, 0);
}
/// <summary>고르는 창 = 칸 격자(cols 열 · 한 쪽 OptPage 칸 · 넘치면 이전 · 다음) + 취소.</summary>
static void Choose(OutGameRoot root, string title, List<KeyValuePair<string, Action>> opts, Action cancel, int cols, int page)
{
LastOptions = opts.Count;
int pages = Mathf.Max(1, (opts.Count + OptPage - 1) / OptPage);
page = Mathf.Clamp(page, 0, pages - 1);
int from = page * OptPage, n = Mathf.Min(OptPage, opts.Count - from);
int lines = Mathf.Max(1, (n + cols - 1) / cols);
var btns = new List<OutGameRoot.ModalButton>();
if (pages > 1) btns.Add(new OutGameRoot.ModalButton("이전", UiKit.Dark, () => Choose(root, title, opts, cancel, cols, (page + pages - 1) % pages)));
btns.Add(new OutGameRoot.ModalButton("취소", UiKit.Dark, cancel));
if (pages > 1) btns.Add(new OutGameRoot.ModalButton("다음", UiKit.Dark, () => Choose(root, title, opts, cancel, cols, (page + 1) % pages)));
var c = root.ModalCustom(title + (pages > 1 ? " (" + (page + 1) + "/" + pages + ")" : ""), Frame + lines * (OptH + OptGap), btns.ToArray());
float bw = (W - (cols - 1) * OptGap) / cols;
for (int i = 0; i < n; i++)
{
var o = opts[from + i];
var b = UiKit.SkinBtn(c, "Opt" + i, "btn_sky", UiKit.White, o.Key, 30, UiKit.White, (i % cols) * (bw + OptGap), (i / cols) * (OptH + OptGap), bw, OptH, () =>
{
root.CloseModal();
o.Value();
});
var t = UiKit.LabelOf(b);
if (t != null) t.supportRichText = true;
}
}
// ---------- 떠돌이 상인(영웅 조각 · 날마다 확률로 등장 · wander_shop · PD 2026-11-24) ----------
/// <summary>오늘의 떠돌이 상인 물품 창(칸 = 영웅 조각 · ★1 · ★2 = 골드 · ★3 = 보옥) — 닫을 때 onClose.</summary>
public static void OpenWander(OutGameRoot root, Action onClose = null)
{
var s = root.Session;
var db = s.Db;
var d = s.Save;
var offers = s.WanderToday();
LastShop = WanderRules.Table;
LastRows = offers.Count;
var c = root.ModalCustom("떠돌이 상인", Frame + HeadH + Mathf.Max(1, offers.Count) * RowH, new OutGameRoot.ModalButton("닫기", UiKit.Dark, onClose));
var head = UiKit.Label(c, "보유 골드 " + d.gold.ToString("N0") + " · 보옥 " + d.gem.ToString("N0"), 36, UiKit.Ink, TextAnchor.MiddleLeft, TL, TL, new Vector2(8f, -6f), new Vector2(W - 16f, 56f));
head.name = "Have";
if (offers.Count == 0)
{
UiKit.Label(c, "오늘은 상인이 들르지 않았습니다 · 내일 다시 찾아보세요", 28, UiKit.Col("text_soft", UiKit.InkSoft), TextAnchor.MiddleCenter, TL, TL, new Vector2(0f, -HeadH - 24f), new Vector2(W, 60f), false).name = "Empty";
return;
}
for (int i = 0; i < offers.Count; i++)
{
var o = offers[i];
var r = WanderRules.Row(o.slot);
var h = db.Heroes.Get(o.heroId);
if (r == null || h == null) continue;
bool gem = r.S("s_currency") == WanderRules.Gem, left = !o.bought;
var hs = d.Hero(h.id);
float y = HeadH + i * RowH;
var row = UiKit.Skin(UiKit.At(c, "Row_" + o.slot, 0f, y, W, RowH - 12f), "panel12", UiKit.Col("row_on_field", UiKit.Dark)).rectTransform;
var name = UiKit.Title(UiKit.Label(row, h.nameKo + " 조각 ×" + r.I("n_amount"), 32, UiKit.White, TextAnchor.MiddleLeft, TL, TL, new Vector2(24f, -12f), new Vector2(470f, 48f)), true);
name.name = "Name";
name.horizontalOverflow = HorizontalWrapMode.Overflow;
string sub = "★" + h.rarity + " · " + (hs != null ? "보유 조각 " + hs.shards : "미보유 · 영입 조각 " + GoodsRules.UnownedShards(d, h.id) + " / " + GoodsRules.UnlockNeed(db, h)) + " · 오늘 1번";
UiKit.Label(row, sub, 24, UiKit.Col("text_soft", UiKit.InkSoft), TextAnchor.MiddleLeft, TL, TL, new Vector2(24f, -62f), new Vector2(520f, 40f), false).name = "Sub";
var price = UiKit.Title(UiKit.Label(row, (gem ? "보옥 " : "골드 ") + r.I("n_price").ToString("N0"), 28, gem ? UiKit.Jade : UiKit.Coin, TextAnchor.MiddleRight, TL, TL, new Vector2(W - 470f, -34f), new Vector2(240f, 48f)), true);
price.name = "Price";
string slot = o.slot;
var b = UiKit.SkinBtn(row, "Buy", left ? "btn_l_yellow" : "btn_s_gray", UiKit.White, left ? "구매" : "완료", 32, left ? UiKit.Col("text_on_yellow", UiKit.Ink) : UiKit.White,
W - 216f, 18f, 196f, RowH - 48f, () => s.WanderBuy(slot, (why, gains) =>
{
root.Toast(why ?? (gains != null && gains.Count > 0 ? GoodsRules.Lines(gains) : "구매 완료"));
OpenWander(root, onClose);
}));
b.interactable = left;
}
}
// ---------- 받을 때 고르는 보상(탑 층 첫 클리어 · SaveData.picks) ----------
/// <summary>
/// 남은 고르는 보상을 하나씩 받는다(없으면 after · 취소 = 남은 것은 보관한 채 after) — 탑 화면 · 보물 > 상자의 「선택 보상」 버튼 · 명장 상자를 연 직후.
/// only = 이 낱말들만(명장 상자에서 방금 나온 비서 등급 · null = 전부) · got = 골라 받은 것을 더해 둘 목록(결과 화면 표시 · null = 안 모음).
/// </summary>
public static void ClaimPicks(OutGameRoot root, Action after, ICollection<string> only = null, List<Gain> got = null)
{
var s = root.Session;
var d = s.Save;
CountEntry next = null;
if (d.picks != null)
foreach (var p in d.picks)
if (p.n > 0 && (only == null || only.Contains(p.id)))
{
next = p;
break;
}
if (next == null)
{
if (after != null) after();
return;
}
string key = next.id;
Pick(root, GoodsRules.NameOf(s.Db, key) + " (남은 " + next.n + ")", GoodsRules.PickKind(s.Db, key) ?? ShopRules.PickNone, key, pick =>
{
var gains = s.ClaimPick(key, pick);
root.Toast(gains.Count > 0 ? GoodsRules.Lines(gains) : "받을 수 없습니다");
if (got != null) GoodsRules.AddAll(got, gains);
if (gains.Count == 0)
{
if (after != null) after(); // 받을 수 없는 줄에서 같은 창이 끝없이 다시 뜨지 않게
return;
}
ClaimPicks(root, after, only, got);
}, after);
}
}
/// <summary>얻은 것(Gain) → 테마 아이콘 id(보상 타일 · 임시 아이콘 포함).</summary>
public static class GoodsUi
{
public static string Icon(OutGameData db, Gain g)
{
switch (g.kind)
{
case "gold": return "cur_gold";
case "gem": return "cur_gem";
case "stamina": return "cur_stamina";
case "iron": return "cur_iron";
case "blueprint": return "cur_blueprint";
case "tome": return "ic_tome";
case "gear": return "cur_gear";
case "chest": return "chest_" + g.key;
case "shard": return "cur_shard";
}
return IconOfKey(db, g.key);
}
/// <summary>보상 낱말 → 아이콘(재화 · 고르는 낱말 포함).</summary>
public static string IconOfKey(OutGameData db, string key)
{
if (key == GoodsRules.Ticket) return "cur_ticket";
if (key == CraftRules.Ore) return "ws_iron_ore"; // 철광석(공방 · 그림은 리소스 제작 뒤 테마에 등록)
if (db != null && db.Awaken != null && key == db.Awaken.StoneKey) return "ic_awaken_stone";
if (GoodsRules.TomeTier(key) > 0 || (db != null && db.Awaken != null && db.Awaken.ItemByKey(key) != null)) return "ic_tome";
if (db != null && GoodsRules.GearGrade(db, key) != null) return "cur_gear";
var chest = GoodsRules.ChestOf(key);
if (chest != null) return "chest_" + chest;
switch (key)
{
case "gold": return "cur_gold";
case "gem": return "cur_gem";
case "stamina": return "cur_stamina";
case "iron": return "cur_iron";
case "blueprint": return "cur_blueprint";
}
return "cur_shard";
}
}
}