public bool CanCreateFeature(FeaturePosition position, Vector2Int size, Tilemap tilemap) { Vector3Int p = new Vector3Int(-1, -1, 0); size += Vector2Int.one * 2; // Check if there is space for a new room for (int i = 0; i < size.x * size.y; i++) { // if we find a tile on the position we are checking that means // we cant add the room there since there's probably something // there already if (tilemap.HasTile(p + position)) { Debug.Log($"{p+position} {tilemap.GetTile(p+position).name}"); return(false); } // increament the position we want to check after we've checked the // current position if (p.x == size.x - 1) { p.x = 0; p.y++; } else { p.x++; } } // we didnt find any existing tiles in the place we want to // add the room, so theres space to add it return(true); }
public Feature(string source, int serialNo, int x, int y, int width, int height, byte[] content) { Source = source; SerialNo = serialNo; Position = new FeaturePosition(x, y, width, height); Content = content; }
public RoomData(FeaturePosition position, Vector2Int size, Vector3Int entrance, Tilemap tilemap) { Position = position; Size = size; Exits = new List <Vector3Int> { entrance }; Bounds = new Bounds(); Bounds.SetMinMax(position, new Vector3(position.x + size.x, position.y + size.y)); _directions = new List <Direction> { Direction.Bottom, Direction.Left, Direction.Right, Direction.Top }; var intD = (int)position.Direction; if (position.Direction != Direction.None) { if (intD > 1) { _directions.Remove((Direction)intD - 2); } else { _directions.Remove((Direction)intD + 2); } } var tile = ScriptableObject.CreateInstance <FloorTile>(); tile.colliderType = Tile.ColliderType.None; tile.color = GameSettings.Instance.HiddenColor; tile.sprite = GameSettings.Instance.FloorTileData.Sprite; tilemap.SetTile(entrance, tile); // Add tiles to the tilemap Vector3Int tilepos = Vector3Int.zero; for (int i = 0; i < Size.x * Size.y; i++) { tile = ScriptableObject.CreateInstance <FloorTile>(); tile.colliderType = Tile.ColliderType.None; tile.color = GameSettings.Instance.HiddenColor; tile.sprite = GameSettings.Instance.FloorTileData.Sprite; tilemap.SetTile(position + tilepos, tile); if (tilepos.x == Size.x - 1) { tilepos.x = 0; tilepos.y++; } else { tilepos.x++; } } }
private void GenerateFeatures() { AllFeatures = new List <IFeature>(); _currFeatureCount = 0; // start top left FeaturePosition position = new FeaturePosition(1, 1, Direction.None); Vector3Int size = RoomSize.GetRandomValue(); IFeature tempFeature = null; StartPos = position + new Vector3Int(size.x / 2, size.y / 2, 0); _factories[0].TryCreateFeature(position, size.ToVector2Int(), Tilemap, ref tempFeature); _featureQueue.Enqueue(tempFeature); AllFeatures.Add(tempFeature); _currFeatureCount++; while (_featureQueue.Count > 0 && _currFeatureCount < FeatureCount) { var lastFeature = _featureQueue.Dequeue(); position = lastFeature.GetNewFeaturePosition(); size = RoomSize.GetRandomValue(); Debug.Log($"feature at {position.ToString()} of size {size}"); IFeatureFactory factory = _factories[Random.Range(0, _factories.Count)]; IFeature newFeature = null; if (factory.TryCreateFeature(position, size.ToVector2Int(), Tilemap, ref newFeature)) { lastFeature.AddExit(position); AllFeatures.Add(newFeature); _featureQueue.Enqueue(newFeature); _currFeatureCount++; } if (lastFeature.CanMakeNewFeature()) { _featureQueue.Enqueue(lastFeature); } } }
public bool TryCreateFeature(FeaturePosition position, Vector2Int size, Tilemap tilemap, ref IFeature feature) { Vector3Int entrance = position; switch (position.Direction) { case Direction.Top: position.x = position.x - Random.Range(1, size.x - 1); position.y = position.y + 1; break; case Direction.Right: position.x = position.x + 1; position.y = position.y - Random.Range(1, size.y - 1); break; case Direction.Bottom: position.x = position.x - Random.Range(1, size.x - 1); position.y = position.y - size.y; break; case Direction.Left: position.x = position.x - size.x; position.y = position.y - Random.Range(1, size.y - 1); break; } if (CanCreateFeature(position, size, tilemap)) { Debug.Log($"room at {position.ToString()}"); // we aren't gonna check if we can add here, we just gonna add the room // even if it overlaps, checking should be done where this is called RoomData room = new RoomData(position, size, entrance, tilemap); _currentRooms.Add(room); feature = room; return(true); } return(false); }