// Use this for initialization
 void Start()
 {
     mapPieceScript = GetComponentInParent <MapPieceScript> ();
 }
Beispiel #2
0
    // Build a layer Container for each level of map (worldSizeY)
    // Build layer of maps inside each Layer Container
    IEnumerator CreateMap(float secs)
    {
        //yield return new WaitForSeconds (secs);

        for (int y = 0; y < worldSizeY; y++)
        {
            ResetComponents();

            BuildSpawnPosXYZGrid((y * 30));
            spawn.transform.position = new Vector3(0, (y * 30), 0);


            // Build the layer Container
            layerContainer = Instantiate(layerPrefab, this.gameObject.transform, true);
            layerContainer.transform.SetParent(this.gameObject.transform);



            for (int i = 0; i < totalMapCountPerLayer; i++)
            {
                /*
                 * Debug.Log ("-------------------------------------- Placeing tile: " + (i + 1));
                 * Debug.Log ("------------------------------------ at GRIDPos X: " + currGridPos [0] + " , Y: " + currGridPos [1]);
                 * Debug.Log ("-------------------------------------- spawnPos: "
                 + spawn.transform.position.x + ","
                 + spawn.transform.position.y + ","
                 + spawn.transform.position.z + "");
                 */

                //////////////////////////////////////////////////////////////////
                /// Update walkedPathList with currentGrid Location
                walkedPathList [walkedPathIndex, 0] = currGridPos [0];
                walkedPathList [walkedPathIndex, 1] = currGridPos [1];
                walkedPathIndex++;
                //////////////////////////////////////////////////////////////////////

                //////////////////////////////////////////////////////////////////////
                ///  Pick and make random map and build and get script reference
                int        randNum  = Random.Range(0, 4);
                GameObject MapPiece = Instantiate(mapPieces [randNum], spawn.transform, false);
                MapPiece.transform.SetParent(layerContainer.transform);
                mapsPlaced.Add(MapPiece);                  // for player
                MapPieceScript mapScript = MapPiece.transform.GetComponent <MapPieceScript> ();
                //////////////////////////////////////////////////////////////////////

                //////////////////////////////////////////
                /// wait for next tile
                if (!mapScript.positionFound)
                {
                    yield return(new WaitForSeconds(secs));
                }
                //////////////////////////////////////////

                ////////////////////////////////////////////////////////////////////////
                /// Create new copy of joinable sides from map piece into AvailSidesGrid
                AvailSidesGrid [currGridPos [0], currGridPos [1]] = new int[4] {
                    0, 0, 0, 0
                };
                System.Array.Copy(mapScript.joinableSides, AvailSidesGrid [currGridPos [0], currGridPos [1]], 4);
                ////////////////////////////////////////////////////////////////////////

                //////////////////////////////////////////////////////////////////////
                /// figure out last move and change AvailSidesArray accordingly
                switch (lastMove)
                {
                case "L":
                    AvailSidesGrid [currGridPos [0], currGridPos [1]] [2] = 0;
                    break;

                case "U":
                    AvailSidesGrid [currGridPos [0], currGridPos [1]] [3] = 0;
                    break;

                case "R":
                    AvailSidesGrid [currGridPos [0], currGridPos [1]] [0] = 0;
                    break;

                case "D":
                    AvailSidesGrid [currGridPos [0], currGridPos [1]] [1] = 0;
                    break;

                default:
                    break;
                }
                //////////////////////////////////////////////////////////////////////

                /*
                 * for (int j = 0; j < walkedPathIndex; j++) {
                 *      Debug.Log ("walkedPathList [" + j + "] = " + walkedPathList [j, 0] + " : " + walkedPathList [j, 1]);
                 *      Debug.Log ("AvailSidesGrid: " + AvailSidesGrid [walkedPathList [j, 0], walkedPathList [j, 1]] [0]
                 + " " + AvailSidesGrid [walkedPathList [j, 0], walkedPathList [j, 1]] [1]
                 + " " + AvailSidesGrid [walkedPathList [j, 0], walkedPathList [j, 1]] [2]
                 + " " + AvailSidesGrid [walkedPathList [j, 0], walkedPathList [j, 1]] [3]);
                 + }
                 + Debug.Log ("---------------------------- Finsihed Placing tile --------");
                 + Debug.Log ("-----------------------------------------------------------");
                 + Debug.Log ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                 + Debug.Log ("");
                 + Debug.Log ("---------Searching for next place to lay --------");
                 */
                int locX = currGridPos [0];
                int locZ = currGridPos [1];

                ////////////////////////////////////////////////////////////////
                /// change AvailSidesArray if next to bounds
                if (!(locX - 1 >= 0))
                {
                    AvailSidesGrid [locX, locZ] [0] = 0;
                }
                if (!(locZ + 1 < worldSizeZ))
                {
                    AvailSidesGrid [locX, locZ] [1] = 0;
                }
                if (!(locX + 1 < worldSizeX))
                {
                    AvailSidesGrid [locX, locZ] [2] = 0;
                }
                if (!(locZ - 1 >= 0))
                {
                    AvailSidesGrid [locX, locZ] [3] = 0;
                }
                ///////////////////////////////////////////////////////////

                //Debug.Log ("shallowRefSides: " + AvailSidesGrid [locX, locZ] [0] + " " + AvailSidesGrid [locX, locZ] [1] + " " + AvailSidesGrid [locX, locZ] [2] + " " + AvailSidesGrid [locX, locZ] [3]);

                // Set new Spawn point
                spawn.transform.localPosition = GetNextSpot(locX, locZ);

                // Back Track if needed
                if (spawn.transform.localPosition == Vector3.zero)
                {
                    //Debug.Log ("SHIT NO SPOTS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                    // back up through previous locations
                    // its -2 because were already looking ahead for a spot
                    int size = walkedPathIndex - 2;
                    for (int j = size; j >= 0; j--)
                    {
                        int newlocX = walkedPathList [j, 0];
                        int newlocZ = walkedPathList [j, 1];

                        /*
                         * Debug.Log ("Looking at Grid [X]: " + newlocX + " [Z]: " + newlocZ);
                         * Debug.Log ("shallowRefSides2: " + AvailSidesGrid [newlocX, newlocZ] [0]
                         + " " + AvailSidesGrid [newlocX, newlocZ] [1]
                         + " " + AvailSidesGrid [newlocX, newlocZ] [2]
                         + " " + AvailSidesGrid [newlocX, newlocZ] [3]);
                         */

                        // Set new Spawn point
                        spawn.transform.localPosition = GetNextSpot(newlocX, newlocZ);

                        // check if spot found
                        if (spawn.transform.localPosition != Vector3.zero)
                        {
                            //Debug.Log ("Found and broken: with J: " + j);
                            walkedPathIndex = j;
                            break;
                        }
                        ////////////////////////

                        // FINAL NO MORE POSSIBILITIES
                        if (j == 0)
                        {
                            i = totalMapCountPerLayer - 1;
                            break;
                        }
                    }
                }

                // FINAL NO MORE POSSIBILITIES
                if (i == totalMapCountPerLayer - 1 || spawn.transform.localPosition == Vector3.zero)
                {
                    Debug.Log("Finished Map layer");
                    break;
                }
            }
        }

        MapsFinishedLoading();
        Destroy(spawn);
    }