Skip to content

Commit

Permalink
feat: try to have a 8kb smaller build
Browse files Browse the repository at this point in the history
  • Loading branch information
Prozi committed Nov 1, 2024
1 parent 99e3e45 commit ffcf637
Show file tree
Hide file tree
Showing 22 changed files with 6,239 additions and 6,015 deletions.
235 changes: 124 additions & 111 deletions dist/base-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,150 +14,163 @@ const polygon_1 = require("./bodies/polygon");
* very base collision system (create, insert, update, draw, remove)
*/
class BaseSystem extends model_1.RBush {
/**
/**
* create point at position with options and add to system
*/
createPoint(position, options, Class) {
const PointClass = Class || point_1.Point;
const point = new PointClass(position, options);
this.insert(point);
return point;
}
/**
createPoint(position, options, Class) {
const PointClass = Class || point_1.Point;
const point = new PointClass(position, options);
this.insert(point);
return point;
}

/**
* create line at position with options and add to system
*/
createLine(start, end, options, Class) {
const LineClass = Class || line_1.Line;
const line = new LineClass(start, end, options);
this.insert(line);
return line;
}
/**
createLine(start, end, options, Class) {
const LineClass = Class || line_1.Line;
const line = new LineClass(start, end, options);
this.insert(line);
return line;
}

/**
* create circle at position with options and add to system
*/
createCircle(position, radius, options, Class) {
const CircleClass = Class || circle_1.Circle;
const circle = new CircleClass(position, radius, options);
this.insert(circle);
return circle;
}
/**
createCircle(position, radius, options, Class) {
const CircleClass = Class || circle_1.Circle;
const circle = new CircleClass(position, radius, options);
this.insert(circle);
return circle;
}

/**
* create box at position with options and add to system
*/
createBox(position, width, height, options, Class) {
const BoxClass = Class || box_1.Box;
const box = new BoxClass(position, width, height, options);
this.insert(box);
return box;
}
/**
createBox(position, width, height, options, Class) {
const BoxClass = Class || box_1.Box;
const box = new BoxClass(position, width, height, options);
this.insert(box);
return box;
}

/**
* create ellipse at position with options and add to system
*/
createEllipse(position, radiusX, radiusY = radiusX, step, options, Class) {
const EllipseClass = Class || ellipse_1.Ellipse;
const ellipse = new EllipseClass(position, radiusX, radiusY, step, options);
this.insert(ellipse);
return ellipse;
}
/**
createEllipse(position, radiusX, radiusY = radiusX, step, options, Class) {
const EllipseClass = Class || ellipse_1.Ellipse;
const ellipse = new EllipseClass(position, radiusX, radiusY, step, options);
this.insert(ellipse);
return ellipse;
}

/**
* create polygon at position with options and add to system
*/
createPolygon(position, points, options, Class) {
const PolygonClass = Class || polygon_1.Polygon;
const polygon = new PolygonClass(position, points, options);
this.insert(polygon);
return polygon;
}
/**
createPolygon(position, points, options, Class) {
const PolygonClass = Class || polygon_1.Polygon;
const polygon = new PolygonClass(position, points, options);
this.insert(polygon);
return polygon;
}

/**
* re-insert body into collision tree and update its bbox
* every body can be part of only one system
*/
insert(body) {
body.bbox = body.getAABBAsBBox();
if (body.system) {
// allow end if body inserted and not moved
if (!(0, utils_1.bodyMoved)(body)) {
return this;
}
// old bounding box *needs* to be removed
body.system.remove(body);
}
// only then we update min, max
body.minX = body.bbox.minX - body.padding;
body.minY = body.bbox.minY - body.padding;
body.maxX = body.bbox.maxX + body.padding;
body.maxY = body.bbox.maxY + body.padding;
// reinsert bounding box to collision tree
return super.insert(body);
insert(body) {
body.bbox = body.getAABBAsBBox();
if (body.system) {
// allow end if body inserted and not moved
if (!(0, utils_1.bodyMoved)(body)) {
return this;
}
// old bounding box *needs* to be removed
body.system.remove(body);
}
/**
// only then we update min, max
body.minX = body.bbox.minX - body.padding;
body.minY = body.bbox.minY - body.padding;
body.maxX = body.bbox.maxX + body.padding;
body.maxY = body.bbox.maxY + body.padding;
// reinsert bounding box to collision tree
return super.insert(body);
}

/**
* updates body in collision tree
*/
updateBody(body) {
body.updateBody();
}
/**
updateBody(body) {
body.updateBody();
}

/**
* update all bodies aabb
*/
update() {
(0, optimized_1.forEach)(this.all(), (body) => {
this.updateBody(body);
});
}
/**
update() {
(0, optimized_1.forEach)(this.all(), body => {
this.updateBody(body);
});
}

/**
* draw exact bodies colliders outline
*/
draw(context) {
(0, optimized_1.forEach)(this.all(), (body) => {
body.draw(context);
});
}
/**
draw(context) {
(0, optimized_1.forEach)(this.all(), body => {
body.draw(context);
});
}

/**
* draw bounding boxes hierarchy outline
*/
drawBVH(context, isTrigger = true) {
const drawChildren = (body) => {
(0, utils_1.drawBVH)(context, body, isTrigger);
if (body.children) {
(0, optimized_1.forEach)(body.children, drawChildren);
}
};
(0, optimized_1.forEach)(this.data.children, drawChildren);
}
/**
drawBVH(context, isTrigger = true) {
const drawChildren = body => {
(0, utils_1.drawBVH)(context, body, isTrigger);
if (body.children) {
(0, optimized_1.forEach)(body.children, drawChildren);
}
};
(0, optimized_1.forEach)(this.data.children, drawChildren);
}

/**
* remove body aabb from collision tree
*/
remove(body, equals) {
body.system = undefined;
return super.remove(body, equals);
}
/**
remove(body, equals) {
body.system = undefined;
return super.remove(body, equals);
}

/**
* get object potential colliders
* @deprecated because it's slower to use than checkOne() or checkAll()
*/
getPotentials(body) {
// filter here is required as collides with self
return (0, optimized_1.filter)(this.search(body), (candidate) => candidate !== body);
}
/**
getPotentials(body) {
// filter here is required as collides with self
return (0, optimized_1.filter)(this.search(body), candidate => candidate !== body);
}

/**
* used to find body deep inside data with finder function returning boolean found or not
*
* @param traverseFunction
* @param tree
*/
traverse(traverseFunction, { children } = this.data) {
return children === null || children === void 0 ? void 0 : children.find((body, index) => {
if (!body) {
return false;
}
if (body.typeGroup && traverseFunction(body, children, index)) {
return true;
}
// if callback returns true, ends forEach
if (body.children) {
this.traverse(traverseFunction, body);
}
});
}
traverse(traverseFunction, { children } = this.data) {
return children === null || children === void 0 ? void 0 : children.find((body, index) => {
if (!body) {
return false;
}
if (body.typeGroup && traverseFunction(body, children, index)) {
return true;
}
// if callback returns true, ends forEach
if (body.children) {
this.traverse(traverseFunction, body);
}
});
}
}
exports.BaseSystem = BaseSystem;
8 changes: 4 additions & 4 deletions dist/benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stressBenchmark = exports.insertionBenchmark = void 0;
var insertion_bench_1 = require("./insertion.bench");
Object.defineProperty(exports, "insertionBenchmark", { enumerable: true, get: function () { return insertion_bench_1.insertionBenchmark; } });
var stress_bench_1 = require("./stress.bench");
Object.defineProperty(exports, "stressBenchmark", { enumerable: true, get: function () { return stress_bench_1.stressBenchmark; } });
const insertion_bench_1 = require("./insertion.bench");
Object.defineProperty(exports, "insertionBenchmark", { enumerable: true, get: function() { return insertion_bench_1.insertionBenchmark; } });
const stress_bench_1 = require("./stress.bench");
Object.defineProperty(exports, "stressBenchmark", { enumerable: true, get: function() { return stress_bench_1.stressBenchmark; } });
Loading

0 comments on commit ffcf637

Please sign in to comment.