GodotComponentTest/components/TaskQueueComponent.cs

73 lines
2.0 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using Godot;
2023-11-18 22:32:57 +01:00
public class TaskQueueComponent : Component {
[Signal]
private delegate void StartInteraction(Entity entity, Entity targetEntity);
public Queue<Task> Queue;
2023-11-18 22:32:57 +01:00
public TaskQueueComponent() {
Queue = new Queue<Task>();
Reset();
}
2023-11-18 22:32:57 +01:00
public void Reset() {
Queue.Clear();
}
2023-11-18 22:32:57 +01:00
public void Process(Entity entity, float delta) {
if (Queue.Count == 0) {
return;
}
2023-11-18 22:32:57 +01:00
do {
Task currentTask = Queue.Peek();
2023-11-18 22:32:57 +01:00
if (currentTask.PerformTask(entity, this, delta)) {
Queue.Dequeue();
2023-11-18 22:32:57 +01:00
} else {
break;
2023-11-18 22:32:57 +01:00
}
} while (Queue.Count > 0);
}
2023-11-18 22:32:57 +01:00
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>
2023-11-18 22:32:57 +01:00
public class NavigationTask : Task {
public NavigationPoint NavigationPoint;
public bool PlanningComplete = false;
2023-02-12 21:10:28 +01:00
2023-11-18 22:32:57 +01:00
public NavigationTask(NavigationPoint navigationPoint) {
2023-02-12 21:10:28 +01:00
NavigationPoint = navigationPoint;
}
2023-11-18 22:32:57 +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);
}
}
2023-11-18 22:32:57 +01:00
public class InteractionTask : Task {
public Entity TargetEntity;
2023-02-12 21:10:28 +01:00
2023-11-18 22:32:57 +01:00
public InteractionTask(Entity entity) {
2023-02-12 21:10:28 +01:00
TargetEntity = entity;
}
2023-11-18 22:32:57 +01:00
public override bool PerformTask(Entity entity, TaskQueueComponent taskQueueComponent, float delta) {
2023-11-10 20:22:55 +01:00
taskQueueComponent.EmitSignal("StartInteraction", entity, TargetEntity);
2023-02-12 21:10:28 +01:00
return true;
}
}
}