BITFALL/Assets/BITKit/Unity/Scripts/Entity/Components/Health/EntityHealth.cs

114 lines
3.3 KiB
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2023-10-24 23:37:59 +08:00
using System.Linq;
2023-08-12 01:43:24 +08:00
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>
2023-10-24 23:37:59 +08:00
public event Func<DamageMessage,int,int> OnDamageFactory;
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-10-30 01:25:53 +08:00
public class EntityHealth : EntityBehavior, IHealth
2023-08-12 01:43:24 +08:00
{
[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-24 23:37:59 +08:00
public event Func<DamageMessage,int, int> OnDamageFactory;
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-30 01:25:53 +08:00
UnityEntity.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)
{
2023-12-03 17:35:43 +08:00
healthPoint =Mathf.Clamp(newHP,-1,maxHealthPoint) ;
2023-08-12 01:43:24 +08:00
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-30 01:25:53 +08:00
if (damageMessage.Target != UnityEntity) return;
2023-10-24 23:37:59 +08:00
if (IsAlive is false) return;
var damage = damageMessage.Damage;
foreach (var x in OnDamageFactory.CastAsFunc().Reverse())
2023-10-20 19:31:12 +08:00
{
2023-11-30 00:23:23 +08:00
damage = x.Invoke(damageMessage, damage);
2023-10-24 23:37:59 +08:00
if (damage <= 0) break;
2023-10-20 19:31:12 +08:00
}
2023-11-30 00:23:23 +08:00
if (Data.Get<bool>("god") is false)
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
}
}