This commit is contained in:
CortexCore
2023-11-06 01:17:23 +08:00
parent bd40165ade
commit 5446067f91
114 changed files with 2023 additions and 414 deletions

View File

@@ -1,53 +1,50 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using BITKit.Entities;
using UnityEngine;
namespace BITKit
{
public class AutoRespawnComponent : EntityComponent,IAction
public class AutoRespawnBehavior : EntityBehavior,IAction
{
[SerializeField] private IntervalUpdate respawnInterval;
private bool requestRespawn;
[Inject] private IHealth _health;
private int _initialHp;
private CancellationTokenSource _cancellationTokenSource;
public override void OnAwake()
{
_initialHp=_health.HealthPoint;
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHP;
}
public override void OnUpdate(float deltaTime)
{
if (requestRespawn && respawnInterval.AllowUpdate)
{
requestRespawn = false;
Execute();
}
}
public void OnSetAlive(bool alive)
private async void OnSetAlive(bool alive)
{
if (alive)
{
requestRespawn = false;
_cancellationTokenSource?.Cancel();
}
else
{
respawnInterval.Reset();
requestRespawn = true;
_cancellationTokenSource?.Cancel();
_cancellationTokenSource = new CancellationTokenSource();
try
{
await Task.Delay(TimeSpan.FromSeconds(respawnInterval.Interval), _cancellationTokenSource.Token);
destroyCancellationToken.ThrowIfCancellationRequested();
Execute();
}
catch(MissingReferenceException){}
catch (OperationCanceledException){}
}
}
public void OnSetHP(int hp)
{
}
public void Execute()
{
if (TryGetComponent<IHealth>(out var health))
{
health.HealthPoint = 100;
}
if (_health.IsAlive is false)
_health.HealthPoint = _initialHp;
}
}