93 lines
2.2 KiB
C#
93 lines
2.2 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using GodotComponentTest.entities;
|
|
using Object = Godot.Object;
|
|
|
|
public class TaskQueueComponent : Component
|
|
{
|
|
[Signal]
|
|
delegate void StartInteraction(Entity entity, Entity targetEntity);
|
|
|
|
public abstract class Task : Object
|
|
{
|
|
/// <summary>
|
|
/// Executes a Task on the specified entity.
|
|
/// </summary>
|
|
/// <returns>true when the Task is complete, false otherwise</returns>
|
|
public abstract bool PerformTask(Entity entity, float delta);
|
|
}
|
|
|
|
public class NavigationTask : Task
|
|
{
|
|
public NavigationComponent.NavigationPoint NavigationPoint;
|
|
public bool PlanningComplete = false;
|
|
|
|
public NavigationTask(NavigationComponent.NavigationPoint navigationPoint)
|
|
{
|
|
NavigationPoint = navigationPoint;
|
|
}
|
|
|
|
public override bool PerformTask(Entity entity, float delta)
|
|
{
|
|
return NavigationPoint.IsReached(entity.GlobalTransform);
|
|
}
|
|
}
|
|
|
|
public class InteractionTask : Task
|
|
{
|
|
public Entity TargetEntity;
|
|
|
|
public InteractionTask(Entity entity)
|
|
{
|
|
TargetEntity = entity;
|
|
}
|
|
|
|
public override bool PerformTask(Entity entity, float delta)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public Queue<Task> Queue;
|
|
|
|
public TaskQueueComponent()
|
|
{
|
|
Queue = new Queue<Task>();
|
|
|
|
Reset();
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Queue.Clear();
|
|
}
|
|
|
|
public void Process(Entity entity, float delta)
|
|
{
|
|
if (Queue.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
do
|
|
{
|
|
Task currentTask = Queue.Peek();
|
|
InteractionTask interactionTask = currentTask as InteractionTask;
|
|
if (interactionTask != null)
|
|
{
|
|
EmitSignal("StartInteraction", entity, interactionTask.TargetEntity);
|
|
}
|
|
|
|
if (currentTask.PerformTask(entity, delta))
|
|
{
|
|
Queue.Dequeue();
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
} while (Queue.Count > 0);
|
|
}
|
|
} |