using System; using System.Collections; using System.Collections.Generic; using BITKit; using BITKit.Entities; using UnityEngine; namespace BITFALL.Entities { public class AutoHealBehavior : EntityBehavior,IDamageCallback { [SerializeField,ReadOnly] private bool _allowHeal; [SerializeField,ReadOnly] private bool _isHealing; [SerializeField,ReadOnly] private int _healDelay; private readonly IntervalUpdate healDelayInterval = new(); private readonly IntervalUpdate healInterval= new(1); private readonly ValidHandle allowHeal = new(); [Inject] private IHealth _health; [Inject(true)] private IKnockdown _knockdown; public override void OnStart() { _health.OnSetAlive += OnSetAlive; Data.AddListener(BITConstant.Environment.sp_health_regeneration_enabled, OnRegenerationEnabled,true); UnityEntity.AddListener(OnGetDamage); if (_knockdown is not null) { _knockdown.OnKnockdown += () => allowHeal.SetDisableElements(this,true); _knockdown.OnRevive += () => allowHeal.SetDisableElements(this,false); } } public override void OnDestroyComponent() { base.OnDestroyComponent(); Data.RemoveListender(BITConstant.Environment.sp_health_regeneration_enabled, OnRegenerationEnabled); } private void Update() { allowHeal.SetElements(this,_health.IsAlive && _health.HealthPoint < _health.MaxHealthPoint); _allowHeal = allowHeal.Allow; _isHealing = healDelayInterval.AllowUpdateWithoutReset; if (!allowHeal.Allow || !healDelayInterval.AllowUpdateWithoutReset || !healInterval.AllowUpdate) return; _health.HealthPoint= Mathf.Clamp( _health.HealthPoint +1 +Data.Get(BITConstant.Environment.sp_health_regeneration_value) ,0 ,_health.MaxHealthPoint); if (_health.HealthPoint == _health.MaxHealthPoint) { allowHeal.RemoveElement(this); } } public void OnSetAlive(bool alive) { allowHeal.SetDisableElements(this,alive is false); } public void OnGetDamage(DamageMessage message) { healDelayInterval.Interval =_healDelay = Data.Get(BITConstant.Environment.sp_health_regeneration_delay); healDelayInterval.Reset(); } private void OnRegenerationEnabled(bool _enabled) { allowHeal.SetDisableElements(this,_enabled is false); } } }