示例#1
0
        /// <summary>
        /// Cast a ray in the target direction and check if its at shooting distance.
        /// </summary>
        /// <param name="_controller">AIActor reference.</param>
        /// <returns></returns>
        protected bool IsTargetInShootRange(AIView _controller)
        {
            bool inRange = false;

            ChaseData data = _controller.GetStateData <ChaseData>();

            Vector3 startPosition  = _controller.GetOwner.GetCenterOfBodyPosition;
            Vector3 targetPosition = data.GetCurrentTarget.GetCenterOfBodyPosition;
            Vector3 vectorToTarget = targetPosition - startPosition;
            float   viewDistance   = _controller.GetAIData.GetViewRange;

            // Raycast in the target direction to see if there is no obstacle.
            Actor hitTarget = RayScan(startPosition, vectorToTarget.normalized, viewDistance, rayCastLayers);

            if (data.GetCurrentTarget == hitTarget)
            {
                Vector3 directionToHit = hitTarget.GetCenterOfBodyPosition - startPosition;
                inRange = directionToHit.sqrMagnitude <= _controller.GetAIData.GetShootRange * _controller.GetAIData.GetShootRange;
                if (inRange)
                {
                    OnTargetInShootRange args = new OnTargetInShootRange()
                    {
                        actor  = data.GetOwner,
                        target = hitTarget
                    };

                    EventController.PushEvent(DecisionEvents.TARGET_IN_SHOOT_RANGE, args);
                }
            }

            return(inRange);
        }
        public override bool Decide(AIView _controller)
        {
            bool lostTarget = false;

            // If the target is not in sight check if the ai reached the chase time limit.
            if (!base.Decide(_controller))
            {
                ChaseData data = _controller.GetStateData <ChaseData>();

                lostTarget = HasLostTarget(data);
            }

            return(lostTarget);
        }
示例#3
0
        private void ChaseTarget(AIView _controller)
        {
            if (_controller.GetAIData.GetNavigationComponent.isStopped)
            {
                _controller.GetAIData.GetNavigationComponent.isStopped = false;
            }

            ChaseData data           = _controller.GetStateData <ChaseData>();
            Vector3   targetPosition = data.GetCurrentTarget.transform.position;

            OnActorCommandReceiveEventArgs args = new OnActorCommandReceiveEventArgs()
            {
                baseArgs = new OnActorEventEventArgs()
                {
                    actor = _controller.GetOwner
                },
                command = ActorCommands.Move,
                value   = targetPosition
            };

            EventController.QueueEvent(ActorEvents.ACTOR_COMMAND_RECEIVE, args);
        }
 private bool HasLostTarget(ChaseData _data) => _data.GetCurrentChaseTime >= _data.GetMaxChaseTime;