Skip to content

Commit b34d18e

Browse files
committed
World to Tile conversion on maps and layers - both accounting for camera scroll
1 parent 205675c commit b34d18e

7 files changed

Lines changed: 107 additions & 1 deletion

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ var Tilemap = new Class({
333333
putTile: function (tile, tileX, tileY, layer)
334334
{
335335
layer = this.getLayer(layer);
336+
if (this._isStaticCall(layer, 'putTile')) { return null; }
336337
if (layer === null) { return null; }
337338
return TilemapComponents.PutTile(tile, tileX, tileY, layer);
338339
},
@@ -414,6 +415,27 @@ var Tilemap = new Class({
414415
return this;
415416
},
416417

418+
worldToTileX: function (worldX, camera, layer)
419+
{
420+
layer = this.getLayer(layer);
421+
if (layer === null) { return null; }
422+
return TilemapComponents.WorldToTileX(worldX, camera, layer);
423+
},
424+
425+
worldToTileY: function (worldY, camera, layer)
426+
{
427+
layer = this.getLayer(layer);
428+
if (layer === null) { return null; }
429+
return TilemapComponents.WorldToTileY(worldY, camera, layer);
430+
},
431+
432+
worldToTileXY: function (worldX, worldY, point, camera, layer)
433+
{
434+
layer = this.getLayer(layer);
435+
if (layer === null) { return null; }
436+
return TilemapComponents.WorldToTileXY(worldX, worldY, point, camera, layer);
437+
},
438+
417439
_isStaticCall: function (layer, functionName)
418440
{
419441
if (layer.tilemapLayer instanceof StaticTilemapLayer)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var SnapFloor = require('../../../math/snap/SnapFloor');
2+
3+
var WorldToTileX = function (worldX, camera, layer)
4+
{
5+
var tilemapLayer = layer.tilemapLayer;
6+
if (tilemapLayer)
7+
{
8+
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
9+
10+
// Find the world position relative to the static or dynamic layer's top left origin,
11+
// factoring in the camera's horizontal scroll
12+
worldX = worldX + (camera.scrollX * tilemapLayer.scrollFactorX) - tilemapLayer.x;
13+
}
14+
15+
return SnapFloor(worldX, layer.tileWidth) / layer.tileWidth;
16+
};
17+
18+
module.exports = WorldToTileX;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var WorldToTileX = require('./WorldToTileX');
2+
var WorldToTileY = require('./WorldToTileY');
3+
var Vector2 = require('../../../math/Vector2');
4+
5+
var WorldToTileXY = function (worldX, worldY, point, camera, layer)
6+
{
7+
if (point === undefined) { point = new Vector2(0, 0); }
8+
9+
point.x = WorldToTileX(worldX, camera, layer);
10+
point.y = WorldToTileY(worldY, camera, layer);
11+
12+
return point;
13+
};
14+
15+
module.exports = WorldToTileXY;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var SnapFloor = require('../../../math/snap/SnapFloor');
2+
3+
var WorldToTileY = function (worldY, camera, layer)
4+
{
5+
var tilemapLayer = layer.tilemapLayer;
6+
if (tilemapLayer)
7+
{
8+
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
9+
10+
// Find the world position relative to the static or dynamic layer's top left origin,
11+
// factoring in the camera's horizontal scroll
12+
worldY = worldY + (camera.scrollY * tilemapLayer.scrollFactorY) - tilemapLayer.y;
13+
}
14+
15+
return SnapFloor(worldY, layer.tileWidth) / layer.tileWidth;
16+
};
17+
18+
module.exports = WorldToTileY;

v3/src/gameobjects/tilemap/components/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = {
1414
RemoveTile: require('./RemoveTile'),
1515
ReplaceByIndex: require('./ReplaceByIndex'),
1616
Shuffle: require('./Shuffle'),
17-
SwapByIndex: require('./SwapByIndex')
17+
SwapByIndex: require('./SwapByIndex'),
18+
WorldToTileX: require('./WorldToTileX'),
19+
WorldToTileXY: require('./WorldToTileXY'),
20+
WorldToTileY: require('./WorldToTileY')
1821

1922
};

v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ var DynamicTilemapLayer = new Class({
184184
{
185185
TilemapComponents.SwapByIndex(indexA, indexB, tileX, tileY, width, height, this.layer);
186186
return this;
187+
},
188+
189+
worldToTileX: function (worldX, camera)
190+
{
191+
return TilemapComponents.WorldToTileX(worldX, camera, this.layer);
192+
},
193+
194+
worldToTileY: function (worldY, camera)
195+
{
196+
return TilemapComponents.WorldToTileY(worldY, camera, this.layer);
197+
},
198+
199+
worldToTileXY: function (worldX, worldY, point, camera)
200+
{
201+
return TilemapComponents.WorldToTileXY(worldX, worldY, point, camera, this.layer);
187202
}
188203

189204
});

v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ var StaticTilemapLayer = new Class({
311311
hasTileAt: function (tileX, tileY)
312312
{
313313
return TilemapComponents.HasTileAt(tileX, tileY, this.layer);
314+
},
315+
316+
worldToTileX: function (worldX, camera)
317+
{
318+
return TilemapComponents.WorldToTileX(worldX, camera, this.layer);
319+
},
320+
321+
worldToTileY: function (worldY, camera)
322+
{
323+
return TilemapComponents.WorldToTileY(worldY, camera, this.layer);
324+
},
325+
326+
worldToTileXY: function (worldX, worldY, point, camera)
327+
{
328+
return TilemapComponents.WorldToTileXY(worldX, worldY, point, camera, this.layer);
314329
}
315330

316331
});

0 commit comments

Comments
 (0)