Skip to content

Commit 66e5a72

Browse files
committed
Add Phaser.Physics.Arcade.World#wrap and friends
- Phaser.Physics.Arcade.World#wrap - Phaser.Physics.Arcade.World#wrapArray - Phaser.Physics.Arcade.World#wrapObject
1 parent add3184 commit 66e5a72

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

src/physics/arcade/World.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var Set = require('../../structs/Set');
2525
var StaticBody = require('./StaticBody');
2626
var TileIntersectsBody = require('./tilemap/TileIntersectsBody');
2727
var Vector2 = require('../../math/Vector2');
28+
var Wrap = require('../../math/Wrap');
2829

2930
/**
3031
* @classdesc
@@ -1692,6 +1693,80 @@ var World = new Class({
16921693
}
16931694
},
16941695

1696+
/**
1697+
* Wrap an object's coordinates (or several objects' coordinates) within {@link Phaser.Physics.Arcade.World#bounds}.
1698+
*
1699+
* If the object is outside any boundary edge (left, top, right, bottom), it will be moved to the same offset from the opposite edge (the interior).
1700+
*
1701+
* @method Phaser.Physics.Arcade.World#wrap
1702+
* @since [version]
1703+
*
1704+
* @param {any} object - A Game Object, a Group, an object with `x` and `y` coordinates, or an array of such objects.
1705+
* @param {number} [padding=0] - An amount added to each boundary edge during the operation.
1706+
*/
1707+
wrap: function (object, padding)
1708+
{
1709+
if (object.body)
1710+
{
1711+
this.wrapObject(object, padding);
1712+
}
1713+
else if (object.getChildren)
1714+
{
1715+
this.wrapArray(object.getChildren(), padding);
1716+
}
1717+
else if (Array.isArray(object))
1718+
{
1719+
this.wrapArray(object, padding);
1720+
}
1721+
else
1722+
{
1723+
this.wrapObject(object, padding);
1724+
}
1725+
},
1726+
1727+
1728+
/**
1729+
* Wrap each object's coordinates within {@link Phaser.Physics.Arcade.World#bounds}.
1730+
*
1731+
* @method Phaser.Physics.Arcade.World#wrapArray
1732+
* @since [version]
1733+
*
1734+
* @param {any[]} arr
1735+
* @param {number} [padding=0] - An amount added to the boundary.
1736+
*/
1737+
wrapArray: function (arr, padding)
1738+
{
1739+
if (arr.length === 0)
1740+
{
1741+
return;
1742+
}
1743+
1744+
for (var i = 0, len = arr.length; i < len; i++)
1745+
{
1746+
this.wrapObject(arr[i], padding);
1747+
}
1748+
},
1749+
1750+
/**
1751+
* Wrap an object's coordinates within {@link Phaser.Physics.Arcade.World#bounds}.
1752+
*
1753+
* @method Phaser.Physics.Arcade.World#wrapObject
1754+
* @since [version]
1755+
*
1756+
* @param {any} object - A Game Object, a Physics Body, or any object with `x` and `y` coordinates
1757+
* @param {number} [padding=0] - An amount added to the boundary.
1758+
*/
1759+
wrapObject: function (object, padding)
1760+
{
1761+
if (padding === undefined)
1762+
{
1763+
padding = 0;
1764+
}
1765+
1766+
object.x = Wrap(object.x, this.bounds.left - padding, this.bounds.right + padding);
1767+
object.y = Wrap(object.y, this.bounds.top - padding, this.bounds.bottom + padding);
1768+
},
1769+
16951770
/**
16961771
* [description]
16971772
*

0 commit comments

Comments
 (0)