Esempio n. 1
0
        /// <summary>
        /// Finding busy cell in 8 cells around current cell and return new position
        /// </summary>
        private Vector3 SetNewPositionForWall(int rowX, int columnZ)
        {
            for (var x = -1; x <= 1; x++)
            {
                for (var z = -1; z <= 1; z++)
                {
                    if (CheckCellInArray(rowX + x, columnZ + z) && _cells[rowX + x, columnZ + z].IsBusy)
                    {
                        var       position   = _cells[rowX + x, columnZ + z].Position;
                        Direction directionX = Direction.None;
                        Direction directionZ = Direction.None;

                        if (z == 1)
                        {
                            directionZ = Direction.Down;
                        }
                        if (z == -1)
                        {
                            directionZ = Direction.Up;
                        }
                        if (x == 1)
                        {
                            directionX = Direction.Left;
                        }
                        if (x == -1)
                        {
                            directionX = Direction.Right;
                        }

                        position = PositionSetter.Set(position, _cellGridSizeX, _cellGridSizeZ, directionX, directionZ);
                        return(position);
                    }
                }
            }
            Debug.Log("defult x= " + rowX + " z= " + columnZ);
            return(default);
Esempio n. 2
0
        /// <summary>
        /// Main logic method
        /// </summary>
        public Labyrinth GenerateLabyrinth(int labyrinthSizeX, int labyrinthSizeZ)
        {
            var cellsPositions = new List <Vector3>();
            var wallsPositions = new List <Vector3>();

            var roadParrent = new GameObject {
                name = PARENT_ROAD
            };

            roadParrent.transform.SetParent(_parent);

            _cells = new Cell[labyrinthSizeX, labyrinthSizeZ];
            var cellsStack = new Stack <Cell>();

            var currentSell = new Cell(labyrinthSizeX / 2, labyrinthSizeZ / 2, _parent.position, true);
            var isGridSet   = false;

            do
            {
                var rowX     = currentSell.RowX;
                var columnZ  = currentSell.ColumnZ;
                var position = currentSell.Position;

                List <Cell> cellsForNextStep = new List <Cell>();

                // Check the options in four directions around current cell for the next step
                // right
                if (CheckCell(rowX + 1, columnZ) && CheckPlaceAroundCell(rowX + 1, columnZ, Direction.Right))
                {
                    cellsForNextStep.Add(new Cell(rowX + 1, columnZ, PositionSetter.Set(position, _cellGridSizeX, _cellGridSizeZ, Direction.Right)));
                }

                // left
                if (CheckCell(rowX - 1, columnZ) && CheckPlaceAroundCell(rowX - 1, columnZ, Direction.Left))
                {
                    cellsForNextStep.Add(new Cell(rowX - 1, columnZ, PositionSetter.Set(position, _cellGridSizeX, _cellGridSizeZ, Direction.Left)));
                }

                // up
                if (CheckCell(rowX, columnZ + 1) && CheckPlaceAroundCell(rowX, columnZ + 1, Direction.Up))
                {
                    cellsForNextStep.Add(new Cell(rowX, columnZ + 1, PositionSetter.Set(position, _cellGridSizeX, _cellGridSizeZ, Direction.Up)));
                }

                // down
                if (CheckCell(rowX, columnZ - 1) && CheckPlaceAroundCell(rowX, columnZ - 1, Direction.Down))
                {
                    cellsForNextStep.Add(new Cell(rowX, columnZ - 1, PositionSetter.Set(position, _cellGridSizeX, _cellGridSizeZ, Direction.Down)));
                }

                // Get random next step
                if (cellsForNextStep.Count > 0)
                {
                    var newCell = cellsForNextStep[Random.Range(0, cellsForNextStep.Count)];

                    _cells[newCell.RowX, newCell.ColumnZ]        = newCell;
                    _cells[newCell.RowX, newCell.ColumnZ].IsBusy = true;
                    cellsStack.Push(_cells[newCell.RowX, newCell.ColumnZ]);
                    currentSell = _cells[newCell.RowX, newCell.ColumnZ];
                    cellsPositions.Add(newCell.Position);

                    var index = 0;
                    if (_cellPrefubs.Length > 1 && Random.Range(0, 100) > 74)
                    {
                        index = Random.Range(1, _cellPrefubs.Length);
                    }

                    var cellObj = Object.Instantiate(_cellPrefubs[index], newCell.Position, Quaternion.identity) as GameObject;
                    //var cellObj = PrefabUtility.InstantiatePrefab(_cellPrefubs[index], SceneManager.GetActiveScene()) as GameObject;
                    cellObj.transform.SetPositionAndRotation(newCell.Position, Quaternion.identity);
                    cellObj.transform.Rotate(Vector3.up, Random.Range(0, 3) * 90f);
                    cellObj.transform.SetParent(roadParrent.transform);

                    if (!isGridSet)
                    {
                        isGridSet = SetGridStepsSize(cellObj);
                    }
                }
                else
                {
                    currentSell = cellsStack.Pop();
                }
            }while (cellsStack.Count > 0);

            if (_wallPrefubs?.Length > 0)
            {
                wallsPositions = FillWalls();
            }

            return(new Labyrinth(wallsPositions.ToArray(), cellsPositions.ToArray(), _parent));
        }