List <Tilemap> allMaps; // including those without uninitialized tile data. public TileRef[] GetTiles(Rectangle screenRectangle) { Tilemap[] tileChunks; screenRectangle.X += CellWidth / 2; screenRectangle.Y += CellHeight / 2; Point topLeft = new Point(screenRectangle.X, screenRectangle.Y); Point bottomRight = new Point(screenRectangle.Right, screenRectangle.Bottom); Tilemap topLeftChunk = ScreenToChunk(topLeft, camera); Tilemap bottomRightChunk = ScreenToChunk(bottomRight, camera); int numberOfTiles = (screenRectangle.Width / CellWidth) * (screenRectangle.Height / CellHeight); if (topLeftChunk == bottomRightChunk) { tileChunks = new Tilemap[1]; tileChunks[0] = topLeftChunk; } else { int numberOfChunks = (screenRectangle.Width / SizeInPixels.X) * (screenRectangle.Height / SizeInPixels.Y); tileChunks = new Tilemap[numberOfChunks]; Point currentChunk = new Point(); int chunk_i = 0; for (int y = topLeft.Y; y < (bottomRight.Y - SizeInPixels.Y); y += SizeInPixels.Y) { for (int x = topLeft.X; x < (bottomRight.X - SizeInPixels.X); x += SizeInPixels.X, chunk_i++) { currentChunk.X = x; currentChunk.Y = y; tileChunks[chunk_i] = ScreenToChunk(currentChunk, camera); } } } TileRef[] returnTiles = new TileRef[numberOfTiles]; int i = 0; Point tileScreenCoords = Point.Zero; for (int y = screenRectangle.Y; y < (screenRectangle.Bottom - CellHeight); y += CellHeight) { for (int x = screenRectangle.X; x < (screenRectangle.Right - CellWidth); x += CellWidth, i++) { tileScreenCoords.X = x; tileScreenCoords.Y = y; TileRef currentTileRef = ScreenToTileRef(tileScreenCoords); returnTiles[i] = currentTileRef; } } return(returnTiles); }
public void SetTileGroup(TileRef[] tileRefs, int tileId) { for (int i = 0; i < tileRefs.Length; i++) { TileRef t = tileRefs[i]; if (t.Chunk != null) { int?index = t.Chunk.TileCoordinatesToTileIndex(t.TileCoordinates); if (index.HasValue) { t.Chunk.Tiles[(int)index] = tileId; } } } }
TileRef ScreenToTileRef(Point screenPosition) { TileRef tile = new TileRef(); Tilemap chunk = ScreenToChunk(screenPosition, camera); if (chunk != null) { Point tileCoords = chunk.ScreenToTileCoordinates(screenPosition, camera); Point chunkCoords = chunk.ChunkCoords; tile.Chunk = chunk; tile.ChunkCoordinates = chunkCoords; tile.TileCoordinates = tileCoords; } else { tile.Chunk = null; } return(tile); }