GodotComponentTest/components/TaskQueueComponent.cs

92 lines
2.1 KiB
C#
Raw Normal View History

using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using GodotComponentTest.entities;
using Object = Godot.Object;
public class TaskQueueComponent : Component
{
[Signal]
delegate void StartInteraction(Entity entity, Entity targetEntity);
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);
}
public class NavigationTask : Task
{
2023-02-12 21:10:28 +01:00
public NavigationComponent.NavigationPoint NavigationPoint;
public NavigationTask(NavigationComponent.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;
}
}
public Queue<Task> Queue;
public TaskQueueComponent()
{
Queue = new Queue<Task>();
Reset();
}
public void Reset()
{
Queue.Clear();
}
2023-02-12 21:10:28 +01:00
public void Process(Entity entity, float delta)
{
if (Queue.Count == 0)
{
return;
}
do
2023-02-12 21:10:28 +01:00
{
Task currentTask = Queue.Peek();
InteractionTask interactionTask = currentTask as InteractionTask;
if (interactionTask != null)
2023-02-12 21:10:28 +01:00
{
EmitSignal("StartInteraction", entity, interactionTask.TargetEntity);
2023-02-12 21:10:28 +01:00
}
if (currentTask.PerformTask(entity, delta))
2023-05-01 18:37:35 +02:00
{
Queue.Dequeue();
2023-05-01 18:37:35 +02:00
}
else
{
break;
}
} while (Queue.Count > 0);
2023-02-12 21:10:28 +01:00
}
}