Ejemplo n.º 1
0
        public static IEnumerator CalculateFlank(AIBlackboard blackboard, ComponentBlackboard components)
        {
            Debug.Log("CalculatingFlank");
            components.stateMachineManager.state = "Calculating Flank";
            components.StopNavigating();
            components.locomotion.move = false;
            blackboard.Waiting         = false;


            yield return(null);

            yield return(new WaitForEndOfFrame());

            blackboard.positionBuffer[0] = Vector3.zero;

            FlankCalculator flanker = new FlankCalculator(components.controller.currentPosition, blackboard.player.transform.position, 200, 50, 80, components.stateMachineManager.flankMask, components.stateMachineManager);

            flanker.OnCalculateFlank += components.SetWaypoints;

            while (flanker.Working)
            {
                yield return(null);
            }

            flanker.OnCalculateFlank -= components.SetWaypoints;

            components.AdvanceState();
        }
Ejemplo n.º 2
0
        public static IEnumerator GotoPerson(AIBlackboard blackboard, int bufferIndex, float speed, ComponentBlackboard components, float radius = 2, bool waiting = false)
        {
            blackboard.Waiting = waiting;
            components.StopShootingPlayer();
            Transform         target      = blackboard.targetBuffer[bufferIndex];
            NavMeshAgent      navigation  = components.navigation;
            MecanimLocomotion locomotion  = components.locomotion;
            Transform         aiTransform = components.aiObject.transform;

            navigation.SetDestination(target.position);

            locomotion.speed           = speed;
            locomotion.lookWhereMoving = true;
            locomotion.turn            = false;
            locomotion.move            = true;

            yield return(null);

            while (Vector3.Distance(target.position, aiTransform.position) > radius)
            {
                navigation.SetDestination(target.position);
                components.Navigate();
                yield return(new WaitForSeconds(0.1f));
            }

            locomotion.move = false;
            components.AdvanceState();
        }
Ejemplo n.º 3
0
        public static IEnumerator CalculatePatrolPath(AIBlackboard blackboard, ComponentBlackboard components, float radius = 80)
        {
            NavMeshAgent      navigation  = components.navigation;
            MecanimLocomotion locomotion  = components.locomotion;
            Transform         aiTransform = components.aiObject.transform;

            Collider[] NearbyRooms = Physics.OverlapSphere(aiTransform.position, radius, components.stateMachineManager.flankMask);
            yield return(null);

            int[] chosenRooms = new int[4];

            int amount = NearbyRooms.Length;

            chosenRooms[0] = Random.Range(0, amount);

            int i;

            for (i = 1; i < chosenRooms.Length && i < amount; ++i)
            {
                int b = (chosenRooms[i - 1] + Random.Range(0, amount - 1)) % amount - 1;
                b = Mathf.Clamp(b, 0, NearbyRooms.Length - 1);
                chosenRooms[i] = b;
            }


            blackboard.positionBuffer[0] = NearbyRooms[chosenRooms[0]].transform.position;
            blackboard.positionBuffer[1] = NearbyRooms[chosenRooms[1]].transform.position;
            if (amount > 2)
            {
                blackboard.positionBuffer[2] = NearbyRooms[chosenRooms[2]].transform.position;
            }
            if (amount > 3)
            {
                blackboard.positionBuffer[3] = NearbyRooms[chosenRooms[3]].transform.position;
            }

            components.AdvanceState();
        }
Ejemplo n.º 4
0
        public static IEnumerator GotoPosition(AIBlackboard blackboard, int bufferIndex, float speed, ComponentBlackboard components, float radius = 2, float waitTime = 0, bool waiting = false)
        {
            components.stateMachineManager.state = "Going to a place";
            blackboard.Waiting = waiting;
            components.StopShootingPlayer();
            Vector3           pos         = blackboard.positionBuffer[bufferIndex];
            NavMeshAgent      navigation  = components.navigation;
            MecanimLocomotion locomotion  = components.locomotion;
            Transform         aiTransform = components.aiObject.transform;

            navigation.SetDestination(pos);

            locomotion.speed           = speed;
            locomotion.lookWhereMoving = true;
            locomotion.turn            = false;
            locomotion.move            = true;

            bool legitLocation = pos.magnitude > 0.001f;

            while (Vector3.Distance(pos, aiTransform.position) > radius && legitLocation)
            {
                navigation.SetDestination(pos);
                components.Navigate();
                yield return(null);
            }

            locomotion.move = false;
            components.StopNavigating();

            if (waitTime > 0)
            {
                yield return(new WaitForSeconds(waitTime));
            }

            components.AdvanceState();
        }
Ejemplo n.º 5
0
        public static IEnumerator GotoCover(AIBlackboard blackboard, ComponentBlackboard components, float speed, bool strafeShoot, float waitTime, bool antiCover = false)
        {
            components.stateMachineManager.state = "COVER";
            //don't want a new instruction
            blackboard.Waiting = false;
            Debug.Log("Cover");
            components.StopShootingPlayer();

            NavMeshAgent      navigation      = components.navigation;
            MecanimLocomotion locomotion      = components.locomotion;
            Transform         aiTransform     = components.aiObject.transform;
            Transform         playerTransform = blackboard.player.transform;

            Vector3    pos   = aiTransform.position;
            CoverPoint cover = antiCover ? blackboard.BestAntiCover : blackboard.BestCover;

            if (cover != null)
            {
                pos = cover.transform.position;
            }
            else
            {
                Debug.Log("NO COVER!!");
            }


            bool validCover = true;

            //don't turn back on player
            float angle = Vector3.Angle(new Vector3(pos.x, 0, pos.z), new Vector3(aiTransform.position.x, aiTransform.position.z));

            if (angle > 100)
            {
                speed = 3;
            }

            navigation.SetDestination(pos);
            locomotion.lookWhereMoving = true;
            locomotion.turn            = false;
            locomotion.move            = true;

            while (Vector3.Distance(pos, aiTransform.position) > 3.5f && validCover)
            {
                /*if(antiCover)
                 * {
                 *  validCover = blackboard.BestAntiCover.GetInstanceID() == cover.GetInstanceID();
                 * }
                 * else
                 * {
                 *  validCover = blackboard.BestCover.GetInstanceID() == cover.GetInstanceID();
                 * }*/


                if (strafeShoot)
                {
                    if (!blackboard.LostPlayer && speed < 6)
                    {
                        locomotion.speed = speed;
                        locomotion.turn  = true;

                        if (playerTransform != null)
                        {
                            locomotion.lookDirection = playerTransform.position - aiTransform.position;
                        }

                        locomotion.lookWhereMoving = false;

                        if (blackboard.LineOfSight)
                        {
                            components.ShootPlayer();
                        }
                        else
                        {
                            components.StopShootingPlayer();
                        }

                        components.AimAtPlayer(playerTransform);
                    }
                    else
                    {
                        locomotion.speed = speed;
                        //locomotion.turn = true;
                        //locomotion.lookWhereMoving = true;
                        components.StopShootingPlayer();
                    }
                }
                navigation.SetDestination(pos);
                components.Navigate();

                yield return(null);

                cover = antiCover ? blackboard.BestAntiCover : blackboard.BestCover;
                if (cover != null)
                {
                    pos = cover.transform.position;
                }
            }

            float leaveTime = Time.time + waitTime;

            locomotion.move = false;
            components.StopNavigating();

            components.stateMachineManager.state = "AT COVER";

            while (leaveTime > Time.time)
            {
                if (blackboard.LineOfSight)
                {
                    locomotion.speed = 0;
                    locomotion.turn  = true;

                    if (playerTransform != null)
                    {
                        locomotion.lookDirection = playerTransform.position - aiTransform.position;
                    }

                    components.ShootPlayer();
                    components.AimAtPlayer(playerTransform);
                }
                else
                {
                    locomotion.lookDirection = aiTransform.forward;
                    locomotion.turn          = false;
                    components.StopShootingPlayer();
                }

                yield return(null);
            }

            components.AdvanceState();
        }