public void CreateDoors() { foreach (Rect room in _rooms) { Vector2Int roomCenter = Vector2Int.CeilToInt(room.center); Array values = Enum.GetValues(typeof(MapUtils.CardinalFourDirections)); MapUtils.CardinalFourDirections randomDirection = (MapUtils.CardinalFourDirections)values.GetValue(_random.Next(0, values.Length)); Vector2Int doorPosition = Vector2Int.zero; switch (randomDirection) { case MapUtils.CardinalFourDirections.NORTH: { doorPosition = new Vector2Int(roomCenter.x, (int)room.max.y); break; } case MapUtils.CardinalFourDirections.SOUTH: { doorPosition = new Vector2Int(roomCenter.x, (int)room.min.y); break; } case MapUtils.CardinalFourDirections.EAST: { doorPosition = new Vector2Int((int)room.min.x, roomCenter.y); break; } case MapUtils.CardinalFourDirections.WEST: { doorPosition = new Vector2Int((int)room.max.x, roomCenter.y); break; } } _map.SetTile(doorPosition.x, doorPosition.y, new Tile(Tile.Type.Empty)); } }
public void Walk(int width, int height) { // ==== Choose Direction ==== float north = 1.0f; float south = 1.0f; float east = 1.0f; float west = 1.0f; //weight the random walk against edges //drunkard is at far left side of map if (_drunkardX < width * 0.25f) { east += _weightedTowardCenter; } //drunkard is at far right side of map else if (_drunkardX > _width * 0.75f) { west += _weightedTowardCenter; } //drunkard is at the top of the map if (_drunkardY < _height * 0.25f) { south += _weightedTowardCenter; } //drunkard is at the bottom of the map else if (_drunkardY > height * 0.75f) { north += _weightedTowardCenter; } //weight the random walk in favor of the previous direction if (_previousDirection == MapUtils.CardinalFourDirections.NORTH) { north += _weightedTowardPreviousDirection; } if (_previousDirection == MapUtils.CardinalFourDirections.SOUTH) { south += _weightedTowardPreviousDirection; } if (_previousDirection == MapUtils.CardinalFourDirections.EAST) { east += _weightedTowardPreviousDirection; } if (_previousDirection == MapUtils.CardinalFourDirections.WEST) { west += _weightedTowardPreviousDirection; } //normalize probabilities so they form a range from 0 to 1 float total = north + south + east + west; north /= total; south /= total; east /= total; west /= total; //Choose the direction MapUtils.CardinalFourDirections direction; double choice = _random.NextDouble(); int dx = 0; int dy = 0; if ((0.0f <= choice) && (choice < north)) { dx = 0; dy = -1; direction = MapUtils.CardinalFourDirections.NORTH; } else if ((north <= choice) && choice < (north + south)) { dx = 0; dy = 1; direction = MapUtils.CardinalFourDirections.SOUTH; } else if (((north + south) <= choice) && (choice < (north + south + east))) { dx = 1; dy = 0; direction = MapUtils.CardinalFourDirections.EAST; } else { dx = -1; dy = 0; direction = MapUtils.CardinalFourDirections.WEST; } //==== Walk ==== if (((0 < _drunkardX + dx) && (_drunkardX + dx < width - 1)) && ((0 < _drunkardY + dy) && (_drunkardY + dy < height - 1))) { _drunkardX += dx; _drunkardY += dy; if (_map.GetTile(_drunkardX, _drunkardY).type.Equals(Tile.Type.Block)) { _map.SetTile(_drunkardX, _drunkardY, new Tile(Tile.Type.Empty)); _filled += 1.0f; } _previousDirection = direction; } }