체력바 추가

플레이어는 상시 표시, 나머지는 체력 변경 시 표시
This commit is contained in:
2026-02-25 15:15:29 +09:00
parent f3923079a4
commit 17b3cf6746
22 changed files with 369 additions and 147 deletions

View File

@@ -7,7 +7,7 @@ namespace Northbound
/// 적대 유닛 (적대세력 또는 몬스터)
/// </summary>
[RequireComponent(typeof(Collider))]
public class EnemyUnit : NetworkBehaviour, IDamageable, ITeamMember
public class EnemyUnit : NetworkBehaviour, IDamageable, ITeamMember, IHealthProvider
{
[Header("Team Settings")]
[Tooltip("이 유닛의 팀 (Hostile = 적대세력, Monster = 몬스터)")]
@@ -20,6 +20,10 @@ namespace Northbound
public GameObject damageEffectPrefab;
public GameObject destroyEffectPrefab;
[Header("Health Bar")]
public bool showHealthBar = true;
public GameObject healthBarPrefab;
private NetworkVariable<int> _currentHealth = new NetworkVariable<int>(
0,
NetworkVariableReadPermission.Everyone,
@@ -37,6 +41,8 @@ namespace Northbound
/// </summary>
public event System.Action<ulong> OnDeath;
private UnitHealthBar _healthBar;
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
@@ -46,13 +52,48 @@ namespace Northbound
_currentHealth.Value = maxHealth;
_team.Value = enemyTeam;
}
// 체력 변경 이벤트 구독
_currentHealth.OnValueChanged += OnHealthChanged;
// 체력바 생성
if (showHealthBar && healthBarPrefab != null)
{
CreateHealthBar();
}
}
public override void OnNetworkDespawn()
{
_currentHealth.OnValueChanged -= OnHealthChanged;
base.OnNetworkDespawn();
}
private void OnHealthChanged(int previousValue, int newValue)
{
if (_healthBar != null)
{
_healthBar.UpdateHealth();
}
}
private void CreateHealthBar()
{
if (_healthBar != null)
return;
if (healthBarPrefab == null)
return;
GameObject healthBarObj = Instantiate(healthBarPrefab, transform);
_healthBar = healthBarObj.GetComponent<UnitHealthBar>();
if (_healthBar != null)
{
_healthBar.Initialize(this);
}
}
#region IDamageable Implementation
public void TakeDamage(int damage, ulong attackerId)
@@ -144,6 +185,20 @@ namespace Northbound
#endregion
#region IHealthProvider Implementation
public int GetCurrentHealth() => _currentHealth.Value;
public int GetMaxHealth() => maxHealth;
public float GetHealthPercentage()
{
int max = GetMaxHealth();
return max > 0 ? (float)_currentHealth.Value / max : 0f;
}
#endregion
private void OnDrawGizmosSelected()
{
#if UNITY_EDITOR