Exemplo n.º 1
0
    // Instantiates the GameObjects that will create the level based off of the directional ids assigned
    private void GenerateLevel()
    {
        List <MazeCell> possibleKeyLocations = new List <MazeCell>();

        // Generate the map and get possible key locations
        for (int i = 0; i < mazeArray.GetLength(0); i++)
        {
            for (int j = 0; j < mazeArray.GetLength(1); j++)
            {
                GameObject mazeSegment = null;
                if (i == customEndPoint[0] && j == customEndPoint[1])
                {
                    mazeSegment = mapObjects[MazeCell.ExitCellConvert(mazeArray[i, j])];
                }
                else
                {
                    mazeSegment = mapObjects[mazeArray[i, j].directionalId];
                }

                if (hideCeiling)
                {
                    mazeSegment.transform.Find("Ceiling").gameObject.GetComponentInChildren <Renderer>().enabled = false;
                }
                else
                {
                    mazeSegment.transform.Find("Ceiling").gameObject.GetComponentInChildren <Renderer>().enabled = true;
                }

                var segment = Instantiate(mazeSegment, new Vector3(i * modularSize, 0, j * modularSize), Quaternion.identity);

                if (IsPossibleKeyLocation(i, j))
                {
                    possibleKeyLocations.Add(mazeArray[i, j]);
                }
            }
        }

        // Generate keys
        if (possibleKeyLocations.Count >= keys.GetLength(0))
        {
            foreach (var key in keys)
            {
                var randomIndex = UnityEngine.Random.Range(0, possibleKeyLocations.Count);
                var randomCell  = possibleKeyLocations.ElementAt(randomIndex);

                Instantiate(key, new Vector3(randomCell.mazeRow * modularSize, 0, randomCell.mazeColumn * modularSize), Quaternion.identity);

                possibleKeyLocations.RemoveAt(randomIndex);
            }
        }
    }