示例#1
0
 private void DoActions(AIView _controller)
 {
     for (int i = 0; i < actions.Length; i++)
     {
         actions[i].DoAction(_controller);
     }
 }
示例#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);
        }
示例#3
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);
        }
示例#4
0
        private void CheckTransitions(AIView _controller)
        {
            List <AIState> trueStates  = new List <AIState>();
            List <AIState> falseStates = new List <AIState>();

            for (int i = 0; i < transitions.Length; i++)
            {
                bool transition = transitions[i].decision.Decide(_controller);

                if (transition)
                {
                    //_controller.TransitionToState(transitions[i].trueState);
                    trueStates.Add(transitions[i].trueState);
                }
                else
                {
                    //_controller.TransitionToState(transitions[i].falseState);
                    falseStates.Add(transitions[i].falseState);
                }
            }

            int trueWeight = 0;

            trueStates.ForEach((state) => trueWeight += state.weight);
            int falseWeight = 0;

            falseStates.ForEach((state) => falseWeight += state.weight);

            AIState stateToTransition = _controller.GetRemainState;

            if (trueWeight >= falseWeight)
            {
                int maxWeight = 0;
                foreach (AIState state in trueStates)
                {
                    if (state.weight > maxWeight)
                    {
                        maxWeight         = state.weight;
                        stateToTransition = state;
                    }
                }
            }
            else
            {
                int maxWeight = 0;
                foreach (AIState state in falseStates)
                {
                    if (state.weight > maxWeight)
                    {
                        maxWeight         = state.weight;
                        stateToTransition = state;
                    }
                }
            }

            _controller.TransitionToState(stateToTransition);
        }
示例#5
0
        private void WaitAction(AIView _controller)
        {
            OnWaitedActionEventArgs args = new OnWaitedActionEventArgs()
            {
                actor      = _controller.GetOwner,
                waitedTime = Time.deltaTime
            };

            EventController.QueueEvent(ActionEvents.WAITED_ACTION, args);
        }
        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>
        /// Check if there is any target in sight of the ai actor.
        /// </summary>
        /// <param name="_controller">Ai actor that is looking.</param>
        /// <returns></returns>
        private bool HasTargetInSight(AIView _controller)
        {
            AIDetector _detector = _controller.GetAIData.GetDetectorComponent;
            bool       hasTarget = false;

            if (_detector.GetNearTargets != null && _detector.GetNearTargets.Count > 0)
            {
                foreach (Actor target in _detector.GetNearTargets)
                {
                    if (IsPotentialTargetInFieldOfView(_controller.GetOwner, target, _controller.GetAIData.GetViewAngle))
                    {
                        hasTarget = IsPotentialTargetInSight(_controller.GetOwner, target, _controller.GetAIData.GetViewRange);
                        break;
                    }
                }
            }

            return(hasTarget);
        }
        /// <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);
        }
示例#9
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);
        }
示例#11
0
        public override bool Decide(AIView _controller)
        {
            bool keepShooting = base.Decide(_controller);

            // If we can't keep shooting send a shoot command with a input value of 0.0f.
            if (!keepShooting)
            {
                OnActorCommandReceiveEventArgs args = new OnActorCommandReceiveEventArgs()
                {
                    baseArgs = new OnActorEventEventArgs()
                    {
                        actor = _controller.GetOwner
                    },
                    command = ActorCommands.Shoot,
                    // Means the shoot button has been released.
                    value = 0.0f
                };

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

            return(keepShooting);
        }
示例#12
0
 public override void DoAction(AIView _controller)
 {
     ChaseTarget(_controller);
 }
 public override bool Decide(AIView _controller)
 {
     return(HasTargetInSight(_controller));
 }
 public override void DoAction(AIView _controller)
 {
     DoShoot(_controller);
 }
 protected abstract void AddToStateContoller(AIView _controller);
示例#16
0
 public override bool Decide(AIView _controller)
 {
     return(HasReachedPatrolPoint(_controller));
 }
示例#17
0
 /// <summary>
 /// Makes a true or false decision, used for AI to transition from one state to another.
 /// </summary>
 /// <param name="_controller"></param>
 /// <returns></returns>
 public abstract bool Decide(AIView _controller);
示例#18
0
 public void OnUpdate(AIView _controller)
 {
     DoActions(_controller);
     CheckTransitions(_controller);
 }
 public override bool Decide(AIView _controller)
 {
     return(HasFinishedWaiting(_controller));
 }
示例#20
0
 public override void DoAction(AIView _controller)
 {
     WaitAction(_controller);
 }
示例#21
0
 public abstract void DoAction(AIView _controller);
示例#22
0
 public override bool Decide(AIView _controller)
 {
     return(IsTargetInShootRange(_controller));
 }