2023-02-12 16:30:56 +01:00
|
|
|
using System.Collections.Generic;
|
2023-11-10 11:11:08 +01:00
|
|
|
using Godot;
|
2023-02-12 16:30:56 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public class TaskQueueComponent : Component {
|
2023-06-23 15:39:07 +02:00
|
|
|
[Signal]
|
2023-11-10 11:11:08 +01:00
|
|
|
private delegate void StartInteraction(Entity entity, Entity targetEntity);
|
2023-11-10 12:22:17 +01:00
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
public Queue<Task> Queue;
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public TaskQueueComponent() {
|
2023-11-10 11:11:08 +01:00
|
|
|
Queue = new Queue<Task>();
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void Reset() {
|
2023-11-10 11:11:08 +01:00
|
|
|
Queue.Clear();
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void Process(Entity entity, float delta) {
|
|
|
|
if (Queue.Count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
do {
|
|
|
|
Task currentTask = Queue.Peek();
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (currentTask.PerformTask(entity, this, delta)) {
|
2023-11-10 11:11:08 +01:00
|
|
|
Queue.Dequeue();
|
2023-11-18 22:32:57 +01:00
|
|
|
} else {
|
2023-11-10 11:11:08 +01:00
|
|
|
break;
|
2023-11-18 22:32:57 +01:00
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
} while (Queue.Count > 0);
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public abstract class Task : Object {
|
2023-06-23 15:39:07 +02:00
|
|
|
/// <summary>
|
2023-11-10 11:11:08 +01:00
|
|
|
/// Executes a Task on the specified entity.
|
2023-06-23 15:39:07 +02:00
|
|
|
/// </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);
|
2023-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
/// <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 {
|
2023-11-10 12:22:17 +01:00
|
|
|
public NavigationPoint NavigationPoint;
|
2023-07-07 17:27:05 +02:00
|
|
|
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-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
|
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-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public class InteractionTask : Task {
|
2023-02-12 16:30:56 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
}
|