1
This commit is contained in:
@@ -1,40 +1,12 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public interface IHealthCallback
|
||||
{
|
||||
void OnSetAlive(bool alive);
|
||||
void OnSetHP(int hp);
|
||||
}
|
||||
[Serializable]
|
||||
public class UnityEventHealthCallback : IHealthCallback
|
||||
{
|
||||
[SerializeField] private UnityEvent<int> onSetHP;
|
||||
[SerializeField] private UnityEvent onSetAlive;
|
||||
[SerializeField] private UnityEvent onSetDead;
|
||||
public void OnSetAlive(bool alive)
|
||||
{
|
||||
if (alive)
|
||||
{
|
||||
onSetAlive.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
onSetDead.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSetHP(int hp)
|
||||
{
|
||||
onSetHP.Invoke(hp);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IHealth
|
||||
{
|
||||
/// <summary>
|
||||
@@ -48,7 +20,7 @@ namespace BITKit.Entities
|
||||
/// <summary>
|
||||
/// 当受到伤害时的回调
|
||||
/// </summary>
|
||||
public event Func<DamageMessage,int,int> OnDamage;
|
||||
public event Func<DamageMessage,int,int> OnDamageFactory;
|
||||
int HealthPoint { get; set; }
|
||||
int MaxHealthPoint { get; set; }
|
||||
bool IsAlive { get; }
|
||||
@@ -60,13 +32,9 @@ namespace BITKit.Entities
|
||||
[SerializeField] private int healthPoint = 100;
|
||||
[SerializeField] private int maxHealthPoint = 100;
|
||||
|
||||
[Header(Constant.Header.Providers)] [SerializeField, SerializeReference, SubclassSelector]
|
||||
[Obsolete]
|
||||
private IHealthCallback[] additiveCallback;
|
||||
|
||||
public event Action<int> OnSetHealthPoint;
|
||||
public event Action<bool> OnSetAlive;
|
||||
public event Func<DamageMessage,int, int> OnDamage;
|
||||
public event Func<DamageMessage,int, int> OnDamageFactory;
|
||||
|
||||
public int HealthPoint
|
||||
{
|
||||
@@ -99,25 +67,12 @@ namespace BITKit.Entities
|
||||
{
|
||||
OnSetAliveInternal(IsAlive = _isAlive);
|
||||
}
|
||||
foreach (var x in entity.GetCallbacks<IHealthCallback>())
|
||||
{
|
||||
x.OnSetHP(newHP);
|
||||
}
|
||||
|
||||
OnSetHealthPoint?.Invoke(newHP);
|
||||
}
|
||||
|
||||
private void OnSetAliveInternal(bool alive)
|
||||
{
|
||||
IsAlive = alive;
|
||||
foreach (var x in entity.GetCallbacks<IHealthCallback>())
|
||||
{
|
||||
x.OnSetAlive(alive);
|
||||
}
|
||||
// foreach (var x in additiveCallback)
|
||||
// {
|
||||
// x.OnSetAlive(alive);
|
||||
// }
|
||||
OnSetAlive?.Invoke(alive);
|
||||
}
|
||||
|
||||
@@ -128,11 +83,13 @@ namespace BITKit.Entities
|
||||
|
||||
private void OnGetDamage(DamageMessage damageMessage)
|
||||
{
|
||||
if (damageMessage.target != entity) return;
|
||||
var damage = damageMessage.damage;
|
||||
foreach (var x in OnDamage.CastAsFunc())
|
||||
if (damageMessage.Target != entity) return;
|
||||
if (IsAlive is false) return;
|
||||
var damage = damageMessage.Damage;
|
||||
foreach (var x in OnDamageFactory.CastAsFunc().Reverse())
|
||||
{
|
||||
damage = x.Invoke(damageMessage,damage);
|
||||
damage = x.Invoke(damageMessage,damage);
|
||||
if (damage <= 0) break;
|
||||
}
|
||||
AddHP(-damage);
|
||||
}
|
||||
|
Reference in New Issue
Block a user