ThreeKingdomsSlingshot/Assets/Scripts/Runtime/Scenario/ScenarioUi.cs

174 lines
8.5 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;
using UnityEngine;
using UnityEngine.UI;
namespace TKS
{
/// <summary>연출이 같이 쓰는 누름·건너뛰기 상태(보스 등장 · 시나리오 이벤트).</summary>
public sealed class TalkState
{
public bool Skip;
public int Taps;
}
/// <summary>
/// 시나리오 연출 공용 UI 조각(보스 등장 BossIntro · 시나리오 이벤트 ScenarioEvent · 2026-11-18 공용화).
/// 말풍선 값 = boss_intro_config.csv bubble_*(PD 확정 말풍선 시안) · 그림 = Resources/Scenario/*.png(없으면 색 면).
/// </summary>
public static class ScenarioUi
{
public static Sprite Spr(string name)
{
return Resources.Load<Sprite>(ScenarioData.Root + name);
}
public static RectTransform Rt(string name, Transform parent, Vector2 aMin, Vector2 aMax, Vector2 pivot, Vector2 pos, Vector2 size)
{
var go = new GameObject(name, typeof(RectTransform));
go.transform.SetParent(parent, false);
var rt = (RectTransform)go.transform;
rt.anchorMin = aMin;
rt.anchorMax = aMax;
rt.pivot = pivot;
rt.anchoredPosition = pos;
rt.sizeDelta = size;
return rt;
}
public static Image Img(string name, Transform parent, Sprite sp, Color col, Vector2 aMin, Vector2 aMax, Vector2 pivot, Vector2 pos, Vector2 size)
{
var img = Rt(name, parent, aMin, aMax, pivot, pos, size).gameObject.AddComponent<Image>();
img.sprite = sp;
img.color = col;
img.raycastTarget = false;
return img;
}
public static Image Full(string name, Transform parent, Sprite sp, Color col)
{
return Img(name, parent, sp, col, Vector2.zero, Vector2.one, new Vector2(0.5f, 0.5f), Vector2.zero, Vector2.zero);
}
public static Text Txt(Transform parent, string s, int size, Color col, TextAnchor anchor, Vector2 pos, Vector2 box, Font font)
{
var t = Rt("Text", parent, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), pos, box).gameObject.AddComponent<Text>();
t.font = font;
t.fontSize = size;
t.color = col;
t.alignment = anchor;
t.text = s;
t.supportRichText = false;
t.raycastTarget = false;
t.horizontalOverflow = HorizontalWrapMode.Wrap;
t.verticalOverflow = VerticalWrapMode.Overflow;
return t;
}
public static void Stroke(Text t, Color col, float d)
{
var o = t.gameObject.AddComponent<Outline>();
o.effectColor = col;
o.effectDistance = new Vector2(d, -d);
var o2 = t.gameObject.AddComponent<Outline>();
o2.effectColor = col;
o2.effectDistance = new Vector2(-d, d);
}
public static float Ease(float k)
{
k = Mathf.Clamp01(k);
return k * k * (3f - 2f * k);
}
/// <summary>유닛 머리 끝(월드) — 그림이면 발(−0.55 R) + 그림 키 · 도형이면 반지름 1.6 배.</summary>
public static Vector2 Head(Unit u)
{
if (u == null) return Vector2.zero;
if (u.View != null && u.View.HasArt) return u.Position + new Vector2(0f, -u.Radius * 0.55f + u.ArtHeight);
return u.Position + new Vector2(0f, u.Radius * 1.6f);
}
/// <summary>월드 좌표 → root 캔버스 안 좌표.</summary>
public static Vector2 ToLocal(RectTransform root, Camera cam, Vector2 world)
{
Vector2 screen = RectTransformUtility.WorldToScreenPoint(cam, world);
Vector2 local;
RectTransformUtility.ScreenPointToLocalPointInRectangle(root, screen, cam, out local);
return local;
}
/// <summary>
/// 말풍선 한 번 — 튀어나옴 → 타자 효과(누르면 전부) → 유지(누르면 닫힘) → 사라짐. head = 꼬리가 가리킬 월드 좌표(매 프레임 따라감 · 화면 밖이면 안쪽으로 밀고 꼬리만 따라감).
/// </summary>
public static IEnumerator Say(RectTransform layer, RectTransform root, Camera cam, Func<Vector2> head, string name, Color tagColor, string text, TalkState st)
{
if (string.IsNullOrEmpty(text) || st.Skip) yield break;
float fs = ScenarioData.Cfg("bubble_font_size");
var bub = Img("Bubble", layer, Spr("bubble"), Color.white, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0f), Vector2.zero, new Vector2(640f, fs * 2f * 1.35f + 80f));
bub.type = Image.Type.Sliced;
var tail = Img("Tail", bub.transform, Spr("bubble_tail"), Color.white, new Vector2(0.5f, 0f), new Vector2(0.5f, 0f), new Vector2(0.5f, 1f), new Vector2(0f, 4f), new Vector2(54f, 40f));
if (!string.IsNullOrEmpty(name))
{
var tag = Img("NameTag", bub.transform, Spr("name_tag"), tagColor, new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(0f, 0.5f), new Vector2(24f, 0f), new Vector2(0f, 52f));
tag.type = Image.Type.Sliced;
var tagText = Txt(tag.transform, name, 30, Color.white, TextAnchor.MiddleCenter, Vector2.zero, new Vector2(200f, 52f), UISkin.TitleFont);
tag.rectTransform.sizeDelta = new Vector2(tagText.preferredWidth + 44f, 52f);
tagText.rectTransform.sizeDelta = tag.rectTransform.sizeDelta;
}
var body = Txt(bub.transform, "", Mathf.RoundToInt(fs), new Color(0.24f, 0.13f, 0.07f), TextAnchor.MiddleCenter, new Vector2(0f, -4f), new Vector2(580f, fs * 2f * 1.35f + 20f), UISkin.TitleFont);
float popT = ScenarioData.Cfg("bubble_pop_sec"), from = ScenarioData.Cfg("bubble_pop_from"), peak = ScenarioData.Cfg("bubble_pop_peak");
st.Taps = 0;
for (float t = 0f; t < popT && !st.Skip; t += Time.unscaledDeltaTime)
{
float k = t / popT;
bub.rectTransform.localScale = Vector3.one * (k < 0.7f ? Mathf.Lerp(from, peak, k / 0.7f) : Mathf.Lerp(peak, 1f, (k - 0.7f) / 0.3f));
Place(root, cam, bub.rectTransform, tail.rectTransform, head());
yield return null;
}
bub.rectTransform.localScale = Vector3.one;
float cps = Mathf.Max(1f, ScenarioData.Cfg("bubble_char_per_sec"));
float shown = 0f;
while (shown < text.Length && !st.Skip && st.Taps == 0)
{
shown += cps * Time.unscaledDeltaTime;
body.text = text.Substring(0, Mathf.Min(text.Length, Mathf.FloorToInt(shown)));
Place(root, cam, bub.rectTransform, tail.rectTransform, head());
yield return null;
}
body.text = text;
if (st.Taps > 0) st.Taps = 0; // 탭 1 = 전부 표시 · 다음 탭 = 닫기
float hold = ScenarioData.Cfg("bubble_hold_sec");
for (float t = 0f; t < hold && !st.Skip && st.Taps == 0; t += Time.unscaledDeltaTime)
{
Place(root, cam, bub.rectTransform, tail.rectTransform, head());
yield return null;
}
st.Taps = 0;
var cg = bub.gameObject.AddComponent<CanvasGroup>();
float fade = ScenarioData.Cfg("bubble_fade_sec");
for (float t = 0f; t < fade && !st.Skip; t += Time.unscaledDeltaTime)
{
cg.alpha = 1f - t / fade;
yield return null;
}
UnityEngine.Object.Destroy(bub.gameObject);
}
/// <summary>말풍선 = 머리 위 · 화면 밖이면 안쪽으로 밀어 넣고 꼬리만 머리를 따라감.</summary>
static void Place(RectTransform root, Camera cam, RectTransform bub, RectTransform tail, Vector2 head)
{
var local = ToLocal(root, cam, head);
var half = root.rect.size * 0.5f;
var size = bub.sizeDelta;
float x = Mathf.Clamp(local.x, -half.x + size.x * 0.5f + 20f, half.x - size.x * 0.5f - 20f);
float y = Mathf.Clamp(local.y + 40f, -half.y + 40f, half.y - size.y - 40f);
var p = new Vector2(x, y);
if (bub.anchoredPosition != p) bub.anchoredPosition = p;
var tp = new Vector2(Mathf.Clamp(local.x - x, -size.x * 0.5f + 40f, size.x * 0.5f - 40f), 4f);
if (tail.anchoredPosition != tp) tail.anchoredPosition = tp;
}
}
}