2026-05-19 hp바 추가
This commit is contained in:
@@ -3,11 +3,9 @@
|
||||
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Health))]
|
||||
public class Enemy : MonoBehaviour, IDamageable
|
||||
{
|
||||
[Header("Stats")]
|
||||
[SerializeField] private int _maxHealth = 30;
|
||||
|
||||
[Header("Hit Feedback")]
|
||||
[SerializeField] private float _hitFlashDuration = 0.1f;
|
||||
[SerializeField] private Color _hitFlashColor = Color.red;
|
||||
@@ -25,7 +23,7 @@ public class Enemy : MonoBehaviour, IDamageable
|
||||
[SerializeField] private LayerMask _separationLayer;
|
||||
private static readonly Collider2D[] _separationBuffer = new Collider2D[16];
|
||||
|
||||
private int _currentHealth;
|
||||
private Health _health;
|
||||
private Rigidbody2D _rb;
|
||||
private Animator _anim;
|
||||
private SpriteRenderer _spriteRenderer;
|
||||
@@ -49,7 +47,8 @@ public class Enemy : MonoBehaviour, IDamageable
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_currentHealth = _maxHealth;
|
||||
_health = GetComponent<Health>();
|
||||
_health.OnDied += HandleDeath;
|
||||
_rb = GetComponent<Rigidbody2D>();
|
||||
_anim = GetComponentInChildren<Animator>();
|
||||
_spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
@@ -58,6 +57,12 @@ private void Awake()
|
||||
_originalColor = _spriteRenderer.color;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (_health != null)
|
||||
_health.OnDied -= HandleDeath;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_flashTimer > 0f)
|
||||
@@ -143,13 +148,10 @@ private void ApplySeparation()
|
||||
|
||||
public void TakeDamage(int amount, Vector2 hitVelocity = default, string hitReactionAnimationState = null, Vector2? hitTargetPosition = null, bool correctHitTargetY = false, int hitPositionSolidMask = 0, float hitPositionCorrectionDuration = 0f)
|
||||
{
|
||||
if (_currentHealth <= 0) return;
|
||||
if (_health == null || _health.IsDead) return;
|
||||
|
||||
_isGrabbed = false;
|
||||
|
||||
_currentHealth -= amount;
|
||||
Debug.Log($"{name} 피격: -{amount} (HP: {_currentHealth}/{_maxHealth})");
|
||||
|
||||
if (_spriteRenderer != null)
|
||||
{
|
||||
Debug.Log($"[Flash START] t={Time.time:F3} duration={_hitFlashDuration:F3} (current color was {_spriteRenderer.color})");
|
||||
@@ -178,13 +180,14 @@ public void TakeDamage(int amount, Vector2 hitVelocity = default, string hitReac
|
||||
}
|
||||
}
|
||||
|
||||
if (_currentHealth <= 0)
|
||||
Die();
|
||||
// 시각/반응 처리가 끝난 뒤 HP를 감산해서 OnDied 이벤트가 마지막에 발화되게 한다.
|
||||
_health.TakeDamage(amount);
|
||||
Debug.Log($"{name} 피격: -{amount} (HP: {_health.CurrentHealth}/{_health.MaxHealth})");
|
||||
}
|
||||
|
||||
public void BeginGrab(string grabbedAnimationState, int solidMask)
|
||||
{
|
||||
if (_currentHealth <= 0) return;
|
||||
if (_health == null || _health.IsDead) return;
|
||||
|
||||
_isGrabbed = true;
|
||||
_grabSolidMask = solidMask;
|
||||
@@ -373,7 +376,7 @@ private void BounceOffWall(Vector2 wallNormal)
|
||||
_hitReactionTimer = _hitReactionDuration;
|
||||
}
|
||||
|
||||
private void Die()
|
||||
private void HandleDeath()
|
||||
{
|
||||
Debug.Log($"{name} 사망");
|
||||
Destroy(gameObject);
|
||||
|
||||
Reference in New Issue
Block a user