1
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class AutoHealComponent : EntityComponent,IHealthCallback,IDamageCallback
|
||||
{
|
||||
[SerializeField] private IntervalUpdate healDelayInterval;
|
||||
[SerializeField] private IntervalUpdate healInterval;
|
||||
[SerializeField] private int healIncrement;
|
||||
private readonly ValidHandle allowHeal = new();
|
||||
private IHealth _health;
|
||||
public override void Initialize(IEntity _entity)
|
||||
{
|
||||
base.Initialize(_entity);
|
||||
_entity.RegisterCallback<IHealthCallback>(this);
|
||||
_entity.RegisterCallback<IDamageCallback>(this);
|
||||
}
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
_health = entity.Get<IHealth>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!allowHeal.Allow || !healDelayInterval.AllowUpdateWithoutReset || !healInterval.AllowUpdate) return;
|
||||
_health.HealthPoint= Mathf.Clamp(_health.HealthPoint+healIncrement,0,_health.MaxHealthPoint);
|
||||
if (_health.HealthPoint == _health.MaxHealthPoint)
|
||||
{
|
||||
allowHeal.RemoveElement(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSetAlive(bool alive)
|
||||
{
|
||||
allowHeal.SetDisableElements(this,alive is false);
|
||||
}
|
||||
|
||||
public void OnSetHP(int hp)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnGetDamage(DamageMessage message)
|
||||
{
|
||||
allowHeal.AddElement(this);
|
||||
healDelayInterval.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1b0288b0c82e40746913ea5780b420a5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.SubSystems;
|
||||
using BITKit.Entities;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
|
||||
public interface IDamageType { }
|
||||
|
||||
public interface IDamageService
|
||||
{
|
||||
public event Action<DamageMessage> OnEntityDamaged;
|
||||
public event Action<DamageMessage> OnEntityKilled;
|
||||
void Execute(DamageMessage damageMessage);
|
||||
ValidHandle AllowDamageHandle { get; }
|
||||
}
|
||||
[Serializable]
|
||||
public class DamageServiceSingleton:IDamageService
|
||||
{
|
||||
private IDamageService _damageServiceImplementation => DamageService.Singleton;
|
||||
public event Action<DamageMessage> OnEntityDamaged
|
||||
{
|
||||
add => _damageServiceImplementation.OnEntityDamaged += value;
|
||||
remove => _damageServiceImplementation.OnEntityDamaged -= value;
|
||||
}
|
||||
|
||||
public event Action<DamageMessage> OnEntityKilled
|
||||
{
|
||||
add => _damageServiceImplementation.OnEntityKilled += value;
|
||||
remove => _damageServiceImplementation.OnEntityKilled -= value;
|
||||
}
|
||||
|
||||
public void Execute(DamageMessage damageMessage)
|
||||
{
|
||||
_damageServiceImplementation.Execute(damageMessage);
|
||||
}
|
||||
public ValidHandle AllowDamageHandle => _damageServiceImplementation.AllowDamageHandle;
|
||||
}
|
||||
[Serializable]
|
||||
public class DamageServiceMonoProxy:IDamageService
|
||||
{
|
||||
[SerializeField] private MonoBehaviour monoBehaviour;
|
||||
private IDamageService _damageServiceImplementation=>monoBehaviour as IDamageService;
|
||||
public event Action<DamageMessage> OnEntityDamaged
|
||||
{
|
||||
add => _damageServiceImplementation.OnEntityDamaged += value;
|
||||
remove => _damageServiceImplementation.OnEntityDamaged -= value;
|
||||
}
|
||||
|
||||
public event Action<DamageMessage> OnEntityKilled
|
||||
{
|
||||
add => _damageServiceImplementation.OnEntityKilled += value;
|
||||
remove => _damageServiceImplementation.OnEntityKilled -= value;
|
||||
}
|
||||
|
||||
public void Execute(DamageMessage damageMessage)
|
||||
{
|
||||
_damageServiceImplementation.Execute(damageMessage);
|
||||
}
|
||||
|
||||
public ValidHandle AllowDamageHandle => _damageServiceImplementation.AllowDamageHandle;
|
||||
}
|
||||
public record DamageMessage
|
||||
{
|
||||
public IEntity initiator;
|
||||
public IEntity target;
|
||||
public int damage;
|
||||
public IDamagable hit;
|
||||
public Location location;
|
||||
public IDamageType damageType;
|
||||
}
|
||||
public interface IDamageCallback
|
||||
{
|
||||
void OnGetDamage(DamageMessage message);
|
||||
}
|
||||
public interface IDamagable
|
||||
{
|
||||
IEntity Entity { get; }
|
||||
Rigidbody Rigidbody { get; }
|
||||
void GiveDamage(DamageMessage message);
|
||||
}
|
||||
public class DamageService:MonoBehaviour,IDamageService
|
||||
{
|
||||
internal static IDamageService Singleton { get; set; }
|
||||
public ValidHandle AllowDamageHandle { get; }= new();
|
||||
private readonly Queue<DamageMessage> Messages = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Singleton = this;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (AllowDamageHandle.Allow is false)
|
||||
{
|
||||
Messages.Clear();
|
||||
return;
|
||||
}
|
||||
while (Messages.TryDequeue(out var damageMessage))
|
||||
{
|
||||
var unityEntity = (Entity)damageMessage.target;
|
||||
if (unityEntity is null || !unityEntity.TryGetComponent<IHealth>(out var heal) || !heal.IsAlive) continue;
|
||||
|
||||
damageMessage.initiator?.Invoke(damageMessage);
|
||||
damageMessage.target?.Invoke(damageMessage);
|
||||
|
||||
foreach (var x in damageMessage.target?.GetCallbacks<IDamageCallback>()!)
|
||||
{
|
||||
x.OnGetDamage(damageMessage);
|
||||
}
|
||||
|
||||
OnEntityDamaged?.Invoke(damageMessage);
|
||||
if (heal.IsAlive is false)
|
||||
{
|
||||
OnEntityKilled?.Invoke(damageMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public event Action<DamageMessage> OnEntityDamaged;
|
||||
public event Action<DamageMessage> OnEntityKilled;
|
||||
public void Execute(DamageMessage damageMessage)=>Messages.Enqueue(damageMessage);
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6745dae71c15a66439ec1aee5f3c275d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
@@ -10,33 +11,72 @@ namespace BITKit.Entities
|
||||
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
|
||||
{
|
||||
public event Action<int> OnSetHealthPoint;
|
||||
public event Action<bool> OnSetAlive;
|
||||
int HealthPoint { get; set; }
|
||||
int MaxHealthPoint { get; set; }
|
||||
bool IsAlive { get; }
|
||||
}
|
||||
|
||||
public class EntityHealth : EntityComponent, IHealth
|
||||
{
|
||||
[Header(Constant.Header.Settings)] [SerializeField]
|
||||
private int healthPoint = 100;
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] private int healthPoint = 100;
|
||||
[SerializeField] private int maxHealthPoint = 100;
|
||||
|
||||
[Header(Constant.Header.Events)] public UnityEvent<bool> onSetAlive = new();
|
||||
[Header(Constant.Header.Events)]
|
||||
[SerializeField] private UnityEvent<bool> onSetAlive = new();
|
||||
|
||||
[Header(Constant.Header.Providers)] [SerializeField, SerializeReference, SubclassSelector]
|
||||
private IHealthCallback[] additiveCallback;
|
||||
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
bool isAlive;
|
||||
public event Action<int> OnSetHealthPoint;
|
||||
public event Action<bool> OnSetAlive;
|
||||
|
||||
public int HealthPoint
|
||||
{
|
||||
get => healthPoint;
|
||||
set => OnHealthPointChanged(healthPoint, value);
|
||||
set => OnHealthPointChangedInternal(healthPoint, value);
|
||||
}
|
||||
public int MaxHealthPoint
|
||||
{
|
||||
get => maxHealthPoint;
|
||||
set => maxHealthPoint = value;
|
||||
}
|
||||
public bool IsAlive { get; private set; }
|
||||
|
||||
bool IHealth.IsAlive => isAlive;
|
||||
public override void Initialize(IEntity _entity)
|
||||
{
|
||||
base.Initialize(_entity);
|
||||
_entity.Set<IHealth>(this);
|
||||
_entity.Set(this);
|
||||
}
|
||||
|
||||
public override void OnAwake()
|
||||
{
|
||||
@@ -45,18 +85,18 @@ namespace BITKit.Entities
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
isAlive = healthPoint >= 0;
|
||||
OnSetAlive(isAlive);
|
||||
OnHealthPointChanged(0, healthPoint);
|
||||
IsAlive = healthPoint >= 0;
|
||||
OnSetAliveInternal(IsAlive);
|
||||
OnHealthPointChangedInternal(0, healthPoint);
|
||||
}
|
||||
|
||||
private void OnHealthPointChanged(int old, int newHP)
|
||||
private void OnHealthPointChangedInternal(int old, int newHP)
|
||||
{
|
||||
healthPoint = newHP;
|
||||
var _isAlive = newHP >= 0;
|
||||
if (_isAlive != isAlive)
|
||||
if (_isAlive != IsAlive)
|
||||
{
|
||||
OnSetAlive(isAlive = _isAlive);
|
||||
OnSetAliveInternal(IsAlive = _isAlive);
|
||||
}
|
||||
|
||||
//entity.Invoke<int>(_onSetHP, newHP);
|
||||
@@ -70,9 +110,11 @@ namespace BITKit.Entities
|
||||
{
|
||||
x.OnSetHP(newHP);
|
||||
}
|
||||
|
||||
OnSetHealthPoint?.Invoke(newHP);
|
||||
}
|
||||
|
||||
private void OnSetAlive(bool alive)
|
||||
private void OnSetAliveInternal(bool alive)
|
||||
{
|
||||
foreach (var x in entity.GetCallbacks<IHealthCallback>())
|
||||
{
|
||||
@@ -87,11 +129,12 @@ namespace BITKit.Entities
|
||||
//entity.Invoke<bool>(_onSetAlive, alive);
|
||||
//entity.Set<bool>(_isAlive, alive);
|
||||
onSetAlive.Invoke(alive);
|
||||
OnSetAlive?.Invoke(alive);
|
||||
}
|
||||
|
||||
private void AddHP(int hp)
|
||||
{
|
||||
OnHealthPointChanged(healthPoint, healthPoint += hp);
|
||||
OnHealthPointChangedInternal(healthPoint, healthPoint += hp);
|
||||
}
|
||||
|
||||
private void OnDamage(DamageMessage damageMessage)
|
||||
@@ -104,14 +147,14 @@ namespace BITKit.Entities
|
||||
public void SetAlive()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
OnHealthPointChanged(-1, 100);
|
||||
OnHealthPointChangedInternal(-1, 100);
|
||||
}
|
||||
|
||||
[BIT]
|
||||
public void SetDead()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
OnHealthPointChanged(100, -1);
|
||||
OnHealthPointChangedInternal(100, -1);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class GetDamageComponent : EntityComponent
|
||||
{
|
||||
private readonly Queue<DamageMessage> DamageMessages = new();
|
||||
[SerializeField] private UnityEvent<DamageMessage> onGetDamage;
|
||||
[SerializeField, SerializeReference, SubclassSelector]
|
||||
private IDamageCallback[] callbacks;
|
||||
public override void OnAwake()
|
||||
{
|
||||
entity.AddListener<DamageMessage>(OnGetDamage);
|
||||
}
|
||||
private void OnGetDamage(DamageMessage obj)
|
||||
{
|
||||
if (obj.target != entity) return;
|
||||
DamageMessages.Enqueue(obj);
|
||||
onGetDamage?.Invoke(obj);
|
||||
foreach (var x in callbacks)
|
||||
{
|
||||
x.OnGetDamage(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca71093573b5a784e8d109ef07261988
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user