/// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Samples { using Opsive.BehaviorDesigner.Runtime.Components; using Opsive.BehaviorDesigner.Runtime.Tasks; using Opsive.GraphDesigner.Runtime; using Unity.Burst; using Unity.Entities; using Unity.Transforms; using UnityEngine; [Opsive.Shared.Utility.Description("Uses DOTS to determine if the entity has a target.")] [Shared.Utility.Category("Behavior Designer Samples/DOTS")] public class HasTarget : ECSConditionalTask, IReevaluateResponder { public override ComponentType Flag { get => typeof(HasTargetFlag); } public ComponentType ReevaluateFlag { get => typeof(HasTargetReevaluateFlag); } public System.Type ReevaluateSystemType { get => typeof(HasTargetReevaluateTaskSystem); } /// /// Returns a new TBufferElement for use by the system. /// /// A new TBufferElement for use by the system. public override HasTargetComponent GetBufferElement() { return new HasTargetComponent() { Index = RuntimeIndex, }; } } /// /// The DOTS data structure for the HasTarget struct. /// public struct HasTargetComponent : IBufferElementData { [Tooltip("The index of the node.")] public ushort Index; } /// /// A DOTS flag indicating when a HasTarget node is active. /// public struct HasTargetFlag : IComponentData, IEnableableComponent { } /// /// Runs the HasTarget logic. /// [DisableAutoCreation] public partial struct HasTargetTaskSystem : ISystem { /// /// Updates the logic. /// /// The current state of the system. [BurstCompile] private void OnUpdate(ref SystemState state) { foreach (var (branchComponents, taskComponents, hasTargetComponents) in SystemAPI.Query, DynamicBuffer, DynamicBuffer>().WithAll()) { for (int i = 0; i < hasTargetComponents.Length; ++i) { var hasTargetComponent = hasTargetComponents[i]; var taskComponent = taskComponents[hasTargetComponent.Index]; var branchComponent = branchComponents[taskComponent.BranchIndex]; if (!branchComponent.CanExecute) { continue; } if (taskComponent.Status != TaskStatus.Queued) { continue; } // Find the target. There will only be one entity with the TargetEntityTag. var hasTarget = false; foreach (var localTransform in SystemAPI.Query>().WithAll()) { hasTarget = true; break; } taskComponent.Status = hasTarget ? TaskStatus.Success : TaskStatus.Failure; var taskComponentBuffer = taskComponents; taskComponentBuffer[hasTargetComponent.Index] = taskComponent; } } } } /// /// A DOTS tag indicating when an HasTarget node needs to be reevaluated. /// public struct HasTargetReevaluateFlag : IComponentData, IEnableableComponent { } /// /// Runs the HasTarget reevaluation logic. /// [DisableAutoCreation] public partial struct HasTargetReevaluateTaskSystem : ISystem { /// /// Updates the reevaluation logic. /// /// The current state of the system. [BurstCompile] private void OnUpdate(ref SystemState state) { foreach (var (branchComponents, taskComponents, hasTargetComponents) in SystemAPI.Query, DynamicBuffer, DynamicBuffer>().WithAll()) { for (int i = 0; i < hasTargetComponents.Length; ++i) { var hasTargetComponent = hasTargetComponents[i]; var taskComponent = taskComponents[hasTargetComponent.Index]; var branchComponent = branchComponents[taskComponent.BranchIndex]; if (!branchComponent.CanExecute) { continue; } if (!taskComponent.Reevaluate) { continue; } // Find the target. There will only be one entity with the TargetEntityTag. var hasTarget = false; foreach (var localTransform in SystemAPI.Query>().WithAll()) { hasTarget = true; break; } var status = hasTarget ? TaskStatus.Success : TaskStatus.Failure; if (status != taskComponent.Status) { taskComponent.Status = status; var buffer = taskComponents; buffer[taskComponent.Index] = taskComponent; } } } } } }