移除 bd
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Systems;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// A TaskObject implementation of the Composite task.
|
||||
/// </summary>
|
||||
[NodeIcon("3afb3814c40717440b175b6fde4e73c2", "7fb12c74939f50b41b1679eb8f9e79ab")]
|
||||
public abstract class CompositeNode : Task, ITreeLogicNode, IParentNode, IComposite, ITaskObjectParentNode
|
||||
{
|
||||
[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;
|
||||
|
||||
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; }
|
||||
|
||||
public virtual int MaxChildCount { get => int.MaxValue; }
|
||||
public virtual ushort NextChildIndex { get => (ushort)(RuntimeIndex + 1); }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 03dee97f70b381e4bb7a230cca3e5f8f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,225 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.BehaviorDesigner.Runtime.Utility;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the parallel task.
|
||||
/// </summary>
|
||||
[NodeIcon("f612c025389b22640b1b6df88f4502e7", "8a4a401bcfb527a48a08351efaf92e14")]
|
||||
[Opsive.Shared.Utility.Description("Similar to the sequence task, the parallel task will run each child task until a child task returns failure. " +
|
||||
"The parallel task will run all of its children tasks simultaneously versus running each task one at a time. " +
|
||||
"Like the sequence class, the parallel task will return success once all of its children tasks have return success. " +
|
||||
"If one tasks returns failure the parallel task will end all of the child tasks and return failure.")]
|
||||
public class Parallel : ECSCompositeTask<ParallelTaskSystem, ParallelComponent>, IParentNode, IParallelNode
|
||||
{
|
||||
public override ComponentType Flag { get => typeof(ParallelFlag); }
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
var index = base.AddBufferElement(world, entity, gameObject);
|
||||
ComponentUtility.AddInterruptComponents(world.EntityManager, entity);
|
||||
return index;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override ParallelComponent GetBufferElement()
|
||||
{
|
||||
return new ParallelComponent()
|
||||
{
|
||||
Index = RuntimeIndex
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the Parallel class.
|
||||
/// </summary>
|
||||
public struct ParallelComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
[SerializeField] ushort m_Index;
|
||||
|
||||
public ushort Index { get => m_Index; set => m_Index = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a Parallel node is active.
|
||||
/// </summary>
|
||||
public struct ParallelFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the Parallel logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct ParallelTaskSystem : ISystem
|
||||
{
|
||||
private EntityQuery m_Query;
|
||||
private JobHandle m_Dependency;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the query.
|
||||
/// </summary>
|
||||
/// <param name="state">THe current SystemState.</param>
|
||||
private void OnCreate(ref SystemState state)
|
||||
{
|
||||
m_Query = SystemAPI.QueryBuilder().WithAllRW<BranchComponent>().WithAllRW<TaskComponent>().WithAllRW<ParallelComponent>().WithAll<ParallelFlag, EvaluateFlag>().Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the job.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
m_Dependency.Complete();
|
||||
|
||||
var ecb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
|
||||
state.Dependency = new ParallelJob()
|
||||
{
|
||||
EntityCommandBuffer = ecb.AsParallelWriter()
|
||||
}.ScheduleParallel(m_Query, state.Dependency);
|
||||
m_Dependency = state.Dependency;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Job which executes the task logic.
|
||||
/// </summary>
|
||||
[BurstCompile]
|
||||
private partial struct ParallelJob : IJobEntity
|
||||
{
|
||||
[Tooltip("CommandBuffer which sets the component data.")]
|
||||
public EntityCommandBuffer.ParallelWriter EntityCommandBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Executes the parallel logic.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity that is being acted upon.</param>
|
||||
/// <param name="entityIndex">The index of the entity.</param>
|
||||
/// <param name="parallelComponents">An array of ParallelComponents.</param>
|
||||
/// <param name="taskComponents">An array of TaskComponents.</param>
|
||||
/// <param name="branchComponents">An array of BranchComponents.</param>
|
||||
[BurstCompile]
|
||||
public void Execute(Entity entity, [EntityIndexInQuery] int entityIndex, ref DynamicBuffer<ParallelComponent> parallelComponents, ref DynamicBuffer<TaskComponent> taskComponents, ref DynamicBuffer<BranchComponent> branchComponents)
|
||||
{
|
||||
for (int i = 0; i < parallelComponents.Length; ++i) {
|
||||
var parallelComponent = parallelComponents[i];
|
||||
var taskComponent = taskComponents[parallelComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ushort childIndex;
|
||||
TaskComponent childTaskComponent;
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
taskComponents[taskComponent.Index] = taskComponent;
|
||||
|
||||
childIndex = (ushort)(parallelComponent.Index + 1);
|
||||
while (childIndex != ushort.MaxValue) {
|
||||
childTaskComponent = taskComponents[childIndex];
|
||||
childTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponents[childIndex] = childTaskComponent;
|
||||
|
||||
var childBranchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
childBranchComponent.NextIndex = childTaskComponent.Index;
|
||||
branchComponents[childTaskComponent.BranchIndex] = childBranchComponent;
|
||||
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var childrenFailure = false;
|
||||
var childrenRunning = false;
|
||||
childIndex = (ushort)(parallelComponent.Index + 1);
|
||||
while (childIndex != ushort.MaxValue) {
|
||||
childTaskComponent = taskComponents[childIndex];
|
||||
if (childTaskComponent.Status == TaskStatus.Queued || childTaskComponent.Status == TaskStatus.Running) {
|
||||
childrenRunning = true;
|
||||
} else if (childTaskComponent.Status == TaskStatus.Failure) {
|
||||
childrenFailure = true;
|
||||
|
||||
var childBranchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
childBranchComponent.NextIndex = ushort.MaxValue;
|
||||
branchComponents[childTaskComponent.BranchIndex] = childBranchComponent;
|
||||
break;
|
||||
} else if (childTaskComponent.Status == TaskStatus.Success) {
|
||||
var childBranchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
if (childBranchComponent.ActiveIndex != ushort.MaxValue) {
|
||||
childBranchComponent.NextIndex = ushort.MaxValue;
|
||||
branchComponents[childTaskComponent.BranchIndex] = childBranchComponent;
|
||||
}
|
||||
}
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
|
||||
// If a single child fails then all tasks should be stopped.
|
||||
if (childrenFailure) {
|
||||
var maxChildIndex = taskComponent.Index + TraversalUtility.GetChildCount(taskComponent.Index, ref taskComponents);
|
||||
for (ushort j = (ushort)(taskComponent.Index + 1); j <= maxChildIndex; ++j) {
|
||||
childTaskComponent = taskComponents[j];
|
||||
if (childTaskComponent.Status == TaskStatus.Running || childTaskComponent.Status == TaskStatus.Queued) {
|
||||
childTaskComponent.Status = TaskStatus.Failure;
|
||||
taskComponents[j] = childTaskComponent;
|
||||
|
||||
branchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
EntityCommandBuffer.SetComponentEnabled<InterruptedFlag>(entityIndex, entity, true);
|
||||
if (branchComponent.ActiveIndex == childTaskComponent.Index) {
|
||||
branchComponent.NextIndex = ushort.MaxValue;
|
||||
branchComponents[childTaskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
taskComponent.Status = TaskStatus.Failure;
|
||||
taskComponents[taskComponent.Index] = taskComponent;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (childrenRunning) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// No more children are running. Resume the parent task.
|
||||
taskComponent.Status = TaskStatus.Success;
|
||||
taskComponents[taskComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 121e856fe6b49d648a50fde8ee6923ed
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,219 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.BehaviorDesigner.Runtime.Utility;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.Jobs;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the parallel selector task.
|
||||
/// </summary>
|
||||
[NodeIcon("d47aff1a00bcc6d4da8ca0df32ed8415", "108591b5d7a6bd94383d16a62cb3b4a7")]
|
||||
[Opsive.Shared.Utility.Description("Similar to the selector task, the parallel selector task will return success as soon as a child task returns success. " +
|
||||
"The parallel task will run all of its children tasks simultaneously versus running each task one at a time. " +
|
||||
"If one tasks returns success the parallel selector task will end all of the child tasks and return success. " +
|
||||
"If every child task returns failure then the parallel selector task will return failure.")]
|
||||
public class ParallelSelector : ECSCompositeTask<ParallelSelectorTaskSystem, ParallelSelectorComponent>, IParentNode, IParallelNode
|
||||
{
|
||||
/// <summary>
|
||||
/// The type of tag that should be enabled when the task is running.
|
||||
/// </summary>
|
||||
public override ComponentType Flag { get => typeof(ParallelSelectorFlag); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override ParallelSelectorComponent GetBufferElement()
|
||||
{
|
||||
return new ParallelSelectorComponent() {
|
||||
Index = RuntimeIndex,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
var index = base.AddBufferElement(world, entity, gameObject);
|
||||
ComponentUtility.AddInterruptComponents(world.EntityManager, entity);
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the ParallelSelector class.
|
||||
/// </summary>
|
||||
public struct ParallelSelectorComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
[SerializeField] ushort m_Index;
|
||||
|
||||
public ushort Index { get => m_Index; set => m_Index = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a ParallelSelector node is active.
|
||||
/// </summary>
|
||||
public struct ParallelSelectorFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the ParallelSelector logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct ParallelSelectorTaskSystem : ISystem
|
||||
{
|
||||
private EntityQuery m_Query;
|
||||
private JobHandle m_Dependency;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the query.
|
||||
/// </summary>
|
||||
/// <param name="state">THe current SystemState.</param>
|
||||
private void OnCreate(ref SystemState state)
|
||||
{
|
||||
m_Query = SystemAPI.QueryBuilder().WithAllRW<BranchComponent>().WithAllRW<TaskComponent>().WithAllRW<ParallelSelectorComponent>().WithAll<ParallelSelectorFlag, EvaluateFlag>().Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the job.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
m_Dependency.Complete();
|
||||
|
||||
var ecb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
|
||||
state.Dependency = new ParallelSelectorJob()
|
||||
{
|
||||
EntityCommandBuffer = ecb.AsParallelWriter()
|
||||
}.ScheduleParallel(m_Query, state.Dependency);
|
||||
m_Dependency = state.Dependency;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Job which executes the task logic.
|
||||
/// </summary>
|
||||
[BurstCompile]
|
||||
private partial struct ParallelSelectorJob : IJobEntity
|
||||
{
|
||||
[Tooltip("CommandBuffer which sets the component data.")]
|
||||
public EntityCommandBuffer.ParallelWriter EntityCommandBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Executes the parallel selector logic.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity that is being acted upon.</param>
|
||||
/// <param name="entityIndex">The index of the entity.</param>
|
||||
/// <param name="branchComponents">An array of BranchComponents.</param>
|
||||
/// <param name="taskComponents">An array of TaskComponents.</param>
|
||||
/// <param name="parallelSelectorComponents">An array of ParallelSelectorComponents.</param>
|
||||
[BurstCompile]
|
||||
public void Execute(Entity entity, [EntityIndexInQuery] int entityIndex, ref DynamicBuffer<BranchComponent> branchComponents, ref DynamicBuffer<TaskComponent> taskComponents, ref DynamicBuffer<ParallelSelectorComponent> parallelSelectorComponents)
|
||||
{
|
||||
for (int i = 0; i < parallelSelectorComponents.Length; ++i) {
|
||||
var parallelSelectorComponent = parallelSelectorComponents[i];
|
||||
var taskComponent = taskComponents[parallelSelectorComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ushort childIndex;
|
||||
TaskComponent childTaskComponent;
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
taskComponents[taskComponent.Index] = taskComponent;
|
||||
|
||||
childIndex = (ushort)(parallelSelectorComponent.Index + 1);
|
||||
while (childIndex != ushort.MaxValue) {
|
||||
childTaskComponent = taskComponents[childIndex];
|
||||
childTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponents[childIndex] = childTaskComponent;
|
||||
|
||||
var childBranchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
childBranchComponent.NextIndex = childTaskComponent.Index;
|
||||
branchComponents[childTaskComponent.BranchIndex] = childBranchComponent;
|
||||
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var childSuccess = false;
|
||||
var childrenRunning = false;
|
||||
childIndex = (ushort)(parallelSelectorComponent.Index + 1);
|
||||
while (childIndex != ushort.MaxValue) {
|
||||
childTaskComponent = taskComponents[childIndex];
|
||||
if (childTaskComponent.Status == TaskStatus.Queued || childTaskComponent.Status == TaskStatus.Running) {
|
||||
childrenRunning = true;
|
||||
} else if (childTaskComponent.Status == TaskStatus.Failure) {
|
||||
var childBranchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
if (childBranchComponent.ActiveIndex != ushort.MaxValue) {
|
||||
childBranchComponent.NextIndex = ushort.MaxValue;
|
||||
branchComponents[childTaskComponent.BranchIndex] = childBranchComponent;
|
||||
}
|
||||
} else if (childTaskComponent.Status == TaskStatus.Success) {
|
||||
childSuccess = true;
|
||||
|
||||
var childBranchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
childBranchComponent.NextIndex = ushort.MaxValue;
|
||||
branchComponents[childTaskComponent.BranchIndex] = childBranchComponent;
|
||||
break;
|
||||
}
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
|
||||
// If a single child succeeds then all tasks should be stopped.
|
||||
if (childSuccess) {
|
||||
var maxChildIndex = taskComponent.Index + TraversalUtility.GetChildCount(taskComponent.Index, ref taskComponents);
|
||||
for (ushort j = (ushort)(taskComponent.Index + 1); j <= maxChildIndex; ++j) {
|
||||
childTaskComponent = taskComponents[j];
|
||||
if (childTaskComponent.Status == TaskStatus.Running || childTaskComponent.Status == TaskStatus.Queued) {
|
||||
childTaskComponent.Status = TaskStatus.Failure;
|
||||
taskComponents[j] = childTaskComponent;
|
||||
|
||||
branchComponent = branchComponents[childTaskComponent.BranchIndex];
|
||||
EntityCommandBuffer.SetComponentEnabled<InterruptedFlag>(entityIndex, entity, true);
|
||||
if (branchComponent.ActiveIndex == childTaskComponent.Index) {
|
||||
branchComponent.NextIndex = ushort.MaxValue;
|
||||
branchComponents[childTaskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (childrenRunning) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// No more children are running. Resume the parent task.
|
||||
taskComponent.Status = TaskStatus.Success;
|
||||
taskComponents[taskComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3ba12965f09f3f4e81b66fa35df8ec3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,331 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.BehaviorDesigner.Runtime.Utility;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Opsive.Shared.Utility;
|
||||
using System;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the priority selector task.
|
||||
/// </summary>
|
||||
[NodeIcon("cea0f2b6cee06a742bb35dcc40202e8e", "744afc2640950e045961296f1d5800d7")]
|
||||
[Opsive.Shared.Utility.Description("Similar to the selector task, the priority selector task will return success as soon as a child task returns success. " +
|
||||
"Instead of running the tasks sequentially from left to right within the tree, the priority selector will ask the task what its priority is to determine the order. " +
|
||||
"The higher priority tasks have a higher chance at being run first.")]
|
||||
public class PrioritySelector : ECSCompositeTask<PrioritySelectorTaskSystem, PrioritySelectorComponent>, IParentNode, ISavableTask, ICloneable
|
||||
{
|
||||
private ushort m_ComponentIndex;
|
||||
|
||||
public override ComponentType Flag { get => typeof(PrioritySelectorFlag); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override PrioritySelectorComponent GetBufferElement()
|
||||
{
|
||||
return new PrioritySelectorComponent()
|
||||
{
|
||||
Index = RuntimeIndex,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
m_ComponentIndex = (ushort)base.AddBufferElement(world, entity, gameObject);
|
||||
return m_ComponentIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the type of reflection that should be used to save the task.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the sub-task. This is used for the task set allowing each contained task to have their own save type.</param>
|
||||
public MemberVisibility GetSaveReflectionType(int index) { return MemberVisibility.None; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current task state.
|
||||
/// </summary>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
/// <returns>The current task state.</returns>
|
||||
public object Save(World world, Entity entity)
|
||||
{
|
||||
var prioritySelectorComponents = world.EntityManager.GetBuffer<PrioritySelectorComponent>(entity);
|
||||
var prioritySelectorComponent = prioritySelectorComponents[m_ComponentIndex];
|
||||
|
||||
// Save the active child and array order.
|
||||
var saveData = new object[2];
|
||||
saveData[0] = prioritySelectorComponent.ActiveRelativeChildIndex;
|
||||
if (prioritySelectorComponent.Priorities.IsCreated) {
|
||||
saveData[1] = prioritySelectorComponent.Priorities.ToArray();
|
||||
}
|
||||
return saveData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the previous task state.
|
||||
/// </summary>
|
||||
/// <param name="saveData">The previous task state.</param>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
public void Load(object saveData, World world, Entity entity)
|
||||
{
|
||||
var prioritySelectorComponents = world.EntityManager.GetBuffer<PrioritySelectorComponent>(entity);
|
||||
var prioritySelectorComponent = prioritySelectorComponents[m_ComponentIndex];
|
||||
|
||||
// saveData is the active child and array order.
|
||||
var taskSaveData = (object[])saveData;
|
||||
prioritySelectorComponent.ActiveRelativeChildIndex = (ushort)taskSaveData[0];
|
||||
if (taskSaveData[1] != null) {
|
||||
prioritySelectorComponent.Priorities = new NativeArray<PrioritySelectorComponent.PriorityItem>((PrioritySelectorComponent.PriorityItem[])taskSaveData[1], Allocator.Persistent);
|
||||
}
|
||||
prioritySelectorComponents[m_ComponentIndex] = prioritySelectorComponent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deep clone of the component.
|
||||
/// </summary>
|
||||
/// <returns>A deep clone of the component.</returns>
|
||||
public object Clone()
|
||||
{
|
||||
var clone = Activator.CreateInstance<PrioritySelector>();
|
||||
clone.Index = Index;
|
||||
clone.ParentIndex = ParentIndex;
|
||||
clone.SiblingIndex = SiblingIndex;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the PrioritySelector class.
|
||||
/// </summary>
|
||||
public struct PrioritySelectorComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The relative index of the child that is currently active.")]
|
||||
public ushort ActiveRelativeChildIndex;
|
||||
[Tooltip("The latest priority values for each child task.")]
|
||||
public NativeArray<PriorityItem> Priorities;
|
||||
|
||||
/// <summary>
|
||||
/// Joins the task index with the priority value.
|
||||
/// </summary>
|
||||
public struct PriorityItem : IComparable<PriorityItem>
|
||||
{
|
||||
[Tooltip("The index of the task.")]
|
||||
public ushort TaskIndex;
|
||||
[Tooltip("The index of the PriorityValueComponent. A value of ushort.MaxValue indicates that there is not a corresponding PriorityValueComponent to this element.")]
|
||||
public ushort PriorityValueIndex;
|
||||
[Tooltip("The priority value.")]
|
||||
public float Value;
|
||||
|
||||
/// <summary>
|
||||
/// Compares the current PriorityItem to the other PriorityItem.
|
||||
/// </summary>
|
||||
/// <param name="other">The other PriorityItem.</param>
|
||||
/// <returns>The comparison between the current PriorityItem and the other PriorityItem.</returns>
|
||||
public int CompareTo(PriorityItem other)
|
||||
{
|
||||
// The higher the value the lower the item is in the array.
|
||||
return other.Value.CompareTo(Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DOTS structure that contains the most recently priority of the task.
|
||||
/// </summary>
|
||||
public struct PriorityValueComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the task.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The current priority value. The higher the value the more likely it will be selected.")]
|
||||
public float Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a PrioritySelector node is active.
|
||||
/// </summary>
|
||||
public struct PrioritySelectorFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the PrioritySelector logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct PrioritySelectorTaskSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Updates the logic.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
var hasPriorityValueComponent = false;
|
||||
foreach (var (branchComponents, taskComponents, prioritySelectorComponents, priorityValueComponents) in
|
||||
SystemAPI.Query<DynamicBuffer<BranchComponent>, DynamicBuffer<TaskComponent>, DynamicBuffer<PrioritySelectorComponent>, DynamicBuffer<PriorityValueComponent>>().WithAll<PrioritySelectorFlag, EvaluateFlag>()) {
|
||||
|
||||
hasPriorityValueComponent = true;
|
||||
for (int i = 0; i < prioritySelectorComponents.Length; ++i) {
|
||||
var prioritySelectorComponent = prioritySelectorComponents[i];
|
||||
var taskComponent = taskComponents[prioritySelectorComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var prioritySelectorComponentsBuffer = prioritySelectorComponents;
|
||||
var taskComponentsBuffer = taskComponents;
|
||||
var branchComponentBuffer = branchComponents;
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
taskComponentsBuffer[taskComponent.Index] = taskComponent;
|
||||
|
||||
// Initialize the priority value array.
|
||||
NativeArray<PrioritySelectorComponent.PriorityItem> priorities;
|
||||
if (prioritySelectorComponent.Priorities.Length == 0) {
|
||||
var childCount = TraversalUtility.GetImmediateChildCount(ref taskComponent, ref taskComponentsBuffer);
|
||||
priorities = new NativeArray<PrioritySelectorComponent.PriorityItem>(childCount, Allocator.Persistent);
|
||||
// Match the PriorityValueComponent with the child index.
|
||||
var childIndex = (ushort)(taskComponent.Index + 1);
|
||||
for (ushort j = 0; j < childCount; ++j) {
|
||||
priorities[j] = new PrioritySelectorComponent.PriorityItem() { TaskIndex = childIndex, PriorityValueIndex = ushort.MaxValue, Value = float.MinValue };
|
||||
for (ushort k = 0; k < priorityValueComponents.Length; ++k) {
|
||||
var priorityValueComponent = priorityValueComponents[k];
|
||||
|
||||
if (priorityValueComponent.Index == childIndex) {
|
||||
var priorityItem = priorities[j];
|
||||
priorityItem.PriorityValueIndex = k;
|
||||
priorities[j] = priorityItem;
|
||||
break;
|
||||
}
|
||||
}
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
|
||||
prioritySelectorComponent.Priorities = priorities;
|
||||
}
|
||||
|
||||
// Determine the child order when the task starts.
|
||||
priorities = prioritySelectorComponent.Priorities;
|
||||
|
||||
for (ushort j = 0; j < priorities.Length; ++j) {
|
||||
var valueIndex = priorities[j].PriorityValueIndex;
|
||||
// The task may not have a matching PriorityValueComponent.
|
||||
if (valueIndex == ushort.MaxValue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var priorityItem = priorities[j];
|
||||
priorityItem.Value = priorityValueComponents[valueIndex].Value;
|
||||
priorities[j] = priorityItem;
|
||||
}
|
||||
priorities.Sort();
|
||||
prioritySelectorComponent.Priorities = priorities;
|
||||
prioritySelectorComponentsBuffer[i] = prioritySelectorComponent;
|
||||
|
||||
prioritySelectorComponent.ActiveRelativeChildIndex = 0;
|
||||
branchComponent.NextIndex = prioritySelectorComponent.Priorities[prioritySelectorComponent.ActiveRelativeChildIndex].TaskIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
|
||||
// Start the child.
|
||||
var nextChildTaskComponent = taskComponents[branchComponent.NextIndex];
|
||||
nextChildTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[branchComponent.NextIndex] = nextChildTaskComponent;
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The prioritySelector task is currently active. Check the first child.
|
||||
var childTaskComponent = taskComponents[prioritySelectorComponent.Priorities[prioritySelectorComponent.ActiveRelativeChildIndex].TaskIndex];
|
||||
if (childTaskComponent.Status == TaskStatus.Queued || childTaskComponent.Status == TaskStatus.Running) {
|
||||
// The child should keep running.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Switch to the next highest priority. If no more priority values exist the task should act as a normal selector.
|
||||
if (prioritySelectorComponent.ActiveRelativeChildIndex == prioritySelectorComponent.Priorities.Length - 1 ||
|
||||
childTaskComponent.Status == TaskStatus.Success) {
|
||||
// There are no more children or the child succeeded. The selector task should end.
|
||||
taskComponent.Status = childTaskComponent.Status;
|
||||
prioritySelectorComponent.ActiveRelativeChildIndex = 0;
|
||||
taskComponentsBuffer[prioritySelectorComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
} else {
|
||||
// The child task returned failure. Move onto the next task.
|
||||
prioritySelectorComponent.ActiveRelativeChildIndex++;
|
||||
var nextIndex = prioritySelectorComponent.Priorities[prioritySelectorComponent.ActiveRelativeChildIndex].TaskIndex;
|
||||
var nextTaskComponent = taskComponents[nextIndex];
|
||||
nextTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[nextIndex] = nextTaskComponent;
|
||||
|
||||
branchComponent.NextIndex = nextIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
prioritySelectorComponentsBuffer[i] = prioritySelectorComponent;
|
||||
}
|
||||
}
|
||||
|
||||
// Special case where the PrioritySelectorComponent has no PriorityValueComponent children.
|
||||
if (!hasPriorityValueComponent) {
|
||||
foreach (var (prioritySelectorComponents, taskComponents, branchComponents) in
|
||||
SystemAPI.Query<DynamicBuffer<PrioritySelectorComponent>, DynamicBuffer<TaskComponent>, DynamicBuffer<BranchComponent>>().WithAll<PrioritySelectorFlag, EvaluateFlag>()) {
|
||||
|
||||
for (int i = 0; i < prioritySelectorComponents.Length; ++i) {
|
||||
var prioritySelectorComponent = prioritySelectorComponents[i];
|
||||
var taskComponent = taskComponents[prioritySelectorComponent.Index];
|
||||
|
||||
// If there are no values then the selector should return failure.
|
||||
if (taskComponent.Status == TaskStatus.Queued && prioritySelectorComponent.Priorities.Length == 0) {
|
||||
taskComponent.Status = TaskStatus.Failure;
|
||||
var taskComponentsBuffer = taskComponents;
|
||||
taskComponentsBuffer[prioritySelectorComponent.Index] = taskComponent;
|
||||
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
var branchComponentBuffer = branchComponents;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The task has been destroyed.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
private void OnDestroy(ref SystemState state)
|
||||
{
|
||||
foreach (var prioritySelectorComponents in SystemAPI.Query<DynamicBuffer<PrioritySelectorComponent>>()) {
|
||||
for (int i = 0; i < prioritySelectorComponents.Length; ++i) {
|
||||
prioritySelectorComponents[i].Priorities.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0df94ec9b57703043babb28f51e4b55d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,324 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.BehaviorDesigner.Runtime.Utility;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Opsive.Shared.Utility;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.Burst;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the random selector task.
|
||||
/// </summary>
|
||||
[NodeIcon("d7c1e0f5830316e449df8a35561df859", "7638e4bc5a1f4cd488801902387ec5ea")]
|
||||
[Opsive.Shared.Utility.Description("Similar to the selector task, the random selector task will return success as soon as a child task returns success. " +
|
||||
"The difference is that the random selector class will run its children in a random order. The selector task is deterministic " +
|
||||
"in that it will always run the tasks from left to right within the tree. The random selector task shuffles the child tasks up and then begins " +
|
||||
"execution in a random order. Other than that the random selector class is the same as the selector class. It will continue running tasks " +
|
||||
"until a task completes successfully. If no child tasks return success then it will return failure.")]
|
||||
public class RandomSelector : ECSCompositeTask<RandomSelectorTaskSystem, RandomSelectorComponent>, IParentNode, IConditionalAbortParent, IInterruptResponder, ISavableTask, ICloneable
|
||||
{
|
||||
[Tooltip("Specifies how the child conditional tasks should be reevaluated.")]
|
||||
[SerializeField] ConditionalAbortType m_AbortType;
|
||||
[Tooltip("The seed of the random number generator. Set to 0 to use the entity index as the seed.")]
|
||||
[SerializeField] uint m_Seed;
|
||||
|
||||
private ushort m_ComponentIndex;
|
||||
|
||||
public ConditionalAbortType AbortType { get => m_AbortType; set => m_AbortType = value; }
|
||||
public uint Seed { get => m_Seed; set => m_Seed = value; }
|
||||
|
||||
public override ComponentType Flag { get => typeof(RandomSelectorFlag); }
|
||||
public Type InterruptSystemType { get => typeof(RandomSelectorInterruptSystem); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override RandomSelectorComponent GetBufferElement()
|
||||
{
|
||||
return new RandomSelectorComponent()
|
||||
{
|
||||
Index = RuntimeIndex,
|
||||
Seed = m_Seed,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
m_ComponentIndex = (ushort)base.AddBufferElement(world, entity, gameObject);
|
||||
return m_ComponentIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the type of reflection that should be used to save the task.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the sub-task. This is used for the task set allowing each contained task to have their own save type.</param>
|
||||
public MemberVisibility GetSaveReflectionType(int index) { return MemberVisibility.None; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current task state.
|
||||
/// </summary>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
/// <returns>The current task state.</returns>
|
||||
public object Save(World world, Entity entity)
|
||||
{
|
||||
var randomSelectorComponents = world.EntityManager.GetBuffer<RandomSelectorComponent>(entity);
|
||||
var randomSelectorComponent = randomSelectorComponents[m_ComponentIndex];
|
||||
|
||||
// Save the active child and array order.
|
||||
var saveData = new object[2];
|
||||
saveData[0] = randomSelectorComponent.ActiveRelativeChildIndex;
|
||||
if (randomSelectorComponent.TaskOrder.IsCreated) {
|
||||
var taskOrder = randomSelectorComponent.TaskOrder.Value.Indicies.ToArray();
|
||||
saveData[1] = taskOrder;
|
||||
}
|
||||
return saveData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the previous task state.
|
||||
/// </summary>
|
||||
/// <param name="saveData">The previous task state.</param>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
public void Load(object saveData, World world, Entity entity)
|
||||
{
|
||||
var randomSelectorComponents = world.EntityManager.GetBuffer<RandomSelectorComponent>(entity);
|
||||
var randomSelectorComponent = randomSelectorComponents[m_ComponentIndex];
|
||||
|
||||
// saveData is the active child and array order.
|
||||
var taskSaveData = (object[])saveData;
|
||||
randomSelectorComponent.ActiveRelativeChildIndex = (ushort)taskSaveData[0];
|
||||
if (taskSaveData[1] != null) {
|
||||
var taskOrder = (ushort[])taskSaveData[1];
|
||||
var builder = new BlobBuilder(Allocator.Temp);
|
||||
ref var root = ref builder.ConstructRoot<IndiciesBlob>();
|
||||
var orderArray = builder.Allocate(ref root.Indicies, taskOrder.Length);
|
||||
for (int i = 0; i < taskOrder.Length; i++) {
|
||||
orderArray[i] = taskOrder[i];
|
||||
}
|
||||
randomSelectorComponent.TaskOrder = builder.CreateBlobAssetReference<IndiciesBlob>(Allocator.Persistent);
|
||||
builder.Dispose();
|
||||
}
|
||||
randomSelectorComponents[m_ComponentIndex] = randomSelectorComponent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deep clone of the component.
|
||||
/// </summary>
|
||||
/// <returns>A deep clone of the component.</returns>
|
||||
public object Clone()
|
||||
{
|
||||
var clone = Activator.CreateInstance<RandomSelector>();
|
||||
clone.Index = Index;
|
||||
clone.ParentIndex = ParentIndex;
|
||||
clone.SiblingIndex = SiblingIndex;
|
||||
clone.AbortType = AbortType;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the RandomSelector class.
|
||||
/// </summary>
|
||||
public struct RandomSelectorComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The relative index of the child that is currently active.")]
|
||||
public ushort ActiveRelativeChildIndex;
|
||||
[Tooltip("The seed of the random number generator.")]
|
||||
public uint Seed;
|
||||
[Tooltip("The random number generator for the task.")]
|
||||
public Unity.Mathematics.Random RandomNumberGenerator;
|
||||
[Tooltip("The indicies of the child task execution order.")]
|
||||
public BlobAssetReference<IndiciesBlob> TaskOrder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a RandomSelector node is active.
|
||||
/// </summary>
|
||||
public struct RandomSelectorFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the RandomSelector logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct RandomSelectorTaskSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Updates the logic.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
foreach (var (branchComponents, taskComponents, randomSelectorComponents, entity) in
|
||||
SystemAPI.Query<DynamicBuffer<BranchComponent>, DynamicBuffer<TaskComponent>, DynamicBuffer<RandomSelectorComponent>>().WithAll<RandomSelectorFlag, EvaluateFlag>().WithEntityAccess()) {
|
||||
for (int i = 0; i < randomSelectorComponents.Length; ++i) {
|
||||
var randomSelectorComponent = randomSelectorComponents[i];
|
||||
var taskComponent = taskComponents[randomSelectorComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var randomSelectorComponentsBuffer = randomSelectorComponents;
|
||||
var taskComponentsBuffer = taskComponents;
|
||||
var branchComponentBuffer = branchComponents;
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
taskComponentsBuffer[taskComponent.Index] = taskComponent;
|
||||
|
||||
// Initialize the task order array.
|
||||
if (!randomSelectorComponent.TaskOrder.IsCreated) {
|
||||
var childCount = TraversalUtility.GetImmediateChildCount(ref taskComponent, ref taskComponentsBuffer);
|
||||
var builder = new BlobBuilder(Allocator.Temp);
|
||||
ref var root = ref builder.ConstructRoot<IndiciesBlob>();
|
||||
var orderArray = builder.Allocate(ref root.Indicies, childCount);
|
||||
var childIndex = taskComponent.Index + 1;
|
||||
for (int j = 0; j < childCount; ++j) {
|
||||
orderArray[j] = (ushort)childIndex;
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
randomSelectorComponent.TaskOrder = builder.CreateBlobAssetReference<IndiciesBlob>(Allocator.Persistent);
|
||||
builder.Dispose();
|
||||
}
|
||||
|
||||
// Generate a new random number seed for each entity.
|
||||
if (randomSelectorComponent.RandomNumberGenerator.state == 0) {
|
||||
randomSelectorComponent.RandomNumberGenerator = Unity.Mathematics.Random.CreateFromIndex(randomSelectorComponent.Seed != 0 ? randomSelectorComponent.Seed : (uint)entity.Index);
|
||||
}
|
||||
|
||||
// Use fisher-yates to shuffle the array in place.
|
||||
ref var initialTaskOrder = ref randomSelectorComponent.TaskOrder.Value.Indicies;
|
||||
var index = initialTaskOrder.Length;
|
||||
while (index != 0) {
|
||||
var randomUnitFloat = randomSelectorComponent.RandomNumberGenerator.NextFloat();
|
||||
var randomIndex = (int)Unity.Mathematics.math.floor(randomUnitFloat * index);
|
||||
index--;
|
||||
|
||||
var element = initialTaskOrder[randomIndex];
|
||||
initialTaskOrder[randomIndex] = initialTaskOrder[index];
|
||||
initialTaskOrder[index] = element;
|
||||
}
|
||||
|
||||
randomSelectorComponent.ActiveRelativeChildIndex = 0;
|
||||
randomSelectorComponentsBuffer[i] = randomSelectorComponent;
|
||||
|
||||
branchComponent.NextIndex = initialTaskOrder[randomSelectorComponent.ActiveRelativeChildIndex];
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
|
||||
// The child may have already ran and have a non-inactive status.
|
||||
var nextChildTaskComponent = taskComponents[branchComponent.NextIndex];
|
||||
nextChildTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[branchComponent.NextIndex] = nextChildTaskComponent;
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The randomSelector task is currently active. Check the first child.
|
||||
ref var taskOrder = ref randomSelectorComponent.TaskOrder.Value.Indicies;
|
||||
var childTaskComponent = taskComponents[taskOrder[randomSelectorComponent.ActiveRelativeChildIndex]];
|
||||
if (childTaskComponent.Status == TaskStatus.Queued || childTaskComponent.Status == TaskStatus.Running) {
|
||||
// The child should keep running.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (randomSelectorComponent.ActiveRelativeChildIndex == taskOrder.Length - 1 || childTaskComponent.Status == TaskStatus.Success) {
|
||||
// There are no more children or the child succeeded. The random selector task should end. A task status of inactive indicates the last task was disabled. Return failure.
|
||||
taskComponent.Status = childTaskComponent.Status != TaskStatus.Inactive ? childTaskComponent.Status : TaskStatus.Failure;
|
||||
randomSelectorComponent.ActiveRelativeChildIndex = 0;
|
||||
taskComponentsBuffer[randomSelectorComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
} else {
|
||||
// The child task returned failure. Move onto the next task.
|
||||
randomSelectorComponent.ActiveRelativeChildIndex++;
|
||||
var nextIndex = taskOrder[randomSelectorComponent.ActiveRelativeChildIndex];
|
||||
var nextTaskComponent = taskComponents[nextIndex];
|
||||
nextTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[nextIndex] = nextTaskComponent;
|
||||
|
||||
branchComponent.NextIndex = nextIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
randomSelectorComponentsBuffer[i] = randomSelectorComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The task has been destroyed.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
private void OnDestroy(ref SystemState state)
|
||||
{
|
||||
foreach (var randomSelectorComponents in SystemAPI.Query<DynamicBuffer<RandomSelectorComponent>>()) {
|
||||
for (int i = 0; i < randomSelectorComponents.Length; ++i) {
|
||||
var randomSelectorComponent = randomSelectorComponents[i];
|
||||
if (randomSelectorComponent.TaskOrder.IsCreated) {
|
||||
randomSelectorComponent.TaskOrder.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An interrupt has occurred. Ensure the task state is correct after the interruption.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
|
||||
public partial struct RandomSelectorInterruptSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs the logic after an interruption.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
foreach (var (taskComponents, randomSelectorComponents) in
|
||||
SystemAPI.Query<DynamicBuffer<TaskComponent>, DynamicBuffer<RandomSelectorComponent>>().WithAll<InterruptFlag>()) {
|
||||
for (int i = 0; i < randomSelectorComponents.Length; ++i) {
|
||||
var randomSelectorComponent = randomSelectorComponents[i];
|
||||
// The active child will have a non-running status if it has been interrupted.
|
||||
var taskComponent = taskComponents[randomSelectorComponent.Index];
|
||||
if (taskComponent.Status == TaskStatus.Running && taskComponents[randomSelectorComponent.TaskOrder.Value.Indicies[randomSelectorComponent.ActiveRelativeChildIndex]].Status != TaskStatus.Running) {
|
||||
ushort relativeChildIndex = 0;
|
||||
// Find the currently active task.
|
||||
while (taskComponents[randomSelectorComponent.TaskOrder.Value.Indicies[relativeChildIndex]].Status != TaskStatus.Running) {
|
||||
relativeChildIndex++;
|
||||
}
|
||||
randomSelectorComponent.ActiveRelativeChildIndex = relativeChildIndex;
|
||||
var randomSelectorBuffer = randomSelectorComponents;
|
||||
randomSelectorBuffer[i] = randomSelectorComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88b2c4cec0256b8478ada94d9f020ae3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,324 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.BehaviorDesigner.Runtime.Utility;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Opsive.Shared.Utility;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.Burst;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the random sequence task.
|
||||
/// </summary>
|
||||
[NodeIcon("edb30349221143a408c76da55a6aa809", "cfb9039832ed52748b617bde070898dc")]
|
||||
[Opsive.Shared.Utility.Description("Similar to the sequence task, the random sequence task will return success as soon as every child task returns success. " +
|
||||
"The difference is that the random sequence class will run its children in a random order. The sequence task is deterministic " +
|
||||
"in that it will always run the tasks from left to right within the tree. The random sequence task shuffles the child tasks up and then begins " +
|
||||
"execution in a random order. Other than that the random sequence class is the same as the sequence class. It will stop running tasks " +
|
||||
"as soon as a single task ends in failure. On a task failure it will stop executing all of the child tasks and return failure. " +
|
||||
"If no child returns failure then it will return success.")]
|
||||
public class RandomSequence : ECSCompositeTask<RandomSequenceTaskSystem, RandomSequenceComponent>, IParentNode, IConditionalAbortParent, IInterruptResponder, ISavableTask, ICloneable
|
||||
{
|
||||
[Tooltip("Specifies how the child conditional tasks should be reevaluated.")]
|
||||
[SerializeField] ConditionalAbortType m_AbortType;
|
||||
[Tooltip("The seed of the random number generator. Set to 0 to use the entity index as the seed.")]
|
||||
[SerializeField] uint m_Seed;
|
||||
|
||||
private ushort m_ComponentIndex;
|
||||
|
||||
public ConditionalAbortType AbortType { get => m_AbortType; set => m_AbortType = value; }
|
||||
public uint Seed { get => m_Seed; set => m_Seed = value; }
|
||||
|
||||
public override ComponentType Flag { get => typeof(RandomSequenceFlag); }
|
||||
public Type InterruptSystemType { get => typeof(RandomSequenceInterruptSystem); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override RandomSequenceComponent GetBufferElement()
|
||||
{
|
||||
return new RandomSequenceComponent()
|
||||
{
|
||||
Index = RuntimeIndex,
|
||||
Seed = m_Seed,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
m_ComponentIndex = (ushort)base.AddBufferElement(world, entity, gameObject);
|
||||
return m_ComponentIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the type of reflection that should be used to save the task.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the sub-task. This is used for the task set allowing each contained task to have their own save type.</param>
|
||||
public MemberVisibility GetSaveReflectionType(int index) { return MemberVisibility.None; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current task state.
|
||||
/// </summary>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
/// <returns>The current task state.</returns>
|
||||
public object Save(World world, Entity entity)
|
||||
{
|
||||
var randomSequenceComponents = world.EntityManager.GetBuffer<RandomSequenceComponent>(entity);
|
||||
var randomSequenceComponent = randomSequenceComponents[m_ComponentIndex];
|
||||
|
||||
// Save the active child and array order.
|
||||
var saveData = new object[2];
|
||||
saveData[0] = randomSequenceComponent.ActiveRelativeChildIndex;
|
||||
if (randomSequenceComponent.TaskOrder.IsCreated) {
|
||||
var taskOrder = randomSequenceComponent.TaskOrder.Value.Indicies.ToArray();
|
||||
saveData[1] = taskOrder;
|
||||
}
|
||||
return saveData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the previous task state.
|
||||
/// </summary>
|
||||
/// <param name="saveData">The previous task state.</param>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
public void Load(object saveData, World world, Entity entity)
|
||||
{
|
||||
var randomSequenceComponents = world.EntityManager.GetBuffer<RandomSequenceComponent>(entity);
|
||||
var randomSequenceComponent = randomSequenceComponents[m_ComponentIndex];
|
||||
|
||||
// saveData is the active child and array order.
|
||||
var taskSaveData = (object[])saveData;
|
||||
randomSequenceComponent.ActiveRelativeChildIndex = (ushort)taskSaveData[0];
|
||||
if (taskSaveData[1] != null) {
|
||||
var taskOrder = (ushort[])taskSaveData[1];
|
||||
var builder = new BlobBuilder(Allocator.Temp);
|
||||
ref var root = ref builder.ConstructRoot<IndiciesBlob>();
|
||||
var orderArray = builder.Allocate(ref root.Indicies, taskOrder.Length);
|
||||
for (int i = 0; i < taskOrder.Length; i++) {
|
||||
orderArray[i] = taskOrder[i];
|
||||
}
|
||||
randomSequenceComponent.TaskOrder = builder.CreateBlobAssetReference<IndiciesBlob>(Allocator.Persistent);
|
||||
builder.Dispose();
|
||||
}
|
||||
randomSequenceComponents[m_ComponentIndex] = randomSequenceComponent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deep clone of the component.
|
||||
/// </summary>
|
||||
/// <returns>A deep clone of the component.</returns>
|
||||
public object Clone()
|
||||
{
|
||||
var clone = Activator.CreateInstance<RandomSequence>();
|
||||
clone.Index = Index;
|
||||
clone.ParentIndex = ParentIndex;
|
||||
clone.SiblingIndex = SiblingIndex;
|
||||
clone.AbortType = AbortType;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the RandomSequence class.
|
||||
/// </summary>
|
||||
public struct RandomSequenceComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The relative index of the child that is currently active.")]
|
||||
public ushort ActiveRelativeChildIndex;
|
||||
[Tooltip("The seed of the random number generator.")]
|
||||
public uint Seed;
|
||||
[Tooltip("The random number generator for the task.")]
|
||||
public Unity.Mathematics.Random RandomNumberGenerator;
|
||||
[Tooltip("The indicies of the child task execution order.")]
|
||||
public BlobAssetReference<IndiciesBlob> TaskOrder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a RandomSequence node is active.
|
||||
/// </summary>
|
||||
public struct RandomSequenceFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the RandomSequence logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct RandomSequenceTaskSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Updates the logic.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
foreach (var (branchComponents, taskComponents, randomSequenceComponents, entity) in
|
||||
SystemAPI.Query<DynamicBuffer<BranchComponent>, DynamicBuffer<TaskComponent>, DynamicBuffer<RandomSequenceComponent>>().WithAll<RandomSequenceFlag, EvaluateFlag>().WithEntityAccess()) {
|
||||
for (int i = 0; i < randomSequenceComponents.Length; ++i) {
|
||||
var randomSequenceComponent = randomSequenceComponents[i];
|
||||
var taskComponent = taskComponents[randomSequenceComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var randomSequenceComponentsBuffer = randomSequenceComponents;
|
||||
var taskComponentsBuffer = taskComponents;
|
||||
var branchComponentBuffer = branchComponents;
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
taskComponentsBuffer[taskComponent.Index] = taskComponent;
|
||||
|
||||
// Initialize the task order array.
|
||||
if (!randomSequenceComponent.TaskOrder.IsCreated) {
|
||||
var childCount = TraversalUtility.GetImmediateChildCount(ref taskComponent, ref taskComponentsBuffer);
|
||||
var builder = new BlobBuilder(Allocator.Temp);
|
||||
ref var root = ref builder.ConstructRoot<IndiciesBlob>();
|
||||
var orderArray = builder.Allocate(ref root.Indicies, childCount);
|
||||
var childIndex = taskComponent.Index + 1;
|
||||
for (int j = 0; j < childCount; ++j) {
|
||||
orderArray[j] = (ushort)childIndex;
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
randomSequenceComponent.TaskOrder = builder.CreateBlobAssetReference<IndiciesBlob>(Allocator.Persistent);
|
||||
builder.Dispose();
|
||||
}
|
||||
|
||||
// Generate a new random number seed for each entity.
|
||||
if (randomSequenceComponent.RandomNumberGenerator.state == 0) {
|
||||
randomSequenceComponent.RandomNumberGenerator = Unity.Mathematics.Random.CreateFromIndex(randomSequenceComponent.Seed != 0 ? randomSequenceComponent.Seed : (uint)entity.Index);
|
||||
}
|
||||
|
||||
// Use fisher-yates to shuffle the array in place.
|
||||
ref var initialTaskOrder = ref randomSequenceComponent.TaskOrder.Value.Indicies;
|
||||
var index = initialTaskOrder.Length;
|
||||
while (index != 0) {
|
||||
var randomUnitFloat = randomSequenceComponent.RandomNumberGenerator.NextFloat();
|
||||
var randomIndex = (int)Unity.Mathematics.math.floor(randomUnitFloat * index);
|
||||
index--;
|
||||
|
||||
var element = initialTaskOrder[randomIndex];
|
||||
initialTaskOrder[randomIndex] = initialTaskOrder[index];
|
||||
initialTaskOrder[index] = element;
|
||||
}
|
||||
|
||||
randomSequenceComponent.ActiveRelativeChildIndex = 0;
|
||||
randomSequenceComponentsBuffer[i] = randomSequenceComponent;
|
||||
|
||||
branchComponent.NextIndex = initialTaskOrder[randomSequenceComponent.ActiveRelativeChildIndex];
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
|
||||
// Start the child.
|
||||
var nextChildTaskComponent = taskComponents[branchComponent.NextIndex];
|
||||
nextChildTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[branchComponent.NextIndex] = nextChildTaskComponent;
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The randomSequence task is currently active. Check the first child.
|
||||
ref var taskOrder = ref randomSequenceComponent.TaskOrder.Value.Indicies;
|
||||
var childTaskComponent = taskComponents[taskOrder[randomSequenceComponent.ActiveRelativeChildIndex]];
|
||||
if (childTaskComponent.Status == TaskStatus.Queued || childTaskComponent.Status == TaskStatus.Running) {
|
||||
// The child should keep running.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (randomSequenceComponent.ActiveRelativeChildIndex == taskOrder.Length - 1 || childTaskComponent.Status == TaskStatus.Failure) {
|
||||
// There are no more children or the child failed. The random sequence task should end. A task status of inactive indicates the last task was disabled. Return success.
|
||||
taskComponent.Status = childTaskComponent.Status != TaskStatus.Inactive ? childTaskComponent.Status : TaskStatus.Success;
|
||||
randomSequenceComponent.ActiveRelativeChildIndex = 0;
|
||||
taskComponentsBuffer[randomSequenceComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
} else {
|
||||
// The child task returned success. Move onto the next task.
|
||||
randomSequenceComponent.ActiveRelativeChildIndex++;
|
||||
var nextIndex = taskOrder[randomSequenceComponent.ActiveRelativeChildIndex];
|
||||
var nextTaskComponent = taskComponents[nextIndex];
|
||||
nextTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[nextIndex] = nextTaskComponent;
|
||||
|
||||
branchComponent.NextIndex = nextIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
randomSequenceComponentsBuffer[i] = randomSequenceComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The task has been destroyed.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
private void OnDestroy(ref SystemState state)
|
||||
{
|
||||
foreach (var randomSequenceComponents in SystemAPI.Query<DynamicBuffer<RandomSequenceComponent>>()) {
|
||||
for (int i = 0; i < randomSequenceComponents.Length; ++i) {
|
||||
var randomSequenceComponent = randomSequenceComponents[i];
|
||||
if (randomSequenceComponent.TaskOrder.IsCreated) {
|
||||
randomSequenceComponent.TaskOrder.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An interrupt has occurred. Ensure the task state is correct after the interruption.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct RandomSequenceInterruptSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs the logic after an interruption.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
foreach (var (taskComponents, randomSequenceComponents) in
|
||||
SystemAPI.Query<DynamicBuffer<TaskComponent>, DynamicBuffer<RandomSequenceComponent>>().WithAll<InterruptFlag>()) {
|
||||
for (int i = 0; i < randomSequenceComponents.Length; ++i) {
|
||||
var randomSequenceComponent = randomSequenceComponents[i];
|
||||
// The active child will have a non-running status if it has been interrupted.
|
||||
var taskComponent = taskComponents[randomSequenceComponent.Index];
|
||||
if (taskComponent.Status == TaskStatus.Running && taskComponents[randomSequenceComponent.TaskOrder.Value.Indicies[randomSequenceComponent.ActiveRelativeChildIndex]].Status != TaskStatus.Running) {
|
||||
ushort relativeChildIndex = 0;
|
||||
// Find the currently active task.
|
||||
while (taskComponents[randomSequenceComponent.TaskOrder.Value.Indicies[relativeChildIndex]].Status != TaskStatus.Running) {
|
||||
relativeChildIndex++;
|
||||
}
|
||||
randomSequenceComponent.ActiveRelativeChildIndex = relativeChildIndex;
|
||||
var randomSequenceBuffer = randomSequenceComponents;
|
||||
randomSequenceBuffer[i] = randomSequenceComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d02a71e18382d8548b9f8f0a4834da93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,258 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Opsive.Shared.Utility;
|
||||
using Unity.Entities;
|
||||
using Unity.Burst;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the selector task.
|
||||
/// </summary>
|
||||
[NodeIcon("4c3d0559a9ebc604e88b16e9a3fdfa05", "de3acf0e386a26246b8bc999b1ef8e32")]
|
||||
[Opsive.Shared.Utility.Description("The selector task is similar to an \"or\" operation. It will return success as soon as one of its child tasks return success. " +
|
||||
"If a child task returns failure then it will sequentially run the next task. If no child task returns success then it will return failure.")]
|
||||
public class Selector : ECSCompositeTask<SelectorTaskSystem, SelectorComponent>, IParentNode, IConditionalAbortParent, IInterruptResponder, ISavableTask, ICloneable
|
||||
{
|
||||
[Tooltip("Specifies how the child conditional tasks should be reevaluated.")]
|
||||
[SerializeField] ConditionalAbortType m_AbortType;
|
||||
|
||||
private ushort m_ComponentIndex;
|
||||
|
||||
public ConditionalAbortType AbortType { get => m_AbortType; set => m_AbortType = value; }
|
||||
public Type InterruptSystemType { get => typeof(SelectorInterruptSystem); }
|
||||
|
||||
/// <summary>
|
||||
/// The type of tag that should be enabled when the task is running.
|
||||
/// </summary>
|
||||
public override ComponentType Flag { get => typeof(SelectorFlag); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override SelectorComponent GetBufferElement()
|
||||
{
|
||||
return new SelectorComponent() {
|
||||
Index = RuntimeIndex,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
m_ComponentIndex = (ushort)base.AddBufferElement(world, entity, gameObject);
|
||||
return m_ComponentIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the type of reflection that should be used to save the task.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the sub-task. This is used for the task set allowing each contained task to have their own save type.</param>
|
||||
public MemberVisibility GetSaveReflectionType(int index) { return MemberVisibility.None; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current task state.
|
||||
/// </summary>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
/// <returns>The current task state.</returns>
|
||||
public object Save(World world, Entity entity)
|
||||
{
|
||||
var selectorComponents = world.EntityManager.GetBuffer<SelectorComponent>(entity);
|
||||
var selectorComponent = selectorComponents[m_ComponentIndex];
|
||||
|
||||
// Save the active child.
|
||||
return selectorComponent.ActiveChildIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the previous task state.
|
||||
/// </summary>
|
||||
/// <param name="saveData">The previous task state.</param>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
public void Load(object saveData, World world, Entity entity)
|
||||
{
|
||||
var selectorComponents = world.EntityManager.GetBuffer<SelectorComponent>(entity);
|
||||
var selectorComponent = selectorComponents[m_ComponentIndex];
|
||||
|
||||
// saveData is the active child.
|
||||
selectorComponent.ActiveChildIndex = (ushort)saveData;
|
||||
selectorComponents[m_ComponentIndex] = selectorComponent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deep clone of the component.
|
||||
/// </summary>
|
||||
/// <returns>A deep clone of the component.</returns>
|
||||
public object Clone()
|
||||
{
|
||||
var clone = Activator.CreateInstance<Selector>();
|
||||
clone.Index = Index;
|
||||
clone.ParentIndex = ParentIndex;
|
||||
clone.SiblingIndex = SiblingIndex;
|
||||
clone.AbortType = AbortType;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the Selector class.
|
||||
/// </summary>
|
||||
public struct SelectorComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The index of the child that is currently active.")]
|
||||
public ushort ActiveChildIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a Selector node is active.
|
||||
/// </summary>
|
||||
public struct SelectorFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the Selector logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct SelectorTaskSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates the job.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
var query = SystemAPI.QueryBuilder().WithAllRW<BranchComponent>().WithAllRW<TaskComponent>().WithAllRW<SelectorComponent>().WithAll<SelectorFlag, EvaluateFlag>().Build();
|
||||
state.Dependency = new SelectorJob().ScheduleParallel(query, state.Dependency);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Job which executes the task logic.
|
||||
/// </summary>
|
||||
[BurstCompile]
|
||||
private partial struct SelectorJob : IJobEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the selector logic.
|
||||
/// </summary>
|
||||
/// <param name="branchComponents">An array of BranchComponents.</param>
|
||||
/// <param name="taskComponents">An array of TaskComponents.</param>
|
||||
/// <param name="selectorComponents">An array of SelectorComponents.</param>
|
||||
[BurstCompile]
|
||||
public void Execute(ref DynamicBuffer<BranchComponent> branchComponents, ref DynamicBuffer<TaskComponent> taskComponents, ref DynamicBuffer<SelectorComponent> selectorComponents)
|
||||
{
|
||||
for (int i = 0; i < selectorComponents.Length; ++i) {
|
||||
var selectorComponent = selectorComponents[i];
|
||||
var taskComponent = taskComponents[selectorComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
taskComponents[taskComponent.Index] = taskComponent;
|
||||
|
||||
selectorComponent.ActiveChildIndex = (ushort)(taskComponent.Index + 1);
|
||||
selectorComponents[i] = selectorComponent;
|
||||
|
||||
branchComponent.NextIndex = selectorComponent.ActiveChildIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
|
||||
// Start the child.
|
||||
var nextChildTaskComponent = taskComponents[branchComponent.NextIndex];
|
||||
nextChildTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponents[branchComponent.NextIndex] = nextChildTaskComponent;
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The selector task is currently active. Check the first child.
|
||||
var childTaskComponent = taskComponents[selectorComponent.ActiveChildIndex];
|
||||
if (childTaskComponent.Status == TaskStatus.Queued || childTaskComponent.Status == TaskStatus.Running) {
|
||||
// The child should keep running.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (childTaskComponent.SiblingIndex == ushort.MaxValue || childTaskComponent.Status == TaskStatus.Success) {
|
||||
// There are no more children or the child succeeded. The selector task should end. A task status of inactive indicates the last task was disabled. Return failure.
|
||||
taskComponent.Status = childTaskComponent.Status != TaskStatus.Inactive ? childTaskComponent.Status : TaskStatus.Failure;
|
||||
selectorComponent.ActiveChildIndex = (ushort)(selectorComponent.Index + 1);
|
||||
taskComponents[selectorComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
} else {
|
||||
// The previous task is no longer running.
|
||||
var siblingTaskComponent = taskComponents[childTaskComponent.SiblingIndex];
|
||||
|
||||
siblingTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponents[childTaskComponent.SiblingIndex] = siblingTaskComponent;
|
||||
// The current index is now the sibling index.
|
||||
selectorComponent.ActiveChildIndex = childTaskComponent.SiblingIndex;
|
||||
|
||||
branchComponent.NextIndex = selectorComponent.ActiveChildIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
selectorComponents[i] = selectorComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An interrupt has occurred. Ensure the task state is correct after the interruption.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct SelectorInterruptSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs the logic after an interruption.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
foreach (var (taskComponents, selectorComponents) in
|
||||
SystemAPI.Query<DynamicBuffer<TaskComponent>, DynamicBuffer<SelectorComponent>>().WithAll<InterruptFlag>()) {
|
||||
for (int i = 0; i < selectorComponents.Length; ++i) {
|
||||
var selectorComponent = selectorComponents[i];
|
||||
// The active child will have a non-running status if it has been interrupted.
|
||||
if (taskComponents[selectorComponent.ActiveChildIndex].Status != TaskStatus.Running) {
|
||||
var childIndex = (ushort)(selectorComponent.Index + 1);
|
||||
// Find the currently active task.
|
||||
while (childIndex != ushort.MaxValue && taskComponents[childIndex].Status != TaskStatus.Running) {
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
if (childIndex != ushort.MaxValue) {
|
||||
selectorComponent.ActiveChildIndex = childIndex;
|
||||
}
|
||||
var selectorBuffer = selectorComponents;
|
||||
selectorBuffer[i] = selectorComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b0cbe842d363e545a41d9ac600430a3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,257 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Opsive.Shared.Utility;
|
||||
using Unity.Burst;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the sequence task.
|
||||
/// </summary>
|
||||
[NodeIcon("8981cc246f900b24da46ae10eb49b68b", "4a7b39d8e0d056a4a9d8eb390b4bc9b8")]
|
||||
[Opsive.Shared.Utility.Description("The sequence task is similar to an \"and\" operation. It will return failure as soon as one of its child tasks return failure. " +
|
||||
"If a child task returns success then it will sequentially run the next task. If all child tasks return success then it will return success.")]
|
||||
public class Sequence : ECSCompositeTask<SequenceTaskSystem, SequenceComponent>, IParentNode, IConditionalAbortParent, IInterruptResponder, ISavableTask, ICloneable
|
||||
{
|
||||
[Tooltip("Specifies how the child conditional tasks should be reevaluated.")]
|
||||
[SerializeField] ConditionalAbortType m_AbortType;
|
||||
|
||||
private ushort m_ComponentIndex;
|
||||
|
||||
public ConditionalAbortType AbortType { get => m_AbortType; set => m_AbortType = value; }
|
||||
public Type InterruptSystemType { get => typeof(SequenceInterruptSystem); }
|
||||
|
||||
/// <summary>
|
||||
/// The type of tag that should be enabled when the task is running.
|
||||
/// </summary>
|
||||
public override ComponentType Flag { get => typeof(SequenceFlag); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override SequenceComponent GetBufferElement()
|
||||
{
|
||||
return new SequenceComponent() {
|
||||
Index = RuntimeIndex,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
m_ComponentIndex = (ushort)base.AddBufferElement(world, entity, gameObject);
|
||||
return m_ComponentIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the type of reflection that should be used to save the task.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the sub-task. This is used for the task set allowing each contained task to have their own save type.</param>
|
||||
public MemberVisibility GetSaveReflectionType(int index) { return MemberVisibility.None; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current task state.
|
||||
/// </summary>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
/// <returns>The current task state.</returns>
|
||||
public object Save(World world, Entity entity)
|
||||
{
|
||||
var sequenceComponents = world.EntityManager.GetBuffer<SequenceComponent>(entity);
|
||||
var sequenceComponent = sequenceComponents[m_ComponentIndex];
|
||||
|
||||
// Save the active child.
|
||||
return sequenceComponent.ActiveChildIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the previous task state.
|
||||
/// </summary>
|
||||
/// <param name="saveData">The previous task state.</param>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
public void Load(object saveData, World world, Entity entity)
|
||||
{
|
||||
var sequenceComponents = world.EntityManager.GetBuffer<SequenceComponent>(entity);
|
||||
var sequenceComponent = sequenceComponents[m_ComponentIndex];
|
||||
|
||||
// saveData is the active child.
|
||||
sequenceComponent.ActiveChildIndex = (ushort)saveData;
|
||||
sequenceComponents[m_ComponentIndex] = sequenceComponent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deep clone of the component.
|
||||
/// </summary>
|
||||
/// <returns>A deep clone of the component.</returns>
|
||||
public object Clone()
|
||||
{
|
||||
var clone = Activator.CreateInstance<Sequence>();
|
||||
clone.Index = Index;
|
||||
clone.ParentIndex = ParentIndex;
|
||||
clone.SiblingIndex = SiblingIndex;
|
||||
clone.AbortType = AbortType;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the Sequence class.
|
||||
/// </summary>
|
||||
public struct SequenceComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The index of the child that is currently active.")]
|
||||
public ushort ActiveChildIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a Sequence node is active.
|
||||
/// </summary>
|
||||
public struct SequenceFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the Sequence logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct SequenceTaskSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates the job.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
var query = SystemAPI.QueryBuilder().WithAllRW<BranchComponent>().WithAllRW<TaskComponent>().WithAllRW<SequenceComponent>().WithAll<SequenceFlag, EvaluateFlag>().Build();
|
||||
state.Dependency = new SequenceJob().ScheduleParallel(query, state.Dependency);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Job which executes the task logic.
|
||||
/// </summary>
|
||||
[BurstCompile]
|
||||
private partial struct SequenceJob : IJobEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the sequence logic.
|
||||
/// </summary>
|
||||
/// <param name="branchComponents">An array of BranchComponents.</param>
|
||||
/// <param name="taskComponents">An array of TaskComponents.</param>
|
||||
/// <param name="sequenceComponents">An array of SequenceComponents.</param>
|
||||
[BurstCompile]
|
||||
public void Execute(ref DynamicBuffer<BranchComponent> branchComponents, ref DynamicBuffer<TaskComponent> taskComponents, ref DynamicBuffer<SequenceComponent> sequenceComponents)
|
||||
{
|
||||
for (int i = 0; i < sequenceComponents.Length; ++i) {
|
||||
var sequenceComponent = sequenceComponents[i];
|
||||
var taskComponent = taskComponents[sequenceComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
taskComponents[taskComponent.Index] = taskComponent;
|
||||
|
||||
sequenceComponent.ActiveChildIndex = (ushort)(taskComponent.Index + 1);
|
||||
sequenceComponents[i] = sequenceComponent;
|
||||
|
||||
branchComponent.NextIndex = sequenceComponent.ActiveChildIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
|
||||
// Start the child.
|
||||
var nextChildTaskComponent = taskComponents[branchComponent.NextIndex];
|
||||
nextChildTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponents[branchComponent.NextIndex] = nextChildTaskComponent;
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The sequence task is currently active. Check the first child.
|
||||
var childTaskComponent = taskComponents[sequenceComponent.ActiveChildIndex];
|
||||
if (childTaskComponent.Status == TaskStatus.Queued || childTaskComponent.Status == TaskStatus.Running) {
|
||||
// The child should keep running.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (childTaskComponent.SiblingIndex == ushort.MaxValue || childTaskComponent.Status == TaskStatus.Failure) {
|
||||
// There are no more children or the child failed. The sequence task should end. A task status of inactive indicates the last task was disabled. Return success.
|
||||
taskComponent.Status = childTaskComponent.Status != TaskStatus.Inactive ? childTaskComponent.Status : TaskStatus.Success;
|
||||
sequenceComponent.ActiveChildIndex = (ushort)(sequenceComponent.Index + 1);
|
||||
taskComponents[sequenceComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
} else {
|
||||
// The previous task is no longer running.
|
||||
var siblingTaskComponent = taskComponents[childTaskComponent.SiblingIndex];
|
||||
|
||||
siblingTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponents[childTaskComponent.SiblingIndex] = siblingTaskComponent;
|
||||
// The current index is now the sibling index.
|
||||
sequenceComponent.ActiveChildIndex = childTaskComponent.SiblingIndex;
|
||||
|
||||
branchComponent.NextIndex = sequenceComponent.ActiveChildIndex;
|
||||
branchComponents[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
sequenceComponents[i] = sequenceComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An interrupt has occurred. Ensure the task state is correct after the interruption.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct SequenceInterruptSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs the logic after an interruption.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
foreach (var (taskComponents, sequenceComponents) in
|
||||
SystemAPI.Query<DynamicBuffer<TaskComponent>, DynamicBuffer<SequenceComponent>>().WithAll<InterruptFlag>()) {
|
||||
for (int i = 0; i < sequenceComponents.Length; ++i) {
|
||||
var sequenceComponent = sequenceComponents[i];
|
||||
if (taskComponents[sequenceComponent.ActiveChildIndex].Status != TaskStatus.Running) {
|
||||
var childIndex = (ushort)(sequenceComponent.Index + 1);
|
||||
// Find the currently active task.
|
||||
while (childIndex != ushort.MaxValue && taskComponents[childIndex].Status != TaskStatus.Running) {
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
if (childIndex != ushort.MaxValue) {
|
||||
sequenceComponent.ActiveChildIndex = childIndex;
|
||||
}
|
||||
var sequenceBuffer = sequenceComponents;
|
||||
sequenceBuffer[i] = sequenceComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 519b18064a01b25499eb333d144065c0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,361 +0,0 @@
|
||||
#if GRAPH_DESIGNER
|
||||
/// ---------------------------------------------
|
||||
/// Behavior Designer
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
namespace Opsive.BehaviorDesigner.Runtime.Tasks.Composites
|
||||
{
|
||||
using Opsive.BehaviorDesigner.Runtime.Components;
|
||||
using Opsive.BehaviorDesigner.Runtime.Utility;
|
||||
using Opsive.GraphDesigner.Runtime;
|
||||
using Opsive.Shared.Utility;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.Burst;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// A node representation of the utility selector task.
|
||||
/// </summary>
|
||||
[NodeIcon("9d36cd363c3e08246a6e9eaf5ad99d69", "db3d0b77c7f9e0b4f9157aa03178836a")]
|
||||
[Opsive.Shared.Utility.Description("The utility selector task evaluates the child tasks using Utility Theory AI. The child task can return the utility value " +
|
||||
"at that particular time. The task with the highest utility value will be selected and the existing running task will be aborted. The utility selector " +
|
||||
"task reevaluates its children every tick.")]
|
||||
public class UtilitySelector : ECSCompositeTask<UtilitySelectorTaskSystem, UtilitySelectorComponent>, IParentNode, ISavableTask, ICloneable
|
||||
{
|
||||
private ushort m_ComponentIndex;
|
||||
|
||||
public override ComponentType Flag { get => typeof(UtilitySelectorFlag); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new TBufferElement for use by the system.
|
||||
/// </summary>
|
||||
/// <returns>A new TBufferElement for use by the system.</returns>
|
||||
public override UtilitySelectorComponent GetBufferElement()
|
||||
{
|
||||
return new UtilitySelectorComponent()
|
||||
{
|
||||
Index = RuntimeIndex,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the IBufferElementData to the entity.
|
||||
/// </summary>
|
||||
/// <param name="world">The world that the entity exists in.</param>
|
||||
/// <param name="entity">The entity that the IBufferElementData should be assigned to.</param>
|
||||
/// <param name="gameObject">The GameObject that the entity is attached to.</param>
|
||||
/// <returns>The index of the element within the buffer.</returns>
|
||||
public override int AddBufferElement(World world, Entity entity, GameObject gameObject)
|
||||
{
|
||||
m_ComponentIndex = (ushort)base.AddBufferElement(world, entity, gameObject);
|
||||
ComponentUtility.AddInterruptComponents(world.EntityManager, entity);
|
||||
return m_ComponentIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the type of reflection that should be used to save the task.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the sub-task. This is used for the task set allowing each contained task to have their own save type.</param>
|
||||
public MemberVisibility GetSaveReflectionType(int index) { return MemberVisibility.None; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current task state.
|
||||
/// </summary>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
/// <returns>The current task state.</returns>
|
||||
public object Save(World world, Entity entity)
|
||||
{
|
||||
var utilitySelectorComponents = world.EntityManager.GetBuffer<UtilitySelectorComponent>(entity);
|
||||
var utilitySelectorComponent = utilitySelectorComponents[m_ComponentIndex];
|
||||
|
||||
// Save the active child.
|
||||
return utilitySelectorComponent.ActiveChildIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the previous task state.
|
||||
/// </summary>
|
||||
/// <param name="saveData">The previous task state.</param>
|
||||
/// <param name="world">The DOTS world.</param>
|
||||
/// <param name="entity">The DOTS entity.</param>
|
||||
public void Load(object saveData, World world, Entity entity)
|
||||
{
|
||||
var utilitySelectorComponents = world.EntityManager.GetBuffer<UtilitySelectorComponent>(entity);
|
||||
var utilitySelectorComponent = utilitySelectorComponents[m_ComponentIndex];
|
||||
|
||||
// saveData is the active child index.
|
||||
utilitySelectorComponent.ActiveChildIndex = (ushort)saveData;
|
||||
utilitySelectorComponents[m_ComponentIndex] = utilitySelectorComponent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a deep clone of the component.
|
||||
/// </summary>
|
||||
/// <returns>A deep clone of the component.</returns>
|
||||
public object Clone()
|
||||
{
|
||||
var clone = Activator.CreateInstance<UtilitySelector>();
|
||||
clone.Index = Index;
|
||||
clone.ParentIndex = ParentIndex;
|
||||
clone.SiblingIndex = SiblingIndex;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The DOTS data structure for the UtilitySelector class.
|
||||
/// </summary>
|
||||
public struct UtilitySelectorComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the node.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The index of the child that is currently active.")]
|
||||
public ushort ActiveChildIndex;
|
||||
[Tooltip("The index of the UtilityValueComponent that corresponds to each child.")]
|
||||
public NativeArray<UtilityItem> UtilityItems;
|
||||
|
||||
/// <summary>
|
||||
/// A mapping between the UnityValueComponent and the task.
|
||||
/// </summary>
|
||||
public struct UtilityItem
|
||||
{
|
||||
[Tooltip("The index of the task.")]
|
||||
public ushort TaskIndex;
|
||||
[Tooltip("The index of the UtilityValueComponent.")]
|
||||
public ushort UtilityValueIndex;
|
||||
[Tooltip("Can the task continue to exectute?")]
|
||||
public bool CanExecute;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DOTS structure that contains the most recently utility of the task.
|
||||
/// </summary>
|
||||
public struct UtilityValueComponent : IBufferElementData
|
||||
{
|
||||
[Tooltip("The index of the task.")]
|
||||
public ushort Index;
|
||||
[Tooltip("The current utility value. The higher the value the more likely it will be selected.")]
|
||||
public float Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A DOTS tag indicating when a UtilitySelector node is active.
|
||||
/// </summary>
|
||||
public struct UtilitySelectorFlag : IComponentData, IEnableableComponent { }
|
||||
|
||||
/// <summary>
|
||||
/// Runs the UtilitySelector logic.
|
||||
/// </summary>
|
||||
[DisableAutoCreation]
|
||||
public partial struct UtilitySelectorTaskSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Updates the logic.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
[BurstCompile]
|
||||
private void OnUpdate(ref SystemState state)
|
||||
{
|
||||
var hasUtilityValueComponent = false;
|
||||
foreach (var (utilitySelectorComponents, utilityValueComponents, taskComponents, branchComponents, entity) in
|
||||
SystemAPI.Query<DynamicBuffer<UtilitySelectorComponent>, DynamicBuffer<UtilityValueComponent>, DynamicBuffer<TaskComponent>, DynamicBuffer<BranchComponent>>().WithAll<EvaluateFlag>().WithEntityAccess()) {
|
||||
|
||||
hasUtilityValueComponent = true;
|
||||
for (int i = 0; i < utilitySelectorComponents.Length; ++i) {
|
||||
var utilitySelectorComponent = utilitySelectorComponents[i];
|
||||
var taskComponent = taskComponents[utilitySelectorComponent.Index];
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
|
||||
// Do not continue if there will be an interrupt or the branch cannot execute.
|
||||
if (branchComponent.InterruptType != InterruptType.None || !branchComponent.CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var utilitySelectorComponentsBuffer = utilitySelectorComponents;
|
||||
var utilityValueComponentBuffer = utilityValueComponents;
|
||||
var taskComponentsBuffer = taskComponents;
|
||||
var branchComponentBuffer = branchComponents;
|
||||
if (taskComponent.Status == TaskStatus.Queued) {
|
||||
// Initialize the UtilityItem array.
|
||||
if (utilitySelectorComponent.UtilityItems.Length == 0) {
|
||||
var childCount = TraversalUtility.GetImmediateChildCount(ref taskComponent, ref taskComponentsBuffer);
|
||||
var utilityItems = new NativeArray<UtilitySelectorComponent.UtilityItem>(childCount, Allocator.Persistent);
|
||||
// Match the UtilitySelectorComponent with the child index.
|
||||
var childIndex = (ushort)(taskComponent.Index + 1);
|
||||
for (ushort j = 0; j < childCount; ++j) {
|
||||
utilityItems[j] = new UtilitySelectorComponent.UtilityItem() { TaskIndex = childIndex, UtilityValueIndex = ushort.MaxValue, CanExecute = !taskComponents[childIndex].Disabled };
|
||||
|
||||
for (ushort k = 0; k < utilityValueComponents.Length; ++k) {
|
||||
var utilityValueComponent = utilityValueComponents[k];
|
||||
if (utilityValueComponent.Index == childIndex) {
|
||||
var utilityItem = utilityItems[j];
|
||||
utilityItem.UtilityValueIndex = k;
|
||||
utilityItems[j] = utilityItem;
|
||||
break;
|
||||
}
|
||||
}
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
|
||||
utilitySelectorComponent.UtilityItems = utilityItems;
|
||||
} else {
|
||||
// Reset the execution status.
|
||||
var childIndex = (ushort)(taskComponent.Index + 1);
|
||||
for (int j = 0; j < utilitySelectorComponent.UtilityItems.Length; ++j) {
|
||||
var utilityItem = utilitySelectorComponent.UtilityItems[j];
|
||||
utilityItem.CanExecute = !taskComponents[childIndex].Disabled;
|
||||
utilitySelectorComponent.UtilityItems[j] = utilityItem;
|
||||
|
||||
childIndex = taskComponents[childIndex].SiblingIndex;
|
||||
}
|
||||
}
|
||||
|
||||
utilitySelectorComponent.ActiveChildIndex = GetHighestUtility(utilitySelectorComponent.UtilityItems, ref utilityValueComponentBuffer);
|
||||
utilitySelectorComponentsBuffer[i] = utilitySelectorComponent;
|
||||
|
||||
if (utilitySelectorComponent.ActiveChildIndex == ushort.MaxValue) {
|
||||
taskComponent.Status = TaskStatus.Failure;
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
} else {
|
||||
taskComponent.Status = TaskStatus.Running;
|
||||
branchComponent.NextIndex = utilitySelectorComponent.UtilityItems[utilitySelectorComponent.ActiveChildIndex].TaskIndex;
|
||||
|
||||
// Start the child.
|
||||
var nextChildTaskComponent = taskComponents[branchComponent.NextIndex];
|
||||
nextChildTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[branchComponent.NextIndex] = nextChildTaskComponent;
|
||||
}
|
||||
taskComponentsBuffer[taskComponent.Index] = taskComponent;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
|
||||
continue;
|
||||
} else if (taskComponent.Status != TaskStatus.Running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var childTaskComponent = taskComponents[utilitySelectorComponent.UtilityItems[utilitySelectorComponent.ActiveChildIndex].TaskIndex];
|
||||
if (childTaskComponent.Status == TaskStatus.Success) {
|
||||
// The child has returned success. Stop the selector.
|
||||
taskComponent.Status = childTaskComponent.Status;
|
||||
taskComponentsBuffer[utilitySelectorComponent.Index] = taskComponent;
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
continue;
|
||||
} else if (childTaskComponent.Status == TaskStatus.Failure) {
|
||||
var utilityItem = utilitySelectorComponent.UtilityItems[utilitySelectorComponent.ActiveChildIndex];
|
||||
utilityItem.CanExecute = false;
|
||||
utilitySelectorComponent.UtilityItems[utilitySelectorComponent.ActiveChildIndex] = utilityItem;
|
||||
|
||||
utilitySelectorComponentsBuffer[i] = utilitySelectorComponent;
|
||||
}
|
||||
|
||||
// The active task returned failure or is currently running. Determine if the active task needs to change.
|
||||
var highestIndex = GetHighestUtility(utilitySelectorComponent.UtilityItems, ref utilityValueComponentBuffer);
|
||||
if (highestIndex == utilitySelectorComponent.ActiveChildIndex) {
|
||||
// No changes are necessary.
|
||||
continue;
|
||||
}
|
||||
|
||||
var activeTaskIndex = utilitySelectorComponent.UtilityItems[utilitySelectorComponent.ActiveChildIndex].TaskIndex;
|
||||
utilitySelectorComponent.ActiveChildIndex = highestIndex;
|
||||
utilitySelectorComponentsBuffer[i] = utilitySelectorComponent;
|
||||
|
||||
// A new branch has been selected. Trigger an interrupt on the currently active branch.
|
||||
if (taskComponents[activeTaskIndex].Status == TaskStatus.Running){
|
||||
branchComponent.InterruptType = InterruptType.Branch;
|
||||
branchComponent.InterruptIndex = utilitySelectorComponent.UtilityItems[highestIndex].TaskIndex;
|
||||
state.EntityManager.SetComponentEnabled<InterruptFlag>(entity, true);
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
continue;
|
||||
}
|
||||
|
||||
// No more tasks may need to run.
|
||||
if (highestIndex == ushort.MaxValue) {
|
||||
taskComponent.Status = TaskStatus.Failure;
|
||||
taskComponentsBuffer[utilitySelectorComponent.Index] = taskComponent;
|
||||
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
} else {
|
||||
// Run the task with the next highest utility value.
|
||||
var nextTaskIndex = utilitySelectorComponent.UtilityItems[utilitySelectorComponent.ActiveChildIndex].TaskIndex;
|
||||
var nextTaskComponent = taskComponents[nextTaskIndex];
|
||||
nextTaskComponent.Status = TaskStatus.Queued;
|
||||
taskComponentsBuffer[nextTaskIndex] = nextTaskComponent;
|
||||
|
||||
branchComponent.NextIndex = nextTaskIndex;
|
||||
}
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
}
|
||||
|
||||
// Special case where the UtilitySelectorComponent has no UtilityValueComponent children.
|
||||
if (!hasUtilityValueComponent) {
|
||||
foreach (var (utilitySelectorComponents, taskComponents, branchComponents) in
|
||||
SystemAPI.Query<DynamicBuffer<UtilitySelectorComponent>, DynamicBuffer<TaskComponent>, DynamicBuffer<BranchComponent>>().WithAll<UtilitySelectorFlag, EvaluateFlag>()) {
|
||||
|
||||
for (int i = 0; i < utilitySelectorComponents.Length; ++i) {
|
||||
var utilitySelectorComponent = utilitySelectorComponents[i];
|
||||
var taskComponent = taskComponents[utilitySelectorComponent.Index];
|
||||
|
||||
// If there are no values then the selector should return failure.
|
||||
if (taskComponent.Status == TaskStatus.Queued && utilitySelectorComponent.UtilityItems.Length == 0) {
|
||||
taskComponent.Status = TaskStatus.Failure;
|
||||
var taskComponentsBuffer = taskComponents;
|
||||
taskComponentsBuffer[utilitySelectorComponent.Index] = taskComponent;
|
||||
|
||||
var branchComponent = branchComponents[taskComponent.BranchIndex];
|
||||
branchComponent.NextIndex = taskComponent.ParentIndex;
|
||||
var branchComponentBuffer = branchComponents;
|
||||
branchComponentBuffer[taskComponent.BranchIndex] = branchComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retruns the task index with the highest utility value.
|
||||
/// </summary>
|
||||
/// <param name="utilityItems">The components with utility values.</param>
|
||||
/// <param name="utilityValueComponents">The utility values of the tasks.</param>
|
||||
/// <returns>The task with the highest utility value.</returns>
|
||||
[BurstCompile]
|
||||
private ushort GetHighestUtility(NativeArray<UtilitySelectorComponent.UtilityItem> utilityItems, ref DynamicBuffer<UtilityValueComponent> utilityValueComponents)
|
||||
{
|
||||
var utilityIndex = ushort.MaxValue;
|
||||
var highestUtility = float.MinValue;
|
||||
for (ushort i = 0; i < utilityItems.Length; ++i) {
|
||||
if (utilityItems[i].UtilityValueIndex == ushort.MaxValue || !utilityItems[i].CanExecute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var utilityValue = utilityValueComponents[utilityItems[i].UtilityValueIndex].Value;
|
||||
if (utilityValue > highestUtility) {
|
||||
highestUtility = utilityValue;
|
||||
utilityIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return utilityIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The task has been destroyed.
|
||||
/// </summary>
|
||||
/// <param name="state">The current state of the system.</param>
|
||||
private void OnDestroy(ref SystemState state)
|
||||
{
|
||||
foreach (var utilitySelectorComponents in SystemAPI.Query<DynamicBuffer<UtilitySelectorComponent>>()) {
|
||||
for (int i = 0; i < utilitySelectorComponents.Length; ++i) {
|
||||
utilitySelectorComponents[i].UtilityItems.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c68beac686ca75c42879aee6c864fc5e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user