GodotComponentTest/components/TaskQueueComponent.cs

82 lines
2.0 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
using Godot;
public class TaskQueueComponent : Component
{
[Signal]
private delegate void StartInteraction(Entity entity, Entity targetEntity);
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
{
var currentTask = Queue.Peek();
2023-11-10 20:22:55 +01:00
if (currentTask.PerformTask(entity, this, delta))
Queue.Dequeue();
else
break;
} while (Queue.Count > 0);
}
public abstract class Task : Object
{
/// <summary>
/// Executes a Task on the specified entity.
/// </summary>
/// <returns>true when the Task is complete, false otherwise</returns>
2023-11-10 20:22:55 +01:00
public abstract bool PerformTask(Entity entity, TaskQueueComponent taskQueueComponent, float delta);
}
/// <summary>
/// Makes an entity move towards a target (translation and or orientation).
/// </summary>
public class NavigationTask : Task
{
public NavigationPoint NavigationPoint;
public bool PlanningComplete = false;
2023-02-12 21:10:28 +01:00
public NavigationTask(NavigationPoint navigationPoint)
{
2023-02-12 21:10:28 +01:00
NavigationPoint = navigationPoint;
}
2023-11-10 20:22:55 +01:00
public override bool PerformTask(Entity entity, TaskQueueComponent taskQueueComponent, float delta)
2023-02-12 21:10:28 +01:00
{
return NavigationPoint.IsReached(entity.GlobalTransform);
}
}
public class InteractionTask : Task
{
public Entity TargetEntity;
2023-02-12 21:10:28 +01:00
public InteractionTask(Entity entity)
{
TargetEntity = entity;
}
2023-11-10 20:22:55 +01:00
public override bool PerformTask(Entity entity, TaskQueueComponent taskQueueComponent, float delta)
2023-02-12 21:10:28 +01:00
{
2023-11-10 20:22:55 +01:00
taskQueueComponent.EmitSignal("StartInteraction", entity, TargetEntity);
2023-02-12 21:10:28 +01:00
return true;
}
}
}