Advertisement
Masadow

Isometric: Mouse to map

Dec 13th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 0.92 KB | None | 0 0
  1.     /**
  2.      * Find the index of the tile at given position.
  3.      * Result is -1 if the point is outside map.
  4.      *
  5.      * @param   Point       A point in world coordinates.
  6.      * @return  An Int containing the index of the tile at this coordinate. -1 if no tile were found.
  7.      */
  8.     public function getIndexFromPoint(Point:FlxPoint):Int
  9.     {
  10.         //Calculate corrected mouse position
  11.         var x0 = Point.x - heightInTiles * _scaledTileWidth * 0.5 - x;
  12.         var y0 = Point.y - y - _scaledTileHeight;
  13.  
  14.         //Calculate coordinates
  15.         var floatrow = y0 / _scaledTileDepth - x0 / _scaledTileWidth;
  16.         var floatcol = y0 / _scaledTileDepth + x0 / _scaledTileWidth;
  17.        
  18.         if (floatrow < 0 || floatcol < 0)
  19.             return -1;
  20.            
  21.         var col = Std.int(floatcol);
  22.         var row = Std.int(floatrow);
  23.  
  24.         //Check if the coordinates are valid
  25.         if (row >= heightInTiles || col >= widthInTiles)
  26.             return -1;
  27.  
  28.         //Finally compute the index
  29.         return row * widthInTiles + col;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement