GodotComponentTest/components/TaskQueueComponent.cs

82 lines
2.0 KiB
C#
Raw 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();
var 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);
}
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-02-12 21:10:28 +01:00
public abstract bool PerformTask(Entity entity, 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-02-12 21:10:28 +01:00
public override bool PerformTask(Entity entity, float delta)
{
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;
}
public override bool PerformTask(Entity entity, float delta)
{
return true;
}
}
}