Skip to content

Commit 0b4a087

Browse files
committed
Adding debug rendering method to tilemap and layers
1 parent 60f20aa commit 0b4a087

5 files changed

Lines changed: 91 additions & 0 deletions

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,14 @@ var Tilemap = new Class({
514514
return TilemapComponents.RemoveTileAtWorldXY(worldX, worldY, replaceWithNull, recalculateFaces, camera, layer);
515515
},
516516

517+
renderDebug: function (graphics, styleConfig, layer)
518+
{
519+
layer = this.getLayer(layer);
520+
if (layer === null) { return this; }
521+
TilemapComponents.RenderDebug(graphics, styleConfig, layer);
522+
return this;
523+
},
524+
517525
replaceByIndex: function (findIndex, newIndex, tileX, tileY, width, height, layer)
518526
{
519527
layer = this.getLayer(layer);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var GetTilesWithin = require('./GetTilesWithin');
2+
var Color = require('../../../display/color');
3+
4+
/**
5+
* Draws a debug representation of the layer to the given Graphics. This is helpful when you want to
6+
* get a quick idea of which of your tiles are colliding and which have interesting faces. The tiles
7+
* are drawn starting at (0, 0) in the Graphics, allowing you to place the debug representation
8+
* wherever you want on the screen.
9+
*
10+
* @param {Graphics} graphics - The target Graphics object to draw upon.
11+
* @param {object} styleConfig - An object specifying the colors to use for the debug drawing.
12+
* @param {Color|null} [styleConfig.tileColor=blue] - Color to use for drawing a filled rectangle at
13+
* non-colliding tile locations. If set to null, non-colliding tiles will not be drawn.
14+
* @param {Color|null} [styleConfig.collidingTileColor=orange] - Color to use for drawing a filled
15+
* rectangle at colliding tile locations. If set to null, colliding tiles will not be drawn.
16+
* @param {Color|null} [styleConfig.faceColor=grey] - Color to use for drawing a line at interesting
17+
* tile faces. If set to null, interesting tile faces will not be drawn.
18+
* @param {LayerData} layer - [description]
19+
*/
20+
var RenderDebug = function (graphics, styleConfig, layer)
21+
{
22+
if (styleConfig === undefined) { styleConfig = {}; }
23+
24+
// Default colors without needlessly creating Color objects
25+
var tileColor = styleConfig.tileColor !== undefined
26+
? styleConfig.tileColor
27+
: new Color(105, 210, 231, 150);
28+
var collidingTileColor = styleConfig.collidingTileColor !== undefined
29+
? styleConfig.collidingTileColor
30+
: new Color(243, 134, 48, 200);
31+
var faceColor = styleConfig.faceColor !== undefined
32+
? styleConfig.faceColor
33+
: new Color(40, 39, 37, 150);
34+
35+
var tiles = GetTilesWithin(0, 0, layer.width, layer.height, null, layer);
36+
37+
for (var i = 0; i < tiles.length; i++)
38+
{
39+
var tile = tiles[i];
40+
41+
var tw = tile.width;
42+
var th = tile.height;
43+
var x = tile.worldX;
44+
var y = tile.worldY;
45+
46+
var color = tile.collides ? collidingTileColor : tileColor;
47+
if (color !== null)
48+
{
49+
graphics.fillStyle(color.color, color.alpha / 255);
50+
graphics.fillRect(x, y, tw, th);
51+
}
52+
53+
// Inset the face line to prevent neighboring tile's lines from overlapping
54+
x += 1;
55+
y += 1;
56+
tw -= 2;
57+
th -= 2;
58+
59+
if (faceColor !== null)
60+
{
61+
graphics.lineStyle(1, faceColor.color, faceColor.alpha / 255);
62+
if (tile.faceTop) { graphics.lineBetween(x, y, x + tw, y); }
63+
if (tile.faceRight) { graphics.lineBetween(x + tw, y, x + tw, y + th); }
64+
if (tile.faceBottom) { graphics.lineBetween(x, y + th, x + tw, y + th); }
65+
if (tile.faceLeft) { graphics.lineBetween(x, y, x, y + th); }
66+
}
67+
}
68+
};
69+
70+
module.exports = RenderDebug;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
RemoveTileAt: require('./RemoveTileAt'),
2323
RemoveTileAtWorldXY: require('./RemoveTileAtWorldXY'),
2424
ReplaceByIndex: require('./ReplaceByIndex'),
25+
RenderDebug: require('./RenderDebug'),
2526
SetCollision: require('./SetCollision'),
2627
SetCollisionBetween: require('./SetCollisionBetween'),
2728
SetCollisionByExclusion: require('./SetCollisionByExclusion'),

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ var DynamicTilemapLayer = new Class({
164164
return TilemapComponents.RemoveTileAtWorldXY(worldX, worldY, replaceWithNull, recalculateFaces, camera, this.layer);
165165
},
166166

167+
renderDebug: function (graphics, styleConfig)
168+
{
169+
TilemapComponents.RenderDebug(graphics, styleConfig, this.layer);
170+
return this;
171+
},
172+
167173
replaceByIndex: function (findIndex, newIndex, tileX, tileY, width, height)
168174
{
169175
TilemapComponents.ReplaceByIndex(findIndex, newIndex, tileX, tileY, width, height, this.layer);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ var StaticTilemapLayer = new Class({
279279
return TilemapComponents.HasTileAtWorldXY(worldX, worldY, camera, this.layer);
280280
},
281281

282+
renderDebug: function (graphics, styleConfig)
283+
{
284+
TilemapComponents.RenderDebug(graphics, styleConfig, this.layer);
285+
return this;
286+
},
287+
282288
setCollision: function (indexes, collides, recalculateFaces)
283289
{
284290
TilemapComponents.SetCollision(indexes, collides, recalculateFaces, this.layer);

0 commit comments

Comments
 (0)