CapersProject/Assets/02.Scripts/Character/Enemy/Boss/BossHealthPoint.cs

199 lines
6.2 KiB
C#
Raw Normal View History

2024-06-03 18:26:03 +00:00
using System;
using System.Collections;
using BlueWater.Audios;
using BlueWater.Interfaces;
using BlueWater.Uis;
using BlueWater.Utility;
using Sirenix.OdinInspector;
2024-06-03 18:26:03 +00:00
using UnityEngine;
namespace BlueWater.Enemies.Bosses
{
public class BossHealthPoint : MonoBehaviour, IDamageable
{
// 컴포넌트
[Title("컴포넌트")]
[SerializeField]
2024-06-03 18:26:03 +00:00
private CapsuleCollider _characterCollider;
[SerializeField]
private Renderer _renderer;
2024-06-03 18:26:03 +00:00
[SerializeField]
private Transform _particleInstantiateLocation;
// 체력 설정
[field: Title("체력 설정")]
[field: SerializeField]
2024-06-03 18:26:03 +00:00
public int MaxHealthPoint { get; private set; }
[field: SerializeField]
public int CurrentHealthPoint { get; private set; }
public bool IsInvincible { get; private set; }
2024-06-03 18:26:03 +00:00
[field: SerializeField]
public float InvincibilityDuration { get; private set; } = 0.1f;
2024-06-03 18:26:03 +00:00
// 효과 설정
[Title("효과 설정")]
2024-06-03 18:26:03 +00:00
[SerializeField]
private string _attackedSfxName;
[SerializeField]
private string _dieSfxName;
[SerializeField]
private ParticleSystem _attackedParticle;
[SerializeField]
private ParticleSystem _dieParticle;
private FieldBossHealthPointUi _fieldBossHealthPointUi;
private WaitForSeconds _flashWhiteWaitTime;
private Coroutine _flashWhiteCoroutine;
private Coroutine _damageIntervalCoroutine;
private bool _enableHealthChangedEvent;
// Hashes
private static readonly int _isHitHash = Shader.PropertyToID("_IsHit");
// Events
public event Action<int> OnHealthChanged;
public event Action OnDead;
private void Awake()
{
InitializeComponent();
}
2024-06-03 18:26:03 +00:00
// Unity events
private void Start()
{
_flashWhiteWaitTime = new WaitForSeconds(InvincibilityDuration);
_fieldBossHealthPointUi = CombatUiManager.Instance.FieldBossHealthPointUi;
2024-06-03 18:26:03 +00:00
}
private void OnDestroy()
{
if (_enableHealthChangedEvent && _fieldBossHealthPointUi)
{
OnHealthChanged -= _fieldBossHealthPointUi.SetCurrentHealthPoint;
}
}
// Initialize methods
[Button("셋팅 초기화")]
private void InitializeComponent()
{
_characterCollider = GetComponent<CapsuleCollider>();
_renderer = GetComponentInChildren<Renderer>();
}
public void Initialize(bool enableHealthChangedEvent, int maxHealthPoint, string bossName, Transform particleInstantiateLocation = null)
2024-06-03 18:26:03 +00:00
{
_enableHealthChangedEvent = enableHealthChangedEvent;
MaxHealthPoint = maxHealthPoint;
_particleInstantiateLocation = particleInstantiateLocation;
_fieldBossHealthPointUi ??= CombatUiManager.Instance.FieldBossHealthPointUi;
2024-06-03 18:26:03 +00:00
if (_enableHealthChangedEvent)
{
OnHealthChanged += _fieldBossHealthPointUi.SetCurrentHealthPoint;
_fieldBossHealthPointUi.SetBoss(MaxHealthPoint, bossName);
2024-06-03 18:26:03 +00:00
}
SetCurrentHealthPoint(MaxHealthPoint);
}
// Methods
public void SetCurrentHealthPoint(int changedHealthPoint)
{
CurrentHealthPoint = changedHealthPoint;
OnHealthChanged?.Invoke(changedHealthPoint);
}
public bool CanDamage() => !IsInvincible;
2024-06-03 18:26:03 +00:00
public void TakeDamage(int damageAmount)
{
IsInvincible = true;
2024-06-03 18:26:03 +00:00
var changeHp = Mathf.Max(CurrentHealthPoint - damageAmount, 0);
SetCurrentHealthPoint(changeHp);
if (_renderer.material.HasInt(_isHitHash))
{
Utils.StartUniqueCoroutine(this, ref _flashWhiteCoroutine, FlashWhiteCoroutine());
}
2024-06-03 18:26:03 +00:00
// 죽었는지 체크
if (changeHp == 0)
2024-06-03 18:26:03 +00:00
{
Die();
return;
}
if (_attackedParticle)
{
var instantiateHitParticle = Instantiate(_attackedParticle, _characterCollider.bounds.center,
_attackedParticle.transform.rotation, _particleInstantiateLocation);
instantiateHitParticle.Play();
}
if (!string.IsNullOrEmpty(_attackedSfxName))
{
AudioManager.Instance.PlaySfx(_attackedSfxName);
}
Utils.StartUniqueCoroutine(this, ref _damageIntervalCoroutine, Utils.CoolDownCoroutine(InvincibilityDuration, EndDamageIntervalCoroutine));
2024-06-03 18:26:03 +00:00
}
public void TryTakeDamage(int damageAmount)
{
if (!CanDamage()) return;
TakeDamage(damageAmount);
}
public void Die()
{
if (_dieParticle)
{
var instantiateHitParticle = Instantiate(_dieParticle, _characterCollider.bounds.center,
_dieParticle.transform.rotation, _particleInstantiateLocation);
2024-06-03 18:26:03 +00:00
instantiateHitParticle.Play();
}
if (!string.IsNullOrEmpty(_dieSfxName))
{
AudioManager.Instance.PlaySfx(_dieSfxName);
}
OnDead?.Invoke();
}
private IEnumerator FlashWhiteCoroutine()
{
_renderer.material.SetInt(_isHitHash, 1);
2024-06-03 18:26:03 +00:00
yield return _flashWhiteWaitTime;
_renderer.material.SetInt(_isHitHash, 0);
2024-06-03 18:26:03 +00:00
Utils.EndUniqueCoroutine(this, ref _flashWhiteCoroutine);
}
private void EndDamageIntervalCoroutine()
{
IsInvincible = false;
2024-06-03 18:26:03 +00:00
Utils.EndUniqueCoroutine(this, ref _damageIntervalCoroutine);
}
public void HideFieldBossHealthPointUi()
{
if (_fieldBossHealthPointUi.gameObject.activeSelf)
{
_fieldBossHealthPointUi.SetActiveHpSlider(false);
}
}
2024-06-03 18:26:03 +00:00
}
}