GodotComponentTest/components/TaskQueueComponent.cs

73 lines
2.0 KiB
C#

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 {
Task currentTask = Queue.Peek();
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>
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;
public NavigationTask(NavigationPoint navigationPoint) {
NavigationPoint = navigationPoint;
}
public override bool PerformTask(Entity entity, TaskQueueComponent taskQueueComponent, 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, TaskQueueComponent taskQueueComponent, float delta) {
taskQueueComponent.EmitSignal("StartInteraction", entity, TargetEntity);
return true;
}
}
}