protected override void OnUpdate()
        {
            var positions = GetComponentDataFromEntity <Translation>(true);
            var buffer    = m_AIStateBuffer.CreateCommandBuffer().AsParallelWriter();

            Entities
            .ForEach(
                (
                    Entity e,
                    int entityInQueryIndex,
                    ref TurnToDestinationBehaviour destination,
                    in PursueBehaviour pursue,
                    in Target target,
                    in Translation pos
                ) =>
            {
                if (target.Value == Entity.Null || !positions.HasComponent(target.Value))
                {
                    // Go to idle state
                    buffer.RemoveComponent <PursueBehaviour>(entityInQueryIndex, e);
                    buffer.AddComponent(entityInQueryIndex, e, new IdleBehaviour());
                    return;
                }

                // Set entity destination to target position
                destination.Destination = positions[target.Value].Value;

                // if too close to target, evasive manoeuvre
                if (math.lengthsq(destination.Destination - pos.Value) < PROXIMITY_RADIUS * PROXIMITY_RADIUS)
                {
                    buffer.RemoveComponent <PursueBehaviour>(entityInQueryIndex, e);
                    buffer.RemoveComponent <TurnToDestinationBehaviour>(entityInQueryIndex, e);
                    buffer.AddComponent(entityInQueryIndex, e, new PeelManoeuvre());
                }
            }
        protected override void OnUpdate()
        {
            var positions = GetComponentDataFromEntity <Translation>(true);
            var buffer    = m_AIStateBuffer.CreateCommandBuffer().AsParallelWriter();

            Entities
            .WithAll <PeelManoeuvre>()
            .ForEach(
                (
                    Entity e,
                    int entityInQueryIndex,
                    ref TurnSpeed turnSpeed,
                    in Target target,
                    in Translation pos,
                    in Heading heading,
                    in MaxTurnSpeed maxTurnSpeed
                ) =>
            {
                if (target.Value == Entity.Null || !positions.HasComponent(target.Value))
                {
                    buffer.RemoveComponent <PeelManoeuvre>(entityInQueryIndex, e);
                    buffer.AddComponent(entityInQueryIndex, e, new IdleBehaviour());
                    buffer.AddComponent(entityInQueryIndex, e, new TurnToDestinationBehaviour());
                    return;
                }

                //Target position
                var targetPos = positions[target.Value];

                // Turn away from the enemy.
                float angleDiff = MathUtil.GetAngleDifference(MathUtil.GetHeadingToPoint(targetPos.Value - pos.Value), heading.Value);
                if (math.abs(angleDiff) < 0.3 * math.PI)
                {
                    turnSpeed.RadiansPerSecond = -maxTurnSpeed.RadiansPerSecond * math.sign(angleDiff);
                }
                else
                {
                    turnSpeed.RadiansPerSecond = 0f;
                }

                // Remain in evasive manoeuvre until a certain distance to target is reached.
                if (math.lengthsq(targetPos.Value - pos.Value) > ENGAGEMENT_RADIUS * ENGAGEMENT_RADIUS)
                {
                    buffer.RemoveComponent <PeelManoeuvre>(entityInQueryIndex, e);
                    buffer.AddComponent(entityInQueryIndex, e, new PursueBehaviour());
                    buffer.AddComponent(entityInQueryIndex, e, new TurnToDestinationBehaviour());
                }
            }
Example #3
0
        protected override void OnUpdate()
        {
            var commands = _commandBufferSystem.CreateCommandBuffer().AsParallelWriter();

            Entities
            .WithAll <IdleBehaviour>()
            .WithChangeFilter <Target>()
            .ForEach((Entity entity, int entityInQueryIndex, in Target target) =>
            {
                if (target.Value != Entity.Null)
                {
                    commands.RemoveComponent <IdleBehaviour>(entityInQueryIndex, entity);
                    commands.AddComponent <PursueBehaviour>(entityInQueryIndex, entity);
                }
            })
            .WithBurst()
            .ScheduleParallel();

            _commandBufferSystem.AddJobHandleForProducer(Dependency);
        }