2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-04 16:05:26 +00:00
|
|
|
var Vector2 = require('../math/Vector2');
|
|
|
|
|
|
|
|
/**
|
2018-10-01 11:01:59 +00:00
|
|
|
* Iterate through the items array changing the position of each element to be that of the element that came before
|
|
|
|
* it in the array (or after it if direction = 1)
|
|
|
|
*
|
2018-03-20 14:57:12 +00:00
|
|
|
* The first items position is set to x/y.
|
2018-10-01 11:01:59 +00:00
|
|
|
*
|
2018-03-20 14:57:12 +00:00
|
|
|
* The final x/y coords are returned
|
2017-10-04 16:05:26 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Actions.ShiftPosition
|
|
|
|
* @since 3.0.0
|
2018-03-20 14:57:12 +00:00
|
|
|
*
|
2018-03-27 11:14:08 +00:00
|
|
|
* @generic {Phaser.GameObjects.GameObject[]} G - [items]
|
|
|
|
* @generic {Phaser.Math.Vector2} O - [output,$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.
|
2018-10-01 11:01:59 +00:00
|
|
|
* @param {number} x - The x coordinate to place the first item in the array at.
|
|
|
|
* @param {number} y - The y coordinate to place the first item in the array at.
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} [direction=0] - The iteration direction. 0 = first to last and 1 = last to first.
|
2018-10-01 11:01:59 +00:00
|
|
|
* @param {(Phaser.Math.Vector2|object)} [output] - An optional objec to store the final objects position in.
|
2017-10-06 02:05:01 +00:00
|
|
|
*
|
2018-03-17 17:16:11 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The output vector.
|
2017-10-04 16:05:26 +00:00
|
|
|
*/
|
2017-06-19 15:02:52 +00:00
|
|
|
var ShiftPosition = function (items, x, y, direction, output)
|
2017-06-16 18:26:26 +00:00
|
|
|
{
|
|
|
|
if (direction === undefined) { direction = 0; }
|
2017-10-04 16:05:26 +00:00
|
|
|
if (output === undefined) { output = new Vector2(); }
|
2017-06-19 15:02:52 +00:00
|
|
|
|
|
|
|
var px;
|
|
|
|
var py;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
|
|
|
if (items.length > 1)
|
|
|
|
{
|
|
|
|
var i;
|
|
|
|
var cx;
|
|
|
|
var cy;
|
|
|
|
var cur;
|
|
|
|
|
|
|
|
if (direction === 0)
|
|
|
|
{
|
|
|
|
// Bottom to Top
|
|
|
|
|
|
|
|
var len = items.length - 1;
|
|
|
|
|
|
|
|
px = items[len].x;
|
|
|
|
py = items[len].y;
|
|
|
|
|
|
|
|
for (i = len - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
// Current item
|
|
|
|
cur = items[i];
|
|
|
|
|
|
|
|
// Get current item x/y, to be passed to the next item in the list
|
|
|
|
cx = cur.x;
|
|
|
|
cy = cur.y;
|
|
|
|
|
|
|
|
// Set current item to the previous items x/y
|
|
|
|
cur.x = px;
|
|
|
|
cur.y = py;
|
|
|
|
|
|
|
|
// Set current as previous
|
|
|
|
px = cx;
|
|
|
|
py = cy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the head item to the new x/y coordinates
|
|
|
|
items[len].x = x;
|
|
|
|
items[len].y = y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Top to Bottom
|
|
|
|
|
|
|
|
px = items[0].x;
|
|
|
|
py = items[0].y;
|
|
|
|
|
|
|
|
for (i = 1; i < items.length; i++)
|
|
|
|
{
|
|
|
|
// Current item
|
|
|
|
cur = items[i];
|
|
|
|
|
|
|
|
// Get current item x/y, to be passed to the next item in the list
|
|
|
|
cx = cur.x;
|
|
|
|
cy = cur.y;
|
|
|
|
|
|
|
|
// Set current item to the previous items x/y
|
|
|
|
cur.x = px;
|
|
|
|
cur.y = py;
|
|
|
|
|
|
|
|
// Set current as previous
|
|
|
|
px = cx;
|
|
|
|
py = cy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the head item to the new x/y coordinates
|
|
|
|
items[0].x = x;
|
|
|
|
items[0].y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-19 15:02:52 +00:00
|
|
|
px = items[0].x;
|
|
|
|
py = items[0].y;
|
|
|
|
|
2017-06-16 18:26:26 +00:00
|
|
|
items[0].x = x;
|
|
|
|
items[0].y = y;
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:02:52 +00:00
|
|
|
// Return the final set of coordinates as they're effectively lost from the shift and may be needed
|
|
|
|
|
|
|
|
output.x = px;
|
|
|
|
output.y = py;
|
|
|
|
|
|
|
|
return output;
|
2017-06-16 18:26:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ShiftPosition;
|