52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BITKit.Entities;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit
|
|
{
|
|
public class AutoHealBehavior : EntityBehavior,IDamageCallback
|
|
{
|
|
[SerializeField] private IntervalUpdate healDelayInterval;
|
|
[SerializeField] private IntervalUpdate healInterval;
|
|
[SerializeField] private int healIncrement;
|
|
private readonly ValidHandle allowHeal = new();
|
|
[Inject]
|
|
private IHealth _health;
|
|
public override void OnStart()
|
|
{
|
|
_health = UnityEntity.Get<IHealth>();
|
|
|
|
_health.OnSetAlive += OnSetAlive;
|
|
_health.OnSetHealthPoint += OnSetHP;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
}
|