这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

74 行
1.8 KiB

using UnityEngine;
using UnityEngine.Events;
public class Damageable : MonoBehaviour
{
[SerializeField] private HealthConfigSO _healthConfigSO;
[SerializeField] private GetHitEffectConfigSO _getHitEffectSO;
[SerializeField] private Renderer _mainMeshRenderer;
[SerializeField] private DroppableRewardConfigSO _droppableRewardSO;
[Header("Broadcasting On")]
[SerializeField] private IntEventChannelSO _setHealthBar = default;
[SerializeField] private IntEventChannelSO _inflictDamage = default;
[SerializeField] private IntEventChannelSO _restoreHealth = default;
public DroppableRewardConfigSO DroppableRewardConfig => _droppableRewardSO;
private int _currentHealth = default;
public bool GetHit { get; set; }
public bool IsDead { get; set; }
public GetHitEffectConfigSO GetHitEffectConfig => _getHitEffectSO;
public Renderer MainMeshRenderer => _mainMeshRenderer;
public int CurrentHealth => _currentHealth;
public UnityAction OnDie;
private void Awake()
{
_currentHealth = _healthConfigSO.MaxHealth;
if (_setHealthBar != null)
_setHealthBar.RaiseEvent(_currentHealth);
}
public void Kill()
{
ReceiveAnAttack(_currentHealth);
}
public void ReceiveAnAttack(int damage)
{
if (IsDead)
return;
if (_inflictDamage != null)
_inflictDamage.RaiseEvent(damage);
_currentHealth -= damage;
GetHit = true;
if (_currentHealth <= 0)
{
IsDead = true;
if (OnDie != null)
OnDie.Invoke();
}
}
public void ResetHealth()
{
_currentHealth = _healthConfigSO.MaxHealth;
if (_setHealthBar != null)
_setHealthBar.RaiseEvent(_currentHealth);
IsDead = false;
}
public void restoreHealth(int healthToAdd)
{
if (IsDead)
return;
_currentHealth += healthToAdd;
if (_restoreHealth != null)
_restoreHealth.RaiseEvent(healthToAdd);
}
}