public void AddRoom(IFloorRoom room, Location origin) { var placedRoom = room.Shift(origin); FloorRooms.Add(placedRoom); foreach (var tile in placedRoom.FloorTiles) { if (tile.X < Width && tile.Y < Height) { var oldTile = GetFloorTile(tile.X, tile.Y); if (oldTile != null) { FloorTiles.Remove(oldTile); } FloorTiles.Add(tile); } } }
// Generates the level from a multi-dimensional array public void CreateBoard(char[,] map) { MAX_ROW = map.GetLength(0); MAX_COL = map.GetLength(1); Node.Walls = new HashSet <Location>(); Node.Pits = new HashSet <Location>(); for (int i = 0; i < MAX_ROW; i++) { for (int j = 0; j < MAX_COL; j++) { Location loc = new Location(i, j); char symbol = map[i, j]; GameObject tile; tile = Instantiate(tilePrefab, loc.ToVector(), Quaternion.identity); tile.transform.SetParent(TileHolder.transform); TileProperties tileProperties = tile.GetComponent <TileProperties>(); if (symbol == '+') { Node.Walls.Add(loc); tileProperties.Type = Item.Wall; } else if (symbol == '-') { Node.Pits.Add(loc); tileProperties.Type = Item.Pit; } else { tileProperties.Type = Item.Floor; FloorTiles.Add(loc, tile); } if (char.ToLower(symbol) >= 'a' && char.ToLower(symbol) <= 'z') { char lwrSymbol = char.ToLower(symbol); if (!Rooms.ContainsKey(lwrSymbol)) { Rooms.Add(lwrSymbol, new Room(lwrSymbol)); } Rooms[lwrSymbol].UnexploredTiles.Add(loc); Util.SetColor(FloorTiles[loc], Color.gray); } else if (symbol == '0') { Cat = new CatAgent(symbol, loc); } else if (symbol >= '1' && symbol <= '9') { Children.Add(symbol, new ChildAgent(symbol, loc)); } else if (symbol == '&') { Obstacles[loc] = new Obstacle(loc); GameObject prefab = objectPrefabs[0]; GameObject go; go = Instantiate(prefab, loc.ToVector(), Quaternion.identity); go.transform.SetParent(ObjectHolder.transform); ObjectPieces.Add(loc, go); } if (symbol >= 'A' && symbol <= 'Z') { Foods[loc] = new Food(loc); GameObject go; go = Instantiate(foodPrefab, loc.ToVector(), Quaternion.identity); go.transform.SetParent(ObjectHolder.transform); ObjectPieces.Add(loc, go); } } } foreach (Room r in Rooms.Values) { foreach (Location loc in r.UnexploredTiles) { HashSet <Location> neighbors = new HashSet <Location>() { new Location(loc.Row + 1, loc.Col), new Location(loc.Row - 1, loc.Col), new Location(loc.Row, loc.Col + 1), new Location(loc.Row, loc.Col - 1) }; foreach (Location neighbor in neighbors) { if (!Node.Walls.Contains(neighbor) && !Obstacles.ContainsKey(neighbor) && !r.UnexploredTiles.Contains(neighbor)) { r.Entrances.Add(loc); } } } } }