BITKit/Packages/Runtime~/Unity/Scripts/Entity/Components/Health/AutoHealComponent.cs

55 lines
1.3 KiB
C#
Raw Normal View History

2023-08-11 23:57:37 +08:00
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();
}
}
}