1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using UnityEngine;
|
||||
@@ -20,21 +21,34 @@ namespace BITKit.Entities
|
||||
/// <summary>
|
||||
/// 当受到伤害时的回调
|
||||
/// </summary>
|
||||
public event Func<DamageMessage,int,int> OnDamageFactory;
|
||||
public event Func<DamageMessage,int,int> OnDamageFactory;
|
||||
/// <summary>
|
||||
/// 收到伤害时的回调
|
||||
/// </summary>
|
||||
public event Action<DamageMessage> OnDamageRelease;
|
||||
/// <summary>
|
||||
/// 当伤害被阻止时的回调
|
||||
/// </summary>
|
||||
public event Action<DamageMessage> OnDamageVoid;
|
||||
|
||||
int HealthPoint { get; set; }
|
||||
int MaxHealthPoint { get; set; }
|
||||
bool IsAlive { get; }
|
||||
}
|
||||
[CustomType(typeof(IHealth))]
|
||||
public class EntityHealth : EntityBehavior, IHealth
|
||||
public class EntityHealth : EntityBehavior, IHealth,IEntityBinaryComponent
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] private int healthPoint = 100;
|
||||
[SerializeField] private int maxHealthPoint = 100;
|
||||
|
||||
public int Id { get; } = 84946486;
|
||||
|
||||
public event Action<int> OnSetHealthPoint;
|
||||
public event Action<bool> OnSetAlive;
|
||||
public event Func<DamageMessage,int, int> OnDamageFactory;
|
||||
public event Action<DamageMessage> OnDamageRelease;
|
||||
public event Action<DamageMessage> OnDamageVoid;
|
||||
|
||||
public int HealthPoint
|
||||
{
|
||||
@@ -61,7 +75,7 @@ namespace BITKit.Entities
|
||||
|
||||
private void OnHealthPointChangedInternal(int old, int newHP)
|
||||
{
|
||||
healthPoint = newHP;
|
||||
healthPoint =Mathf.Clamp(newHP,-1,maxHealthPoint) ;
|
||||
var _isAlive = newHP >= 0;
|
||||
if (_isAlive != IsAlive)
|
||||
{
|
||||
@@ -84,15 +98,28 @@ namespace BITKit.Entities
|
||||
private void OnGetDamage(DamageMessage damageMessage)
|
||||
{
|
||||
if (damageMessage.Target != UnityEntity) return;
|
||||
if (IsAlive is false) return;
|
||||
var damage = damageMessage.Damage;
|
||||
foreach (var x in OnDamageFactory.CastAsFunc().Reverse())
|
||||
if (IsAlive is false)
|
||||
{
|
||||
damage = x.Invoke(damageMessage, damage);
|
||||
if (damage <= 0) break;
|
||||
OnDamageVoid?.Invoke(damageMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
var damage = damageMessage.Damage;
|
||||
foreach (var x in OnDamageFactory.CastAsFunc())
|
||||
{
|
||||
damage = x.Invoke(damageMessage, damage);
|
||||
if (damage <= 0)
|
||||
{
|
||||
OnDamageVoid?.Invoke(damageMessage);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (Data.Get<bool>("god") is false)
|
||||
AddHP(-damage);
|
||||
|
||||
damageMessage.Damage = damage;
|
||||
OnDamageRelease?.Invoke(damageMessage);
|
||||
}
|
||||
if (Data.Get<bool>("god") is false)
|
||||
AddHP(-damage);
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[BIT]
|
||||
@@ -109,6 +136,17 @@ namespace BITKit.Entities
|
||||
OnHealthPointChangedInternal(100, -1);
|
||||
}
|
||||
#endif
|
||||
|
||||
public void Serialize(BinaryWriter writer)
|
||||
{
|
||||
writer.Write(HealthPoint);
|
||||
}
|
||||
|
||||
public void Deserialize(BinaryReader reader)
|
||||
{
|
||||
var newHealthPoint = reader.ReadInt32();
|
||||
if (HealthPoint != newHealthPoint)
|
||||
HealthPoint = newHealthPoint;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user