GodotComponentTest/components/TaskQueueComponent.cs

91 lines
2.2 KiB
C#
Raw Normal View History

using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
public class TaskQueueComponent : Component
{
2023-02-12 21:10:28 +01:00
public abstract class Task
{
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)
{
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();
}
2023-02-12 21:10:28 +01:00
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();
2023-05-01 18:37:35 +02:00
if (currentTask is NavigationTask)
{
NavigationTask navigationTask = (NavigationTask)currentTask;
if (navigationTask != null && navigationTask.NavigationPoint.Flags ==
NavigationComponent.NavigationPoint.NavigationFlags.Orientation)
{
GD.Print("Current task is orientation task!");
}
}
2023-02-12 21:10:28 +01:00
}
}
}