using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace TKS
{
///
/// 월드맵 밤 불빛 깜빡임(PD 2026-11-26 「본영에 불 빛이 비춰지는 형태로 자연스럽게」) — 불빛마다 다른 박자의 펄린 잡음으로 세기 · 바깥 번짐 크기를 조금씩 흔든다(모닥불 · 횃불 느낌 · 규칙적 맥동 아님).
/// 시간 = 실시간. 매 프레임 색이 바뀌므로 이 부품이 붙은 층은 자기 Frame 캔버스(WorldMapScreen.BuildLights · P41 — 맵 · 노드 캔버스를 매 프레임 다시 묶지 않게).
/// 모양 값 = 표 worldmap_hard 의 <필터>_light 줄(WorldMapScreen.MapLight).
///
public class MapLightFlicker : MonoBehaviour
{
readonly List outers = new List();
readonly List inners = new List();
readonly List seeds = new List();
WorldMapScreen.MapLight style;
public int Count { get { return outers.Count; } }
public void Style(WorldMapScreen.MapLight s)
{
style = s;
}
public void Clear()
{
outers.Clear();
inners.Clear();
seeds.Clear();
}
public void Add(Image outer, Image inner, float seed)
{
outers.Add(outer);
inners.Add(inner);
seeds.Add(seed);
Apply(outers.Count - 1, Time.unscaledTime); // 첫 프레임(캡처)부터 제 색
}
void Update()
{
if (style == null) return;
float t = Time.unscaledTime;
for (int i = 0; i < outers.Count; i++) Apply(i, t);
}
void Apply(int i, float time)
{
var o = outers[i];
var n = inners[i];
if (o == null || n == null || style == null) return;
float s = seeds[i];
float t = time * style.flickerSpeed;
// 느린 일렁임 + 빠른 떨림(펄린 0.25~0.75 를 0~1 로 펼침)
float raw = Mathf.PerlinNoise(t + s, s * 0.37f) * 0.65f + Mathf.PerlinNoise(t * 2.9f + s * 1.7f, 3.1f) * 0.35f;
float k = Mathf.Lerp(style.flickerMin, style.flickerMax, Mathf.Clamp01((raw - 0.25f) * 2f));
var oc = style.outer;
oc.a *= k;
o.color = oc;
var ic = style.inner;
ic.a *= k;
n.color = ic;
float sc = 1f + style.sway * (k - 0.5f * (style.flickerMin + style.flickerMax)) / Mathf.Max(0.01f, style.flickerMax - style.flickerMin) * 2f;
o.rectTransform.localScale = new Vector3(sc, sc, 1f);
}
}
}