|
| 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; |
0 commit comments