Skip to content

Commit

Permalink
feat: create functions accept custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
Prozi committed Nov 1, 2024
1 parent 44a8cd9 commit 2329bd0
Show file tree
Hide file tree
Showing 36 changed files with 548 additions and 427 deletions.
24 changes: 12 additions & 12 deletions dist/base-system.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Body, BodyOptions, ChildrenData, Data, InTest, Leaf, PotentialVector, RBush, TraverseFunction, Vector } from "./model";
import { Box } from "./bodies/box";
import { Circle } from "./bodies/circle";
import { Ellipse } from "./bodies/ellipse";
import { Line } from "./bodies/line";
import { Point } from "./bodies/point";
import { Polygon } from "./bodies/polygon";
import { Box, BoxConstructor } from "./bodies/box";
import { Circle, CircleConstructor } from "./bodies/circle";
import { Ellipse, EllipseConstructor } from "./bodies/ellipse";
import { Line, LineConstructor } from "./bodies/line";
import { Point, PointConstructor } from "./bodies/point";
import { Polygon, PolygonConstructor } from "./bodies/polygon";
/**
* very base collision system (create, insert, update, draw, remove)
*/
Expand All @@ -13,27 +13,27 @@ export declare class BaseSystem<TBody extends Body = Body> extends RBush impleme
/**
* create point at position with options and add to system
*/
createPoint<TPoint extends Point>(position: PotentialVector, options?: BodyOptions): TPoint;
createPoint<TPoint extends Point>(position: PotentialVector, options?: BodyOptions, Class?: PointConstructor<TPoint>): TPoint | Point;
/**
* create line at position with options and add to system
*/
createLine<TLine extends Line>(start: Vector, end: Vector, options?: BodyOptions): TLine;
createLine<TLine extends Line>(start: Vector, end: Vector, options?: BodyOptions, Class?: LineConstructor<TLine>): TLine | Line;
/**
* create circle at position with options and add to system
*/
createCircle<TCircle extends Circle>(position: PotentialVector, radius: number, options?: BodyOptions): TCircle;
createCircle<TCircle extends Circle>(position: PotentialVector, radius: number, options?: BodyOptions, Class?: CircleConstructor<TCircle>): TCircle | Circle;
/**
* create box at position with options and add to system
*/
createBox<TBox extends Box>(position: PotentialVector, width: number, height: number, options?: BodyOptions): TBox;
createBox<TBox extends Box>(position: PotentialVector, width: number, height: number, options?: BodyOptions, Class?: BoxConstructor<TBox>): TBox | Box;
/**
* create ellipse at position with options and add to system
*/
createEllipse<TEllipse extends Ellipse>(position: PotentialVector, radiusX: number, radiusY?: number, step?: number, options?: BodyOptions): TEllipse;
createEllipse<TEllipse extends Ellipse>(position: PotentialVector, radiusX: number, radiusY?: number, step?: number, options?: BodyOptions, Class?: EllipseConstructor<TEllipse>): TEllipse | Ellipse;
/**
* create polygon at position with options and add to system
*/
createPolygon<TPolygon extends Polygon>(position: PotentialVector, points: PotentialVector[], options?: BodyOptions): TPolygon;
createPolygon<TPolygon extends Polygon>(position: PotentialVector, points: PotentialVector[], options?: BodyOptions, Class?: PolygonConstructor<TPolygon>): TPolygon | Polygon;
/**
* re-insert body into collision tree and update its bbox
* every body can be part of only one system
Expand Down
30 changes: 18 additions & 12 deletions dist/base-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,54 @@ class BaseSystem extends model_1.RBush {
/**
* create point at position with options and add to system
*/
createPoint(position, options) {
const point = new point_1.Point(position, options);
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) {
const line = new line_1.Line(start, end, options);
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) {
const circle = new circle_1.Circle(position, radius, options);
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) {
const box = new box_1.Box(position, width, height, options);
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) {
const ellipse = new ellipse_1.Ellipse(position, radiusX, radiusY, step, options);
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) {
const polygon = new polygon_1.Polygon(position, points, options);
createPolygon(position, points, options, Class) {
const PolygonClass = Class || polygon_1.Polygon;
const polygon = new PolygonClass(position, points, options);
this.insert(polygon);
return polygon;
}
Expand Down
3 changes: 3 additions & 0 deletions dist/bodies/box.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BodyGroup, BodyOptions, BodyType, PotentialVector } from "../model";
import { Polygon } from "./polygon";
export interface BoxConstructor<TBox extends Box> {
new (position: PotentialVector, width: number, height: number, options?: BodyOptions): TBox;
}
/**
* collider - box
*/
Expand Down
3 changes: 3 additions & 0 deletions dist/bodies/circle.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BBox, BodyGroup, BodyOptions, BodyProps, BodyType, PotentialVector, SATVector, Vector } from "../model";
import { Circle as SATCircle } from "sat";
import { System } from "../system";
export interface CircleConstructor<TCircle extends Circle> {
new (position: PotentialVector, radius: number, options?: BodyOptions): TCircle;
}
/**
* collider - circle
*/
Expand Down
3 changes: 3 additions & 0 deletions dist/bodies/ellipse.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BodyGroup, BodyOptions, BodyType, PotentialVector } from "../model";
import { Polygon } from "./polygon";
export interface EllipseConstructor<TEllipse extends Ellipse> {
new (position: PotentialVector, radiusX: number, radiusY?: number, step?: number, options?: BodyOptions): TEllipse;
}
/**
* collider - ellipse
*/
Expand Down
3 changes: 3 additions & 0 deletions dist/bodies/line.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BodyGroup, BodyOptions, BodyType, Vector } from "../model";
import { Vector as SATVector } from "sat";
import { Polygon } from "./polygon";
export interface LineConstructor<TLine extends Line> {
new (start: Vector, end: Vector, options?: BodyOptions): TLine;
}
/**
* collider - line
*/
Expand Down
3 changes: 3 additions & 0 deletions dist/bodies/point.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BodyGroup, BodyOptions, BodyType, PotentialVector } from "../model";
import { Box } from "./box";
export interface PointConstructor<TPoint extends Point> {
new (position: PotentialVector, options?: BodyOptions): TPoint;
}
/**
* collider - point (very tiny box)
*/
Expand Down
5 changes: 3 additions & 2 deletions dist/bodies/polygon.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isSimple } from "poly-decomp-es";
import { BBox, BodyGroup, BodyOptions, BodyProps, BodyType, DecompPolygon, PotentialVector, SATVector, Vector } from "../model";
import { Polygon as SATPolygon } from "sat";
import { System } from "../system";
export { isSimple };
export interface PolygonConstructor<TPolygon extends Polygon> {
new (position: PotentialVector, points: PotentialVector[], options?: BodyOptions): TPolygon;
}
/**
* collider - polygon
*/
Expand Down
3 changes: 1 addition & 2 deletions dist/bodies/polygon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Polygon = exports.isSimple = void 0;
exports.Polygon = void 0;
const poly_decomp_es_1 = require("poly-decomp-es");
Object.defineProperty(exports, "isSimple", { enumerable: true, get: function () { return poly_decomp_es_1.isSimple; } });
const model_1 = require("../model");
const optimized_1 = require("../optimized");
const utils_1 = require("../utils");
Expand Down
33 changes: 19 additions & 14 deletions dist/demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2067,48 +2067,54 @@ class BaseSystem extends model_1.RBush {
/**
* create point at position with options and add to system
*/
createPoint(position, options) {
const point = new point_1.Point(position, options);
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) {
const line = new line_1.Line(start, end, options);
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) {
const circle = new circle_1.Circle(position, radius, options);
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) {
const box = new box_1.Box(position, width, height, options);
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) {
const ellipse = new ellipse_1.Ellipse(position, radiusX, radiusY, step, options);
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) {
const polygon = new polygon_1.Polygon(position, points, options);
createPolygon(position, points, options, Class) {
const PolygonClass = Class || polygon_1.Polygon;
const polygon = new PolygonClass(position, points, options);
this.insert(polygon);
return polygon;
}
Expand Down Expand Up @@ -2769,9 +2775,8 @@ exports.Point = Point;
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Polygon = exports.isSimple = void 0;
exports.Polygon = void 0;
const poly_decomp_es_1 = __webpack_require__(/*! poly-decomp-es */ "./node_modules/poly-decomp-es/dist/poly-decomp-es.js");
Object.defineProperty(exports, "isSimple", ({ enumerable: true, get: function () { return poly_decomp_es_1.isSimple; } }));
const model_1 = __webpack_require__(/*! ../model */ "./src/model.ts");
const optimized_1 = __webpack_require__(/*! ../optimized */ "./src/optimized.ts");
const utils_1 = __webpack_require__(/*! ../utils */ "./src/utils.ts");
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/navigation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/assets/search.js

Large diffs are not rendered by default.

Loading

0 comments on commit 2329bd0

Please sign in to comment.