2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.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-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-04 16:05:26 +00:00
|
|
|
var Vector2 = require('../math/Vector2');
|
|
|
|
|
|
|
|
/**
|
2022-10-26 22:02:50 +00:00
|
|
|
* Takes an array of items, such as Game Objects, or any objects with public `x` and
|
|
|
|
* `y` properties and then iterates through them. As this function iterates, it moves
|
|
|
|
* the position of the current element to be that of the previous entry in the array.
|
|
|
|
* This repeats until all items have been moved.
|
|
|
|
*
|
|
|
|
* The direction controls the order of iteration. A value of 0 (the default) assumes
|
|
|
|
* that the final item in the array is the 'head' item.
|
|
|
|
*
|
|
|
|
* A direction value of 1 assumes that the first item in the array is the 'head' item.
|
|
|
|
*
|
|
|
|
* The position of the 'head' item is set to the x/y values given to this function.
|
|
|
|
* Every other item in the array is then updated, in sequence, to be that of the
|
|
|
|
* previous (or next) entry in the array.
|
|
|
|
*
|
|
|
|
* The final x/y coords are returned, or set in the 'output' Vector2.
|
|
|
|
*
|
|
|
|
* Think of it as being like the game Snake, where the 'head' is moved and then
|
|
|
|
* each body piece is moved into the space of the previous piece.
|
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]
|
|
|
|
*
|
2022-10-26 22:02:50 +00:00
|
|
|
* @param {(Phaser.Types.Math.Vector2Like[]|Phaser.GameObjects.GameObject[])} items - An array of Game Objects, or objects with public x and y positions. The contents of this array are updated by this Action.
|
|
|
|
* @param {number} x - The x coordinate to place the head item at.
|
|
|
|
* @param {number} y - The y coordinate to place the head item 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.
|
2022-10-26 22:02:50 +00:00
|
|
|
* @param {Phaser.Types.Math.Vector2Like} [output] - An optional Vec2Like object to store the final position in.
|
2017-10-06 02:05:01 +00:00
|
|
|
*
|
2022-10-26 22:02:50 +00:00
|
|
|
* @return {Phaser.Types.Math.Vector2Like} 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;
|
2022-10-26 22:02:50 +00:00
|
|
|
var len = items.length;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
if (len === 1)
|
2017-06-16 18:26:26 +00:00
|
|
|
{
|
2022-10-26 22:02:50 +00:00
|
|
|
px = items[0].x;
|
|
|
|
py = items[0].y;
|
|
|
|
|
|
|
|
items[0].x = x;
|
|
|
|
items[0].y = y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var i = 1;
|
|
|
|
var pos = 0;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
|
|
|
if (direction === 0)
|
|
|
|
{
|
2022-10-26 22:02:50 +00:00
|
|
|
pos = len - 1;
|
|
|
|
i = len - 2;
|
|
|
|
}
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
px = items[pos].x;
|
|
|
|
py = items[pos].y;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
// Update the head item to the new x/y coordinates
|
|
|
|
items[pos].x = x;
|
|
|
|
items[pos].y = y;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
for (var c = 0; c < len; c++)
|
|
|
|
{
|
|
|
|
if (i >= len || i === -1)
|
2017-06-16 18:26:26 +00:00
|
|
|
{
|
2022-10-26 22:02:50 +00:00
|
|
|
continue;
|
2017-06-16 18:26:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
// Current item
|
|
|
|
var cur = items[i];
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
// Get current item x/y, to be passed to the next item in the list
|
|
|
|
var cx = cur.x;
|
|
|
|
var cy = cur.y;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
// Set current item to the previous items x/y
|
|
|
|
cur.x = px;
|
|
|
|
cur.y = py;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
// Set current as previous
|
|
|
|
px = cx;
|
|
|
|
py = cy;
|
2017-06-16 18:26:26 +00:00
|
|
|
|
2022-10-26 22:02:50 +00:00
|
|
|
if (direction === 0)
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i++;
|
2017-06-16 18:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|