Open ddanushkin opened 7 months ago
I managed to solve my problem I used this code from tiled as reference I dont know if it breaks while scale/rotate objects in tiled, but if works for me)
const tilemap = this.add.tilemap(Timemaps.test);
const tileWidth = tilemap.tileWidth;
const halfTileWidth = tileWidth * 0.5;
const tileHeight = tilemap.tileHeight;
const originX = tilemap.height * tileWidth * 0.5;
const tile_grass = tilemap.addTilesetImage('tile_grass', Images.tile_grass)!;
const layer = tilemap.createLayer('Tile Layer 1', tile_grass)!;
//! Layer position fix
const tilemapWidth = tilemap.widthInPixels;
const halfTilemapWidth = tilemapWidth * 0.5;
layer.setPosition(halfTilemapWidth - halfTileWidth, 0);
const objects = tilemap.objects[0].objects;
for (let i = 0; i < objects.length; i++) {
//! Ref: https://github.com/mapeditor/tiled/blob/12ca7077d3d5cd9655929030092681a986dd749a/src/libtiled/isometricrenderer.cpp#L495-L505
const object = objects[i];
const tileY = object.y! / tileHeight;
const tileX = object.x! / tileHeight;
const x = (tileX - tileY) * tileWidth * 0.5 + originX;
const y = (tileX + tileY) * tileHeight * 0.5;
const sprite = this.add.sprite(x, y, (Images as any)[object.name]);
sprite.setOrigin(0.5, 1);
}
About "Layer position fix" part, layer getBounds doesn't match its rendered position
layer.setInteractive();
this.input.enableDebug(layer, 0);
@ddanushkin you are the beast! Thank you!
I spent more than a day on this.
I had a lot of map layers and "layer position fix" just broke my game, but I iterated a bit and found an alternative.
const tileWidth = tilemap.tileWidth;
const tileHeight = tilemap.tileHeight;
// magic offset difference between tiled and phaser
const offsetY = tileHeight * 3;
const offsetX = tileWidth / 2;
const convertObjectCoordinates = (object) => {
const tileY = object.y / tileHeight;
const tileX = object.x / tileHeight;
const x = offsetX + (tileX - tileY) * tileWidth * 0.5;
const y = offsetY + (tileX + tileY) * tileHeight * 0.5;
return { x, y };
};
const objectLayer = tilemap.getObjectLayer("objects");
const objects = objectLayer.objects;
objects.map((item) => {
const { x, y } = convertObjectCoordinates(item);
scene.add
.text(x, y, `I'm here!`, {
fontSize: "16px",
color: "#ffff00",
stroke: "#000000",
strokeThickness: 4,
align: "center",
})
.setOrigin(0.5)
});
Version
Description
I wanted to create the tilemap objects myself and copied logic from here But the positions of the objects were not correct
I created a new map and got exactly the same result with createFromObjects
Example Test Code
Tiled JSON
``` { "compressionlevel": -1, "height": 4, "infinite": false, "layers": [ { "data": [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], "height": 4, "id": 1, "name": "Tile Layer 1", "opacity": 1, "type": "tilelayer", "visible": true, "width": 4, "x": 0, "y": 0 }, { "draworder": "topdown", "id": 2, "name": "Object Layer 1", "objects": [ { "gid": 2, "height": 40, "id": 1, "name": "", "rotation": 0, "type": "", "visible": true, "width": 40, "x": 178.41935483871, "y": 179.58064516129 } ], "opacity": 1, "type": "objectgroup", "visible": true, "x": 0, "y": 0 } ], "nextlayerid": 3, "nextobjectid": 2, "orientation": "isometric", "renderorder": "right-down", "tiledversion": "1.10.2", "tileheight": 108, "tilesets": [ { "columns": 1, "firstgid": 1, "image": "tile_grass.png", "imageheight": 108, "imagewidth": 186, "margin": 0, "name": "tile_grass", "spacing": 0, "tilecount": 1, "tileheight": 108, "tilewidth": 186 }, { "columns": 0, "firstgid": 2, "grid": { "height": 1, "orientation": "orthogonal", "width": 1 }, "margin": 0, "name": "marker", "spacing": 0, "tilecount": 1, "tileheight": 40, "tiles": [ { "id": 0, "image": "marker.png", "imageheight": 40, "imagewidth": 40 } ], "tilewidth": 40 } ], "tilewidth": 186, "type": "map", "version": "1.10", "width": 4 } ```