|
12 | 12 | */ |
13 | 13 | Phaser.Utils = { |
14 | 14 |
|
| 15 | + /** |
| 16 | + * Transposes the elements of the given Array. |
| 17 | + * |
| 18 | + * @method transposeArray |
| 19 | + * @param {array} array - The array to transpose. |
| 20 | + * @return {array} The transposed array. |
| 21 | + */ |
| 22 | + transposeArray: function (array) { |
| 23 | + |
| 24 | + var result = new Array(array[0].length); |
| 25 | + |
| 26 | + for (var i = 0; i < array[0].length; i++) |
| 27 | + { |
| 28 | + result[i] = new Array(array.length - 1); |
| 29 | + |
| 30 | + for (var j = array.length - 1; j > -1; j--) |
| 31 | + { |
| 32 | + result[i][j] = array[j][i]; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return result; |
| 37 | + |
| 38 | + }, |
| 39 | + |
| 40 | + /** |
| 41 | + * Rotates the given array. |
| 42 | + * Based on the routine from http://jsfiddle.net/MrPolywhirl/NH42z/ |
| 43 | + * |
| 44 | + * @method Phaser.Utils.rotateArray |
| 45 | + * @param {array} matrix - The array to rotate. |
| 46 | + * @param {number|string} direction - The amount to rotate. Either a number: 90, -90, 270, -270, 180 or a string: 'rotateLeft', 'rotateRight' or 'rotate180' |
| 47 | + * @return {array} The rotated array |
| 48 | + */ |
| 49 | + rotateArray: function (matrix, direction) { |
| 50 | + |
| 51 | + if (typeof direction !== 'String') |
| 52 | + { |
| 53 | + direction = ((direction % 360) + 360) % 360; |
| 54 | + } |
| 55 | + |
| 56 | + if (direction === 90 || direction === -270 || direction === 'rotateLeft') |
| 57 | + { |
| 58 | + matrix = Phaser.Utils.transposeArray(matrix); |
| 59 | + matrix = matrix.reverse(); |
| 60 | + } |
| 61 | + else if (direction === -90 || direction === 270 || direction === 'rotateRight') |
| 62 | + { |
| 63 | + matrix = matrix.reverse(); |
| 64 | + matrix = Phaser.Utils.transposeArray(matrix); |
| 65 | + } |
| 66 | + else if (Math.abs(direction) === 180 || direction === 'rotate180') |
| 67 | + { |
| 68 | + for (var i = 0; i < matrix.length; i++) |
| 69 | + { |
| 70 | + matrix[i].reverse(); |
| 71 | + } |
| 72 | + |
| 73 | + matrix = matrix.reverse(); |
| 74 | + } |
| 75 | + |
| 76 | + return matrix; |
| 77 | + |
| 78 | + }, |
| 79 | + |
15 | 80 | /** |
16 | 81 | * Get a unit dimension from a string. |
17 | 82 | * |
|
0 commit comments