This commit is contained in:
ohgushiyuga
2026-08-27 12:51:15 +09:00
parent 1441c96f74
commit 52341100ed
32 changed files with 840 additions and 1076 deletions

View File

@@ -1,68 +1,74 @@
using System;
using UnityEngine;
public class EnemyHealth : MonoBehaviour, IDamageble
public class EnemyHealth : MonoBehaviour, IDamageable
{
[SerializeField] private float maxHealth = 100f;
private float currentHealth;
[SerializeField] private float knockbackDuration = 0.5f;
public float CurrentHealth { get; private set; }
public bool IsDead { get; private set; }
public event Action<float> OnKnockbackRequested;
public event Action OnDeath;
private EnemyAI enemyAI;
private Animator anim;
private Collider col;
private bool isDead = false;
private static readonly int DeadHash = Animator.StringToHash("Dead");
void Start()
private void Awake()
{
currentHealth = maxHealth; //HPを代入
CurrentHealth = maxHealth;
enemyAI = GetComponent<EnemyAI>();
anim = GetComponent<Animator>();
col = GetComponent<Collider>();
}
private void OnEnable()
{
if (enemyAI != null)
{
OnKnockbackRequested += enemyAI.TriggerKnockback;
OnDeath += enemyAI.DisableAIOnDeath;
}
}
private void OnDisable()
{
if (enemyAI != null)
{
OnKnockbackRequested -= enemyAI.TriggerKnockback;
OnDeath -= enemyAI.DisableAIOnDeath;
}
}
public void TakeDamage(DamageInfo damageInfo)
{
if(currentHealth <= 0) return;
if (IsDead) return;
currentHealth -= damageInfo.amount; //ダメージ計算
CurrentHealth = Mathf.Max(0f, CurrentHealth - damageInfo.amount);
if(damageInfo.isPoseBonus)
{
Debug.Log("構えボーナス");
}
if(currentHealth <= 0)
if (CurrentHealth <= 0f)
{
Die();
}
else
{
if(enemyAI != null)
{
enemyAI.TriggerKnockback(knockbackDuration);
}
OnKnockbackRequested?.Invoke(knockbackDuration);
}
}
private void Die()
{
if(isDead) return;
isDead = true;
if (IsDead) return;
IsDead = true;
Animator anim = GetComponent<Animator>();
if(anim != null)
{
anim.SetTrigger("Dead");
}
if (anim != null) anim.SetTrigger(DeadHash);
if (col != null) col.enabled = false;
Collider col = GetComponent<Collider>();
if(col != null)
{
col.enabled = false;
}
if(enemyAI != null)
{
enemyAI.DisableAIOnDeath();
}
OnDeath?.Invoke();
Destroy(gameObject, 3.0f);
}
}
}