示例#1
0
        /// <summary>
        /// Check if the ai has reached the current patrol point, if so send an event.
        /// </summary>
        /// <param name="_controller">Patrolling ai.</param>
        /// <returns></returns>
        private bool HasReachedPatrolPoint(AIView _controller)
        {
            Actor      _actor = _controller.GetOwner;
            PatrolData _data  = _controller.GetStateData <PatrolData>();

            bool reachedPatrolPoint = false;

            Vector3 currentPosition = _actor.transform.position;
            Vector3 patrolPosition  = _data.GetPatrolPosition;

            // So the vector is flat and in the same plane as the character.
            patrolPosition.y = currentPosition.y;
            float sqrdistance = (patrolPosition - currentPosition).sqrMagnitude;

            if (currentPosition.Equals(patrolPosition) || sqrdistance <= 0.1f)
            {
                reachedPatrolPoint = true;

                OnPatrolPointReachedEventArgs args = new OnPatrolPointReachedEventArgs()
                {
                    actor = _actor
                };

                EventController.QueueEvent(DecisionEvents.PATROL_POINT_REACHED, args);
            }

            return(reachedPatrolPoint);
        }
示例#2
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);
        }
        /// <summary>
        /// Checks iff the AI finished waiting in the current point.
        /// </summary>
        /// <param name="_controller"></param>
        /// <returns></returns>
        private bool HasFinishedWaiting(AIView _controller)
        {
            Actor    actor = _controller.GetOwner;
            WaitData data  = _controller.GetStateData <WaitData>();

            bool finishedWaiting = false;

            if (data.GetWaitedTime >= data.GetMaxTime)
            {
                finishedWaiting = true;

                OnWaitFinishedEventArgs args = new OnWaitFinishedEventArgs()
                {
                    actor = actor,
                };

                EventController.QueueEvent(DecisionEvents.WAIT_FINISH, args);
            }

            return(finishedWaiting);
        }
示例#5
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);
        }
        /// <summary>
        /// Sends a shoot and aim command to the ai.
        /// </summary>
        /// <param name="_controller"></param>
        private void DoShoot(AIView _controller)
        {
            if (!_controller.GetAIData.GetNavigationComponent.isStopped)
            {
                _controller.GetAIData.GetNavigationComponent.isStopped = true;
            }

            ShootData data           = _controller.GetStateData <ShootData>();
            Vector3   startPosition  = _controller.GetOwner.GetCenterOfBodyPosition;
            Vector3   targetPosition = data.GetCurrentTarget.GetCenterOfBodyPosition;
            Vector3   vectorToTarget = targetPosition - startPosition;

            OnActorCommandReceiveEventArgs aimArgs = new OnActorCommandReceiveEventArgs()
            {
                baseArgs = new OnActorEventEventArgs()
                {
                    actor = _controller.GetOwner
                },
                command = ActorCommands.Aim,
                // Means the shoot button is pressed.
                value = vectorToTarget.normalized
            };

            EventController.QueueEvent(ActorEvents.ACTOR_COMMAND_RECEIVE, aimArgs);

            OnActorCommandReceiveEventArgs shootArgs = new OnActorCommandReceiveEventArgs()
            {
                baseArgs = new OnActorEventEventArgs()
                {
                    actor = _controller.GetOwner
                },
                command = ActorCommands.Shoot,
                // Means the shoot button is pressed.
                value = 1.0f
            };

            EventController.QueueEvent(ActorEvents.ACTOR_COMMAND_RECEIVE, shootArgs);
        }