Skip to content

Commit 7bcfb0e

Browse files
committed
Swapped to PointWithinHitArea instead for clarity.
1 parent c3ba02a commit 7bcfb0e

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

v3/src/input/components/HitTest.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
var GetTransformedPoint = require('./GetTransformedPoint');
2-
var PointWithinGameObject = require('./PointWithinGameObject');
3-
var PointWithinInteractiveObject = require('./PointWithinInteractiveObject');
2+
var PointWithinHitArea = require('./PointWithinHitArea');
43

54
// Will always return an array.
65
// Array contains matching Interactive Objects.
76
// Array will be empty if no objects were matched.
87

9-
var HitTest = function (tempMatrix, x, y, gameObjectArray, camera, output)
8+
var HitTest = function (tempMatrix, x, y, gameObjects, camera, output)
109
{
1110
var cameraW = camera.width;
1211
var cameraH = camera.height;
@@ -18,21 +17,28 @@ var HitTest = function (tempMatrix, x, y, gameObjectArray, camera, output)
1817
return output;
1918
}
2019

21-
var scrollX = camera.scrollX;
22-
var scrollY = camera.scrollY;
2320
var screenPoint = camera.cameraToScreen({ x: x, y: y });
24-
var culled = camera.cullHitTest(gameObjectArray);
21+
var culledGameObjects = camera.cull(gameObjects);
2522

26-
for (var i = 0; i < culled.length; i++)
23+
for (var i = 0; i < culledGameObjects.length; i++)
2724
{
28-
var object = culled[i];
29-
var gameObject = object.gameObject;
25+
var gameObject = culledGameObjects[i];
3026

31-
var tpoint = GetTransformedPoint(tempMatrix, gameObject, screenPoint.x + scrollX * gameObject.scrollFactorX, screenPoint.y + scrollY * gameObject.scrollFactorY);
27+
if (!gameObject.input.enabled)
28+
{
29+
continue;
30+
}
31+
32+
var point = GetTransformedPoint(
33+
tempMatrix,
34+
gameObject,
35+
(screenPoint.x + camera.scrollX) * gameObject.scrollFactorX,
36+
(screenPoint.y + camera.scrollY) * gameObject.scrollFactorY
37+
);
3238

33-
if (PointWithinInteractiveObject(object, tpoint.x, tpoint.y))
39+
if (PointWithinHitArea(gameObject, point.x, point.y))
3440
{
35-
output.push(object);
41+
output.push(gameObject);
3642
}
3743
}
3844

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// x/y MUST be translated before being passed to this function,
2+
// unless the gameObject is guaranteed to not be rotated or scaled in any way
3+
4+
var PointWithinHitArea = function (gameObject, x, y)
5+
{
6+
var input = gameObject.input;
7+
8+
if (!input)
9+
{
10+
return false;
11+
}
12+
13+
// Normalize the origin
14+
x += gameObject.displayOriginX;
15+
y += gameObject.displayOriginY;
16+
17+
if (input.hitAreaCallback(input.hitArea, x, y, gameObject))
18+
{
19+
input.localX = x;
20+
input.localY = y;
21+
22+
return true;
23+
}
24+
else
25+
{
26+
return false;
27+
}
28+
};
29+
30+
module.exports = PointWithinHitArea;

0 commit comments

Comments
 (0)