60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
using UnityEngine;
|
|
|
|
public class Visibility : MonoBehaviour
|
|
{
|
|
public Unit[] playerUnits;
|
|
public Unit[] enemyUnits;
|
|
public Flag enemyFlag;
|
|
|
|
void Update()
|
|
{
|
|
// Update visibility of enemy units
|
|
foreach (var enemy in enemyUnits)
|
|
{
|
|
if (enemy == null) continue;
|
|
|
|
bool visible = IsVisibleToAnyPlayerUnit(enemy.transform.position);
|
|
var sr = enemy.GetComponent<SpriteRenderer>();
|
|
if (sr != null)
|
|
{
|
|
sr.enabled = visible || enemy.isTaggedOut; // Show tagged out units (they're faded anyway)
|
|
}
|
|
}
|
|
|
|
// Update visibility of enemy flag (only when not carried)
|
|
if (enemyFlag != null && enemyFlag.carriedBy == null)
|
|
{
|
|
bool flagVisible = IsVisibleToAnyPlayerUnit(enemyFlag.transform.position);
|
|
var flagSr = enemyFlag.GetComponent<SpriteRenderer>();
|
|
if (flagSr != null)
|
|
{
|
|
flagSr.enabled = flagVisible;
|
|
}
|
|
}
|
|
else if (enemyFlag != null && enemyFlag.carriedBy != null)
|
|
{
|
|
// Flag is carried - always show it (it's attached to a visible unit)
|
|
var flagSr = enemyFlag.GetComponent<SpriteRenderer>();
|
|
if (flagSr != null)
|
|
{
|
|
flagSr.enabled = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool IsVisibleToAnyPlayerUnit(Vector2 position)
|
|
{
|
|
foreach (var unit in playerUnits)
|
|
{
|
|
if (unit == null || unit.isTaggedOut) continue;
|
|
|
|
float distance = Vector2.Distance(unit.transform.position, position);
|
|
if (distance < Game.VisionRadius)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|