2023-08-12 01:43:24 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.Remoting.Contexts;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
namespace BITKit.Entities
|
|
|
|
{
|
|
|
|
public interface IHealth
|
|
|
|
{
|
2023-10-20 19:31:12 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 当设置HP时的回调
|
|
|
|
/// </summary>
|
2023-08-12 01:43:24 +08:00
|
|
|
public event Action<int> OnSetHealthPoint;
|
2023-10-20 19:31:12 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 当设置生存状态时的回调
|
|
|
|
/// </summary>
|
2023-08-12 01:43:24 +08:00
|
|
|
public event Action<bool> OnSetAlive;
|
2023-10-20 19:31:12 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 当受到伤害时的回调
|
|
|
|
/// </summary>
|
|
|
|
public event Func<DamageMessage,int,int> OnDamage;
|
2023-08-12 01:43:24 +08:00
|
|
|
int HealthPoint { get; set; }
|
|
|
|
int MaxHealthPoint { get; set; }
|
|
|
|
bool IsAlive { get; }
|
|
|
|
}
|
2023-08-27 02:58:19 +08:00
|
|
|
[CustomType(typeof(IHealth))]
|
2023-08-12 01:43:24 +08:00
|
|
|
public class EntityHealth : EntityComponent, IHealth
|
|
|
|
{
|
|
|
|
[Header(Constant.Header.Settings)]
|
|
|
|
[SerializeField] private int healthPoint = 100;
|
|
|
|
[SerializeField] private int maxHealthPoint = 100;
|
|
|
|
|
|
|
|
public event Action<int> OnSetHealthPoint;
|
|
|
|
public event Action<bool> OnSetAlive;
|
2023-10-20 19:31:12 +08:00
|
|
|
public event Func<DamageMessage,int, int> OnDamage;
|
2023-08-12 01:43:24 +08:00
|
|
|
|
|
|
|
public int HealthPoint
|
|
|
|
{
|
|
|
|
get => healthPoint;
|
|
|
|
set => OnHealthPointChangedInternal(healthPoint, value);
|
|
|
|
}
|
|
|
|
public int MaxHealthPoint
|
|
|
|
{
|
|
|
|
get => maxHealthPoint;
|
|
|
|
set => maxHealthPoint = value;
|
|
|
|
}
|
|
|
|
public bool IsAlive { get; private set; }
|
|
|
|
public override void OnAwake()
|
|
|
|
{
|
2023-10-20 19:31:12 +08:00
|
|
|
entity.AddListener<DamageMessage>(OnGetDamage);
|
2023-08-12 01:43:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnStart()
|
|
|
|
{
|
|
|
|
IsAlive = healthPoint >= 0;
|
|
|
|
OnSetAliveInternal(IsAlive);
|
|
|
|
OnHealthPointChangedInternal(0, healthPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnHealthPointChangedInternal(int old, int newHP)
|
|
|
|
{
|
|
|
|
healthPoint = newHP;
|
|
|
|
var _isAlive = newHP >= 0;
|
|
|
|
if (_isAlive != IsAlive)
|
|
|
|
{
|
|
|
|
OnSetAliveInternal(IsAlive = _isAlive);
|
|
|
|
}
|
|
|
|
OnSetHealthPoint?.Invoke(newHP);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnSetAliveInternal(bool alive)
|
|
|
|
{
|
2023-09-01 14:33:54 +08:00
|
|
|
IsAlive = alive;
|
2023-08-12 01:43:24 +08:00
|
|
|
OnSetAlive?.Invoke(alive);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddHP(int hp)
|
|
|
|
{
|
|
|
|
OnHealthPointChangedInternal(healthPoint, healthPoint += hp);
|
|
|
|
}
|
|
|
|
|
2023-10-20 19:31:12 +08:00
|
|
|
private void OnGetDamage(DamageMessage damageMessage)
|
2023-08-12 01:43:24 +08:00
|
|
|
{
|
2023-10-20 19:31:12 +08:00
|
|
|
if (damageMessage.target != entity) return;
|
|
|
|
var damage = damageMessage.damage;
|
|
|
|
foreach (var x in OnDamage.CastAsFunc())
|
|
|
|
{
|
|
|
|
damage = x.Invoke(damageMessage,damage);
|
|
|
|
}
|
|
|
|
AddHP(-damage);
|
2023-08-12 01:43:24 +08:00
|
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
[BIT]
|
|
|
|
public void SetAlive()
|
|
|
|
{
|
|
|
|
BITAppForUnity.ThrowIfNotPlaying();
|
|
|
|
OnHealthPointChangedInternal(-1, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
[BIT]
|
|
|
|
public void SetDead()
|
|
|
|
{
|
|
|
|
BITAppForUnity.ThrowIfNotPlaying();
|
|
|
|
OnHealthPointChangedInternal(100, -1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|