Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 12x 19x 19x 19x 19x 19x 1x 30x 30x 30x 30x 30x 30x 1x 17x 17x 17x 17x 6x 1x 1x 5x 5x 5x 5x 1x 1x 8x 8x 8x | import RBush from "rbush";
import Check from "./Check.js";
/**
* Wrapper around rbush for use with Rectangle types.
* @private
*/
function RectangleCollisionChecker() {
this._tree = new RBush();
}
function RectangleWithId() {
this.minX = 0.0;
this.minY = 0.0;
this.maxX = 0.0;
this.maxY = 0.0;
this.id = "";
}
RectangleWithId.fromRectangleAndId = function (id, rectangle, result) {
result.minX = rectangle.west;
result.minY = rectangle.south;
result.maxX = rectangle.east;
result.maxY = rectangle.north;
result.id = id;
return result;
};
/**
* Insert a rectangle into the collision checker.
*
* @param {string} id Unique string ID for the rectangle being inserted.
* @param {Rectangle} rectangle A Rectangle
* @private
*/
RectangleCollisionChecker.prototype.insert = function (id, rectangle) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("id", id);
Check.typeOf.object("rectangle", rectangle);
//>>includeEnd('debug');
const withId = RectangleWithId.fromRectangleAndId(
id,
rectangle,
new RectangleWithId(),
);
this._tree.insert(withId);
};
function idCompare(a, b) {
return a.id === b.id;
}
const removalScratch = new RectangleWithId();
/**
* Remove a rectangle from the collision checker.
*
* @param {string} id Unique string ID for the rectangle being removed.
* @param {Rectangle} rectangle A Rectangle
* @private
*/
RectangleCollisionChecker.prototype.remove = function (id, rectangle) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("id", id);
Check.typeOf.object("rectangle", rectangle);
//>>includeEnd('debug');
const withId = RectangleWithId.fromRectangleAndId(
id,
rectangle,
removalScratch,
);
this._tree.remove(withId, idCompare);
};
const collisionScratch = new RectangleWithId();
/**
* Checks if a given rectangle collides with any of the rectangles in the collection.
*
* @param {Rectangle} rectangle A Rectangle that should be checked against the rectangles in the collision checker.
* @returns {boolean} Whether the rectangle collides with any of the rectangles in the collision checker.
*/
RectangleCollisionChecker.prototype.collides = function (rectangle) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("rectangle", rectangle);
//>>includeEnd('debug');
const withId = RectangleWithId.fromRectangleAndId(
"",
rectangle,
collisionScratch,
);
return this._tree.collides(withId);
};
export default RectangleCollisionChecker;
|