protected override void SetSpace() { for (int i = Position.x; i < Position.x + Width; i++) { for (int j = Position.y; j < Position.y + Height; j++) { PositionList.Add(new Vector2Int(i, j)); if (i == Position.x || i == Position.x + Width - 1 || j == Position.y || j == Position.y + Height - 1) { WallList.Add(new Vector2Int(i, j)); } } } Vector2Int treasurePosition = new Vector2Int(); List <Vector2Int> tempList = new List <Vector2Int>(PositionList); for (int i = 0; i < _treasureAmount; i++) { treasurePosition = tempList[Random.Range(0, tempList.Count)]; tempList.Remove(treasurePosition); if (!WallList.Contains(treasurePosition)) { TreasureDic.Add(treasurePosition, new Treasure(RoomData.GetRandomTreasureID())); } else { i--; } } Vector2Int moneyPosition = new Vector2Int(); for (int i = 0; i < _moneyAmount; i++) { moneyPosition = tempList[Random.Range(0, tempList.Count)]; tempList.Remove(moneyPosition); if (!WallList.Contains(moneyPosition)) { MoneyDic.Add(moneyPosition, Random.Range(DungeonData.MinMoney, DungeonData.MaxMoney + 1)); } else { i--; } } }
private void DFS(Vector2Int walker) { Vector2Int direction = new Vector2Int(); Vector2Int newWalker = new Vector2Int(); List <Vector2Int> tempList = new List <Vector2Int>(); while (_spaceQueue.Count > 0) { tempList = new List <Vector2Int>(_directions); while (tempList.Count > 0) { direction = tempList[Random.Range(0, tempList.Count)]; newWalker = walker + direction * 2; if (newWalker.x >= Position.x && newWalker.x < Position.x + Width && newWalker.y >= Position.y && newWalker.y < Position.y + Height && !PositionList.Contains(newWalker)) { PositionList.Add(walker + direction); PositionList.Add(walker + direction * 2); _spaceQueue.Enqueue(walker + direction * 2); if (walker.x == Position.x || walker.x == Position.x + Width - 1 || walker.y == Position.y || walker.y == Position.y + Height - 1) { WallList.Add(walker); } break; } else { tempList.Remove(direction); } } if (tempList.Count > 0) { DFS(newWalker); } else //如果所有的方向都沒有路 { if (TreasureDic.Count < _treasureAmount) { TreasureDic.Add(walker, new Treasure(RoomData.GetRandomTreasureID())); } DFS(_spaceQueue.Dequeue()); } } }
protected override void SetSpace() { for (int i = Position.x; i < Position.x + Width; i++) { for (int j = Position.y; j < Position.y + Height; j++) { PositionList.Add(new Vector2Int(i, j)); if (i == Position.x || i == Position.x + Width - 1 || j == Position.y || j == Position.y + Height - 1) { WallList.Add(new Vector2Int(i, j)); } } } TreasureDic.Add(new Vector2Int(Position.x + Width / 2, Position.y + Height / 2), new Treasure(RoomData.GetRandomTreasureID())); }