PointScreenToWorldHitTest added to Global Input Manager

This commit is contained in:
Felipe Alfonso 2017-07-07 15:59:17 -04:00
parent 423326c71b
commit ebea116809
4 changed files with 69 additions and 14 deletions

View file

@ -70,15 +70,24 @@ var Camera = new Class({
for (var index = 0; index < length; ++index) for (var index = 0; index < length; ++index)
{ {
var object = renderableObjects[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 && /* Not every renderable object has a dimension */
objectX < cullW && objectY < cullH) 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); culledObjects.push(object);
} }

View file

@ -1,6 +0,0 @@
var IntersectGameObject = function (point, gameObjectArray, camera)
{
return false;
};
module.exports = IntersectGameObject;

View file

@ -8,6 +8,7 @@ var Mouse = require('./mouse/MouseManager');
var MouseEvent = require('./mouse/events/'); var MouseEvent = require('./mouse/events/');
var PointWithinGameObject = require('./components/PointWithinGameObject'); var PointWithinGameObject = require('./components/PointWithinGameObject');
var TransformMatrix = require('../gameobjects/components/TransformMatrix'); var TransformMatrix = require('../gameobjects/components/TransformMatrix');
var PointScreenToWorldHitTest = require('./components/PointScreenToWorldHitTest');
var GlobalInputManager = new Class({ var GlobalInputManager = new Class({
@ -32,6 +33,7 @@ var GlobalInputManager = new Class({
this._tempMatrix = new TransformMatrix(); this._tempMatrix = new TransformMatrix();
this._tempPoint = { x: 0, y: 0 }; this._tempPoint = { x: 0, y: 0 };
this._tempHitTest = [];
}, },
/** /**
@ -92,6 +94,11 @@ var GlobalInputManager = new Class({
pointWithinGameObject: function (gameObject, x, y) pointWithinGameObject: function (gameObject, x, y)
{ {
return PointWithinGameObject(gameObject, x, y); return PointWithinGameObject(gameObject, x, y);
},
pointScreenToWorldHitTest: function (gameObjects, x, y, camera)
{
return PointScreenToWorldHitTest(this._tempMatrix, x, y, gameObjects, camera, this._tempHitTest);
} }
}); });

View 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;