Skip to content

Commit

Permalink
Line now has x, y, width, height, top, bottom, left and right propert…
Browse files Browse the repository at this point in the history
…ies, so you can effectively get its bounds.
  • Loading branch information
photonstorm committed Mar 14, 2014
1 parent a83a76b commit f678d1f
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 72 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ New features:
* Merged Georges p2 BodyDebug and reformatted for jshint pass. Looks awesome :)
* ArcadePhysics.Body has a new gravityScale property, which is a modifier multiplied against the world gravity value on a Body.
* Line.coordinatesOnLine will return all coordinates on the line using Bresenhams line algorithm.
* Line now has x, y, width, height, top, bottom, left and right properties, so you can effectively get its bounds.


Updates:
Expand Down
43 changes: 43 additions & 0 deletions examples/geometry/line bounds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { create: create, update: update, render: render });

var line;
var setting = false;

function create() {

line = new Phaser.Line(64, 64, 200, 300);

game.input.onDown.add(click, this);

}

function update() {

if (setting)
{
if (game.input.activePointer.isDown)
{
line.end.set(game.input.activePointer.x, game.input.activePointer.y);
}
else
{
setting = false;
}
}

}

function click(pointer) {

setting = true;
line.start.set(pointer.x, pointer.y);

}

function render() {

game.debug.geom(line);
game.debug.rectangle(line);

}
43 changes: 43 additions & 0 deletions examples/wip/line bounds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { create: create, update: update, render: render });

var line;
var setting = false;

function create() {

line = new Phaser.Line(64, 64, 200, 300);

game.input.onDown.add(click, this);

}

function update() {

if (setting)
{
if (game.input.activePointer.isDown)
{
line.end.set(game.input.activePointer.x, game.input.activePointer.y);
}
else
{
setting = false;
}
}

}

function click(pointer) {

setting = true;
line.start.set(pointer.x, pointer.y);

}

function render() {

game.debug.geom(line);
game.debug.rectangle(line);

}
104 changes: 104 additions & 0 deletions src/geom/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,110 @@ Object.defineProperty(Phaser.Line.prototype, "perpSlope", {

});

/**
* @name Phaser.Line#x
* @property {number} x - Gets the x coordinate of the top left of the bounds around this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "x", {

get: function () {
return Math.min(this.start.x, this.end.x);
}

});

/**
* @name Phaser.Line#y
* @property {number} y - Gets the y coordinate of the top left of the bounds around this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "y", {

get: function () {
return Math.min(this.start.y, this.end.y);
}

});

/**
* @name Phaser.Line#left
* @property {number} left - Gets the left-most point of this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "left", {

get: function () {
return Math.min(this.start.x, this.end.x);
}

});

/**
* @name Phaser.Line#right
* @property {number} right - Gets the right-most point of this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "right", {

get: function () {
return Math.max(this.start.x, this.end.x);
}

});

/**
* @name Phaser.Line#top
* @property {number} top - Gets the top-most point of this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "top", {

get: function () {
return Math.min(this.start.y, this.end.y);
}

});

/**
* @name Phaser.Line#bottom
* @property {number} bottom - Gets the bottom-most point of this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "bottom", {

get: function () {
return Math.max(this.start.y, this.end.y);
}

});

/**
* @name Phaser.Line#width
* @property {number} width - Gets the width of this bounds of this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "width", {

get: function () {
return Math.abs(this.start.x - this.end.x);
}

});

/**
* @name Phaser.Line#height
* @property {number} height - Gets the height of this bounds of this line.
* @readonly
*/
Object.defineProperty(Phaser.Line.prototype, "height", {

get: function () {
return Math.abs(this.start.y - this.end.y);
}

});

/**
* Checks for intersection between two lines as defined by the given start and end points.
* If asSegment is true it will check for line segment intersection. If asSegment is false it will check for line intersection.
Expand Down
81 changes: 10 additions & 71 deletions src/tilemap/TilemapLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,84 +454,23 @@ Phaser.TilemapLayer.prototype.getTileXY = function (x, y, point) {

}

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

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

// We only want the ones that we actually intersect with
var i = tiles.length;

while (i--)
{
if (!tiles[i].intersects(x, y, right, bottom))
{
tiles.pop();
}
}

return tiles;

}

/*
Phaser.TilemapLayer.prototype.getTilesX = function (x, y, width, height, collides) {
// Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all)
if (typeof collides === 'undefined') { collides = false; }
// adjust the x,y coordinates for scrollFactor
x = this._fixX(x);
y = this._fixY(y);
if (width > this.layer.widthInPixels)
{
width = this.layer.widthInPixels;
}
if (height > this.layer.heightInPixels)
{
height = this.layer.heightInPixels;
}
// Convert the pixel values into tile coordinates
// this._tx = this.game.math.snapToFloor(x, this._cw) / this._cw;
// this._ty = this.game.math.snapToFloor(y, this._ch) / this._ch;
// this._tw = (this.game.math.snapToCeil(width, this._cw) + this._cw) / this._cw;
// this._th = (this.game.math.snapToCeil(height, this._ch) + this._ch) / this._ch;
// var firstTileX = Math.max( Math.floor(res.pos.x / this.tilesize), 0 );
// var lastTileX = Math.min( Math.ceil((res.pos.x + width) / this.tilesize), this.width );
// var tileY = Math.floor( (res.pos.y + pxOffsetY) / this.tilesize );
this._tx = Math.max(Math.floor(x / this.tileWidth), 0);
this._tw = Math.min(Math.ceil((x + width) / this.tileWidth), this.width);
this._ty = Math.floor((y + px) / this.tileHeight);
this._results.length = 0;
for (var wy = this._ty; wy < this._ty + this._th; wy++)
{
for (var wx = this._tx; wx < this._tx + this._tw; wx++)
{
if (this.layer.data[wy] && this.layer.data[wy][wx])
{
if (collides === false || (collides && this.layer.data[wy][wx].canCollide))
{
this._results.push(this.layer.data[wy][wx]);
}
}
}
}
// DEBUG ONLY - REMOVE
this.layer.dirty = true;
return this._results;
}
*/


/**
* 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.
* @method Phaser.TilemapLayer#getTiles
Expand All @@ -542,7 +481,7 @@ Phaser.TilemapLayer.prototype.getTilesX = function (x, y, width, height, collide
* @param {number} height - Height of the area to get.
* @param {boolean} [collides=false] - If true only return tiles that collide on one or more faces.
* @param {boolean} [interestingFace=false] - If true only return tiles that have interesting faces.
* @return {array} Array with tiles informations (each contains x, y, and the tile).
* @return {array<Phaser.Tile>} An array of Phaser.Tiles.
*/
Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides, interestingFace) {

Expand Down
2 changes: 1 addition & 1 deletion src/utils/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ Phaser.Utils.Debug.prototype = {

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

color = color || 'rgba(0,255,0,0.4)';
color = color || 'rgba(0, 255, 0, 0.4)';

this.start();

Expand Down

0 comments on commit f678d1f

Please sign in to comment.