Exemple #1
0
 void CheckForOpenTile(GameObject Tile) // check if the Tile called is a viable Tile to move to
 {
     OpenTiles.Add(Tile);               //add Tile to list
     if (ClosedTiles.Count > 0)
     {
         foreach (GameObject item in ClosedTiles)
         {
             if (item == Tile)
             {
                 if (OpenTiles.Contains(Tile))
                 {
                     OpenTiles.Remove(Tile);
                 }
             }
         }
     }
     if (Tile.transform.childCount > 0) //does Tile have children?
     {
         foreach (Transform item in Tile.transform)
         {
             if (item.name == "Tower") //foreach child is child tower? ie Tile is built upon
             {
                 if (OpenTiles.Contains(Tile))
                 {
                     OpenTiles.Remove(Tile);//Tile is invalid to move to
                 }
                 if (!ClosedTiles.Contains(Tile))
                 {
                     ClosedTiles.Add(Tile);
                 }
             }
         }
     }
 }
Exemple #2
0
    private void StandardMovement()
    {
        if (FinalPath.Count <= 0)     //if path is not written
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position, Vector3.down, out hit)) //raycast starting grid tile
            {
                ChosenTile = hit.transform.gameObject;                      //this tile is the tile we are working from
            }
            else
            {
                ChosenTile = null;
                Destroy(gameObject);     //this unit has gotten knocked off the grid and does not have a path generated
            }
            if (ChosenTile != null)
            {
                var i = 0;
                do     //loop until the last tile is the killzonepathing, ie, the end of the path
                {
                    i++;
                    if (GetGridCoords(ChosenTile).x + 1 >= 0 && GetGridCoords(ChosenTile).x + 1 <= CGG.GetLength(0) - 1)   //is the tile to the right out of bounds?
                    {
                        CheckForOpenTile(CGG[(int)GetGridCoords(ChosenTile).x + 1, (int)GetGridCoords(ChosenTile).y]);     //is this tile built upon?
                    }
                    if (GetGridCoords(ChosenTile).x - 1 >= 0 && GetGridCoords(ChosenTile).x - 1 <= CGG.GetLength(0) - 1)   //is the tile to the left out of bounds?
                    {
                        CheckForOpenTile(CGG[(int)GetGridCoords(ChosenTile).x - 1, (int)GetGridCoords(ChosenTile).y]);
                    }
                    if (GetGridCoords(ChosenTile).y + 1 >= 0 && GetGridCoords(ChosenTile).y + 1 <= CGG.GetLength(1) - 1)    //is the tile above out of bounds?
                    {
                        CheckForOpenTile(CGG[(int)GetGridCoords(ChosenTile).x, (int)GetGridCoords(ChosenTile).y + 1]);
                    }
                    if (GetGridCoords(ChosenTile).y - 1 >= 0 && GetGridCoords(ChosenTile).y - 1 <= CGG.GetLength(1) - 1)    //is the tile below out of bounds? (out of the grid array size, ie there is no tile below the last row of tiles)
                    {
                        CheckForOpenTile(CGG[(int)GetGridCoords(ChosenTile).x, (int)GetGridCoords(ChosenTile).y - 1]);
                    }
                    AddTile();                                                // get the best tile from opentiles and add it to the final path
                    OpenTiles.Clear();
                    if (i >= (CGG.GetLength(0) - 1) * (CGG.GetLength(1) - 1)) //if the loop gets out of control or the enemy gets stuck between closed tiles, the closed tiles are reset and the loop is broken and restarted
                    {
                        ClosedTiles.Clear();
                        ClosedTiles.Add(hit.transform.gameObject);     // this stops 2 long final path loops, still creates longer pathing problems though

                        break;
                    }
                } while (ChosenTile != killzonepathing);     // while the path is not finished repeat
            }
        }
        else                                                                                                                                                                                                //if path is already written
        {
            Vector3 moveto = new Vector3(FinalPath[0].transform.position.x, transform.position.y, FinalPath[0].transform.position.z);                                                                       //height fix for velocity of enemies. having the to position at the same height reduces stuttering in the movement
            if (Vector3.Distance(FinalPath[0].transform.position + GetExtents(FinalPath[0]), new Vector3(transform.position.x, 0, transform.position.z)) <= 0.5f && FinalPath[0].name != "killzonepathing") //if the enemy has reached the next tile remove it
            {
                if (!ClosedTiles.Contains(FinalPath[0]))
                {
                    ClosedTiles.Add(FinalPath[0]);
                }
                FinalPath.Remove(FinalPath[0]);                                                                                                     //note that this will never remove killzone pathing because the enemy game object this script is attached to, will be destroyed before it ever hits this mark
            }
            if (CurrentEnemyID == EnemyID.Charger)                                                                                                  //chargers have different movement
            {
                var CurrentMoveSpeed = Mathf.PingPong(Time.time, MoveSpeed / 4);                                                                    // pingpong speed of charger
                if (FinalPath.Count > 0 && CurrentMoveSpeed >= 1)                                                                                   //there is one instance where at this point the path can contain nothing, if thisis the case there is nothing to move to, so stand still and then get a new path because path.coun == 0
                {
                    GetComponent <Rigidbody>().velocity = ((moveto + GetExtents(FinalPath[0])) - transform.position).normalized * CurrentMoveSpeed; //aim for the middle of each tile
                }
            }
            if (FinalPath.Count > 0)    //there is one instance where at this point the path can contain nothing, if thisis the case there is nothing to move to, so stand still and then get a new path because path.coun == 0
            {
                GetComponent <Rigidbody>().velocity = ((moveto + GetExtents(FinalPath[0])) - transform.position).normalized * MoveSpeed;
            }
            foreach (GameObject item in FinalPath)     //since you build the path has a tile selected been built upon?
            {
                bool flag = false;
                foreach (Transform item2 in item.transform)
                {
                    if (item2.name == "Tower")
                    {
                        FinalPath.Clear(); //yes so rewrite path
                        flag = true;
                        break;             //break inner loop
                    }
                }
                if (flag)
                {
                    break;     //break outer loop
                }
            }
        }
    }