Added Oversimplified.GameObjectsAtPoint() to return a list of all objects overlapping with a point, altered Oversimplified.CollisionAtPoint() to use Oversimpified.GameObjectsAtPoint() to reduce code.

This commit is contained in:
Robbie Antenesse 2016-04-08 07:33:11 -06:00
parent 9ad26265d1
commit fbfa3faa53
1 changed files with 37 additions and 22 deletions

View File

@ -889,17 +889,20 @@ Oversimplified.GameObject.prototype.IsOverlapping = function (doSimple) {
Oversimplified.GameObject.prototype.IfOverlappingThenMove = function (doSimple) { Oversimplified.GameObject.prototype.IfOverlappingThenMove = function (doSimple) {
var overlappingObject = this.IsOverlapping(doSimple); var overlappingObject = this.IsOverlapping(doSimple);
if (overlappingObject != false) if (overlappingObject != false) {
{
if (this.x < overlappingObject.x) if (this.x < overlappingObject.x)
this.x--; this.x--;
if (this.x >= overlappingObject.x) if (this.x >= overlappingObject.x)
this.x++; this.x++;
if (this.y < overlappingObject.y) if (this.y < overlappingObject.y)
this.y--; this.y--;
if (this.y >= overlappingObject.y) if (this.y >= overlappingObject.y)
this.y++; this.y++;
}
return true;
} else {
return false;
}
} }
// Prevents the object from moving outside of the room's boundaries. // Prevents the object from moving outside of the room's boundaries.
@ -987,28 +990,40 @@ Oversimplified.GameObject.prototype.Destroy = function () {
// Check if the point (x, y) lies inside the bounds of ANY object in the room. // Check if the point (x, y) lies inside the bounds of ANY object in the room.
// If yes and if that object is flagged as solid, then there is a collision. // If yes and if that object is flagged as solid, then there is a collision.
Oversimplified.CollisionAtPoint = function (x, y) { Oversimplified.GameObjectsAtPoint = function (x, y) {
var currentRoom = Oversimplified.R[Oversimplified.R.currentRoom]; var objectsAtPoint = [];
for (var obj in Oversimplified.O) { for (var obj in Oversimplified.O) {
var object = Oversimplified.O[obj]; var object = Oversimplified.O[obj];
if (object != this) { for (var i = 0; i < 2 * object.xBound; i++) {
for (var i = 0; i < 2 * object.xBound; i++) { for (var j = 0; j < 2 * object.yBound; j++) {
for (var j = 0; j < 2 * object.yBound; j++) { var xToCheck = (object.x - object.xBound) + i;
var xToCheck = (object.x - object.xBound) + i; var yToCheck = (object.y - object.yBound) + j;
var yToCheck = (object.y - object.yBound) + j;
if (xToCheck == x && yToCheck == y) if (xToCheck == x && yToCheck == y)
{ {
if (object.solid) { objectsAtPoint.push(object);
return true;
}
}
} }
} }
} }
} }
if (objectsAtPoint.length > 0) {
return objectsAtPoint;
} else {
return false;
}
}
// Check if the point (x, y) lies inside the bounds of ANY object in the room.
// If yes and if that object is flagged as solid, then there is a collision.
Oversimplified.CollisionAtPoint = function (x, y) {
var objectsAtPoint = Oversimplified.GameObjectsAtPoint(x, y);
for (var i = 0; i < objectsAtPoint.length; i++) {
if (objectsAtPoint[i].solid == true) {
return true;
}
}
return false; return false;
} }