67 lines
1.3 KiB
C#
67 lines
1.3 KiB
C#
using System;
|
|
|
|
namespace Questionable.Controller.Steps;
|
|
|
|
internal abstract class TaskExecutor<T> : ITaskExecutor where T : class, ITask
|
|
{
|
|
private long _lastProgressAt;
|
|
|
|
protected T Task { get; set; }
|
|
|
|
public InteractionProgressContext? ProgressContext { get; set; }
|
|
|
|
ITask ITaskExecutor.CurrentTask => Task;
|
|
|
|
public virtual bool WasInterrupted()
|
|
{
|
|
InteractionProgressContext progressContext = ProgressContext;
|
|
if (progressContext != null)
|
|
{
|
|
progressContext.Update();
|
|
return progressContext.WasInterrupted();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public virtual bool HasTimedOut(float timeoutSeconds)
|
|
{
|
|
if (ProgressContext == null)
|
|
{
|
|
return false;
|
|
}
|
|
return Environment.TickCount64 - _lastProgressAt > (long)(timeoutSeconds * 1000f);
|
|
}
|
|
|
|
public virtual void ResetTimeout()
|
|
{
|
|
ResetProgressTimer();
|
|
}
|
|
|
|
protected void ResetProgressTimer()
|
|
{
|
|
_lastProgressAt = Environment.TickCount64;
|
|
}
|
|
|
|
public Type GetTaskType()
|
|
{
|
|
return typeof(T);
|
|
}
|
|
|
|
protected abstract bool Start();
|
|
|
|
public bool Start(ITask task)
|
|
{
|
|
if (task is T task2)
|
|
{
|
|
Task = task2;
|
|
ProgressContext = null;
|
|
_lastProgressAt = Environment.TickCount64;
|
|
return Start();
|
|
}
|
|
throw new TaskException($"Unable to cast {task.GetType()} to {typeof(T)}");
|
|
}
|
|
|
|
public abstract ETaskResult Update();
|
|
|
|
public abstract bool ShouldInterruptOnDamage();
|
|
}
|