#if GRAPH_DESIGNER
/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.Runtime.Tasks
{
using Opsive.GraphDesigner.Runtime;
using Unity.Entities;
using UnityEngine;
///
/// Base class for a boilerplate ECS task.
///
public abstract class ECSTask : ITreeLogicNode, IAuthoringTask where TBufferElement : unmanaged, IBufferElementData
{
[Tooltip("The index of the node.")]
[SerializeField] ushort m_Index;
[Tooltip("The parent index of the node. ushort.MaxValue indicates no parent.")]
[SerializeField] ushort m_ParentIndex;
[Tooltip("The sibling index of the node. ushort.MaxValue indicates no sibling.")]
[SerializeField] ushort m_SiblingIndex;
///
/// The type of flag that should be enabled when the task is running.
///
public abstract ComponentType Flag { get; }
///
/// The system type that the component uses.
///
public System.Type SystemType => typeof(TSystem);
public ushort Index
{
get => m_Index;
set => m_Index = value;
}
public ushort ParentIndex
{
get => m_ParentIndex;
set => m_ParentIndex = value;
}
public ushort SiblingIndex
{
get => m_SiblingIndex;
set => m_SiblingIndex = value;
}
public ushort RuntimeIndex { get; set; }
///
/// Resets the node values back to their default.
///
public virtual void Reset() { }
///
/// Adds the IBufferElementData to the entity.
///
/// The world that the entity exists in.
/// The entity that the IBufferElementData should be assigned to.
/// The GameObject that the entity is attached to.
/// The index of the element within the buffer.
public virtual int AddBufferElement(World world, Entity entity, GameObject gameObject)
{
DynamicBuffer buffer;
if (world.EntityManager.HasBuffer(entity)) {
buffer = world.EntityManager.GetBuffer(entity);
} else {
buffer = world.EntityManager.AddBuffer(entity);
}
buffer.Add(GetBufferElement());
return buffer.Length - 1;
}
///
/// Returns a new TBufferElement for use by the system.
///
/// A new TBufferElement for use by the system.
public abstract TBufferElement GetBufferElement();
///
/// Clears the IBufferElementData from the entity.
///
/// The world that the entity exists in.
/// The entity that the IBufferElementData should be cleared from.
public void ClearBufferElement(World world, Entity entity)
{
DynamicBuffer buffer;
if (world.EntityManager.HasBuffer(entity)) {
buffer = world.EntityManager.GetBuffer(entity);
buffer.Clear();
}
}
}
///
/// Base class for an ECS action task.
///
public abstract class ECSActionTask : ECSTask, IAction where TComponent : unmanaged, IBufferElementData { }
///
/// Base class for an ECS composite task.
///
public abstract class ECSCompositeTask : ECSTask, IComposite, IParentNode where TComponent : unmanaged, IBufferElementData
{
///
/// The maximum number of children the node can have.
///
public virtual int MaxChildCount { get => ushort.MaxValue; }
}
///
/// Base class for an ECS conditional task.
///
public abstract class ECSConditionalTask : ECSTask, IConditional where TComponent : unmanaged, IBufferElementData { }
///
/// Base class for an ECS decorator task.
///
public abstract class ECSDecoratorTask : ECSTask, IDecorator, IParentNode where TComponent : unmanaged, IBufferElementData
{
///
/// The maximum number of children the node can have.
///
public int MaxChildCount { get => 1; }
}
}
#endif