From add318491f0da2568bfac344e25ff899e43521c0 Mon Sep 17 00:00:00 2001 From: samme Date: Sat, 10 Mar 2018 13:35:02 -0800 Subject: [PATCH 1/4] Add Phaser.Actions.WrapInRectangle --- src/actions/WrapInRectangle.js | 40 ++++++++++++++++++++++++++++++++++ src/actions/index.js | 3 ++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/actions/WrapInRectangle.js diff --git a/src/actions/WrapInRectangle.js b/src/actions/WrapInRectangle.js new file mode 100644 index 000000000..f54cce471 --- /dev/null +++ b/src/actions/WrapInRectangle.js @@ -0,0 +1,40 @@ +/** + * @author Richard Davey + * @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; diff --git a/src/actions/index.js b/src/actions/index.js index 498d7941d..e5a1102ba 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -54,6 +54,7 @@ module.exports = { SmootherStep: require('./SmootherStep'), SmoothStep: require('./SmoothStep'), Spread: require('./Spread'), - ToggleVisible: require('./ToggleVisible') + ToggleVisible: require('./ToggleVisible'), + WrapInRectangle: require('./WrapInRectangle') }; From 66e5a72dae77306c094d8944799051d951d0e9c9 Mon Sep 17 00:00:00 2001 From: samme Date: Sat, 10 Mar 2018 13:36:18 -0800 Subject: [PATCH 2/4] Add Phaser.Physics.Arcade.World#wrap and friends - Phaser.Physics.Arcade.World#wrap - Phaser.Physics.Arcade.World#wrapArray - Phaser.Physics.Arcade.World#wrapObject --- src/physics/arcade/World.js | 75 +++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/physics/arcade/World.js b/src/physics/arcade/World.js index 9d7f7eee6..40eb06377 100644 --- a/src/physics/arcade/World.js +++ b/src/physics/arcade/World.js @@ -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 @@ -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 [version] + * + * @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 [version] + * + * @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 [version] + * + * @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] * From c7fd31d3d200057d5f06050ed19c5f111d1bf862 Mon Sep 17 00:00:00 2001 From: samme Date: Mon, 12 Mar 2018 07:29:04 -0700 Subject: [PATCH 3/4] Note samme --- src/actions/WrapInRectangle.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/actions/WrapInRectangle.js b/src/actions/WrapInRectangle.js index f54cce471..f946a538a 100644 --- a/src/actions/WrapInRectangle.js +++ b/src/actions/WrapInRectangle.js @@ -1,5 +1,6 @@ /** * @author Richard Davey + * @author samme * @copyright 2018 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ From 4b25574d82029c2afc6f46aac8ddfd080587f19e Mon Sep 17 00:00:00 2001 From: samme Date: Mon, 12 Mar 2018 07:29:49 -0700 Subject: [PATCH 4/4] Expect v3.3.0 --- src/physics/arcade/World.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/physics/arcade/World.js b/src/physics/arcade/World.js index 40eb06377..ce3b6ec60 100644 --- a/src/physics/arcade/World.js +++ b/src/physics/arcade/World.js @@ -618,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) @@ -1699,7 +1699,7 @@ var World = new Class({ * 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 [version] + * @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. @@ -1729,7 +1729,7 @@ var World = new Class({ * Wrap each object's coordinates within {@link Phaser.Physics.Arcade.World#bounds}. * * @method Phaser.Physics.Arcade.World#wrapArray - * @since [version] + * @since 3.3.0 * * @param {any[]} arr * @param {number} [padding=0] - An amount added to the boundary. @@ -1751,7 +1751,7 @@ var World = new Class({ * Wrap an object's coordinates within {@link Phaser.Physics.Arcade.World#bounds}. * * @method Phaser.Physics.Arcade.World#wrapObject - * @since [version] + * @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.