// Get the room tile location of the given level tile coordinate. public Point2I GetTileLocation(LevelTileCoord levelCoord) { Point2I roomLocation = GetRoomLocation(levelCoord); return(new Point2I(levelCoord.X - (roomLocation.X * roomSize.X), levelCoord.Y - (roomLocation.Y * roomSize.Y))); }
//----------------------------------------------------------------------------- // Internal Iteration //----------------------------------------------------------------------------- private IEnumerable <BaseTileDataInstance> GetTilesInArea(Rectangle2I area) { // Make sure the area is within the level bounds. area = Rectangle2I.Intersect(area, new Rectangle2I(Point2I.Zero, roomSize * dimensions)); // Iterate the tile grid. for (int x = 0; x < area.Width; x++) { for (int y = 0; y < area.Height; y++) { LevelTileCoord coord = (LevelTileCoord)(area.Point + new Point2I(x, y)); Room room = GetRoom(coord); if (room != null) { Point2I tileLocation = GetTileLocation(coord); for (int i = 0; i < roomLayerCount; i++) { TileDataInstance tile = room.GetTile(tileLocation, i); if (tile != null && tile.Location == tileLocation) { yield return(tile); } } } } } // Determine the collection of rooms that will contain the event tiles. Point2I roomAreaMin = GetRoomLocation((LevelTileCoord)area.Min); Point2I roomAreaMax = GetRoomLocation((LevelTileCoord)area.Max); Rectangle2I roomArea = new Rectangle2I(roomAreaMin, roomAreaMax - roomAreaMin + Point2I.One); roomArea = Rectangle2I.Intersect(roomArea, new Rectangle2I(Point2I.Zero, dimensions)); Rectangle2I pixelArea = new Rectangle2I( area.Point * GameSettings.TILE_SIZE, area.Size * GameSettings.TILE_SIZE); // Iterate event tiles. for (int x = roomArea.Left; x < roomArea.Right; x++) { for (int y = roomArea.Top; y < roomArea.Bottom; y++) { Room room = rooms[x, y]; for (int i = 0; i < room.EventData.Count; i++) { EventTileDataInstance eventTile = room.EventData[i]; Rectangle2I tileBounds = eventTile.GetBounds(); tileBounds.Point += room.Location * roomSize * GameSettings.TILE_SIZE; if (pixelArea.Contains(tileBounds.Point)) { yield return(eventTile); } } } } }
// Get the room containing the given level tile coordinate. public Room GetRoom(LevelTileCoord levelCoord) { Point2I loc = GetRoomLocation(levelCoord); if (ContainsRoom(loc)) { return(rooms[loc.X, loc.Y]); } return(null); }
// Get the room location containing the given level tile coordinate. public Point2I GetRoomLocation(LevelTileCoord levelCoord) { Point2I roomLocation = new Point2I( levelCoord.X / roomSize.X, levelCoord.Y / roomSize.Y); if (levelCoord.X < 0) { roomLocation.X--; } if (levelCoord.Y < 0) { roomLocation.Y--; } return(roomLocation); }
// Place the tiles in a tile grid starting at the given location. public void PlaceTileGrid(TileGrid tileGrid, LevelTileCoord location) { // Remove tiles. Rectangle2I area = new Rectangle2I((Point2I)location, tileGrid.Size); RemoveArea(area); // Place tiles. foreach (BaseTileDataInstance baseTile in tileGrid.GetTiles()) { if (baseTile is TileDataInstance) { TileDataInstance tile = (TileDataInstance)baseTile; LevelTileCoord coord = (LevelTileCoord)((Point2I)location + tile.Location); Room room = GetRoom(coord); if (room != null) { tile.Location = GetTileLocation(coord); room.PlaceTile(tile, tile.Location, tile.Layer); } } else if (baseTile is EventTileDataInstance) { EventTileDataInstance eventTile = (EventTileDataInstance)baseTile; eventTile.Position += (Point2I)location * GameSettings.TILE_SIZE; Point2I roomLocation = eventTile.Position / (roomSize * GameSettings.TILE_SIZE); Room room = GetRoomAt(roomLocation); if (room != null) { eventTile.Position -= roomLocation * roomSize * GameSettings.TILE_SIZE; room.AddEventTile(eventTile); } } } }
// Place the tiles in a tile grid starting at the given location. public void PlaceTileGrid(TileGrid tileGrid, LevelTileCoord location) { // Remove tiles. Rectangle2I area = new Rectangle2I((Point2I) location, tileGrid.Size); RemoveArea(area); // Place tiles. foreach (BaseTileDataInstance baseTile in tileGrid.GetTiles()) { if (baseTile is TileDataInstance) { TileDataInstance tile = (TileDataInstance) baseTile; LevelTileCoord coord = (LevelTileCoord) ((Point2I) location + tile.Location); Room room = GetRoom(coord); if (room != null) { tile.Location = GetTileLocation(coord); room.PlaceTile(tile, tile.Location, tile.Layer); } } else if (baseTile is EventTileDataInstance) { EventTileDataInstance eventTile = (EventTileDataInstance) baseTile; eventTile.Position += (Point2I) location * GameSettings.TILE_SIZE; Point2I roomLocation = eventTile.Position / (roomSize * GameSettings.TILE_SIZE); Room room = GetRoomAt(roomLocation); if (room != null) { eventTile.Position -= roomLocation * roomSize * GameSettings.TILE_SIZE; room.AddEventTile(eventTile); } } } }
//----------------------------------------------------------------------------- // Level Coordinates //----------------------------------------------------------------------------- // Return true if the level tile coordinate is within the level's bounds. public bool IsInBounds(LevelTileCoord levelCoord) { return ContainsRoom(GetRoomLocation(levelCoord)); }
// Get the room tile location of the given level tile coordinate. public Point2I GetTileLocation(LevelTileCoord levelCoord) { Point2I roomLocation = GetRoomLocation(levelCoord); return new Point2I(levelCoord.X - (roomLocation.X * roomSize.X), levelCoord.Y - (roomLocation.Y * roomSize.Y)); }
// Get the room location containing the given level tile coordinate, clamped to the level's bounds. public Point2I GetRoomLocationClamped(LevelTileCoord levelCoord) { return new Point2I( GMath.Clamp(levelCoord.X / roomSize.X, 0, dimensions.X - 1), GMath.Clamp(levelCoord.Y / roomSize.Y, 0, dimensions.Y - 1)); }
// Get the room location containing the given level tile coordinate. public Point2I GetRoomLocation(LevelTileCoord levelCoord) { Point2I roomLocation = new Point2I( levelCoord.X / roomSize.X, levelCoord.Y / roomSize.Y); if (levelCoord.X < 0) roomLocation.X--; if (levelCoord.Y < 0) roomLocation.Y--; return roomLocation; }
// Get the room containing the given level tile coordinate. public Room GetRoom(LevelTileCoord levelCoord) { Point2I loc = GetRoomLocation(levelCoord); if (ContainsRoom(loc)) return rooms[loc.X, loc.Y]; return null; }
// Get the room location containing the given level tile coordinate, clamped to the level's bounds. public Point2I GetRoomLocationClamped(LevelTileCoord levelCoord) { return(new Point2I( GMath.Clamp(levelCoord.X / roomSize.X, 0, dimensions.X - 1), GMath.Clamp(levelCoord.Y / roomSize.Y, 0, dimensions.Y - 1))); }
//----------------------------------------------------------------------------- // Level Coordinates //----------------------------------------------------------------------------- // Return true if the level tile coordinate is within the level's bounds. public bool IsInBounds(LevelTileCoord levelCoord) { return(ContainsRoom(GetRoomLocation(levelCoord))); }