RB.MapChunkEmpty
Method - Static
public static bool MapChunkEmpty(int layer, Vector2i pos)
|
Parameters
layer
|
int
|
Chunk layer
|
pos
|
Vector2i
|
Offset of the chunk in tile coordinates
|
Returns
bool
True if empty, false otherwise
DescriptionCheck if a map chunk at the given position is empty/unloaded. A chunk can become empty if its been shifted out by RB.MapShiftChunks, or has been released internally because it has not been visible for some time. This method is especially useful when handling infinite maps and dynamically loading map chunks as the player moves around the world. Exampleconst int LAYER_TERRAIN = 0; TMXMap tmxMap;
Vector2i cameraPos
Vector2i topLeftChunk;
void Initialize() { tmxMap.Load("tilemaps/world"); }
void Render() { var chunkPixelSize = new Vector2i( RB.MapChunkSize.width * RB.SpriteSize(0).width, RB.MapChunkSize.height * RB.SpriteSize(0).height);
var newTopLeftChunk = new Vector2i( cameraPos.x / chunkPixelSize.width, cameraPos.y / chunkPixelSize.height);
if (newTopLeftChunk != topLeftChunk) { var shift = topLeftChunk - newTopLeftChunk; RB.MapShiftChunks(0, shift);
for (int cy = 0; cy <= (RB.DisplaySize.height / chunkPixelSize.height) + 1; cy++) { for (int cx = 0; cx <= (RB.DisplaySize.width / chunkPixelSize.width) + 1; cx++) { var chunkPos = new Vector2i(cx * RB.MapChunkSize.x, cy * RB.MapChunkSize.y); var mapPos = new Vector2i( newTopLeftChunk.x * RB.MapChunkSize.x, newTopLeftChunk.y * RB.MapChunkSize.y) + chunkPos; mapPos.x = mapPos.x % tmxMap.size.width;
if (RB.MapChunkEmpty(LAYER_TERRAIN, chunkPos)) { tmxMap.LoadLayerChunk("Terrain", LAYER_TERRAIN, mapPos, chunkPos); } } }
topLeftChunk = newTopLeftChunk; }
var newCameraPos = new Vector2i( cameraPos.x % chunkPixelSize.width, cameraPos.y % chunkPixelSize.height);
cameraPos = newCameraPos;
RB.CameraSet(cameraPos); RB.DrawMapLayer(0, new Vector2i(x + 1, y + 1));
RB.CameraReset(); } |
See AlsoTMXMapAsset RB.MapShiftChunksSee DocsFeatures - Tilemaps Features - Tiled TMX Support |