407 lines
20 KiB
C#
407 lines
20 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace TKS
|
||
{
|
||
/// <summary>
|
||
/// 선택(터치)한 유닛의 사거리 표시 (PD 2026-11-04).
|
||
/// · 모든 병종: 슬링샷 최대 거리 = 얇은 반투명 원 (아군·적군 모두)
|
||
/// · 궁병·책사: 추가로 슬링샷 범위 ~ 슬링샷 범위 × (1 + archerRangeRatio) 까지를 채워진 반투명 붉은 도넛으로 (실제 공격 가능 범위)
|
||
/// — 그 범위 안에 적이 하나도 없으면 회색.
|
||
/// · 액티브 스킬이 자신 주위 반경에 미치는 유닛: 스킬 범위 = 금색 얇은 원(PD 2026-11-11 「캐릭터를 선택할 때 스킬 범위도 함께 보이도록」 · 반경 = SkillSystem.ActiveRadius) —
|
||
/// 슬링샷 원과 반경이 거의 같으면(관우 = 행동 반경) 선이 겹쳐 하나로 보이므로 바깥으로 조금 띄워 두 줄로 보이게 한다.
|
||
/// </summary>
|
||
public class RangeIndicator : MonoBehaviour
|
||
{
|
||
public static readonly Color RingColor = new Color(1f, 1f, 1f, 0.42f);
|
||
public static readonly Color DonutColor = new Color(0.95f, 0.25f, 0.2f, 0.3f);
|
||
public static readonly Color DonutIdleColor = new Color(0.6f, 0.6f, 0.6f, 0.18f);
|
||
/// <summary>스킬 범위 원 색(스킬 이름 표시와 같은 금색 계열).</summary>
|
||
public static readonly Color SkillRingColor = new Color(1f, 0.8f, 0.25f, 0.75f);
|
||
/// <summary>슬링샷 원과 겹칠 때 스킬 원을 바깥으로 띄우는 거리(월드).</summary>
|
||
const float SkillRingGap = 0.09f;
|
||
|
||
SpriteRenderer ring;
|
||
SpriteRenderer donut;
|
||
SpriteRenderer skillRing;
|
||
Unit target;
|
||
float ratio;
|
||
|
||
public Unit Target
|
||
{
|
||
get { return target; }
|
||
}
|
||
|
||
/// <summary>사거리 원(예전 그림 고리 또는 벡터 선)이 보이는가.</summary>
|
||
public bool RingVisible
|
||
{
|
||
get { return (ring != null && ring.gameObject.activeSelf) || VectorVisible; }
|
||
}
|
||
|
||
public bool SkillRingVisible
|
||
{
|
||
get { return (skillRing != null && skillRing.gameObject.activeSelf) || (skillVec != null && skillVec.gameObject.activeSelf); }
|
||
}
|
||
|
||
/// <summary>스킬 범위 원의 지름(월드) = 스킬 반경 × 2(슬링샷 원과 겹치면 조금 큼).</summary>
|
||
public float SkillRingDiameter
|
||
{
|
||
get { return skillVec != null && skillVec.gameObject.activeSelf ? skillVec.Radius * 2f : skillRing != null ? skillRing.transform.localScale.x : 0f; }
|
||
}
|
||
|
||
public bool DonutVisible
|
||
{
|
||
get { return donut != null && donut.gameObject.activeSelf; }
|
||
}
|
||
|
||
/// <summary>얇은 원의 지름(월드) = 슬링샷 최대 거리 × 2.</summary>
|
||
public float RingDiameter
|
||
{
|
||
get { return VectorVisible ? radius * 2f : ring != null ? ring.transform.localScale.x : 0f; }
|
||
}
|
||
|
||
/// <summary>도넛 바깥 지름(월드) = 슬링샷 최대 거리 × (1 + 비율) × 2.</summary>
|
||
public float DonutDiameter
|
||
{
|
||
get { return donut != null ? donut.transform.localScale.x : 0f; }
|
||
}
|
||
|
||
public void Init(float archerRangeRatio)
|
||
{
|
||
ratio = Mathf.Max(0f, archerRangeRatio);
|
||
ring = ProceduralSprites.NewSprite(transform, "SlingRange", ProceduralSprites.ThinRing, RingColor, 1f, -9);
|
||
// 도넛 안쪽 지름 = 바깥 지름 ÷ (1 + 비율) → 안쪽 테두리가 슬링샷 범위 원과 일치
|
||
donut = ProceduralSprites.NewSprite(transform, "ArcherReach", ProceduralSprites.Donut(1f / (1f + ratio)), DonutColor, 1f, -10);
|
||
skillRing = ProceduralSprites.NewSprite(transform, "SkillRange", ProceduralSprites.ThinRing, SkillRingColor, 1f, -9);
|
||
ring.gameObject.SetActive(false);
|
||
donut.gameObject.SetActive(false);
|
||
skillRing.gameObject.SetActive(false);
|
||
BuildVector();
|
||
}
|
||
|
||
// ---------- 벡터 사거리 원(PD 2026-11-23 「해상도가 떨어지는 굵은 베이지색 원형 선 → 개선 · 사정거리 안에 공격 가능한 적이 있을 때 변화」) ----------
|
||
// 선 = LineRenderer(화면 두께가 늘 같음 · 기준 1080×1920 의 px · 크게 늘려도 흐려지지 않음) · 모양 = 표 ib_ui range_style
|
||
// 1 = 또렷한 점선 고리 + 아주 옅은 바탕 · 2 = 실선 + 가장자리가 진한 은은한 원판 · 3 = 실선 + 안쪽 눈금 + 공격 가능한 적 방향 꺾쇠
|
||
// 공격 가능 = SlingshotInput.ReachableAtMax(사정거리 안 · 실제 행동과 같은 경로 질의) 가 1 이상 → 선 · 원판 = 공격 색(붉게) + 숨쉬기
|
||
const int Segs = 160;
|
||
LineRenderer line;
|
||
LineRenderer under;
|
||
Material lineMat;
|
||
SpriteRenderer disc;
|
||
readonly List<SpriteRenderer> ticks = new List<SpriteRenderer>();
|
||
readonly List<SpriteRenderer> chevrons = new List<SpriteRenderer>();
|
||
readonly List<Unit> attackTargets = new List<Unit>();
|
||
int style;
|
||
float radius;
|
||
bool attackable;
|
||
|
||
/// <summary>캡처 · 시험용 모양 강제(-1 = 표 range_style).</summary>
|
||
public static int StyleOverride = -1;
|
||
|
||
/// <summary>캡처용: 스킬 반경 원을 숨김(스킬 범위 표시 대안 목업의 바탕 화면).</summary>
|
||
public static bool HideSkillForCapture;
|
||
|
||
/// <summary>지금 모양(0 = 예전 그림 고리).</summary>
|
||
public int Style { get { return style; } }
|
||
|
||
/// <summary>사정거리 안에 공격 가능한 적이 있어 공격 색으로 그리는 중인가.</summary>
|
||
public bool Attackable { get { return attackable; } }
|
||
|
||
public bool VectorVisible { get { return line != null && line.gameObject.activeSelf; } }
|
||
|
||
void BuildVector()
|
||
{
|
||
var go = new GameObject("RangeLine");
|
||
go.transform.SetParent(transform, false);
|
||
line = go.AddComponent<LineRenderer>();
|
||
line.useWorldSpace = false;
|
||
line.loop = true;
|
||
line.positionCount = Segs;
|
||
line.numCapVertices = 0;
|
||
line.numCornerVertices = 0;
|
||
line.alignment = LineAlignment.View;
|
||
lineMat = new Material(Shader.Find("Sprites/Default"));
|
||
line.sharedMaterial = lineMat;
|
||
line.sortingOrder = -8;
|
||
// 받침 선(짙은 고동 반투명 · 선보다 조금 두껍게 · 밝은 모래 위에서도 원이 보이게 · QA M5)
|
||
var ug = new GameObject("RangeLineUnder");
|
||
ug.transform.SetParent(go.transform, false);
|
||
under = ug.AddComponent<LineRenderer>();
|
||
under.useWorldSpace = false;
|
||
under.loop = true;
|
||
under.positionCount = Segs;
|
||
under.numCapVertices = 0;
|
||
under.numCornerVertices = 0;
|
||
under.alignment = LineAlignment.View;
|
||
under.sharedMaterial = lineMat;
|
||
under.sortingOrder = -9;
|
||
disc = ProceduralSprites.NewSprite(transform, "RangeDisc", ProceduralSprites.SoftDisc, Color.white, 1f, -11);
|
||
go.SetActive(false);
|
||
disc.gameObject.SetActive(false);
|
||
skillVec = VectorRing.Create(transform, "SkillRangeVec", -7);
|
||
skillVec.gameObject.SetActive(false);
|
||
maxVec = VectorRing.Create(transform, "SlingMaxVec", -10);
|
||
maxVec.gameObject.SetActive(false);
|
||
}
|
||
|
||
// ---------- 파워에 따른 원(PD 2026-11-24 「슬링샷 파워(화살표 길이)에 따라 원형 반경이 조절 · 내 파워가 정확히 어느 위치까지 갈지」) ----------
|
||
// 조준 중 = 사거리 원 · 원판이 지금 화살표 길이(나갈 거리)로 줄고 늘어남 · 최대 거리 = 옅은 가는 선으로 남김 · 조준을 거두면 최대 거리로 돌아감 · 표 ib_ui range_power_*
|
||
VectorRing maxVec;
|
||
float maxRadius;
|
||
bool powerOn;
|
||
|
||
/// <summary>조준 중인가(원 = 지금 파워 거리).</summary>
|
||
public bool PowerShown { get { return powerOn; } }
|
||
|
||
/// <summary>지금 원 반지름(월드 · 조준 중 = 파워 거리 · 아니면 최대 거리).</summary>
|
||
public float Radius { get { return radius; } }
|
||
|
||
/// <summary>조준 중: 원 반지름 = r(지금 화살표 길이 · 월드).</summary>
|
||
public void SetPower(float r)
|
||
{
|
||
if (target == null || !VectorVisible || IbUi.Num("range_power_follow", 1f) < 0.5f) return;
|
||
if (!powerOn)
|
||
{
|
||
powerOn = true;
|
||
maxRadius = radius;
|
||
if (maxVec != null && IbUi.Num("range_power_max_px", 2f) > 0f)
|
||
{
|
||
maxVec.Indicator = false;
|
||
maxVec.LineColor = IbUi.Col("range_power_max_color", new Color(1f, 0.95f, 0.84f, 0.45f));
|
||
maxVec.UnderColor = new Color(0f, 0f, 0f, 0f);
|
||
maxVec.LinePx = IbUi.Num("range_power_max_px", 2f);
|
||
maxVec.UnderPx = 0f;
|
||
maxVec.DiscAlpha = 0f;
|
||
maxVec.gameObject.SetActive(true);
|
||
maxVec.SetRadius(maxRadius);
|
||
}
|
||
}
|
||
ApplyRadius(Mathf.Clamp(r, 0.05f, Mathf.Max(maxRadius, r)));
|
||
}
|
||
|
||
/// <summary>조준을 거둠: 원 = 다시 최대 거리.</summary>
|
||
public void ClearPower()
|
||
{
|
||
if (!powerOn) return;
|
||
powerOn = false;
|
||
if (maxVec != null) maxVec.gameObject.SetActive(false);
|
||
ApplyRadius(maxRadius);
|
||
}
|
||
|
||
void ApplyRadius(float r)
|
||
{
|
||
radius = r;
|
||
for (int i = 0; i < Segs; i++)
|
||
{
|
||
float a = i * Mathf.PI * 2f / Segs;
|
||
var p = new Vector3(Mathf.Cos(a) * r, Mathf.Sin(a) * r, 0f);
|
||
line.SetPosition(i, p);
|
||
under.SetPosition(i, p);
|
||
}
|
||
disc.transform.localScale = new Vector3(r * 2f, r * 2f, 1f);
|
||
UpdateVector();
|
||
}
|
||
|
||
VectorRing skillVec;
|
||
|
||
SpriteRenderer Pooled(List<SpriteRenderer> pool, int i, string name, Sprite sp, int order)
|
||
{
|
||
while (pool.Count <= i) pool.Add(ProceduralSprites.NewSprite(transform, name + pool.Count, sp, Color.white, 1f, order));
|
||
return pool[i];
|
||
}
|
||
|
||
void ShowVector(float r)
|
||
{
|
||
style = StyleOverride >= 0 ? StyleOverride : Mathf.RoundToInt(IbUi.Num("range_style", 1f));
|
||
radius = r;
|
||
powerOn = false;
|
||
if (maxVec != null) maxVec.gameObject.SetActive(false);
|
||
bool on = style > 0 && line != null;
|
||
if (ring != null) ring.gameObject.SetActive(!on);
|
||
if (line == null) return;
|
||
line.gameObject.SetActive(on);
|
||
disc.gameObject.SetActive(on && (style == 1 || style == 2));
|
||
foreach (var t in ticks) t.gameObject.SetActive(false);
|
||
foreach (var c in chevrons) c.gameObject.SetActive(false);
|
||
if (!on) return;
|
||
for (int i = 0; i < Segs; i++)
|
||
{
|
||
float a = i * Mathf.PI * 2f / Segs;
|
||
line.SetPosition(i, new Vector3(Mathf.Cos(a) * r, Mathf.Sin(a) * r, 0f));
|
||
under.SetPosition(i, new Vector3(Mathf.Cos(a) * r, Mathf.Sin(a) * r, 0f));
|
||
}
|
||
bool dashed = style == 1;
|
||
line.textureMode = dashed ? LineTextureMode.Tile : LineTextureMode.Stretch;
|
||
under.textureMode = line.textureMode;
|
||
lineMat.mainTexture = dashed ? ProceduralSprites.DashTexture(IbUi.Num("range_dash_fill", 0.6f)) : null;
|
||
disc.transform.localScale = new Vector3(r * 2f, r * 2f, 1f);
|
||
if (style == 3)
|
||
{
|
||
int n = Mathf.Max(0, Mathf.RoundToInt(IbUi.Num("range_tick_count", 36f)));
|
||
for (int i = 0; i < n; i++) Pooled(ticks, i, "Tick", ProceduralSprites.Square, -8).gameObject.SetActive(true);
|
||
}
|
||
UpdateVector();
|
||
}
|
||
|
||
/// <summary>사정거리 안 공격 가능한 적(없으면 빈 목록) — 원 색 · 꺾쇠(모양 3)를 바꾼다.</summary>
|
||
public void SetAttackable(List<Unit> targets)
|
||
{
|
||
attackTargets.Clear();
|
||
if (targets != null) attackTargets.AddRange(targets);
|
||
attackable = attackTargets.Count > 0;
|
||
foreach (var c in chevrons) c.gameObject.SetActive(false);
|
||
if (style == 3 && VectorVisible)
|
||
for (int i = 0; i < attackTargets.Count; i++) Pooled(chevrons, i, "Chevron", ProceduralSprites.Chevron, -7).gameObject.SetActive(true);
|
||
UpdateVector();
|
||
}
|
||
|
||
void UpdateVector()
|
||
{
|
||
if (!VectorVisible) return;
|
||
var cam = Camera.main;
|
||
float wpp = cam != null && cam.orthographic ? cam.orthographicSize * 2f / 1920f : 0.01f; // 기준 화면(1080×1920) 1 px 의 월드 길이 = UI 와 같이 화면 크기에 맞춰 커짐
|
||
float px = IbUi.Num("range_line_px", style == 1 ? 5f : 4f);
|
||
line.widthMultiplier = px * wpp;
|
||
if (style == 1) lineMat.mainTextureScale = new Vector2(1f / Mathf.Max(0.001f, IbUi.Num("range_dash_px", 22f) * wpp), 1f);
|
||
Color baseC = attackable ? IbUi.Col("range_color_attack", new Color(1f, 0.35f, 0.24f, 0.95f)) : IbUi.Col("range_color", new Color(1f, 0.95f, 0.84f, 0.9f));
|
||
float pulse = 1f;
|
||
if (attackable)
|
||
{
|
||
float period = Mathf.Max(0.1f, IbUi.Num("range_pulse_sec", 1.2f));
|
||
pulse = Mathf.Lerp(IbUi.Num("range_pulse_min", 0.6f), 1f, 0.5f + 0.5f * Mathf.Sin(Time.unscaledTime * Mathf.PI * 2f / period));
|
||
}
|
||
var lc = new Color(baseC.r, baseC.g, baseC.b, baseC.a * pulse);
|
||
line.startColor = line.endColor = lc;
|
||
under.widthMultiplier = (px + IbUi.Num("range_under_px", 2f)) * wpp;
|
||
var uc = IbUi.Col("range_under_color", new Color(0.23f, 0.14f, 0.08f, 0.35f));
|
||
under.startColor = under.endColor = new Color(uc.r, uc.g, uc.b, uc.a * pulse);
|
||
float fa = style == 2 ? IbUi.Num("range_disc_alpha", 0.22f) : IbUi.Num("range_fill_alpha", 0.08f);
|
||
if (disc != null) disc.color = new Color(baseC.r, baseC.g, baseC.b, fa * (attackable ? Mathf.Lerp(1f, 1.4f, pulse - 0.6f) : 1f));
|
||
if (style == 3)
|
||
{
|
||
float tl = IbUi.Num("range_tick_px", 14f) * wpp;
|
||
int n = 0;
|
||
foreach (var t in ticks)
|
||
{
|
||
if (!t.gameObject.activeSelf) continue;
|
||
n++;
|
||
}
|
||
for (int i = 0; i < n; i++)
|
||
{
|
||
float a = i * 360f / n;
|
||
var t = ticks[i];
|
||
Vector2 d = new Vector2(Mathf.Cos(a * Mathf.Deg2Rad), Mathf.Sin(a * Mathf.Deg2Rad));
|
||
t.transform.localPosition = d * (radius - tl * 0.5f);
|
||
t.transform.localRotation = Quaternion.Euler(0f, 0f, a);
|
||
t.transform.localScale = new Vector3(tl, px * 0.75f * wpp, 1f);
|
||
t.color = new Color(lc.r, lc.g, lc.b, lc.a * 0.8f);
|
||
}
|
||
float cs = IbUi.Num("range_chevron_px", 36f) * wpp;
|
||
for (int i = 0; i < attackTargets.Count && i < chevrons.Count; i++)
|
||
{
|
||
var e = attackTargets[i];
|
||
var c = chevrons[i];
|
||
if (e == null || target == null) continue;
|
||
Vector2 d = e.Position - target.Position;
|
||
if (d.sqrMagnitude < 1e-6f) continue;
|
||
d.Normalize();
|
||
c.transform.localPosition = d * (radius + cs * 0.3f);
|
||
c.transform.localRotation = Quaternion.Euler(0f, 0f, Mathf.Atan2(d.y, d.x) * Mathf.Rad2Deg - 90f);
|
||
c.transform.localScale = new Vector3(cs, cs, 1f);
|
||
c.color = new Color(lc.r, lc.g, lc.b, Mathf.Max(0.85f, lc.a));
|
||
}
|
||
}
|
||
}
|
||
|
||
void HideVector()
|
||
{
|
||
powerOn = false;
|
||
if (maxVec != null) maxVec.gameObject.SetActive(false);
|
||
if (line != null) line.gameObject.SetActive(false);
|
||
if (disc != null) disc.gameObject.SetActive(false);
|
||
foreach (var t in ticks) t.gameObject.SetActive(false);
|
||
foreach (var c in chevrons) c.gameObject.SetActive(false);
|
||
attackable = false;
|
||
attackTargets.Clear();
|
||
}
|
||
|
||
public void Show(Unit u)
|
||
{
|
||
target = u;
|
||
if (u == null)
|
||
{
|
||
Hide();
|
||
return;
|
||
}
|
||
float r = SkillMods.SlingMax(u); // 스킬(동남풍·적토마) 반영 · 없으면 슬링샷 최대 거리
|
||
ring.transform.localScale = new Vector3(r * 2f, r * 2f, 1f);
|
||
ring.gameObject.SetActive(true);
|
||
|
||
bool archer = UnitTypeInfo.IsRanged(u.Type); // 궁병·책사(#1087 ⑤ 같은 사거리 규칙)
|
||
if (archer)
|
||
{
|
||
float ro = SkillMods.Range(u); // 사거리 = 이동 범위 × (1 + 비율) = 도넛 바깥(안쪽 테두리 = 이동 범위 원 · 2026-11-16) · // 사거리 스킬(단기·대도독) 반영 · 없으면 = 슬링 × (1 + 비율)
|
||
donut.transform.localScale = new Vector3(ro * 2f, ro * 2f, 1f);
|
||
donut.color = DonutColor;
|
||
}
|
||
bool reach = archer && ratio > 0.001f; // 추가 사거리 비율이 0 이면 붉은 도넛 없음(PD 2026-11-21 · 공격 사거리 = 이동 범위 원 하나)
|
||
if (donut.gameObject.activeSelf != reach) donut.gameObject.SetActive(reach);
|
||
|
||
float rs = SkillSystem.Current != null ? SkillSystem.Current.ActiveRadius(u) : 0f;
|
||
bool hasSkill = rs > 0.01f && !HideSkillForCapture && SkillIndicatorTable.On("radius", true); // 표 skill_indicator.csv radius 줄(원형 · PD 2026-11-24)
|
||
if (hasSkill)
|
||
{
|
||
if (Mathf.Abs(rs - r) < SkillRingGap) rs = r + SkillRingGap; // 두 원이 겹치면 한 줄로 보임 → 스킬 원을 바깥으로
|
||
skillRing.transform.localScale = new Vector3(rs * 2f, rs * 2f, 1f);
|
||
}
|
||
if (skillRing.gameObject.activeSelf != hasSkill) skillRing.gameObject.SetActive(hasSkill);
|
||
// 스킬 반경 = 벡터 금 실선 + 은은한 금 원판(PD 2026-11-23 「스킬 시전 범위도 수정」 · 사거리 원과 같은 방식 · 표 skill_range_*)
|
||
bool vec = skillVec != null && (StyleOverride >= 0 ? StyleOverride : Mathf.RoundToInt(IbUi.Num("range_style", 1f))) > 0;
|
||
if (vec && hasSkill)
|
||
{
|
||
skillRing.gameObject.SetActive(false);
|
||
skillVec.UseSkillStyle(IbUi.Num("skill_range_disc_alpha", 0.1f)); // 공격 인디케이터(PD 2026-11-24 예시) 또는 실선 + 원판
|
||
skillVec.SetRadius(rs);
|
||
}
|
||
if (skillVec != null && skillVec.gameObject.activeSelf != (vec && hasSkill)) skillVec.gameObject.SetActive(vec && hasSkill);
|
||
HideVector();
|
||
ShowVector(r); // 표 range_style ≥ 1 = 벡터 선(예전 그림 고리는 숨김)
|
||
Follow();
|
||
}
|
||
|
||
public void Hide()
|
||
{
|
||
target = null;
|
||
if (ring != null) ring.gameObject.SetActive(false);
|
||
if (donut != null) donut.gameObject.SetActive(false);
|
||
if (skillRing != null) skillRing.gameObject.SetActive(false);
|
||
if (skillVec != null) skillVec.gameObject.SetActive(false);
|
||
HideVector();
|
||
}
|
||
|
||
/// <summary>궁병 도넛 색: 공격 가능 범위 안에 적이 있으면 붉게, 없으면 회색.</summary>
|
||
public void SetArcherReachable(bool any)
|
||
{
|
||
if (donut != null) donut.color = any ? DonutColor : DonutIdleColor;
|
||
}
|
||
|
||
void LateUpdate()
|
||
{
|
||
if (target == null) return;
|
||
if (!target.IsAlive)
|
||
{
|
||
Hide();
|
||
return;
|
||
}
|
||
Follow();
|
||
UpdateVector(); // 카메라 확대 · 숨쉬기 · 꺾쇠 방향
|
||
}
|
||
|
||
void Follow()
|
||
{
|
||
Vector2 p = target.Position;
|
||
transform.position = new Vector3(p.x, p.y, 0f);
|
||
}
|
||
}
|
||
}
|