-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
WrapInRectangle.js
45 lines (39 loc) · 1.43 KB
/
WrapInRectangle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @author Richard Davey <rich@phaser.io>
* @author samme <samme.npm@gmail.com>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Wrap = require('../math/Wrap');
/**
* Iterates through the given array and makes sure that each objects x and y
* properties are wrapped to keep them contained within the given Rectangles
* area.
*
* @function Phaser.Actions.WrapInRectangle
* @since 3.0.0
* @see Phaser.Math.Wrap
*
* @generic {Phaser.GameObjects.GameObject[]} G - [items,$return]
*
* @param {(array|Phaser.GameObjects.GameObject[])} items - An array of Game Objects. The contents of this array are updated by this Action.
* @param {Phaser.Geom.Rectangle} rect - The rectangle which the objects will be wrapped to remain within.
* @param {number} [padding=0] - An amount added to each side of the rectangle during the operation.
*
* @return {(array|Phaser.GameObjects.GameObject[])} 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;