Skip to content

Commit e564a6c

Browse files
committed
Tile helper methods for getting actual world bounds of a tile
1 parent f941cd4 commit e564a6c

1 file changed

Lines changed: 83 additions & 78 deletions

File tree

src/gameobjects/tilemap/Tile.js

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,89 @@ var Tile = new Class({
240240
return this.tileset ? this.tileset.getTileCollisionGroup(this.index) : null;
241241
},
242242

243+
244+
/**
245+
* Gets the world X position of the left side of the tile, factoring in the layer's position,
246+
* scale and scroll.
247+
* @param {Camera} [camera=main camera] - [description]
248+
* @returns {number}
249+
*/
250+
getLeft: function (camera)
251+
{
252+
var tilemapLayer = this.tilemapLayer;
253+
return tilemapLayer
254+
? tilemapLayer.tileToWorldX(this.x, camera)
255+
: this.x * this.baseWidth;
256+
},
257+
258+
/**
259+
* Gets the world X position of the right side of the tile, factoring in the layer's position,
260+
* scale and scroll.
261+
* @param {Camera} [camera=main camera] - [description]
262+
* @returns {number}
263+
*/
264+
getRight: function (camera)
265+
{
266+
var tilemapLayer = this.tilemapLayer;
267+
return tilemapLayer
268+
? this.getLeft(camera) + this.width * tilemapLayer.scaleX
269+
: this.getLeft(camera) + this.width;
270+
},
271+
272+
/**
273+
* Gets the world Y position of the top side of the tile, factoring in the layer's position,
274+
* scale and scroll.
275+
* @param {Camera} [camera=main camera] - [description]
276+
* @returns {number}
277+
*/
278+
getTop: function (camera)
279+
{
280+
var tilemapLayer = this.tilemapLayer;
281+
282+
// Tiled places tiles on a grid of baseWidth x baseHeight. The origin for a tile in grid
283+
// units is the bottom left, so the y coordinate needs to be adjusted by the difference
284+
// between the base size and this tile's size.
285+
return tilemapLayer
286+
? tilemapLayer.tileToWorldY(this.y, camera) - (this.height - this.baseHeight) * tilemapLayer.scaleY
287+
: this.y * this.baseHeight - (this.height - this.baseHeight);
288+
},
289+
290+
/**
291+
* Gets the world Y position of the bottom side of the tile, factoring in the layer's position,
292+
* scale and scroll.
293+
* @param {Camera} [camera=main camera] - [description]
294+
* @returns {number}
295+
*/
296+
getBottom: function (camera)
297+
{
298+
var tilemapLayer = this.tilemapLayer;
299+
return tilemapLayer
300+
? this.getTop(camera) + this.height * tilemapLayer.scaleY
301+
: this.getTop(camera) + this.height;
302+
},
303+
304+
/**
305+
* Gets the world X position of the center of the tile, factoring in the layer's position,
306+
* scale and scroll.
307+
* @param {Camera} [camera=main camera] - [description]
308+
* @returns {number}
309+
*/
310+
getCenterX: function (camera)
311+
{
312+
return this.getLeft(camera) + this.width / 2;
313+
},
314+
315+
/**
316+
* Gets the world Y position of the center of the tile, factoring in the layer's position,
317+
* scale and scroll.
318+
* @param {Camera} [camera=main camera] - [description]
319+
* @returns {number}
320+
*/
321+
getCenterY: function (camera)
322+
{
323+
return this.getTop(camera) + this.height / 2;
324+
},
325+
243326
/**
244327
* Clean up memory.
245328
*/
@@ -445,84 +528,6 @@ var Tile = new Class({
445528
}
446529
},
447530

448-
/**
449-
* The world position of the left side of the tile. This does not factor in camera scroll, layer
450-
* scale or layer position.
451-
* @property {integer} left
452-
* @readonly
453-
*/
454-
left: {
455-
get: function ()
456-
{
457-
return this.pixelX;
458-
}
459-
},
460-
461-
/**
462-
* The world position of the right side of the tile. This does not factor in camera scroll,
463-
* layer scale or layer position.
464-
* @property {integer} right
465-
* @readonly
466-
*/
467-
right: {
468-
get: function ()
469-
{
470-
return this.pixelX + this.width;
471-
}
472-
},
473-
474-
/**
475-
* The world position of the top side of the tile. This does not factor in camera scroll,
476-
* layer scale or layer position.
477-
* @property {integer} top
478-
* @readonly
479-
*/
480-
top: {
481-
get: function ()
482-
{
483-
return this.pixelY;
484-
}
485-
},
486-
487-
/**
488-
* The world position of the bottom side of the tile. This does not factor in camera scroll,
489-
* layer scale or layer position.
490-
* @property {integer} bottom
491-
* @readonly
492-
*/
493-
bottom: {
494-
get: function ()
495-
{
496-
return this.pixelY + this.height;
497-
}
498-
},
499-
500-
/**
501-
* The x world position of the center of the tile. This does not factor in camera scroll, layer
502-
* scale or layer position.
503-
* @property {integer} centerX
504-
* @readonly
505-
*/
506-
centerX: {
507-
get: function ()
508-
{
509-
return this.pixelX + this.width / 2;
510-
}
511-
},
512-
513-
/**
514-
* The y world position of the center of the tile. This does not factor in camera scroll, layer
515-
* scale or layer position.
516-
* @property {integer} centerY
517-
* @readonly
518-
*/
519-
centerY: {
520-
get: function ()
521-
{
522-
return this.pixelY + this.height / 2;
523-
}
524-
},
525-
526531
/**
527532
* The tileset that contains this Tile. This will only return null if accessed from a LayerData
528533
* instance before the tile is placed within a StaticTilemapLayer or DynamicTilemapLayer.

0 commit comments

Comments
 (0)