코드 리팩토링
재사용성 및 확장성을 고려하여 코드 전반을 리팩토링함
This commit is contained in:
@@ -1,34 +1,51 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class Gate : MonoBehaviour, IDamageable
|
||||
/// <summary>
|
||||
/// Gate structure that can be destroyed.
|
||||
/// Uses HealthComponent for health management.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(HealthComponent))]
|
||||
public class Gate : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float maxHealth = 50f;
|
||||
[SerializeField] private float currentHealth = 50f;
|
||||
private float CurrentHealth;
|
||||
private HealthComponent _health;
|
||||
private NavMeshObstacle _obstacle;
|
||||
|
||||
// 체력이 변경될 때 UI 등에 알리기 위한 이벤트 (Observer 패턴)
|
||||
public static event Action<float> OnHealthChanged;
|
||||
public static event Action OnGateDestroyed;
|
||||
|
||||
void Awake() => currentHealth = maxHealth;
|
||||
|
||||
public void TakeDamage(float amount)
|
||||
void Awake()
|
||||
{
|
||||
currentHealth -= amount;
|
||||
OnHealthChanged?.Invoke(currentHealth / maxHealth);
|
||||
_health = GetComponent<HealthComponent>();
|
||||
_obstacle = GetComponent<NavMeshObstacle>();
|
||||
|
||||
if (currentHealth <= 0)
|
||||
// Subscribe to health component events
|
||||
_health.OnDeath += HandleDeath;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (_health != null)
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
var obstacle = GetComponent<UnityEngine.AI.NavMeshObstacle>();
|
||||
if(obstacle != null)
|
||||
{
|
||||
obstacle.carving = false;
|
||||
obstacle.enabled = false;
|
||||
}
|
||||
OnGateDestroyed?.Invoke();
|
||||
Destroy(gameObject, 0.1f);
|
||||
_health.OnDeath -= HandleDeath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleDeath()
|
||||
{
|
||||
// Disable the gate visually
|
||||
gameObject.SetActive(false);
|
||||
|
||||
// Disable NavMesh carving so enemies can pass through
|
||||
if (_obstacle != null)
|
||||
{
|
||||
_obstacle.carving = false;
|
||||
_obstacle.enabled = false;
|
||||
}
|
||||
|
||||
// Destroy the object after a short delay
|
||||
Destroy(gameObject, 0.1f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the health component for direct access.
|
||||
/// </summary>
|
||||
public HealthComponent Health => _health;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user