/// --------------------------------------------- /// 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.Variables; using Opsive.GraphDesigner.Runtime.Variables.ECS; using System; using Unity.Burst; using Unity.Collections; using Unity.Entities; 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 FindTarget : ECSActionTask { [Tooltip("The entity that should be targeted.")] [SerializeField] [RequireShared] SharedVariable m_TargetEntity; private ECSSharedVariableIndex m_TargetEntityIndex; /// /// Registers the target SharedVariable and adds the buffer element to the entity. /// /// The world that the entity exists in. /// The entity that the IBufferElementData should be assigned to. /// The ECS variable registry for registering SharedVariable fields. /// The GameObject that the entity is attached to. /// The index of the element within the buffer. public override int AddBufferElement(World world, Entity entity, ECSVariableRegistry registry, GameObject gameObject) { m_TargetEntityIndex = new ECSSharedVariableIndex(registry.Register(m_TargetEntity)); return base.AddBufferElement(world, entity, registry, gameObject); } /// /// Returns a new TBufferElement for use by the system. /// /// A new TBufferElement for use by the system. public override FindTargetComponent GetBufferElement() { return new FindTargetComponent() { Index = RuntimeIndex, TargetEntityVariableIndex = m_TargetEntityIndex.Index, }; } } /// /// The DOTS data structure for the FindTarget struct. /// public struct FindTargetComponent : IBufferElementData { [Tooltip("The index of the node.")] public ushort Index; [Tooltip("Buffer index into SharedVariableElement for the target entity.")] public int TargetEntityVariableIndex; } /// /// A DOTS flag indicating when a FindTarget node is active. /// public struct FindTargetFlag : IComponentData, IEnableableComponent { } /// /// Runs the FindTarget logic. /// [DisableAutoCreation] public partial struct FindTargetTaskSystem : ISystem { Unity.Mathematics.Random randomGenerator; /// /// The system has been created. /// /// The current SystemState. private void OnCreate(ref SystemState state) { randomGenerator = new Unity.Mathematics.Random((uint)DateTime.Now.Ticks); } /// /// Updates the logic. /// /// The current state of the system. [BurstCompile] private void OnUpdate(ref SystemState state) { foreach (var (branchComponents, taskComponents, findTargetComponents, sharedVariables) in SystemAPI.Query, DynamicBuffer, DynamicBuffer, DynamicBuffer>().WithAll()) { for (int i = 0; i < findTargetComponents.Length; ++i) { var fndTargetComponent = findTargetComponents[i]; var taskComponent = taskComponents[fndTargetComponent.Index]; var branchComponent = branchComponents[taskComponent.BranchIndex]; if (!branchComponent.CanExecute) { continue; } if (taskComponent.Status != TaskStatus.Queued) { continue; } var index = -1; var count = 0; var targetEntity = Entity.Null; var entities = state.EntityManager.GetAllEntities(Allocator.Temp); var foundAgent = false; if (entities.Length > 0) { do { index = randomGenerator.NextInt(entities.Length); count++; } while (count < entities.Length * 2 && !(foundAgent = state.EntityManager.HasComponent(entities[index]))); } // Store the found target in the shared variable buffer. if (foundAgent) { targetEntity = entities[index]; } sharedVariables.Set(fndTargetComponent.TargetEntityVariableIndex, targetEntity); entities.Dispose(); // The task is complete, return to the parent. taskComponent.Status = foundAgent ? TaskStatus.Success : TaskStatus.Failure; var taskComponentBuffer = taskComponents; taskComponentBuffer[fndTargetComponent.Index] = taskComponent; } } } } }