AudioAsset
Ease
FastString
FontAsset
NineSlice
PackedSprite
PackedSpriteID
RB
   HardwareSettings
   IRetroBlitGame
RBAsset
Rect2i
ShaderAsset
SoundReference
SpriteGrid
SpriteSheetAsset
TMXMapAsset
   TMXLayer
   TMXLayerLoadState
   TMXObject
   TMXObjectGroup
   TMXProperties
Vector2i

RB.MapShiftChunks

Method  -  Static

public static void MapShiftChunks(int layer, Vector2i shiftChunks)

Parameters

layer int Map layer
shiftChunks Vector2i Shift amount in chunk numbers in the x and y direction.

Returns

Nothing.

Description

Shift the tilemap chunks by the given amount chunks in the X and Y direction. This method is particularly useful while loading infinite maps chunk by chunk.

When handling infinite maps it's wise to only load as many chunks as needed. When the player moves outside of the currently loaded chunks new chunks can be loaded with TMXMapAsset.LoadLayerChunk. However, in the usual case when new chunks are needed many of the existing chunks still apply, and should not need to be reloaded. To help with this the RB.MapShiftChunks can be used to shift chunks by any amount in any direction. This is similar to shifting a 2D array.

After shifting some chunks will be cleared. Which chunks get cleared can be determined algorithmically, or RB.MapChunkEmpty can be called on visible chunks to see if they've become empty and should be backfilled with newly loaded chunks.

Example

const int LAYER_TERRAIN = 0;
TMXMap tmxMap;

Vector2i cameraPos

// Track the top-left-most currently loaded chunk
Vector2i topLeftChunk;

void Initialize() {
    tmxMap.Load("tilemaps/world");
}

void Render() {
    // Calculate the size of a single map chunk
    var chunkPixelSize = new Vector2i(
        RB.MapChunkSize.width * RB.SpriteSize(0).width,
        RB.MapChunkSize.height * RB.SpriteSize(0).height);

    // Figure out which map chunk is in the top left corner of the camera view
    var newTopLeftChunk = new Vector2i(
        cameraPos.x / chunkPixelSize.width,
        cameraPos.y / chunkPixelSize.height);

    if (newTopLeftChunk != topLeftChunk) {
        // Calculate how much the chunks should be shifted
        var shift = topLeftChunk - newTopLeftChunk;
        RB.MapShiftChunks(0, shift);

        // Iterate through all potentially visible chunks. If any are currently empty
        // then load them
        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;
    }

    // Calculate the new camera position
    var newCameraPos = new Vector2i(
        cameraPos.x % chunkPixelSize.width,
        cameraPos.y % chunkPixelSize.height);

    cameraPos = newCameraPos;

    // Update the camera position before drawing
    RB.CameraSet(cameraPos);
    RB.DrawMapLayer(0new Vector2i(x + 1, y + 1));

    RB.CameraReset();
}

See Also

TMXMapAsset
RB.MapChunkEmpty

See Docs

Features - Tilemaps
Features - Tiled TMX Support