Files
Backyard-CTF/Backyard CTF/Assets/Scripts/Visibility.cs
John Lamb 95b37a1606 feat(core): implement playable CTF prototype with 6 core scripts
Implements the minimal playable core for the teaser prototype:
- Game.cs: Bootstrap, scene setup, scoring, round reset, win condition
- Unit.cs: Movement, route following, tagging, respawn
- RouteDrawer.cs: Click-drag route input with LineRenderer preview
- Flag.cs: Pickup, drop, return mechanics
- Visibility.cs: Fog of war via SpriteRenderer visibility
- SimpleAI.cs: Enemy AI that chases player flag

All game objects created programmatically (no Editor setup required).
Uses RuntimeInitializeOnLoadMethod for automatic bootstrap.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-01 21:03:11 -06:00

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;
}
}