Beispiel #1
0
 void Stun()
 {
     State     = InuState.Stun;
     AnimState = InuAnim.Stunned;
     stunTimer = 20;
     agent.SetDestination(transform.position);
 }
Beispiel #2
0
 void animSit()
 {
     if (agent.velocity.magnitude < 0.5)
     {
     }
     else
     {
         AnimState = InuAnim.Creep;
     }
 }
Beispiel #3
0
 void animRun()
 {
     if (agent.velocity.magnitude > 2.5)
     {
     }
     else
     {
         AnimState = InuAnim.Walk;
     }
 }
Beispiel #4
0
 void animIdle()
 {
     if (agent.velocity.magnitude < 0.5)
     {
     }
     else
     {
         AnimState = InuAnim.Walk;
     }
 }
Beispiel #5
0
 void animWalk()
 {
     if (agent.velocity.magnitude > 0.5)
     {
     }
     if (agent.velocity.magnitude > 2.5)
     {
         AnimState = InuAnim.Run;
     }
     else
     {
         AnimState = InuAnim.Idle;
     }
 }
Beispiel #6
0
    void Start()
    {
        //intialize variables
        anim             = GetComponentInChildren <Animator>();
        rb               = GetComponent <Rigidbody>();
        home             = gameObject.transform.position;
        startingRotation = gameObject.transform.rotation;
        actorID          = GetComponent <Actor>();
        State            = InuState.Idle;
        AnimState        = InuAnim.Idle;
        awake            = false;
        PlayerObject     = GameObject.FindGameObjectWithTag("Player");
        beenTooClose     = false;
        oldPosition      = home;
        posTimer         = 10;
        posTimer2        = 5;
        stunTimer        = 0;
        AttackTimer      = AttackTime;
        root             = MazeGenerator.getSectionBasedOnLocation(home);
        if (root != null)
        {
            nodes = MazeGenerator.GetIntersectionNodes(root);
        }
        currentNode          = StartingNode;
        agent                = GetComponent <NavMeshAgent>();
        agent.updatePosition = false;
        agent.updateRotation = true;
        agent.nextPosition   = transform.position;
        //transform.position = agent.nextPosition;
        agent.Warp(transform.position);
        retreating = false;

        column = (int)((home.x - 8) / 6);
        row    = (int)((home.z - 8) / 6);

        allNodes = MazeGenerator.nodesInSection(root);

        foreach (MazeNode n in allNodes)
        {
            if (n.Col == column && n.Row == row)
            {
                homeNode = n;
            }
        }
    }
Beispiel #7
0
    void animCreep()
    {
        if (state != InuState.Stalk)
        {
            if (state != InuState.Cornered)
            {
                AnimState = InuAnim.Idle;
            }
        }

        if (agent.velocity.magnitude > 0.5)
        {
        }
        else
        {
            AnimState = InuAnim.Sit;
        }
    }
Beispiel #8
0
    //function to execute in stalk state, contains transitions, and code to maintain distance from player and attempt to avoid being cornered
    void stalk()
    {
        posTimer  = 30;
        posTimer2 = 25;
        if (AttackTimer > 0)
        {
            AttackTimer -= Time.deltaTime;
        }
        //print(AttackTimer);
        //attack timer reachers 0 attack
        if (AttackTimer <= 0)
        {
            State = InuState.Cornered;
            return;
        }

        AnimState = InuAnim.Creep;
        //rayDirection = playerTransform.position - transform.position;
        //rayDirection.y = 0;
        playerCloseToEnemy = Vector3.Distance(playerTransform.position, transform.position) < StalkDistance;
        if (!playerCloseToEnemy)
        {
            beenTooClose = false;
            seen         = false;
            seen         = SeeObject(PlayerObject, LevelMask, home);
            if (seen)
            {
                State = InuState.Chase;
                return;
            }
            foundFootprint = SeeFootprint(allNodes, LevelMask, home);
            if (foundFootprint != null)
            {
                State = InuState.Follow;
                return;
            }
            else if (StartingNode != null)
            {
                State = InuState.Patrol;
                return;
            }
            else
            {
                State = InuState.Idle;
                return;
            }
        }

        //check to see if player is close enough to trigger cornered state
        playerTooCloseToEnemy = Vector3.Distance(playerTransform.position, transform.position) < StartCorneredDistance;
        if (playerTooCloseToEnemy)
        {
            //signify the player is too close to the inu
            //print("too close");
            beenTooClose = true;
            //get the distance from player to inu
            newdir = transform.position - playerTransform.position;
            //create containers for attempted destinations
            destinationNode = null;
            secondDestNode  = null;
            tertDestNode    = null;
            //get current node based on location
            currentLocation = new Vector3(transform.position.x, home.y + 1.5F, transform.position.z);
            fromNode        = MazeGenerator.getNodeBasedOnLocation(currentLocation);
            //print("current location " + new Vector3(transform.position.x, home.y + 1.5F, transform.position.z));
            //print("from node " + new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
            //get the player's current node
            playerNode = MazeGenerator.getNodeBasedOnLocation(playerTransform.position);

            //if the change in x is greater than the change in z try to move in the x direction first
            if (Math.Abs(newdir.x) > Math.Abs(newdir.z))
            {
                //if the change in x is positive
                if (newdir.x > 0)
                {
                    //set primary destination to be the node with the next higher value in the x direction
                    destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col + 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));

                    //if change in z is positive
                    if (newdir.z > 0)
                    {
                        //set secondary destination to be the node with the next higher value in the z direction
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row + 1) * 6 + 8));
                    }

                    if (newdir.z < 0)
                    {
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row - 1) * 6 + 8));
                    }
                }

                if (newdir.x < 0)
                {
                    destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col - 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));

                    if (newdir.z > 0)
                    {
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row + 1) * 6 + 8));
                    }

                    if (newdir.z < 0)
                    {
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row - 1) * 6 + 8));
                    }
                }
            }
            //if the change in x is the same as the change in the z direction, used rand with two possible values
            if (Math.Abs(newdir.x) == Math.Abs(newdir.z))
            {
                rand = UnityEngine.Random.Range(0, 1);

                if (rand == 0)
                {
                    if (newdir.x > 0)
                    {
                        destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col + 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));

                        if (newdir.z > 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row + 1) * 6 + 8));
                        }

                        if (newdir.z < 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row - 1) * 6 + 8));
                        }
                    }

                    if (newdir.x < 0)
                    {
                        destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col - 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));

                        if (newdir.z > 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row + 1) * 6 + 8));
                        }

                        if (newdir.z < 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row - 1) * 6 + 8));
                        }
                    }
                }

                if (rand == 1)
                {
                    if (newdir.z > 0)
                    {
                        destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row + 1) * 6 + 8));

                        if (newdir.x > 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col + 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                        }

                        if (newdir.x < 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col - 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                        }
                    }

                    if (newdir.z < 0)
                    {
                        destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row - 1) * 6 + 8));


                        if (newdir.x > 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col + 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                        }

                        if (newdir.x < 0)
                        {
                            secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col - 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                        }
                    }
                }
            }
            //if the change in x is less than the change in z try to move in the z direction first
            if (Math.Abs(newdir.x) < Math.Abs(newdir.z))
            {
                if (newdir.z > 0)
                {
                    destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row + 1) * 6 + 8));

                    if (newdir.x > 0)
                    {
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col + 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                    }

                    if (newdir.x < 0)
                    {
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col - 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                    }
                }

                if (newdir.z < 0)
                {
                    destinationNode = MazeGenerator.getNodeBasedOnLocation(new Vector3(fromNode.Col * 6 + 8, fromNode.Floor * 30, (fromNode.Row - 1) * 6 + 8));

                    if (newdir.x > 0)
                    {
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col + 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                    }

                    if (newdir.x < 0)
                    {
                        secondDestNode = MazeGenerator.getNodeBasedOnLocation(new Vector3((fromNode.Col - 1) * 6 + 8, fromNode.Floor * 30, fromNode.Row * 6 + 8));
                    }
                }
            }

            //get the list of nodes adjacent to the inu's current node
            adjacent = fromNode.GetAdjacentNodes();
            for (int iter = 0; iter < adjacent.Count; iter++)
            {
                //if new node, it is recored as tertiary destination
                if (adjacent[iter] != destinationNode && adjacent[iter] != secondDestNode && adjacent[iter] != playerNode)
                {
                    tertDestNode = adjacent[iter];
                    //check to see if tertiary is behind player and thus not valid
                    inuToPlayer = playerTransform.position - transform.position;
                    inuToTert   = new Vector3(tertDestNode.Col * 6 + 8, tertDestNode.Floor * 30, tertDestNode.Row * 6 + 8) - transform.position;
                    if (inuToPlayer.x > 0 && inuToTert.x > 0)
                    {
                        if (inuToPlayer.z > 0 && inuToTert.z > 0)
                        {
                            tertDestNode = null;
                        }

                        if (inuToPlayer.z < 0 && inuToTert.z < 0)
                        {
                            tertDestNode = null;
                        }
                    }
                    if (inuToPlayer.x < 0 && inuToTert.x < 0)
                    {
                        if (inuToPlayer.z > 0 && inuToTert.z > 0)
                        {
                            tertDestNode = null;
                        }

                        if (inuToPlayer.z < 0 && inuToTert.z < 0)
                        {
                            tertDestNode = null;
                        }
                    }
                }
            }

            //check if primary and secondary are valid nodes
            if (adjacent.Contains(destinationNode) == false)
            {
                if (destinationNode != null)
                {
                    //print("primary not adjacent " + new Vector3(destinationNode.Col * 6 + 8, fromNode.Floor * 30, destinationNode.Row * 6 + 8));
                    destinationNode = null;
                }
                else
                {
                    //print("no primary dest found");
                }
            }
            if (adjacent.Contains(secondDestNode) == false)
            {
                if (secondDestNode != null)
                {
                    //print("secondary not adjacent " + new Vector3(secondDestNode.Col * 6 + 8, fromNode.Floor * 30, secondDestNode.Row * 6 + 8));
                    secondDestNode = null;
                }
                else
                {
                    //print("no secondary dest found");
                }
            }


            //try nodes in order
            if (destinationNode == null)
            {
                //print("primary not valid");
                if (secondDestNode == null)
                {
                    //print("secondary not valid");
                    if (tertDestNode == null)
                    {
                        //print("tertiary not valid");
                        //try to move backwards anyways
                        newdir.y = 0;
                        //normalize to get direction only
                        newdir.Normalize();
                        //create a scalar
                        scalar = (float)Math.Sqrt(15);
                        //scale direction vector to set distace to go
                        newdir.Scale(new Vector3(scalar, 1, scalar));
                        //set inu to go from current direction to scalar distance in normalized direction

                        goal         = playerTransform.position + newdir;
                        wallDistance = newdir.magnitude;
                        ray          = new Ray(playerTransform.position, newdir);

                        //if wall in the way transition to cornered
                        if (Physics.Raycast(ray, out rayHit, wallDistance, LevelMask))
                        {
                            State = InuState.Cornered;
                            return;
                        }
                        //else move backwards away from player
                        else
                        {
                            agent.ResetPath();
                            agent.SetDestination(goal);
                            retreating = true;
                            return;
                        }
                    }
                    //move to tertiary destination
                    else
                    {
                        //print("trying to go to tert " + new Vector3(tertDestNode.Col * 6 + 8, fromNode.Floor * 30, tertDestNode.Row * 6 + 8));
                        agent.ResetPath();
                        agent.SetDestination(new Vector3(tertDestNode.Col * 6 + 8, fromNode.Floor * 30, tertDestNode.Row * 6 + 8));
                        retreating = true;
                        return;
                    }
                }
                //move to secondary destination
                else
                {
                    //print("trying to go to second " + new Vector3(secondDestNode.Col * 6 + 8, fromNode.Floor * 30, secondDestNode.Row * 6 + 8));
                    agent.ResetPath();
                    agent.SetDestination(new Vector3(secondDestNode.Col * 6 + 8, fromNode.Floor * 30, secondDestNode.Row * 6 + 8));
                    retreating = true;
                    return;
                }
            }
            //move to primary destination
            else
            {
                print("trying to go to primary " + new Vector3(destinationNode.Col * 6 + 8, fromNode.Floor * 30, destinationNode.Row * 6 + 8));
                agent.ResetPath();
                agent.SetDestination(new Vector3(destinationNode.Col * 6 + 8, fromNode.Floor * 30, destinationNode.Row * 6 + 8));
                retreating = true;
                return;
            }
        }

        //if player is not close enough for cornered
        if (!playerTooCloseToEnemy && beenTooClose == true)
        {
            retreating   = false;
            beenTooClose = false;
        }

        //if not retrreating maintain distance from player
        if (retreating != true)
        {
            agent.ResetPath();
            dest = playerTransform.position;

            if (Vector3.Distance(transform.position, dest) < 5)
            {
                agent.ResetPath();
                agent.SetDestination(transform.position);
            }
            else
            {
                //print("stalking towards player");
                agent.SetDestination(dest);
            }
        }

        //if player has tripper kill them, trip not currently implemented
        if (hasPlayerTripped())
        {
            if (UnityEngine.XR.XRDevice.isPresent)
            {
                player = PlayerObject.GetComponentInParent <Actor>();
                GameManager.Instance.ActorKilled(actorID, player);
            }
            else
            {
                GameManager.Instance.ActorKilled(actorID, PlayerObject.GetComponent <Actor>());
            }
            GameManager.Instance.GameOver();
            PlayerObject.SetActive(false);
            print("GameOver");
        }
    }