ThreeKingdomsSlingshot/Assets/Scripts/Runtime/DangerZone.cs

146 lines
6.8 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
{
/// <summary>
/// 적 위험 범위(PD 2026-11-24 「적의 사정거리 범위가 어디인지 알 수 있는 옵션 · On / Off · 턴제 SRPG 처럼 붉은색 영역으로 위험 범위」) —
/// 다음 적 턴에 적의 공격이 닿을 수 있는 곳 = 적마다 원의 합집합을 붉은 반투명 면 + 바깥 테두리 선으로 전장 위(유닛 아래)에 깐다. 아군 차례에만 보인다.
/// 원 반지름: 근접 · 기병 = 슬링 최대 거리(SkillMods.SlingMax · 스킬 반영) + 적 몸 반지름 + 아군 몸 반지름 · 궁병 · 책사 = 사거리(SkillMods.Range) + 아군 몸 반지름(쏘거나 옮기거나 한 번).
/// 빼는 적 = 다음 차례를 쉬는 적(기절 · 행동 불가) · 공격력 0(허수아비) · 연출이 숨긴 적 · NPC. 벽 · 장애물로 막히는 길은 계산하지 않는다(원 = 닿을 수 있는 최대 범위).
/// 켬/끔 = HUD 「위험 범위」 단추(BattleHUD.TryDangerTap) · 기억 = PlayerPrefs · 값 = 표 ib_ui danger_*(C45).
/// </summary>
public class DangerZone : MonoBehaviour
{
const string Pref = "tks_danger_zone";
const int Order = -13; // 배경(−20) · 구름(−15) 위 · 사거리 원판(−11) · 선(−9 · −8) · 유닛 아래
public static bool On
{
get { return PlayerPrefs.GetInt(Pref, 0) == 1; }
set { PlayerPrefs.SetInt(Pref, value ? 1 : 0); }
}
BattleManager battle;
SpriteRenderer sr;
Texture2D tex;
string sig = "";
/// <summary>지금 보이는가(시험 · 캡처).</summary>
public bool Shown { get { return sr != null && sr.enabled; } }
/// <summary>지금 그린 원 수(시험 · 캡처).</summary>
public int Circles { get; private set; }
public static DangerZone Create(BattleManager b)
{
if (b == null || b.Board == null) return null;
var go = new GameObject("DangerZone");
go.transform.SetParent(b.Board.transform, false);
var z = go.AddComponent<DangerZone>();
z.battle = b;
z.sr = go.AddComponent<SpriteRenderer>();
z.sr.sortingOrder = Order;
z.sr.enabled = false;
return z;
}
/// <summary>적 하나의 위험 원 반지름(월드 · 원의 중심 = 적 위치).</summary>
public static float Reach(Unit e, float allyRadius)
{
if (UnitTypeInfo.IsRanged(e.Type)) return Mathf.Max(0f, SkillMods.Range(e)) + allyRadius;
return Mathf.Max(0f, SkillMods.SlingMax(e)) + e.Radius + allyRadius;
}
static bool Counts(Unit e)
{
return e != null && e.IsAlive && !e.Npc && e.Atk > 0 && !ScenarioEvent.Absent(e) && !SkillStatus.Stunned(e);
}
void LateUpdate()
{
bool show = On && battle != null && battle.Board != null && battle.Phase == BattlePhase.PlayerTurn;
if (!show)
{
if (sr.enabled) sr.enabled = false;
return;
}
float allyR = 0f;
foreach (var a in battle.AliveUnits(Faction.Player)) allyR = Mathf.Max(allyR, a.Radius);
// 바뀔 때만 다시 굽는다(적 위치 · 반지름 · 수)
var sb = new System.Text.StringBuilder();
foreach (var e in battle.AliveUnits(Faction.Enemy))
{
if (!Counts(e)) continue;
sb.Append(Mathf.RoundToInt(e.Position.x * 20f)).Append(',').Append(Mathf.RoundToInt(e.Position.y * 20f)).Append(',')
.Append(Mathf.RoundToInt(Reach(e, allyR) * 20f)).Append(';');
}
string s = sb.ToString();
if (s != sig)
{
sig = s;
Rebuild(allyR);
}
if (!sr.enabled) sr.enabled = Circles > 0;
}
void Rebuild(float allyR)
{
var rect = battle.Board.Rect;
float ppu = Mathf.Clamp(IbUi.Num("danger_ppu", 12f), 4f, 32f);
int w = Mathf.Max(4, Mathf.CeilToInt(rect.width * ppu)), h = Mathf.Max(4, Mathf.CeilToInt(rect.height * ppu));
var cs = new List<Vector3>();
foreach (var e in battle.AliveUnits(Faction.Enemy))
if (Counts(e)) cs.Add(new Vector3(e.Position.x, e.Position.y, Reach(e, allyR)));
Circles = cs.Count;
if (tex == null || tex.width != w || tex.height != h)
{
if (tex != null) Destroy(tex);
tex = new Texture2D(w, h, TextureFormat.RGBA32, false) { filterMode = FilterMode.Bilinear, wrapMode = TextureWrapMode.Clamp };
}
Color fill = IbUi.Col("danger_fill", new Color(0.9f, 0.12f, 0.1f, 0.22f));
Color edge = IbUi.Col("danger_edge", new Color(0.95f, 0.15f, 0.12f, 0.9f));
float edgeW = Mathf.Max(0.01f, IbUi.Num("danger_edge_w", 0.12f));
float aa = 1f / ppu; // 가장자리 부드럽게(1 텍셀)
var px = new Color32[w * h];
for (int y = 0; y < h; y++)
{
float wy = rect.yMin + (y + 0.5f) / ppu;
for (int x = 0; x < w; x++)
{
float wx = rect.xMin + (x + 0.5f) / ppu;
float d = float.NegativeInfinity; // 합집합 안쪽 깊이(+ = 안)
for (int i = 0; i < cs.Count; i++)
{
float dx = wx - cs[i].x, dy = wy - cs[i].y;
float v = cs[i].z - Mathf.Sqrt(dx * dx + dy * dy);
if (v > d) d = v;
}
Color c;
if (d <= -aa) c = new Color(0f, 0f, 0f, 0f);
else
{
float inside = Mathf.Clamp01((d + aa) / (2f * aa)); // 바깥 → 안 경계 부드럽게
float ring = Mathf.Clamp01((edgeW - d) / aa); // 경계에서 edgeW 안쪽까지 = 테두리
c = Color.Lerp(fill, edge, ring);
c.a *= inside;
}
px[y * w + x] = c;
}
}
tex.SetPixels32(px);
tex.Apply(false);
if (sr.sprite != null) Destroy(sr.sprite);
sr.sprite = Sprite.Create(tex, new Rect(0, 0, w, h), new Vector2(0.5f, 0.5f), ppu);
transform.localPosition = new Vector3(rect.center.x, rect.center.y, 0f);
transform.localScale = new Vector3(rect.width / (w / ppu), rect.height / (h / ppu), 1f);
}
void OnDestroy()
{
if (sr != null && sr.sprite != null) Destroy(sr.sprite);
if (tex != null) Destroy(tex);
}
}
}