TheDogOfChaos
TheDogOfChaos

Reputation: 11

Multiple TiledMap layers not rendering

I'm having trouble with rendering multiple tile layers in a single programatically generated TiledMap within LibGDX.

I tried to make it so that a floor layer (the first for loop) gets generated underneath the procedurally generated layers (second for loop), but it only renders what was added before the first invocation of layers.add();, which is my floor layer. Is there something I'm blatently missing here?

public class WorldGenerator {
    private static Texture tiles;
    private static TiledMap map;
    public static TiledMap GenerateWorld(int mapWidth, int mapHeight, int tileWidth, int tileHeight, int exponent, int mapLayers, String noiseType) {
        int x; int y; int l;
        tiles = new Texture(Gdx.files.internal("spriteAtlas.png"));
        TextureRegion[][] splitTiles = TextureRegion.split(tiles, tileWidth, tileHeight);
        map = new TiledMap();
        TiledMapTileLayer layer = new TiledMapTileLayer(mapWidth, mapHeight, tileWidth, tileHeight);
        MapLayers layers = map.getLayers();
        x=0;y=0;
        for (int i = 0; i < (mapWidth * mapHeight); i++) {
            Cell cell = new Cell();
            cell.setTile(new StaticTiledMapTile(splitTiles[1][0]));
            layer.setCell(x, y, cell);
            x++;
            if (x == mapWidth) {
                y++;
                x = 0;
            }
        }
        layers.add(layer);
        for (l = 0; l < mapLayers; l++) {
            double[] noise;
            switch (noiseType) {
                case "perlin":
                    noise = NoiseGenerator.normalise(NoiseGenerator.perlinNoise(mapWidth, mapHeight, exponent));
                    break;
                case "smooth":
                     noise = NoiseGenerator.normalise(NoiseGenerator.smoothNoise(mapWidth, mapHeight, exponent));
                    break;
                case "turbulence":
                     noise = NoiseGenerator.normalise(NoiseGenerator.turbulence(mapWidth, mapHeight, exponent));
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + noiseType);
            }
            for (double v : noise) {
                Cell cell = new Cell();
                if (Math.round(v) == 1) {
                    cell.setTile(new StaticTiledMapTile(splitTiles[0][1]));
                }
                layer.setCell(x, y, cell);
                x++;
                if (x == mapWidth) {
                    y++;
                    x = 0;
                }
            }
            layers.add(layer);
        }
        return map;
    }
}

Update (19/04/2024):

i have given up with trying to do this via TiledMaps, and have instead just resorted to a 3D Array

Upvotes: 1

Views: 36

Answers (0)

Related Questions