NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/AI/Task/BTWait.cs

86 lines
2.3 KiB
C#

using BehaviorDesigner.Runtime.Tasks;
using Gameplay.AI;
using Gameplay.BT.Variables;
using Gameplay.Common;
using Gameplay.Unit;
using UnityEngine;
namespace Code.Scripts.Gameplay.BT.Task
{
[TaskIcon("{SkinColor}WaitIcon.png")]
public class BTWait : Action
{
public SharedBattleInfo SharedBattleInfo;
private float waitTime;
private float waitDuration;
private float startTime;
private float pauseTime;
private GameUnit _owner;
public override void OnStart()
{
// Remember the start time.
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerId);
startTime = Time.time;
waitTime = SharedBattleInfo.Value.PatrolInfos[SharedBattleInfo.Value.PartolPointIndex].waitTime;
waitDuration = waitTime;
}
public override TaskStatus OnUpdate()
{
// The task is done waiting if the time waitDuration has elapsed since the task was started.
if (startTime + waitDuration < Time.time)
{
CalculatePatrolIndex();
return TaskStatus.Success;
}
return AIUtils.IsNeedStop(_owner)
? TaskStatus.Failure
: TaskStatus.Running;
}
public override void OnPause(bool paused)
{
if (paused)
{
// Remember the time that the behavior was paused.
pauseTime = Time.time;
}
else
{
// Add the difference between Time.time and pauseTime to figure out a new start time.
startTime += (Time.time - pauseTime);
}
}
private void CalculatePatrolIndex()
{
if (SharedBattleInfo.Value.PartolPointIndex == SharedBattleInfo.Value.PatrolInfos.Count - 1)
{
SharedBattleInfo.Value.PartolPointIndex = 0;
}
else
{
SharedBattleInfo.Value.PartolPointIndex++;
}
}
public override void OnReset()
{
// Reset the public properties back to their original values
waitTime = SharedBattleInfo.Value.PatrolInfos[SharedBattleInfo.Value.PartolPointIndex].waitTime;
}
}
}