Throwing Knife Added
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -10,44 +13,37 @@ namespace BITKit
|
||||
[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 (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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user