phaser/src/actions/WrapInRectangle.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-03-10 21:35:02 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2018-03-12 14:29:04 +00:00
* @author samme <samme.npm@gmail.com>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-03-10 21:35:02 +00:00
*/
var Wrap = require('../math/Wrap');
/**
2022-10-26 22:02:36 +00:00
* 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.
2018-03-10 21:35:02 +00:00
*
* @function Phaser.Actions.WrapInRectangle
2018-03-20 14:57:12 +00:00
* @since 3.0.0
2018-03-10 21:35:02 +00:00
* @see Phaser.Math.Wrap
*
2018-03-27 11:14:08 +00:00
* @generic {Phaser.GameObjects.GameObject[]} G - [items,$return]
*
2018-03-20 14:57:12 +00:00
* @param {(array|Phaser.GameObjects.GameObject[])} items - An array of Game Objects. The contents of this array are updated by this Action.
2022-10-26 22:02:36 +00:00
* @param {Phaser.Geom.Rectangle} rect - The rectangle which the objects will be wrapped to remain within.
2018-03-10 21:35:02 +00:00
* @param {number} [padding=0] - An amount added to each side of the rectangle during the operation.
*
2018-03-27 11:14:08 +00:00
* @return {(array|Phaser.GameObjects.GameObject[])} The array of Game Objects that was passed to this Action.
2018-03-10 21:35:02 +00:00
*/
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;