Skip to content

Commit 721b64f

Browse files
committed
FlxTilemap: remove redundant rayHit() + small fix for ray()
ray() has exactly the same functionality, it just needed a small fix (see AdamAtomic/flixel#225)
1 parent e2fa085 commit 721b64f

File tree

1 file changed

+12
-121
lines changed

1 file changed

+12
-121
lines changed

flixel/tile/FlxTilemap.hx

+12-121
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ class FlxTilemap extends FlxObject
10821082
}
10831083

10841084
/**
1085-
* Returns a new Flash Array full of every map index of the requested tile type.
1085+
* Returns a new array full of every map index of the requested tile type.
10861086
*
10871087
* @param Index The requested tile type.
10881088
* @return An Array with a list of all map indices of that tile type.
@@ -1111,7 +1111,7 @@ class FlxTilemap extends FlxObject
11111111
}
11121112

11131113
/**
1114-
* Returns a new Flash Array full of every coordinate of the requested tile type.
1114+
* Returns a new array full of every coordinate of the requested tile type.
11151115
*
11161116
* @param Index The requested tile type.
11171117
* @param Midpoint Whether to return the coordinates of the tile midpoint, or upper left corner. Default is true, return midpoint.
@@ -1297,9 +1297,9 @@ class FlxTilemap extends FlxObject
12971297
*
12981298
* @param Start The world coordinates of the start of the ray.
12991299
* @param End The world coordinates of the end of the ray.
1300-
* @param Result A Point object containing the first wall impact.
1300+
* @param Result An optional point containing the first wall impact if there was one. Null otherwise.
13011301
* @param Resolution Defaults to 1, meaning check every tile or so. Higher means more checks!
1302-
* @return Returns true if the ray made it from Start to End without hitting anything. Returns false and fills Result if a tile was hit.
1302+
* @return Returns true if the ray made it from Start to End without hitting anything. Returns false and fills Result if a tile was hit.
13031303
*/
13041304
public function ray(Start:FlxPoint, End:FlxPoint, ?Result:FlxPoint, Resolution:Float = 1):Bool
13051305
{
@@ -1323,6 +1323,9 @@ class FlxTilemap extends FlxObject
13231323
var tileY:Int;
13241324
var i:Int = 0;
13251325

1326+
Start.putWeak();
1327+
End.putWeak();
1328+
13261329
while (i < steps)
13271330
{
13281331
curX += stepX;
@@ -1361,12 +1364,14 @@ class FlxTilemap extends FlxObject
13611364

13621365
if ((ry > tileY) && (ry < tileY + _scaledTileHeight))
13631366
{
1364-
if (Result != null)
1367+
if (Result == null)
13651368
{
1366-
Result.x = rx;
1367-
Result.y = ry;
1369+
Result = FlxPoint.get();
13681370
}
13691371

1372+
Result.x = rx;
1373+
Result.y = ry;
1374+
13701375
return false;
13711376
}
13721377

@@ -1401,120 +1406,6 @@ class FlxTilemap extends FlxObject
14011406
return true;
14021407
}
14031408

1404-
/**
1405-
* Works exactly like ray() except it explicitly returns the hit result. Shoots a ray from the start point to the end point.
1406-
* If/when it passes through a tile, it returns that point. If it does not, it returns null.
1407-
* Usage:
1408-
* var hit:FlxPoint = tilemap.rayHit(startPoint, endPoint);
1409-
* if (hit != null) //code ;
1410-
*
1411-
* @param Start The world coordinates of the start of the ray.
1412-
* @param End The world coordinates of the end of the ray.
1413-
* @param Resolution Defaults to 1, meaning check every tile or so. Higher means more checks!
1414-
* @return Returns null if the ray made it from Start to End without hitting anything. Returns FlxPoint if a tile was hit.
1415-
*/
1416-
public function rayHit(Start:FlxPoint, End:FlxPoint, Resolution:Float = 1):FlxPoint
1417-
{
1418-
var Result:FlxPoint = null;
1419-
var step:Float = _scaledTileWidth;
1420-
1421-
if (_scaledTileHeight < _scaledTileWidth)
1422-
{
1423-
step = _scaledTileHeight;
1424-
}
1425-
1426-
step /= Resolution;
1427-
var deltaX:Float = End.x - Start.x;
1428-
var deltaY:Float = End.y - Start.y;
1429-
var distance:Float = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
1430-
var steps:Int = Math.ceil(distance / step);
1431-
var stepX:Float = deltaX / steps;
1432-
var stepY:Float = deltaY / steps;
1433-
var curX:Float = Start.x - stepX - x;
1434-
var curY:Float = Start.y - stepY - y;
1435-
var tileX:Int;
1436-
var tileY:Int;
1437-
var i:Int = 0;
1438-
1439-
Start.putWeak();
1440-
End.putWeak();
1441-
1442-
while (i < steps)
1443-
{
1444-
curX += stepX;
1445-
curY += stepY;
1446-
1447-
if ((curX < 0) || (curX > width) || (curY < 0) || (curY > height))
1448-
{
1449-
i++;
1450-
continue;
1451-
}
1452-
1453-
tileX = Math.floor(curX / _scaledTileWidth);
1454-
tileY = Math.floor(curY / _scaledTileHeight);
1455-
1456-
if (_tileObjects[_data[tileY * widthInTiles + tileX]].allowCollisions != 0)
1457-
{
1458-
// Some basic helper stuff
1459-
tileX *= Std.int(_scaledTileWidth);
1460-
tileY *= Std.int(_scaledTileHeight);
1461-
var rx:Float = 0;
1462-
var ry:Float = 0;
1463-
var q:Float;
1464-
var lx:Float = curX - stepX;
1465-
var ly:Float = curY - stepY;
1466-
1467-
// Figure out if it crosses the X boundary
1468-
q = tileX;
1469-
1470-
if (deltaX < 0)
1471-
{
1472-
q += _scaledTileWidth;
1473-
}
1474-
1475-
rx = q;
1476-
ry = ly + stepY * ((q - lx) / stepX);
1477-
1478-
if ((ry > tileY) && (ry < tileY + _scaledTileHeight))
1479-
{
1480-
if (Result == null)
1481-
{
1482-
Result = FlxPoint.get();
1483-
}
1484-
1485-
return Result.set(rx, ry);
1486-
}
1487-
1488-
// Else, figure out if it crosses the Y boundary
1489-
q = tileY;
1490-
1491-
if (deltaY < 0)
1492-
{
1493-
q += _scaledTileHeight;
1494-
}
1495-
1496-
rx = lx + stepX * ((q - ly) / stepY);
1497-
ry = q;
1498-
1499-
if ((rx > tileX) && (rx < tileX + _scaledTileWidth))
1500-
{
1501-
if (Result == null)
1502-
{
1503-
Result = FlxPoint.get();
1504-
}
1505-
1506-
return Result.set(rx, ry);
1507-
}
1508-
1509-
return null;
1510-
}
1511-
1512-
i++;
1513-
}
1514-
1515-
return null;
1516-
}
1517-
15181409
/**
15191410
* Use this method for creating tileSheet for FlxTilemap. Must be called after loadMap() method.
15201411
* If you forget to call it then you will not see this FlxTilemap on c++ target

0 commit comments

Comments
 (0)