2019-06-25 15:57:05 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-06-25 15:57:05 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Vector2 = require('./Vector2');
|
|
|
|
|
|
|
|
/**
|
2020-01-11 14:46:25 +00:00
|
|
|
* Returns a Vector2 containing the x and y position of the given index in a `width` x `height` sized grid.
|
2019-06-25 15:57:05 +00:00
|
|
|
*
|
|
|
|
* For example, in a 6 x 4 grid, index 16 would equal x: 4 y: 2.
|
|
|
|
*
|
2020-01-11 14:46:25 +00:00
|
|
|
* If the given index is out of range an empty Vector2 is returned.
|
2019-06-25 15:57:05 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Math.ToXY
|
|
|
|
* @since 3.19.0
|
|
|
|
*
|
|
|
|
* @param {integer} index - The position within the grid to get the x/y value for.
|
|
|
|
* @param {integer} width - The width of the grid.
|
|
|
|
* @param {integer} height - The height of the grid.
|
|
|
|
* @param {Phaser.Math.Vector2} [out] - An optional Vector2 to store the result in. If not given, a new Vector2 instance will be created.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2} A Vector2 where the x and y properties contain the given grid index.
|
|
|
|
*/
|
|
|
|
var ToXY = function (index, width, height, out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
|
|
|
var x = 0;
|
|
|
|
var y = 0;
|
|
|
|
var total = width * height;
|
|
|
|
|
|
|
|
if (index > 0 && index <= total)
|
|
|
|
{
|
|
|
|
if (index > width - 1)
|
|
|
|
{
|
|
|
|
y = Math.floor(index / width);
|
|
|
|
x = index - (y * width);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.set(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ToXY;
|