GodotComponentTest/components/TaskQueueComponent.cs

82 lines
1.8 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
public class TaskQueueComponent : Component
{
public abstract class Task
{
public abstract bool PerformTask(Entity entity, float delta);
}
public class NavigationTask : Task
{
public NavigationComponent.NavigationPoint NavigationPoint;
public NavigationTask(NavigationComponent.NavigationPoint navigationPoint)
{
NavigationPoint = navigationPoint;
}
public override bool PerformTask(Entity entity, 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, float delta)
{
GD.Print("Interaction of " + entity + " with " + TargetEntity);
if (TargetEntity is Chest)
{
Chest chest = (Chest)TargetEntity;
chest.OnInteract();
}
return true;
}
}
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;
}
Task currentTask = Queue.Peek();
while (currentTask.PerformTask(entity, delta))
{
Queue.Dequeue();
if (Queue.Count == 0)
{
break;
}
currentTask = Queue.Peek();
}
}
}