44 * @license {@link https://opensource.org/licenses/MIT|MIT License }
55 */
66
7- var CONST = require ( '../../const.js' ) ;
8- var WorldToTileX = require ( './WorldToTileX' ) . func ;
9- var WorldToTileY = require ( './WorldToTileY' ) . func ;
7+ var WorldToTileX = require ( './WorldToTileX' ) ;
8+ var WorldToTileY = require ( './WorldToTileY' ) ;
109var Vector2 = require ( '../../math/Vector2' ) ;
1110
1211/**
13- * Converts from world XY coordinates (pixels) to orthogonal tile XY coordinates (tile units), factoring in the
12+ * Converts from world XY coordinates (pixels) to tile XY coordinates (tile units), factoring in the
1413 * layer's position, scale and scroll. This will return a new Vector2 object or update the given
1514 * `point` object.
1615 *
17- * @function Phaser.Tilemaps.Components.OrthoWorldToTileXY
18- * @private
16+ * @function Phaser.Tilemaps.Components.WorldToTileXY
1917 * @since 3.0.0
2018 *
2119 * @param {number } worldX - The x coordinate to be converted, in pixels, not tiles.
@@ -27,208 +25,14 @@ var Vector2 = require('../../math/Vector2');
2725 *
2826 * @return {Phaser.Math.Vector2 } The XY location in tile units.
2927 */
30- var OrthoWorldToTileXY = function ( worldX , worldY , snapToFloor , point , camera , layer )
28+ var WorldToTileXY = function ( worldX , worldY , snapToFloor , point , camera , layer )
3129{
3230 if ( point === undefined ) { point = new Vector2 ( 0 , 0 ) ; }
3331
34- point . x = WorldToTileX ( CONST . ORTHOGONAL ) ( worldX , snapToFloor , camera , layer ) ;
35- point . y = WorldToTileY ( CONST . ORTHOGONAL ) ( worldY , snapToFloor , camera , layer ) ;
36- return point ;
37- } ;
38-
39- /**
40- * Converts from world XY coordinates (pixels) to isometric tile XY coordinates (tile units), factoring in the
41- * layer's position, scale and scroll. This will return a new Vector2 object or update the given
42- * `point` object.
43- *
44- * @function Phaser.Tilemaps.Components.IsoWorldToTileXY
45- * @private
46- * @since 3.0.0
47- *
48- * @param {number } worldX - The x coordinate to be converted, in pixels, not tiles.
49- * @param {number } worldY - The y coordinate to be converted, in pixels, not tiles.
50- * @param {boolean } [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
51- * @param {Phaser.Math.Vector2 } [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
52- * @param {Phaser.Cameras.Scene2D.Camera } [camera=main camera] - The Camera to use when calculating the tile index from the world values.
53- * @param {Phaser.Tilemaps.LayerData } layer - The Tilemap Layer to act upon.
54- *
55- * @return {Phaser.Math.Vector2 } The XY location in tile units.
56- */
57- var IsoWorldToTileXY = function ( worldX , worldY , snapToFloor , point , camera , layer )
58- {
59- if ( point === undefined ) { point = new Vector2 ( 0 , 0 ) ; }
60-
61- var tileWidth = layer . baseTileWidth ;
62- var tileHeight = layer . baseTileHeight ;
63- var tilemapLayer = layer . tilemapLayer ;
64-
65- if ( tilemapLayer )
66- {
67- if ( camera === undefined ) { camera = tilemapLayer . scene . cameras . main ; }
68-
69- // Find the world position relative to the static or dynamic layer's top left origin,
70- // factoring in the camera's vertical scroll
71- // console.log(1,worldY)
72- worldY = worldY - ( tilemapLayer . y + camera . scrollY * ( 1 - tilemapLayer . scrollFactorY ) ) ;
73-
74- // console.log(worldY)
75- tileHeight *= tilemapLayer . scaleY ;
76-
77- // Find the world position relative to the static or dynamic layer's top left origin,
78- // factoring in the camera's horizontal scroll
79- worldX = worldX - ( tilemapLayer . x + camera . scrollX * ( 1 - tilemapLayer . scrollFactorX ) ) ;
80-
81- tileWidth *= tilemapLayer . scaleX ;
82- }
83- worldX -= tileWidth / 2 ;
84-
85-
86-
87- point . x = snapToFloor
88- ? Math . floor ( ( worldX / ( tileWidth / 2 ) + worldY / ( tileHeight / 2 ) ) / 2 )
89- : ( ( worldX / ( tileWidth / 2 ) + worldY / ( tileHeight / 2 ) ) / 2 ) ;
90-
91- point . y = snapToFloor
92- ? Math . floor ( ( worldY / ( tileHeight / 2 ) - worldX / ( tileWidth / 2 ) ) / 2 )
93- : ( ( worldY / ( tileHeight / 2 ) - worldX / ( tileWidth / 2 ) ) / 2 ) ;
94-
95- return point ;
96- } ;
97-
98- /**
99- * Converts from world XY coordinates (pixels) to hexagonal tile XY coordinates (tile units), factoring in the
100- * layer's position, scale and scroll. This will return a new Vector2 object or update the given
101- * `point` object.
102- *
103- * @function Phaser.Tilemaps.Components.HexWorldToTileXY
104- * @private
105- * @since 3.0.0
106- *
107- * @param {number } worldX - The x coordinate to be converted, in pixels, not tiles.
108- * @param {number } worldY - The y coordinate to be converted, in pixels, not tiles.
109- * @param {boolean } [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
110- * @param {Phaser.Math.Vector2 } [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
111- * @param {Phaser.Cameras.Scene2D.Camera } [camera=main camera] - The Camera to use when calculating the tile index from the world values.
112- * @param {Phaser.Tilemaps.LayerData } layer - The Tilemap Layer to act upon.
113- *
114- * @return {Phaser.Math.Vector2 } The XY location in tile units.
115- */
116- var HexWorldToTileXY = function ( worldX , worldY , snapToFloor , point , camera , layer )
117- {
118- if ( point === undefined ) { point = new Vector2 ( 0 , 0 ) ; }
119-
120- var tileWidth = layer . baseTileWidth ;
121- var tileHeight = layer . baseTileHeight ;
122- var tilemapLayer = layer . tilemapLayer ;
123-
124- if ( tilemapLayer )
125- {
126- if ( camera === undefined ) { camera = tilemapLayer . scene . cameras . main ; }
127-
128- // Find the world position relative to the static or dynamic layer's top left origin,
129- // factoring in the camera's vertical scroll
130- // console.log(1,worldY)
131- worldY = worldY - ( tilemapLayer . y + camera . scrollY * ( 1 - tilemapLayer . scrollFactorY ) ) ;
132-
133- // console.log(worldY)
134- tileHeight *= tilemapLayer . scaleY ;
135-
136- // Find the world position relative to the static or dynamic layer's top left origin,
137- // factoring in the camera's horizontal scroll
138- worldX = worldX - ( tilemapLayer . x + camera . scrollX * ( 1 - tilemapLayer . scrollFactorX ) ) ;
139-
140- tileWidth *= tilemapLayer . scaleX ;
141- }
142-
143- var sidel = layer . hexSideLength ;
144- var rowHeight = ( ( tileHeight - sidel ) / 2 + sidel ) ;
145-
146- // similar to staggered, because Tiled uses the oddr representation.
147- point . y = snapToFloor
148- ? Math . floor ( ( worldY / rowHeight ) )
149- : ( worldY / rowHeight ) ;
150- point . x = snapToFloor
151- ? Math . floor ( ( worldX - ( point . y % 2 ) * 0.5 * tileWidth ) / tileWidth )
152- : ( worldX - ( point . y % 2 ) * 0.5 * tileWidth ) / tileWidth ;
32+ point . x = WorldToTileX ( worldX , snapToFloor , camera , layer ) ;
33+ point . y = WorldToTileY ( worldY , snapToFloor , camera , layer ) ;
15334
15435 return point ;
15536} ;
15637
157- /**
158- * Converts from world XY coordinates (pixels) to staggered tile XY coordinates (tile units), factoring in the
159- * layer's position, scale and scroll. This will return a new Vector2 object or update the given
160- * `point` object.
161- *
162- * @function Phaser.Tilemaps.Components.StagWorldToTileXY
163- * @private
164- * @since 3.0.0
165- *
166- * @param {number } worldX - The x coordinate to be converted, in pixels, not tiles.
167- * @param {number } worldY - The y coordinate to be converted, in pixels, not tiles.
168- * @param {boolean } [snapToFloor=true] - Whether or not to round the tile coordinate down to the nearest integer.
169- * @param {Phaser.Math.Vector2 } [point] - A Vector2 to store the coordinates in. If not given a new Vector2 is created.
170- * @param {Phaser.Cameras.Scene2D.Camera } [camera=main camera] - The Camera to use when calculating the tile index from the world values.
171- * @param {Phaser.Tilemaps.LayerData } layer - The Tilemap Layer to act upon.
172- *
173- * @return {Phaser.Math.Vector2 } The XY location in tile units.
174- */
175- var StagWorldToTileXY = function ( worldX , worldY , snapToFloor , point , camera , layer )
176- {
177- if ( point === undefined ) { point = new Vector2 ( 0 , 0 ) ; }
178-
179- var tileWidth = layer . baseTileWidth ;
180- var tileHeight = layer . baseTileHeight ;
181- var tilemapLayer = layer . tilemapLayer ;
182-
183- if ( tilemapLayer )
184- {
185- if ( camera === undefined ) { camera = tilemapLayer . scene . cameras . main ; }
186-
187- // Find the world position relative to the static or dynamic layer's top left origin,
188- // factoring in the camera's vertical scroll
189- // console.log(1,worldY)
190- worldY = worldY - ( tilemapLayer . y + camera . scrollY * ( 1 - tilemapLayer . scrollFactorY ) ) ;
191-
192- // console.log(worldY)
193- tileHeight *= tilemapLayer . scaleY ;
194-
195- // Find the world position relative to the static or dynamic layer's top left origin,
196- // factoring in the camera's horizontal scroll
197- worldX = worldX - ( tilemapLayer . x + camera . scrollX * ( 1 - tilemapLayer . scrollFactorX ) ) ;
198-
199- tileWidth *= tilemapLayer . scaleX ;
200-
201- point . y = snapToFloor
202- ? Math . floor ( ( worldY / ( tileHeight / 2 ) ) )
203- : ( worldY / ( tileHeight / 2 ) ) ;
204- point . x = snapToFloor
205- ? Math . floor ( ( worldX + ( point . y % 2 ) * 0.5 * tileWidth ) / tileWidth )
206- : ( worldX + ( point . y % 2 ) * 0.5 * tileWidth ) / tileWidth ;
207-
208-
209- }
210-
211- return point ;
212- } ;
213-
214- var WorldToTileXY = function ( orientation )
215- {
216- switch ( orientation )
217- {
218- case CONST . STAGGERED :
219- return StagWorldToTileXY ;
220-
221- case CONST . ISOMETRIC :
222- return IsoWorldToTileXY ;
223-
224- case CONST . HEXAGONAL :
225- return HexWorldToTileXY ;
226-
227- default :
228- return OrthoWorldToTileXY ;
229-
230- }
231-
232- } ;
233-
23438module . exports = WorldToTileXY ;
0 commit comments