protected override void Init(AIBehaviors fsm)
        {
            fsm.PlayAudio();

            aiRenderers = transform.root.GetComponentsInChildren <Renderer> ();
            aiColliders = transform.root.GetComponentsInChildren <Collider> ();

            foreach (Renderer aiRenderer in aiRenderers)
            {
                aiRenderer.enabled = false;
            }

            if (disableColliders)
            {
                foreach (Collider col in aiColliders)
                {
                    col.enabled = false;
                }
            }

            if (returnToPreviousState && fsm.previousState != null)
            {
                fsm.ChangeActiveState(fsm.previousState);
            }
            else
            {
                fsm.ChangeActiveState(goToState);
            }

            StartCoroutine(EndInvisibility(fsm));
        }
Example #2
0
        public override void HandleAction(AIBehaviors fsm)
        {
            // Can we run this state's action again ?
            if (CoolDownFinished())
            {
                if (hasCooldownLimit)
                {
                    if (cooldowns > cooldownLimit)
                    {
                        fsm.ChangeActiveState(cooldownLimitExceededState);
                        return;
                    }
                    else
                    {
                        cooldowns++;
                    }
                }

                base.HandleAction(fsm);
            }
            else if (switchStateIfStillCoolingDown)
            {
                fsm.ChangeActiveState(stillCoolingDownState);
            }
        }
Example #3
0
        protected override bool Reason(AIBehaviors fsm)
        {
            if (navMeshAgent == null)
            {
                if (returnToPreviousState)
                {
                    fsm.ChangeActiveState(fsm.previousState);
                }
                else
                {
                    fsm.ChangeActiveState(transitionState);
                }
                return(false);
            }
            else if (!hasLinkData)
            {
                OffMeshLinkData offMeshLinkData = navMeshAgent.currentOffMeshLinkData;

                startPosition = fsmTransform.position;
                endPosition   = offMeshLinkData.endPos + AddDistanceToEndPosition(offMeshLinkData);

                hasLinkData = true;

                navMeshAgentEnabledState = navMeshAgent.enabled;
                navMeshAgent.enabled     = false;
            }

            return(true);
        }
Example #4
0
        protected override void Action(AIBehaviors fsm)
        {
            if (player != null)
            {
                // Rotate towards the target
                fsm.transform.LookAt(new Vector3(player.position.x, transform.position.y, player.position.z));

                direction      = (fsm.transform.position - player.position).normalized;
                direction.y    = 0.0f;
                targetPosition = player.position + (direction * distance);

                // Move back in the opposite direction
                if (navMeshAgent != null)
                {
                    navMeshAgent.updateRotation = false;                    // disable the automatic rotation
                    fsm.MoveAgent(targetPosition, movementSpeed, 0.0f);
                    navMeshAgent.updateRotation = true;                     //when no longer need to step back then go to normal
                }
                else
                {
                    fsm.transform.Translate(direction * Time.deltaTime * movementSpeed, Space.World);
                }
            }

            // No movement check
            if (Time.time > nextCheckHasMovement && !HasMovement(fsm.transform.position))
            {
                fsm.ChangeActiveState(fsm.previousState);
            }

            previousPosition = fsm.transform.position;
        }
Example #5
0
 protected virtual void ChangeToTransitionState(AIBehaviors fsm)
 {
     if (transitionState != null && transitionState.CanSwitchToState())
     {
         fsm.ChangeActiveState(transitionState);
     }
 }
Example #6
0
        protected override bool Reason(AIBehaviors fsm)
        {
            if (seekTarget == null)
            {
                GameObject[] seekObjects = GameObject.FindGameObjectsWithTag(seekItemsWithTag);
                Vector3      pos         = fsm.transform.position;
                Vector3      diff;
                float        nearestItem = Mathf.Infinity;

                if (seekObjects == null)
                {
                    fsm.ChangeActiveState(noSeekTargetFoundState);
                }

                foreach (GameObject go in seekObjects)
                {
                    Transform tfm = go.transform;
                    float     sqrMagnitude;

                    diff         = tfm.position - pos;
                    sqrMagnitude = diff.sqrMagnitude;

                    if (sqrMagnitude < nearestItem)
                    {
                        seekTarget  = tfm;
                        nearestItem = sqrMagnitude;
                    }
                }
            }
            else
            {
                float sqrDist = (fsm.transform.position - seekTarget.position).sqrMagnitude;

                if (sqrDist < sqrDistanceToTargetThreshold)
                {
                    if (destroyTargetWhenReached)
                    {
                        Destroy(seekTarget.gameObject);
                    }

                    fsm.ChangeActiveState(seekTargetReachedState);
                    return(false);
                }
            }

            return(true);
        }
Example #7
0
 protected override bool Reason(AIBehaviors fsm)
 {
     if ((fsm.transform.position - targetPosition).magnitude < stopThreshold)
     {
         fsm.ChangeActiveState(nextState);
         return(false);
     }
     return(true);
 }
Example #8
0
 protected override bool Reason(AIBehaviors fsm)
 {
     if (Time.time > endStateTime)
     {
         fsm.ChangeActiveState(changeToState);
         return(false);
     }
     return(true);
 }
 protected override void ChangeToTransitionState(AIBehaviors fsm)
 {
     if (transitionState != null && transitionState.CanSwitchToState())
     {
         (transitionState as SeekState).specifySeekTarget = true;
         (transitionState as SeekState).seekTarget        = seenPlayer;
         fsm.ChangeActiveState(transitionState);
     }
 }
Example #10
0
        void HandleTargetReached(AIBehaviors fsm)
        {
            if (destroyTargetWhenReached)
            {
                Destroy(seekTarget.gameObject);
            }

            fsm.ChangeActiveState(seekTargetReachedState);
        }
Example #11
0
        // Change the other FSMs state to the HelpState
        private void HelpAnotherFSM(Vector3 helpTargetPosition, AIBehaviors otherFSM)
        {
            HelpState helpState = null;

            helpState = otherFSM.GetState <HelpState>();

            if (helpState != null && helpState.CanHelp(otherFSM))
            {
                helpState.helpPoint = helpTargetPosition;
                otherFSM.ChangeActiveState(helpState);
            }
        }
Example #12
0
        protected override bool Reason(AIBehaviors fsm)
        {
            if (Vector3.Distance(fsm.aiTransform.position, helpPoint) < withinHelpPointDistance)
            {
                if (helpPointReachedState != null)
                {
                    fsm.ChangeActiveState(helpPointReachedState);
                    return(false);
                }
            }

            return(true);
        }
Example #13
0
        protected virtual void Attack(AIBehaviors fsm, Transform target)
        {
            scriptWithAttackMethod.SendMessage(methodName, new AttackData(fsm, this, target));
            fsm.PlayAudio();

            attackCount++;

            if (attackCount > attacksBeforeReload)
            {
                attackCount = 0;
                fsm.ChangeActiveState(reloadState);
            }
        }
Example #14
0
        protected override bool Reason(AIBehaviors fsm)
        {
            if (Time.time > endStateTime)
            {
                if (returnToPreviousState)
                {
                    fsm.ChangeActiveState(fsm.previousState);
                }
                else if (changeToState != null)
                {
                    fsm.ChangeActiveState(changeToState);
                }
                else
                {
                    Debug.LogWarning("No state set to change into");
                }

                return(false);
            }

            return(true);
        }
        protected override void ChangeToTransitionState(AIBehaviors fsm)
        {
            if (transitionState != null && transitionState.CanSwitchToState())
            {
                if (fsm.previousState is AttackState && transitionState is SeekState)
                {
                    (transitionState as SeekState).specifySeekTarget = true;
                    (transitionState as SeekState).seekTarget        = (fsm.previousState as AttackState).GetLastKnownTarget();
                }

                fsm.ChangeActiveState(transitionState);
            }
        }
Example #16
0
        protected override bool Reason(AIBehaviors fsm)
        {
            float sqrDist;

            // Checks if the flight ended
            switch (fleeMode)
            {
            case FleeMode.NearestTaggedObject:
            case FleeMode.FixedTarget:
                if (currentTarget != null)
                {
                    sqrDist = (fsm.aiTransform.position - currentTarget.position).sqrMagnitude;

                    if (sqrDist < sqrDistanceToTargetThreshold)
                    {
                        fsm.ChangeActiveState(flightEndedState);
                        return(false);
                    }
                }

                break;

            case FleeMode.Direction:
            case FleeMode.AwayFromNearestTaggedObject:
                Vector3 nearestObjectPosition = fsm.GetClosestPlayer(objectFinder.GetTransforms()).position;

                sqrDist = (fsm.aiTransform.position - nearestObjectPosition).sqrMagnitude;

                if (sqrDist > sqrStopFleeDistance)
                {
                    fsm.ChangeActiveState(flightEndedState);
                    return(false);
                }

                break;
            }

            return(true);
        }
Example #17
0
        protected override void Action(AIBehaviors fsm)
        {
            target = GetTarget(fsm);

            if (target != null)
            {
                fsm.RotateAgent(target, rotationSpeed);
                HandlePreviousStateMovement(fsm, target);
                HandleAttack(fsm, target);
            }
            else
            {
                fsm.ChangeActiveState(noTargetState);
            }
        }
Example #18
0
        IEnumerator HandleJump(Vector3 startLocation, Vector3 endLocation, AIBehaviors fsm)
        {
            jumpHeight   = GetJumpHeight(startLocation, endLocation);
            jumpDuration = GetHeightDuration(jumpHeight);
            fallDistance = GetFallDistance(startLocation, endLocation);
            fallDuration = GetHeightDuration(fallDistance);

            transform.position = startLocation;

            StartCoroutine(DoHorizontal(startLocation, endLocation, jumpDuration + fallDuration));
            yield return(StartCoroutine(DoJump(startLocation, endLocation, jumpDuration)));

            yield return(StartCoroutine(DoFall(startLocation.y + jumpHeight, endLocation, fallDuration)));

            fsmTransform.position = endPosition;
            if (returnToPreviousState)
            {
                fsm.ChangeActiveState(fsm.previousState);
            }
            else
            {
                fsm.ChangeActiveState(transitionState);
            }
        }
Example #19
0
        protected override bool Reason(AIBehaviors fsm)
        {
            if (currentTarget != null)
            {
                float sqrDist = (fsm.transform.position - currentTarget.position).sqrMagnitude;

                if (sqrDist < sqrDistanceToTargetThreshold)
                {
                    fsm.ChangeActiveState(fleeTargetReachedState);
                    return(false);
                }
            }

            return(true);
        }
Example #20
0
        protected override void Action(AIBehaviors fsm)
        {
            if (seekTarget != null)
            {
                targetPosition = GetNextMovement(fsm) + GetEncircleOffset();

                fsm.MoveAgent(targetPosition, movementSpeed, rotationSpeed);

                if (Time.time > nextCheckHasMovement && !HasMovement(fsm.transform.position))
                {
                    fsm.ChangeActiveState(noMovementState);
                }
            }

            previousPosition = fsm.transform.position;
        }
Example #21
0
        protected override bool Reason(AIBehaviors fsm)
        {
            if (patrolMode == PatrolMode.Once)
            {
                // Is the patrol ended?
                if (currentPatrolPoint >= patrolPoints.Length)
                {
                    currentPatrolPoint = 0;
                    fsm.ChangeActiveState(patrolEndedState);

                    return(false);
                }
            }

            return(true);
        }
Example #22
0
        protected override void Action(AIBehaviors fsm)
        {
            if (seekTarget != null)
            {
                targetPosition = GetNextMovement(fsm);

                if (HasMovement(fsm.transform.position))
                {
                    fsm.MoveAgent(targetPosition, movementSpeed, rotationSpeed);
                }
                else
                {
                    fsm.ChangeActiveState(noMovementState);
                }
            }

            previousPosition = fsm.transform.position;
        }
Example #23
0
        protected override IEnumerator ChangeStateWhenAnimationFinished(AIBehaviors fsm)
        {
            BaseState         currentAttackState = fsm.currentState;
            AnimatorStateInfo stateInfo;
            float             curNormalizedTime;

            do
            {
                yield return(null);

                stateInfo         = animator.GetCurrentAnimatorStateInfo(mecanimLayerIndex);
                curNormalizedTime = stateInfo.normalizedTime % 1.0f;
            }while (curNormalizedTime < 0.95f || curNormalizedTime < attackPoint);

            if (fsm.currentState == currentAttackState)             // Check if the state hasn't changed for some reason
            {
                fsm.ChangeActiveState(reloadState);
            }
        }
Example #24
0
        protected override bool Reason(AIBehaviors fsm)
        {
            if (seekTarget == null)
            {
                Transform[] seekTfms = objectFinder.GetTransforms();
                Vector3     pos      = fsm.transform.position;
                Vector3     diff;
                float       nearestItem = Mathf.Infinity;

                if (seekTfms == null)
                {
                    fsm.ChangeActiveState(noSeekTargetFoundState);
                }

                foreach (Transform tfm in seekTfms)
                {
                    float sqrMagnitude;

                    diff         = tfm.position - pos;
                    sqrMagnitude = diff.sqrMagnitude;

                    if (sqrMagnitude < nearestItem)
                    {
                        seekTarget     = tfm;
                        nearestItem    = sqrMagnitude;
                        targetPosition = seekTarget.position;
                    }
                }
            }
            else
            {
                float sqrDist = (fsm.transform.position - targetPosition).sqrMagnitude;

                if (sqrDist < sqrDistanceToTargetThreshold)
                {
                    HandleTargetReached(fsm);
                    return(false);
                }
            }

            return(true);
        }
Example #25
0
        protected virtual IEnumerator ChangeStateWhenAnimationFinished(AIBehaviors fsm)
        {
            BaseState currentAttackState = fsm.currentState;
            string    animationName      = animationStates[0].name;

            if (attackAnimation != null && attackAnimation[animationName] != null)
            {
                do
                {
                    yield return(null);

                    curAnimPosition = attackAnimation [animationName].normalizedTime % 1.0f;
                }while (curAnimPosition < 0.95f || curAnimPosition < attackPoint);
            }

            if (fsm.currentState == currentAttackState)             // Check if the state hasn't changed for some reason
            {
                fsm.ChangeActiveState(reloadState);
            }
        }
Example #26
0
        bool InitCooldown(AIBehaviors fsm)
        {
            cooldowns = 0;

            if (initResetsCooldown)
            {
                cooledDownTime = 0.0f;
            }
            else if (switchStateIfStillCoolingDown && !CoolDownFinished())
            {
                if (stillCoolingDownState == this)
                {
                    Debug.LogWarning("Switching back to the same state when a cooldown isn't finished would lock up the system, choose another state other than '" + name + "' in the state '" + name + "'");
                }
                else
                {
                    fsm.ChangeActiveState(stillCoolingDownState);
                    return(false);
                }
            }

            return(true);
        }
Example #27
0
        protected override void Action(AIBehaviors fsm)
        {
            //playerSystem.PerformAction();

            lastKnownTarget = GetTarget(fsm);
            if (target != null)
            {
                if (counterattackMode == CounterattackMode.Active)
                {
                    HandlePreviousStateMovement(fsm, target);
                }
                fsm.RotateAgent(target, rotationSpeed);

                if (!goingToReload)
                {
                    float   sqrDistanceThreshold = attackRange * attackRange;
                    Vector3 targetDir            = target.transform.position - fsm.transform.position;
                    if (targetDir.sqrMagnitude < sqrDistanceThreshold)
                    {
                        HandleAttack(fsm, target);
                    }
                }
            }
            else
            {
                target = lastKnownTarget;
                if (target == null)
                {
                    fsm.ChangeActiveState(noTargetState);
                }
            }

            //Vector3 targetDir = target.transform.position - fsm.transform.position;
            //Debug.Log("targetDir:" + targetDir.sqrMagnitude);

            //currentAttackState = playerSystem.attack_state.CurrentState;
        }
Example #28
0
 protected override void ChangeToTransitionState(AIBehaviors fsm)
 {
     fsm.ChangeActiveState(result);
 }
Example #29
0
 protected virtual void ChangeToTransitionState(AIBehaviors fsm)
 {
     fsm.ChangeActiveState(transitionState);
 }