using UnityEngine;
namespace TKS
{
///
/// 선택(터치)한 유닛의 사거리 표시 (PD 2026-11-04).
/// · 모든 병종: 슬링샷 최대 거리 = 얇은 반투명 원 (아군·적군 모두)
/// · 궁병·책사: 추가로 슬링샷 범위 ~ 슬링샷 범위 × (1 + archerRangeRatio) 까지를 채워진 반투명 붉은 도넛으로 (실제 공격 가능 범위)
/// — 그 범위 안에 적이 하나도 없으면 회색.
/// · 액티브 스킬이 자신 주위 반경에 미치는 유닛: 스킬 범위 = 금색 얇은 원(PD 2026-11-11 「캐릭터를 선택할 때 스킬 범위도 함께 보이도록」 · 반경 = SkillSystem.ActiveRadius) —
/// 슬링샷 원과 반경이 거의 같으면(관우 = 행동 반경) 선이 겹쳐 하나로 보이므로 바깥으로 조금 띄워 두 줄로 보이게 한다.
///
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);
/// 스킬 범위 원 색(스킬 이름 표시와 같은 금색 계열).
public static readonly Color SkillRingColor = new Color(1f, 0.8f, 0.25f, 0.75f);
/// 슬링샷 원과 겹칠 때 스킬 원을 바깥으로 띄우는 거리(월드).
const float SkillRingGap = 0.09f;
SpriteRenderer ring;
SpriteRenderer donut;
SpriteRenderer skillRing;
Unit target;
float ratio;
public Unit Target
{
get { return target; }
}
public bool RingVisible
{
get { return ring != null && ring.gameObject.activeSelf; }
}
public bool SkillRingVisible
{
get { return skillRing != null && skillRing.gameObject.activeSelf; }
}
/// 스킬 범위 원의 지름(월드) = 스킬 반경 × 2(슬링샷 원과 겹치면 조금 큼).
public float SkillRingDiameter
{
get { return skillRing != null ? skillRing.transform.localScale.x : 0f; }
}
public bool DonutVisible
{
get { return donut != null && donut.gameObject.activeSelf; }
}
/// 얇은 원의 지름(월드) = 슬링샷 최대 거리 × 2.
public float RingDiameter
{
get { return ring != null ? ring.transform.localScale.x : 0f; }
}
/// 도넛 바깥 지름(월드) = 슬링샷 최대 거리 × (1 + 비율) × 2.
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);
}
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;
}
if (donut.gameObject.activeSelf != archer) donut.gameObject.SetActive(archer);
float rs = SkillSystem.Current != null ? SkillSystem.Current.ActiveRadius(u) : 0f;
bool hasSkill = rs > 0.01f;
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);
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);
}
/// 궁병 도넛 색: 공격 가능 범위 안에 적이 있으면 붉게, 없으면 회색.
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();
}
void Follow()
{
Vector2 p = target.Position;
transform.position = new Vector3(p.x, p.y, 0f);
}
}
}