Example #1
0
    public void GenerateLevel()
    {
        Level[] levelToDestroy = FindObjectsOfType(typeof(Level)) as Level[];

        if (levelToDestroy.Length > 0)
        {
            for (int i = 0; i < levelToDestroy.Length; i++)
            {
                DestroyImmediate(levelToDestroy[i].gameObject);
            }
        }


        level = new GameObject("Level");
        level.AddComponent <Level>();
        searchSpace = level.AddComponent <SearchSpace>();

        GameObject rows = new GameObject("Rows");

        rows.transform.SetParent(level.transform);

        levelObstacles = new GameObject("Obstacles");
        levelObstacles.transform.SetParent(level.transform);


        if (numberOfLanes <= 0 || numberOfRows <= 0)
        {
            return;
        }

        //start adding rows
        for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
        {
            //Create new row
            GameObject row = new GameObject("Row");
            row.transform.position += Vector3.forward * rowIndex;
            row.transform.SetParent(rows.transform);


            //prepare pathfinding for this row
            searchSpace.startRow.Clear();
            searchSpace.UpdateTiles();
            if (rowIndex >= rowToStartPathfinderFrom)
            {
                for (int i = 0; i < numberOfLanes; i++)
                {
                    int x = ((rowIndex - rowToStartPathfinderFrom) * numberOfLanes) + i;

                    //Debug.Log("Row " + rowIndex + ": " + x);
                    searchSpace.startRow.Add(searchSpace.tiles[x]);
                }
            }



            //start adding tiles
            for (int laneIndex = 0; laneIndex < numberOfLanes; laneIndex++)
            {
                // Add a tile to row
                GameObject prefab    = theme.lanes[laneIndex].GetPrefab();
                Vector3    prefabPos = prefab.transform.position;
                Vector3    tempPos   = new Vector3(row.transform.position.x + laneIndex, row.transform.position.y, row.transform.position.z);
                tempPos += prefabPos;

                GameObject currentTile = Instantiate(prefab, tempPos, Quaternion.identity, row.transform);
                searchSpace.endNode = currentTile;

                Tile currentNode = currentTile.GetComponent <Tile>();
                searchSpace.AddTile(currentNode);

                currentNode.hasObstacle = currentNode.CheckForObstacle(0);


                //if there's already an obstacle here, we don't want to add a new one
                if (currentNode.hasObstacle)
                {
                    goto ObstacleAdded;
                }


                //do pathfinder
                bool placeObstacle = true;

                /* if (rowIndex > rowToStartPathfinderFrom - 1)
                 * {
                 *   placeObstacle = UsePathfinder(currentNode);
                 * }*/


                if (placeObstacle)
                {
                    if (theme.lanes[laneIndex].obstacles.Count > 0 && placeObstacle)
                    {
                        currentNode.obstacle = ChooseObstacle(theme.lanes[laneIndex].obstacles, currentTile.transform);
                        if (currentNode.obstacle != null)
                        {
                            currentNode.hasObstacle = true;
                        }
                    }
                }
                ObstacleAdded : { }
            }
        }
    }