Exemple #1
0
    private MazeCell CreateCell(IntVector2 coordinates)
    {
        MazeCell newCell = Instantiate(cellPrefab) as MazeCell;

        cells[coordinates.x, coordinates.z] = newCell;
        newCell.coordinates             = coordinates;
        newCell.name                    = "Maze Cell " + coordinates.x + ", " + coordinates.z;
        newCell.transform.parent        = transform;
        newCell.transform.localPosition = new Vector3(coordinates.x - size.x * 0.5f + 0.5f, 0f, coordinates.z - size.z * 0.5f + 0.5f);
        if (coordinates.x == 0 && coordinates.z == 0)
        {
            newCell.GetComponentInChildren <Renderer>().material = floorColors[0];
        }
        else if (coordinates.x == size.x - 1 && coordinates.z == size.z - 1)
        {
            newCell.GetComponentInChildren <Renderer>().material = floorColors[1];
        }
        return(newCell);
    }
Exemple #2
0
    private void CreateExit(List <MazeCellEdge> createdWalls, List <MazeCell> cells)
    {
        /* Version 3
         *
         */
        //Debug statement, if the function is called but the exit has been created already
        if (ExitCreated)
        {
            return;
        }

        outsideCells = new List <MazeCell>();
        foreach (MazeCell cell in cells)
        {
            if (cell.coordinates.x == 0 && cell.coordinates.z < size.z ||
                cell.coordinates.x < size.x && cell.coordinates.z == 0 ||
                cell.coordinates.x == size.x - 1 && cell.coordinates.z < size.z ||
                cell.coordinates.x < size.x && cell.coordinates.z == size.z - 1)
            {
                outsideCells.Add(cell);
            }
        }
        while (!ExitCreated)
        {
            int      cellIndex    = Random.Range(0, outsideCells.Count - 1);
            MazeCell selectedCell = outsideCells[cellIndex];
            int      wallIndex    = 0;
            //wall is child
            foreach (MazeCellEdge wall in createdWalls)
            {
                if (selectedCell.coordinates.x == 0 && selectedCell.coordinates.z < size.z && wall.direction == MazeDirection.West ||
                    selectedCell.coordinates.x < size.x && selectedCell.coordinates.z == 0 && wall.direction == MazeDirection.South ||
                    selectedCell.coordinates.x == size.x - 1 && selectedCell.coordinates.z < size.z && wall.direction == MazeDirection.East ||
                    selectedCell.coordinates.x < size.x && selectedCell.coordinates.z == size.z - 1 && wall.direction == MazeDirection.North)
                {
                    Destroy(selectedCell.GetComponentInChildren <MazeCell>().GetEdge(wall.direction).gameObject);

                    MazeExit exitWall = Instantiate(exitPrefab) as MazeExit;
                    exitWall.Initialize(selectedCell, null, wall.direction);
                    ExitCreated = true;
                    break;
                }
                wallIndex++;
            }
        }
    }