Merge pull request #3366 from samme/feature/world-wrap

Add Arcade.World wrap methods and Actions.WrapInRectangle
This commit is contained in:
Richard Davey 2018-03-16 12:58:16 +00:00 committed by GitHub
commit 7a8e4cf5e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 2 deletions

View file

@ -0,0 +1,41 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @author samme <samme.npm@gmail.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Wrap = require('../math/Wrap');
/**
* Wrap each item's coordinates within a rectangle's area.
*
* @function Phaser.Actions.WrapInRectangle
* @since [version]
* @see Phaser.Math.Wrap
*
* @param {array} items - An array of Game Objects. The contents of this array are updated by this Action.
* @param {Phaser.Geom.Rectangle} rect - The rectangle.
* @param {number} [padding=0] - An amount added to each side of the rectangle during the operation.
*
* @return {array} The array of Game Objects that was passed to this Action.
*/
var WrapInRectangle = function (items, rect, padding)
{
if (padding === undefined)
{
padding = 0;
}
for (var i = 0; i < items.length; i++)
{
var item = items[i];
item.x = Wrap(item.x, rect.left - padding, rect.right + padding);
item.y = Wrap(item.y, rect.top - padding, rect.bottom + padding);
}
return items;
};
module.exports = WrapInRectangle;

View file

@ -54,6 +54,7 @@ module.exports = {
SmootherStep: require('./SmootherStep'),
SmoothStep: require('./SmoothStep'),
Spread: require('./Spread'),
ToggleVisible: require('./ToggleVisible')
ToggleVisible: require('./ToggleVisible'),
WrapInRectangle: require('./WrapInRectangle')
};

View file

@ -25,6 +25,7 @@ var Set = require('../../structs/Set');
var StaticBody = require('./StaticBody');
var TileIntersectsBody = require('./tilemap/TileIntersectsBody');
var Vector2 = require('../../math/Vector2');
var Wrap = require('../../math/Wrap');
/**
* @classdesc
@ -617,7 +618,7 @@ var World = new Class({
* @since 3.0.0
*
* @param {Phaser.Physics.Arcade.Collider} collider - [description]
*
*
* @return {Phaser.Physics.Arcade.World} This World object.
*/
removeCollider: function (collider)
@ -1692,6 +1693,80 @@ var World = new Class({
}
},
/**
* Wrap an object's coordinates (or several objects' coordinates) within {@link Phaser.Physics.Arcade.World#bounds}.
*
* 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).
*
* @method Phaser.Physics.Arcade.World#wrap
* @since 3.3.0
*
* @param {any} object - A Game Object, a Group, an object with `x` and `y` coordinates, or an array of such objects.
* @param {number} [padding=0] - An amount added to each boundary edge during the operation.
*/
wrap: function (object, padding)
{
if (object.body)
{
this.wrapObject(object, padding);
}
else if (object.getChildren)
{
this.wrapArray(object.getChildren(), padding);
}
else if (Array.isArray(object))
{
this.wrapArray(object, padding);
}
else
{
this.wrapObject(object, padding);
}
},
/**
* Wrap each object's coordinates within {@link Phaser.Physics.Arcade.World#bounds}.
*
* @method Phaser.Physics.Arcade.World#wrapArray
* @since 3.3.0
*
* @param {any[]} arr
* @param {number} [padding=0] - An amount added to the boundary.
*/
wrapArray: function (arr, padding)
{
if (arr.length === 0)
{
return;
}
for (var i = 0, len = arr.length; i < len; i++)
{
this.wrapObject(arr[i], padding);
}
},
/**
* Wrap an object's coordinates within {@link Phaser.Physics.Arcade.World#bounds}.
*
* @method Phaser.Physics.Arcade.World#wrapObject
* @since 3.3.0
*
* @param {any} object - A Game Object, a Physics Body, or any object with `x` and `y` coordinates
* @param {number} [padding=0] - An amount added to the boundary.
*/
wrapObject: function (object, padding)
{
if (padding === undefined)
{
padding = 0;
}
object.x = Wrap(object.x, this.bounds.left - padding, this.bounds.right + padding);
object.y = Wrap(object.y, this.bounds.top - padding, this.bounds.bottom + padding);
},
/**
* [description]
*