Files
HacknSlash/Assets/Scripts/Fortification.cs

68 lines
1.8 KiB
C#
Raw Normal View History

using UnityEngine;
using System.Collections.Generic;
public class Fortification : MonoBehaviour
{
// Global list so Siege enemies can easily find targets
public static List<Fortification> AllFortifications = new List<Fortification>();
[Header("Health Settings")]
public float maxHealth = 500f;
public float currentHealth;
[Header("Visual Stages (Assign GameObjects)")]
public GameObject fullyBuiltModel;
public GameObject damagedModel;
public GameObject ruinedModel;
private Collider barrierCollider;
public bool IsDestroyed => currentHealth <= 0;
void Awake()
{
barrierCollider = GetComponent<Collider>();
}
void OnEnable()
{
AllFortifications.Add(this);
currentHealth = maxHealth;
UpdateVisuals();
}
void OnDisable()
{
AllFortifications.Remove(this);
}
public void TakeDamage(float amount)
{
if (IsDestroyed) return;
currentHealth -= amount;
UpdateVisuals();
}
public void Repair(float amount)
{
if (currentHealth >= maxHealth) return;
currentHealth += amount;
currentHealth = Mathf.Min(currentHealth, maxHealth); // Cap at max
UpdateVisuals();
}
private void UpdateVisuals()
{
// Swap models based on health thresholds
fullyBuiltModel.SetActive(currentHealth > maxHealth * 0.5f);
damagedModel.SetActive(currentHealth > 0 && currentHealth <= maxHealth * 0.5f);
ruinedModel.SetActive(IsDestroyed);
// When destroyed, turn the collider into a trigger so the horde can pour through the rubble
if (barrierCollider != null)
{
barrierCollider.isTrigger = IsDestroyed;
}
}
}