#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions { using Opsive.BehaviorDesigner.Runtime.Components; using Opsive.GraphDesigner.Runtime; using Unity.Entities; using Unity.Burst; using UnityEngine; /// /// A node representation of the idle task. /// [NodeIcon("fc4d1b83384913b4abfbd8455db6df5b", "79a6985a753bb244fb5b32dc0f26addb")] [Opsive.Shared.Utility.Description("Returns a TaskStatus of running. The task will only stop when interrupted or a conditional abort is triggered.")] public class Idle : ECSActionTask { /// /// The type of tag that should be enabled when the task is running. /// public override ComponentType Flag { get => typeof(IdleFlag); } /// /// Returns a new TBufferElement for use by the system. /// /// A new TBufferElement for use by the system. public override IdleComponent GetBufferElement() { return new IdleComponent() { Index = RuntimeIndex }; } } /// /// The DOTS data structure for the Idle class. /// public struct IdleComponent : IBufferElementData { [Tooltip("The index of the node.")] public ushort Index; } /// /// A DOTS tag indicating when a Idle node is active. /// public struct IdleFlag : IComponentData, IEnableableComponent { } /// /// Runs the Idle logic. /// [DisableAutoCreation] public partial struct IdleTaskSystem : ISystem { /// /// Creates the job. /// /// The current state of the system. [BurstCompile] private void OnUpdate(ref SystemState state) { var query = SystemAPI.QueryBuilder().WithAllRW().WithAll().Build(); state.Dependency = new IdleJob().ScheduleParallel(query, state.Dependency); } /// /// Job which executes the task logic. /// [BurstCompile] private partial struct IdleJob : IJobEntity { /// /// Executes the idle logic. /// /// An array of TaskComponents. /// An array of IdleComponents. [BurstCompile] public void Execute(ref DynamicBuffer taskComponents, ref DynamicBuffer idleComponents) { for (int i = 0; i < idleComponents.Length; ++i) { var idleComponent = idleComponents[i]; var taskComponent = taskComponents[idleComponent.Index]; if (taskComponent.Status == TaskStatus.Queued) { taskComponent.Status = TaskStatus.Running; taskComponents[idleComponent.Index] = taskComponent; } } } } } } #endif