mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 05:03:37 +00:00
PointScreenToWorldHitTest added to Global Input Manager
This commit is contained in:
parent
423326c71b
commit
ebea116809
4 changed files with 69 additions and 14 deletions
|
@ -70,15 +70,24 @@ var Camera = new Class({
|
|||
for (var index = 0; index < length; ++index)
|
||||
{
|
||||
var object = renderableObjects[index];
|
||||
var objectW = object.width;
|
||||
var objectH = object.height;
|
||||
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
|
||||
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
|
||||
var cullW = cameraW + objectW;
|
||||
var cullH = cameraH + objectH;
|
||||
|
||||
if (objectX > -objectW && objectY > -objectH &&
|
||||
objectX < cullW && objectY < cullH)
|
||||
/* Not every renderable object has a dimension */
|
||||
if (typeof object.width === 'number')
|
||||
{
|
||||
var objectW = object.width;
|
||||
var objectH = object.height;
|
||||
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
|
||||
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
|
||||
var cullW = cameraW + objectW;
|
||||
var cullH = cameraH + objectH;
|
||||
|
||||
if (objectX > -objectW && objectY > -objectH &&
|
||||
objectX < cullW && objectY < cullH)
|
||||
{
|
||||
culledObjects.push(object);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
culledObjects.push(object);
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
var IntersectGameObject = function (point, gameObjectArray, camera)
|
||||
{
|
||||
return false;
|
||||
};
|
||||
|
||||
module.exports = IntersectGameObject;
|
|
@ -8,6 +8,7 @@ var Mouse = require('./mouse/MouseManager');
|
|||
var MouseEvent = require('./mouse/events/');
|
||||
var PointWithinGameObject = require('./components/PointWithinGameObject');
|
||||
var TransformMatrix = require('../gameobjects/components/TransformMatrix');
|
||||
var PointScreenToWorldHitTest = require('./components/PointScreenToWorldHitTest');
|
||||
|
||||
var GlobalInputManager = new Class({
|
||||
|
||||
|
@ -32,6 +33,7 @@ var GlobalInputManager = new Class({
|
|||
|
||||
this._tempMatrix = new TransformMatrix();
|
||||
this._tempPoint = { x: 0, y: 0 };
|
||||
this._tempHitTest = [];
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -92,6 +94,11 @@ var GlobalInputManager = new Class({
|
|||
pointWithinGameObject: function (gameObject, x, y)
|
||||
{
|
||||
return PointWithinGameObject(gameObject, x, y);
|
||||
},
|
||||
|
||||
pointScreenToWorldHitTest: function (gameObjects, x, y, camera)
|
||||
{
|
||||
return PointScreenToWorldHitTest(this._tempMatrix, x, y, gameObjects, camera, this._tempHitTest);
|
||||
}
|
||||
|
||||
});
|
||||
|
|
45
v3/src/input/components/PointScreenToWorldHitTest.js
Normal file
45
v3/src/input/components/PointScreenToWorldHitTest.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
var GetTransformedPoint = require('./GetTransformedPoint');
|
||||
var PointWithinGameObject = require('./PointWithinGameObject');
|
||||
|
||||
var PointScreenToWorldHitTest = function (tempMatrix, x, y, gameObjectArray, camera, output)
|
||||
{
|
||||
var length = gameObjectArray.length;
|
||||
var scrollX = camera.scrollX;
|
||||
var scrollY = camera.scrollY;
|
||||
var cameraW = camera.width;
|
||||
var cameraH = camera.height;
|
||||
|
||||
output.length = 0;
|
||||
|
||||
if (gameObjectArray instanceof Array)
|
||||
{
|
||||
var culled = camera.cull(gameObjectArray);
|
||||
var culledLength = culled.length;
|
||||
|
||||
for (var index = 0; index < culledLength; ++index)
|
||||
{
|
||||
var object = culled[index];
|
||||
var tpoint = GetTransformedPoint(tempMatrix, object, x + scrollX * object.scrollFactorX, y + scrollY * object.scrollFactorY);
|
||||
|
||||
if (PointWithinGameObject(object, tpoint.x, tpoint.y))
|
||||
{
|
||||
output.push(object);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
else
|
||||
{
|
||||
var tpoint = GetTransformedPoint(tempMatrix, object, x + scrollX * object.scrollFactorX, y + scrollY * object.scrollFactorY);
|
||||
|
||||
if (PointWithinGameObject(object, tpoint.x, tpoint.y))
|
||||
{
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
module.exports = PointScreenToWorldHitTest;
|
Loading…
Reference in a new issue