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

TMXMapAsset.LoadLayerChunk

Method

public TMXLayerLoadState LoadLayerChunk(string sourceLayerName, int destinationLayer, Vector2i chunkOffset, Vector2i destPos)
public TMXLayerLoadState LoadLayerChunk(string sourceLayerName, int destinationLayer, Vector2i chunkOffset, Vector2i destPos, PackedSpriteID [] packedSpriteLookup)
public TMXLayerLoadState LoadLayerChunk(string sourceLayerName, int destinationLayer, Vector2i chunkOffset, Vector2i destPos, PackedSprite [] packedSpriteLookup)
public TMXLayerLoadState LoadLayerChunk(string sourceLayerName, int destinationLayer, Vector2i chunkOffset, Vector2i destPos, FastString [] packedSpriteLookup)
public TMXLayerLoadState LoadLayerChunk(string sourceLayerName, int destinationLayer, Vector2i chunkOffset, Vector2i destPos, string [] packedSpriteLookup)

Parameters

sourceLayerName string Name of the TMX layer. Duplicate layer names are not supported
destinationLayer int The RetroBlit layer to load into
chunkOffset Vector2i Chunk offset in the TMX layer in tile coordinates
destPos Vector2i Destination position in the RetroBlit layer in tile coordinates
packedSpriteLookup PackedSpriteID [] Lookup table for translating TMX tile indexes to packed sprites

Returns

TMXLayerLoadState

Layer loading state

Description

Load a TMX map layer chunk from an infinite map. For standard fixed maps use TMXMapAsset.LoadLayer instead. There are various asset sources supported:

  • Resources - Synchronously loaded TMX Map assets from a Resources folder. This was the only asset source supported in RetroBlit prior to 3.0.
  • ResourcesAsync - Asynchronously loaded TMX Map assets from a Resources folder.
  • WWW - Asynchronously loaded TMX Map assets from a URL.
  • AddressableAssets - Asynchronously loaded TMX Map assets from Unity Addressable Assets.

The layer into which the layer chunk tile data is loaded if specified by destinationLayer. The source is specified by chunkOffset, and the destination position of the tile data is specified with destPos.

The sprite sheet or sprite pack can be specified for this layer by passing spriteSheet parameter, or later by RB.MapLayerSpriteSheetSet. If a sprite pack is specified then a lookup array can be passed using packedSpriteLookup, which will help RetroBlit translate from tilemap sprite indices to packed sprites.

Besides tile sprite information TMXMapAsset.LoadLayerChunk also loads TMXProperties for each tile. These properties can be set inside of Tiled. This can be very useful for defining gameplay affecting properties such as whether a particular tile is "blocking".

Example

const int LAYER_TERRAIN = 0;
TMXMapAsset tmxMap = new TMXMapAsset;

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.LoadTMXLayerChunk("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(LAYER_TERRAIN, new Vector2i(x + 1, y + 1));

    RB.CameraReset();
}

See Also

TMXMapAsset.Load
TMXMapAsset.LoadLayerChunk
RB.MapChunkEmpty
RB.MapShiftChunks
RB.Result
RB.AssetStatus
RB.AssetSource

See Docs

Features - Tilemaps
Features - Tiled TMX Support
Features - Asynchronous Asset Loading