/// --------------------------------------------- /// 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 Unity.Entities; using UnityEngine; [Tooltip("Fires any entity that has the HealthComponent.")] [Shared.Utility.Category("Behavior Designer Samples/DOTS")] public class Fire : 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 FireComponent GetBufferElement() { return new FireComponent() { Index = RuntimeIndex, TargetEntityVariableIndex = m_TargetEntityIndex.Index, }; } } /// /// The DOTS data structure for the Fire struct. /// public struct FireComponent : 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 Fire node is active. /// public struct FireFlag : IComponentData, IEnableableComponent { } /// /// Runs the Fire logic. /// [DisableAutoCreation] public partial struct FireTaskSystem : ISystem { /// /// Updates the logic. /// /// The current SystemState. private void OnUpdate(ref SystemState state) { var ecb = new EntityCommandBuffer(state.WorldUpdateAllocator); foreach (var (branchComponents, taskComponents, fireComponents, sharedVariables) in SystemAPI.Query, DynamicBuffer, DynamicBuffer, DynamicBuffer>().WithAll()) { for (int i = 0; i < fireComponents.Length; ++i) { var fireComponent = fireComponents[i]; var taskComponent = taskComponents[fireComponent.Index]; var branchComponent = branchComponents[taskComponent.BranchIndex]; if (!branchComponent.CanExecute) { continue; } if (taskComponent.Status != TaskStatus.Queued) { continue; } var targetEntity = sharedVariables.Get(fireComponent.TargetEntityVariableIndex); if (targetEntity != Entity.Null && state.EntityManager.Exists(targetEntity)) { ecb.AddComponent(targetEntity); } // The task will always return immediately. taskComponent.Status = TaskStatus.Success; var taskComponentBuffer = taskComponents; taskComponentBuffer[fireComponent.Index] = taskComponent; // The turret has fired - apply a recoil. foreach (var (_, turretEntity) in SystemAPI.Query>().WithOptions(EntityQueryOptions.IgnoreComponentEnabledState).WithEntityAccess()) { ecb.SetComponentEnabled(turretEntity, true); break; } } } ecb.Playback(state.EntityManager); ecb.Dispose(); } } }