43 lines
1.0 KiB
C#
43 lines
1.0 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(WorldHealthBar))]
|
|
public class BuildingHealth : MonoBehaviour
|
|
{
|
|
[Header("建筑生命")]
|
|
[Min(1f)] public float maxHealth = 500f;
|
|
|
|
private WorldHealthBar healthBar;
|
|
|
|
public float ProductionProgress => healthBar != null
|
|
? Mathf.Clamp01(healthBar.currentEnergy / healthBar.maxEnergy)
|
|
: 0f;
|
|
|
|
private void Awake()
|
|
{
|
|
healthBar = GetComponent<WorldHealthBar>();
|
|
healthBar.maxHealth = maxHealth;
|
|
healthBar.currentHealth = maxHealth;
|
|
healthBar.maxEnergy = 1f;
|
|
healthBar.currentEnergy = 0f;
|
|
}
|
|
|
|
public void SetProductionProgress(float normalizedProgress)
|
|
{
|
|
if (healthBar != null)
|
|
{
|
|
healthBar.SetEnergy(Mathf.Clamp01(normalizedProgress) * healthBar.maxEnergy);
|
|
}
|
|
}
|
|
|
|
public bool ReceiveDamage(float damage, GameObject attacker = null)
|
|
{
|
|
if (damage <= 0f || healthBar == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
healthBar.TakeDamage(damage);
|
|
return true;
|
|
}
|
|
}
|