177 lines
5.7 KiB
C#
177 lines
5.7 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using BlueWater.Audios;
|
||
|
using BlueWater.Interfaces;
|
||
|
using BlueWater.Uis;
|
||
|
using BlueWater.Utility;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace BlueWater.Enemies.Bosses
|
||
|
{
|
||
|
public class BossHealthPoint : MonoBehaviour, IDamageable
|
||
|
{
|
||
|
// Components
|
||
|
private CapsuleCollider _characterCollider;
|
||
|
private SpriteRenderer _spriteRenderer;
|
||
|
|
||
|
// Variables
|
||
|
public int MaxHealthPoint { get; private set; }
|
||
|
|
||
|
[field: SerializeField]
|
||
|
public int CurrentHealthPoint { get; private set; }
|
||
|
|
||
|
[SerializeField]
|
||
|
private float _damageInterval = 0.1f;
|
||
|
|
||
|
private bool _enableTakeDamage = true;
|
||
|
|
||
|
[SerializeField]
|
||
|
private string _attackedSfxName;
|
||
|
|
||
|
[SerializeField]
|
||
|
private string _dieSfxName;
|
||
|
|
||
|
[SerializeField]
|
||
|
private ParticleSystem _attackedParticle;
|
||
|
|
||
|
[SerializeField]
|
||
|
private ParticleSystem _dieParticle;
|
||
|
|
||
|
[SerializeField]
|
||
|
private Transform _particleInstantiateLocation;
|
||
|
|
||
|
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;
|
||
|
|
||
|
// Unity events
|
||
|
private void Start()
|
||
|
{
|
||
|
_flashWhiteWaitTime = new WaitForSeconds(_damageInterval);
|
||
|
}
|
||
|
|
||
|
private void OnDestroy()
|
||
|
{
|
||
|
if (_enableHealthChangedEvent && _fieldBossHealthPointUi)
|
||
|
{
|
||
|
OnHealthChanged -= _fieldBossHealthPointUi.SetCurrentHealthPoint;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Initialize methods
|
||
|
public void InitializeComponents(bool enableHealthChangedEvent, CapsuleCollider characterCollider,
|
||
|
SpriteRenderer spriteRenderer, int maxHealthPoint, string bossName, Transform particleInstantiateLocation = null)
|
||
|
{
|
||
|
_fieldBossHealthPointUi = CombatUiManager.Instance.FieldBossHealthPointUi;
|
||
|
_enableHealthChangedEvent = enableHealthChangedEvent;
|
||
|
if (_enableHealthChangedEvent)
|
||
|
{
|
||
|
OnHealthChanged += _fieldBossHealthPointUi.SetCurrentHealthPoint;
|
||
|
_fieldBossHealthPointUi.SetBoss(maxHealthPoint, bossName);
|
||
|
}
|
||
|
|
||
|
_characterCollider = characterCollider;
|
||
|
_spriteRenderer = spriteRenderer;
|
||
|
MaxHealthPoint = maxHealthPoint;
|
||
|
_particleInstantiateLocation = particleInstantiateLocation;
|
||
|
|
||
|
SetCurrentHealthPoint(MaxHealthPoint);
|
||
|
}
|
||
|
|
||
|
// Events methods
|
||
|
public void HandleEnableTakeDamage() => _enableTakeDamage = true;
|
||
|
public void HandleDisableTakeDamage() => _enableTakeDamage = false;
|
||
|
|
||
|
// Methods
|
||
|
public void SetCurrentHealthPoint(int changedHealthPoint)
|
||
|
{
|
||
|
CurrentHealthPoint = changedHealthPoint;
|
||
|
OnHealthChanged?.Invoke(changedHealthPoint);
|
||
|
}
|
||
|
|
||
|
public bool CanDamage()
|
||
|
{
|
||
|
return _enableTakeDamage;
|
||
|
}
|
||
|
|
||
|
public void TakeDamage(int damageAmount)
|
||
|
{
|
||
|
HandleDisableTakeDamage();
|
||
|
var changeHp = Mathf.Max(CurrentHealthPoint - damageAmount, 0);
|
||
|
SetCurrentHealthPoint(changeHp);
|
||
|
|
||
|
// 죽었는지 체크
|
||
|
if (changeHp == 0f)
|
||
|
{
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
if (_spriteRenderer.material.HasInt(_isHitHash))
|
||
|
{
|
||
|
Utils.StartUniqueCoroutine(this, ref _flashWhiteCoroutine, FlashWhiteCoroutine());
|
||
|
}
|
||
|
Utils.StartUniqueCoroutine(this, ref _damageIntervalCoroutine, Utils.CoolDownCoroutine(_damageInterval, EndDamageIntervalCoroutine));
|
||
|
}
|
||
|
|
||
|
public void TryTakeDamage(int damageAmount)
|
||
|
{
|
||
|
if (!CanDamage()) return;
|
||
|
|
||
|
TakeDamage(damageAmount);
|
||
|
}
|
||
|
|
||
|
public void Die()
|
||
|
{
|
||
|
if (_dieParticle)
|
||
|
{
|
||
|
var instantiateHitParticle = Instantiate(_dieParticle, _characterCollider.center, _dieParticle.transform.rotation, _particleInstantiateLocation);
|
||
|
instantiateHitParticle.Play();
|
||
|
}
|
||
|
|
||
|
if (!string.IsNullOrEmpty(_dieSfxName))
|
||
|
{
|
||
|
AudioManager.Instance.PlaySfx(_dieSfxName);
|
||
|
}
|
||
|
|
||
|
OnDead?.Invoke();
|
||
|
}
|
||
|
|
||
|
private IEnumerator FlashWhiteCoroutine()
|
||
|
{
|
||
|
_spriteRenderer.material.SetInt(_isHitHash, 1);
|
||
|
yield return _flashWhiteWaitTime;
|
||
|
_spriteRenderer.material.SetInt(_isHitHash, 0);
|
||
|
|
||
|
Utils.EndUniqueCoroutine(this, ref _flashWhiteCoroutine);
|
||
|
}
|
||
|
|
||
|
private void EndDamageIntervalCoroutine()
|
||
|
{
|
||
|
Utils.EndUniqueCoroutine(this, ref _damageIntervalCoroutine);
|
||
|
HandleEnableTakeDamage();
|
||
|
}
|
||
|
}
|
||
|
}
|