Skip to content

Commit f678d1f

Browse files
committed
Line now has x, y, width, height, top, bottom, left and right properties, so you can effectively get its bounds.
1 parent a83a76b commit f678d1f

6 files changed

Lines changed: 202 additions & 72 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ New features:
152152
* Merged Georges p2 BodyDebug and reformatted for jshint pass. Looks awesome :)
153153
* ArcadePhysics.Body has a new gravityScale property, which is a modifier multiplied against the world gravity value on a Body.
154154
* Line.coordinatesOnLine will return all coordinates on the line using Bresenhams line algorithm.
155+
* Line now has x, y, width, height, top, bottom, left and right properties, so you can effectively get its bounds.
155156

156157

157158
Updates:

examples/geometry/line bounds.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { create: create, update: update, render: render });
3+
4+
var line;
5+
var setting = false;
6+
7+
function create() {
8+
9+
line = new Phaser.Line(64, 64, 200, 300);
10+
11+
game.input.onDown.add(click, this);
12+
13+
}
14+
15+
function update() {
16+
17+
if (setting)
18+
{
19+
if (game.input.activePointer.isDown)
20+
{
21+
line.end.set(game.input.activePointer.x, game.input.activePointer.y);
22+
}
23+
else
24+
{
25+
setting = false;
26+
}
27+
}
28+
29+
}
30+
31+
function click(pointer) {
32+
33+
setting = true;
34+
line.start.set(pointer.x, pointer.y);
35+
36+
}
37+
38+
function render() {
39+
40+
game.debug.geom(line);
41+
game.debug.rectangle(line);
42+
43+
}

examples/wip/line bounds.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { create: create, update: update, render: render });
3+
4+
var line;
5+
var setting = false;
6+
7+
function create() {
8+
9+
line = new Phaser.Line(64, 64, 200, 300);
10+
11+
game.input.onDown.add(click, this);
12+
13+
}
14+
15+
function update() {
16+
17+
if (setting)
18+
{
19+
if (game.input.activePointer.isDown)
20+
{
21+
line.end.set(game.input.activePointer.x, game.input.activePointer.y);
22+
}
23+
else
24+
{
25+
setting = false;
26+
}
27+
}
28+
29+
}
30+
31+
function click(pointer) {
32+
33+
setting = true;
34+
line.start.set(pointer.x, pointer.y);
35+
36+
}
37+
38+
function render() {
39+
40+
game.debug.geom(line);
41+
game.debug.rectangle(line);
42+
43+
}

src/geom/Line.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,110 @@ Object.defineProperty(Phaser.Line.prototype, "perpSlope", {
228228

229229
});
230230

231+
/**
232+
* @name Phaser.Line#x
233+
* @property {number} x - Gets the x coordinate of the top left of the bounds around this line.
234+
* @readonly
235+
*/
236+
Object.defineProperty(Phaser.Line.prototype, "x", {
237+
238+
get: function () {
239+
return Math.min(this.start.x, this.end.x);
240+
}
241+
242+
});
243+
244+
/**
245+
* @name Phaser.Line#y
246+
* @property {number} y - Gets the y coordinate of the top left of the bounds around this line.
247+
* @readonly
248+
*/
249+
Object.defineProperty(Phaser.Line.prototype, "y", {
250+
251+
get: function () {
252+
return Math.min(this.start.y, this.end.y);
253+
}
254+
255+
});
256+
257+
/**
258+
* @name Phaser.Line#left
259+
* @property {number} left - Gets the left-most point of this line.
260+
* @readonly
261+
*/
262+
Object.defineProperty(Phaser.Line.prototype, "left", {
263+
264+
get: function () {
265+
return Math.min(this.start.x, this.end.x);
266+
}
267+
268+
});
269+
270+
/**
271+
* @name Phaser.Line#right
272+
* @property {number} right - Gets the right-most point of this line.
273+
* @readonly
274+
*/
275+
Object.defineProperty(Phaser.Line.prototype, "right", {
276+
277+
get: function () {
278+
return Math.max(this.start.x, this.end.x);
279+
}
280+
281+
});
282+
283+
/**
284+
* @name Phaser.Line#top
285+
* @property {number} top - Gets the top-most point of this line.
286+
* @readonly
287+
*/
288+
Object.defineProperty(Phaser.Line.prototype, "top", {
289+
290+
get: function () {
291+
return Math.min(this.start.y, this.end.y);
292+
}
293+
294+
});
295+
296+
/**
297+
* @name Phaser.Line#bottom
298+
* @property {number} bottom - Gets the bottom-most point of this line.
299+
* @readonly
300+
*/
301+
Object.defineProperty(Phaser.Line.prototype, "bottom", {
302+
303+
get: function () {
304+
return Math.max(this.start.y, this.end.y);
305+
}
306+
307+
});
308+
309+
/**
310+
* @name Phaser.Line#width
311+
* @property {number} width - Gets the width of this bounds of this line.
312+
* @readonly
313+
*/
314+
Object.defineProperty(Phaser.Line.prototype, "width", {
315+
316+
get: function () {
317+
return Math.abs(this.start.x - this.end.x);
318+
}
319+
320+
});
321+
322+
/**
323+
* @name Phaser.Line#height
324+
* @property {number} height - Gets the height of this bounds of this line.
325+
* @readonly
326+
*/
327+
Object.defineProperty(Phaser.Line.prototype, "height", {
328+
329+
get: function () {
330+
return Math.abs(this.start.y - this.end.y);
331+
}
332+
333+
});
334+
231335
/**
232336
* Checks for intersection between two lines as defined by the given start and end points.
233337
* If asSegment is true it will check for line segment intersection. If asSegment is false it will check for line intersection.

src/tilemap/TilemapLayer.js

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -454,84 +454,23 @@ Phaser.TilemapLayer.prototype.getTileXY = function (x, y, point) {
454454

455455
}
456456

457-
Phaser.TilemapLayer.prototype.getIntersectingTiles = function (x, y, width, height, right, bottom) {
457+
/**
458+
* Gets all tiles that intersect with the given line.
459+
*
460+
* @method Phaser.TilemapLayer#getIntersectingTiles
461+
* @memberof Phaser.TilemapLayer
462+
* @param {Phaser.Line} line - The line used to determine which tiles to return.
463+
* @return {array<Phaser.Tile>} An array of Phaser.Tiles.
464+
*/
465+
Phaser.TilemapLayer.prototype.getIntersectingTiles = function (line) {
458466

459467
var tiles = this.getTiles(x, y, width, height, false);
460468

461-
// We only want the ones that we actually intersect with
462-
var i = tiles.length;
463-
464-
while (i--)
465-
{
466-
if (!tiles[i].intersects(x, y, right, bottom))
467-
{
468-
tiles.pop();
469-
}
470-
}
471469

472470
return tiles;
473471

474472
}
475473

476-
/*
477-
Phaser.TilemapLayer.prototype.getTilesX = function (x, y, width, height, collides) {
478-
479-
// Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all)
480-
if (typeof collides === 'undefined') { collides = false; }
481-
482-
// adjust the x,y coordinates for scrollFactor
483-
x = this._fixX(x);
484-
y = this._fixY(y);
485-
486-
if (width > this.layer.widthInPixels)
487-
{
488-
width = this.layer.widthInPixels;
489-
}
490-
491-
if (height > this.layer.heightInPixels)
492-
{
493-
height = this.layer.heightInPixels;
494-
}
495-
496-
// Convert the pixel values into tile coordinates
497-
// this._tx = this.game.math.snapToFloor(x, this._cw) / this._cw;
498-
// this._ty = this.game.math.snapToFloor(y, this._ch) / this._ch;
499-
// this._tw = (this.game.math.snapToCeil(width, this._cw) + this._cw) / this._cw;
500-
// this._th = (this.game.math.snapToCeil(height, this._ch) + this._ch) / this._ch;
501-
502-
// var firstTileX = Math.max( Math.floor(res.pos.x / this.tilesize), 0 );
503-
// var lastTileX = Math.min( Math.ceil((res.pos.x + width) / this.tilesize), this.width );
504-
// var tileY = Math.floor( (res.pos.y + pxOffsetY) / this.tilesize );
505-
506-
this._tx = Math.max(Math.floor(x / this.tileWidth), 0);
507-
this._tw = Math.min(Math.ceil((x + width) / this.tileWidth), this.width);
508-
this._ty = Math.floor((y + px) / this.tileHeight);
509-
510-
this._results.length = 0;
511-
512-
for (var wy = this._ty; wy < this._ty + this._th; wy++)
513-
{
514-
for (var wx = this._tx; wx < this._tx + this._tw; wx++)
515-
{
516-
if (this.layer.data[wy] && this.layer.data[wy][wx])
517-
{
518-
if (collides === false || (collides && this.layer.data[wy][wx].canCollide))
519-
{
520-
this._results.push(this.layer.data[wy][wx]);
521-
}
522-
}
523-
}
524-
}
525-
526-
// DEBUG ONLY - REMOVE
527-
this.layer.dirty = true;
528-
529-
return this._results;
530-
531-
}
532-
*/
533-
534-
535474
/**
536475
* Get all tiles that exist within the given area, defined by the top-left corner, width and height. Values given are in pixels, not tiles.
537476
* @method Phaser.TilemapLayer#getTiles
@@ -542,7 +481,7 @@ Phaser.TilemapLayer.prototype.getTilesX = function (x, y, width, height, collide
542481
* @param {number} height - Height of the area to get.
543482
* @param {boolean} [collides=false] - If true only return tiles that collide on one or more faces.
544483
* @param {boolean} [interestingFace=false] - If true only return tiles that have interesting faces.
545-
* @return {array} Array with tiles informations (each contains x, y, and the tile).
484+
* @return {array<Phaser.Tile>} An array of Phaser.Tiles.
546485
*/
547486
Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides, interestingFace) {
548487

src/utils/Debug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ Phaser.Utils.Debug.prototype = {
575575

576576
if (typeof filled === 'undefined') { filled = true; }
577577

578-
color = color || 'rgba(0,255,0,0.4)';
578+
color = color || 'rgba(0, 255, 0, 0.4)';
579579

580580
this.start();
581581

0 commit comments

Comments
 (0)