Compare commits
12 Commits
master
...
restructur
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39c3020e1d | ||
|
|
3874c8d2db | ||
|
|
b7c2ccd03d | ||
|
|
1808e52d8f | ||
|
|
a0c21dbc8b | ||
|
|
86f9b40691 | ||
|
|
eb09c3c24d | ||
|
|
42c35c0ac4 | ||
|
|
c4bc16309e | ||
|
|
f5804ad2f4 | ||
|
|
3cbb8c5b0d | ||
|
|
6ef6d16606 |
@ -1,157 +0,0 @@
|
||||
import ts from 'typescript';
|
||||
|
||||
export default options => context => node => {
|
||||
return ts.visitEachChild(node, (node) => visitor(context.factory, node, options), context);
|
||||
}
|
||||
|
||||
function visitor(factory, node, options) {
|
||||
if (!ts.isClassDeclaration(node) ||
|
||||
!isClassIncluded(node.name.escapedText, options)) {
|
||||
return node;
|
||||
}
|
||||
if (isClassDerived(node)) {
|
||||
// workaround for https://github.com/microsoft/TypeScript/issues/46503
|
||||
return splitClassIntoConstAndInterface(factory, node);
|
||||
}
|
||||
return createNodeWithFactories(factory, node);
|
||||
}
|
||||
|
||||
function isClassIncluded(className, options) {
|
||||
return options.classes.includes(className);
|
||||
}
|
||||
|
||||
function isClassDerived(node) {
|
||||
return node.heritageClauses[0] !== undefined;
|
||||
}
|
||||
|
||||
function splitClassIntoConstAndInterface(factory, node) {
|
||||
return [
|
||||
createConstNodeWithStaticMethods(factory, node),
|
||||
createInterfaceWithMethods(factory, node)
|
||||
];
|
||||
}
|
||||
|
||||
function createConstNodeWithStaticMethods(factory, node) {
|
||||
return factory.createVariableStatement(
|
||||
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
|
||||
factory.createVariableDeclarationList(
|
||||
[factory.createVariableDeclaration(
|
||||
node.name,
|
||||
undefined,
|
||||
factory.createTypeLiteralNode([
|
||||
// constructors
|
||||
...node.members
|
||||
.filter(ts.isConstructorDeclaration)
|
||||
.flatMap(member => [
|
||||
factory.createConstructSignature(
|
||||
undefined,
|
||||
member.parameters,
|
||||
factory.createTypeReferenceNode(
|
||||
node.name,
|
||||
undefined
|
||||
)
|
||||
),
|
||||
factory.createCallSignature(
|
||||
undefined,
|
||||
member.parameters,
|
||||
factory.createTypeReferenceNode(
|
||||
node.name,
|
||||
undefined
|
||||
)
|
||||
)
|
||||
].map(node => copyComments(node, member))
|
||||
),
|
||||
// static properties
|
||||
...node.members
|
||||
.filter(member => hasModifier(ts.SyntaxKind.StaticKeyword, member))
|
||||
.filter(member => !hasModifier(ts.SyntaxKind.PrivateKeyword, member))
|
||||
.map(member => declarationToSignature(factory, member))
|
||||
]
|
||||
),
|
||||
undefined
|
||||
)],
|
||||
ts.NodeFlags.Const | ts.NodeFlags.Ambient | ts.NodeFlags.ContextFlags
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function createInterfaceWithMethods(factory, node) {
|
||||
const result = factory.createInterfaceDeclaration(
|
||||
undefined,
|
||||
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
|
||||
node.name,
|
||||
undefined,
|
||||
node.heritageClauses,
|
||||
// instance properties
|
||||
node.members
|
||||
.filter(member => !ts.isConstructorDeclaration(member))
|
||||
.filter(member => !hasModifier(ts.SyntaxKind.StaticKeyword, member))
|
||||
.filter(member => !hasModifier(ts.SyntaxKind.PrivateKeyword, member))
|
||||
.map(member => declarationToSignature(factory, member))
|
||||
);
|
||||
return copyComments(result, node);
|
||||
}
|
||||
|
||||
function hasModifier(kind, node) {
|
||||
return node.modifiers?.some?.(modifier => modifier.kind === kind);
|
||||
}
|
||||
|
||||
function declarationToSignature(factory, node) {
|
||||
let result;
|
||||
switch (node.kind) {
|
||||
case ts.SyntaxKind.PropertyDeclaration:
|
||||
result = factory.createPropertySignature(
|
||||
undefined,
|
||||
node.name,
|
||||
undefined,
|
||||
node.type
|
||||
);
|
||||
break;
|
||||
case ts.SyntaxKind.MethodDeclaration:
|
||||
result = factory.createMethodSignature(
|
||||
undefined,
|
||||
node.name,
|
||||
undefined,
|
||||
undefined,
|
||||
node.parameters,
|
||||
node.type
|
||||
);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Could not convert node of kind ${node.kind} to signature.`);
|
||||
}
|
||||
return copyComments(result, node);
|
||||
}
|
||||
|
||||
function createNodeWithFactories(factory, node) {
|
||||
return [
|
||||
...node.members
|
||||
.filter(ts.isConstructorDeclaration)
|
||||
.map(member => member.parameters)
|
||||
.map(parameters => createFunctionDeclaration(factory, node, parameters)),
|
||||
node
|
||||
];
|
||||
}
|
||||
|
||||
function createFunctionDeclaration(factory, node, parameters) {
|
||||
const result = factory.createFunctionDeclaration(
|
||||
undefined,
|
||||
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
|
||||
undefined,
|
||||
node.name,
|
||||
undefined,
|
||||
parameters,
|
||||
factory.createTypeReferenceNode(
|
||||
node.name,
|
||||
undefined
|
||||
),
|
||||
undefined
|
||||
);
|
||||
return copyComments(result, node);
|
||||
}
|
||||
|
||||
function copyComments(node, original) {
|
||||
ts.setSyntheticLeadingComments(node, ts.getSyntheticLeadingComments(original));
|
||||
ts.setSyntheticTrailingComments(node, ts.getSyntheticTrailingComments(original));
|
||||
return node;
|
||||
}
|
||||
142
build-utils/factoryConstructorTransformer.js
Normal file
142
build-utils/factoryConstructorTransformer.js
Normal file
@ -0,0 +1,142 @@
|
||||
import ts from 'typescript';
|
||||
|
||||
export default ({ enabled }) => shared => {
|
||||
shared.classes = new Set();
|
||||
return {
|
||||
before: context => node => {
|
||||
node = ts.visitEachChild(node, (node) => removeFactoryConstructorDecoratorVisitor(context.factory, node, shared), context);
|
||||
if (!enabled) {
|
||||
return node;
|
||||
}
|
||||
return ts.visitEachChild(node, (node) => applyFactoryConstructorDecoratorVisitor(context.factory, node, shared), context);
|
||||
},
|
||||
afterDeclarations: context => node => {
|
||||
if (!enabled) {
|
||||
return node;
|
||||
}
|
||||
return ts.visitEachChild(node, (node) => visitor(context.factory, node, shared), context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeFactoryConstructorDecoratorVisitor(factory, node, shared) {
|
||||
if (ts.isClassDeclaration(node) && hasFactoryConstructorDecorator(node.decorators)) {
|
||||
shared.classes.add(node.name.escapedText);
|
||||
return factory.updateClassDeclaration(
|
||||
node,
|
||||
removeFactoryConstructorDecorator(node.decorators),
|
||||
node.modifiers,
|
||||
node.name,
|
||||
node.typeParameters,
|
||||
node.heritageClauses,
|
||||
node.members
|
||||
);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
function applyFactoryConstructorDecoratorVisitor(factory, node, shared) {
|
||||
if (isRelevantClassDeclaration(node, shared)) {
|
||||
const members = node.members.map(member => {
|
||||
if (ts.isConstructorDeclaration(member) && member.body) {
|
||||
const block = factory.updateBlock(member.body, [
|
||||
createRecallWithNewKeyword(factory, node.name, member.parameters),
|
||||
...member.body.statements
|
||||
]);
|
||||
return factory.updateConstructorDeclaration(member, member.decorators, member.modifiers, member.parameters, block);
|
||||
}
|
||||
return member;
|
||||
});
|
||||
return factory.updateClassDeclaration(
|
||||
node,
|
||||
removeFactoryConstructorDecorator(node.decorators),
|
||||
node.modifiers,
|
||||
node.name,
|
||||
node.typeParameters,
|
||||
node.heritageClauses,
|
||||
members
|
||||
);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
function hasFactoryConstructorDecorator(decorators) {
|
||||
return decorators?.some(isFactoryConstructorDecorator);
|
||||
}
|
||||
|
||||
function removeFactoryConstructorDecorator(decorators) {
|
||||
const filtered = decorators?.filter(decorator => !isFactoryConstructorDecorator(decorator));
|
||||
if (filtered?.length > 0) {
|
||||
return filtered;
|
||||
}
|
||||
}
|
||||
|
||||
function isFactoryConstructorDecorator(decorator) {
|
||||
return ts.isIdentifier(decorator.expression) && decorator.expression.escapedText === 'factoryConstructor';
|
||||
}
|
||||
|
||||
function createRecallWithNewKeyword(factory, className, parameters) {
|
||||
return factory.createIfStatement(
|
||||
factory.createPrefixUnaryExpression(
|
||||
ts.SyntaxKind.ExclamationToken,
|
||||
factory.createParenthesizedExpression(factory.createBinaryExpression(
|
||||
factory.createThis(),
|
||||
factory.createToken(ts.SyntaxKind.InstanceOfKeyword),
|
||||
className
|
||||
))
|
||||
),
|
||||
factory.createBlock(
|
||||
[factory.createReturnStatement(factory.createNewExpression(
|
||||
className,
|
||||
undefined,
|
||||
parameters.map(parameter => parameter.name)
|
||||
))],
|
||||
true
|
||||
),
|
||||
undefined
|
||||
)
|
||||
}
|
||||
|
||||
function visitor(factory, node, shared) {
|
||||
if (!isRelevantClassDeclaration(node, shared)) {
|
||||
return node;
|
||||
}
|
||||
return createNodeWithFactories(factory, node);
|
||||
}
|
||||
|
||||
function isRelevantClassDeclaration(node, shared) {
|
||||
return ts.isClassDeclaration(node) && shared.classes.has(node.name.escapedText);
|
||||
}
|
||||
|
||||
function createNodeWithFactories(factory, node) {
|
||||
return [
|
||||
...node.members
|
||||
.filter(ts.isConstructorDeclaration)
|
||||
.map(member => member.parameters)
|
||||
.map(parameters => createFunctionDeclaration(factory, node, parameters)),
|
||||
node
|
||||
];
|
||||
}
|
||||
|
||||
function createFunctionDeclaration(factory, node, parameters) {
|
||||
const result = factory.createFunctionDeclaration(
|
||||
undefined,
|
||||
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
|
||||
undefined,
|
||||
node.name,
|
||||
undefined,
|
||||
parameters,
|
||||
factory.createTypeReferenceNode(
|
||||
node.name,
|
||||
undefined
|
||||
),
|
||||
undefined
|
||||
);
|
||||
return copyComments(result, node);
|
||||
}
|
||||
|
||||
function copyComments(node, original) {
|
||||
ts.setSyntheticLeadingComments(node, ts.getSyntheticLeadingComments(original));
|
||||
ts.setSyntheticTrailingComments(node, ts.getSyntheticTrailingComments(original));
|
||||
return node;
|
||||
}
|
||||
1149
package-lock.json
generated
1149
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -55,13 +55,13 @@
|
||||
"rollup-plugin-filesize": "^9.1.1",
|
||||
"rollup-plugin-license": "^2.3.0",
|
||||
"rollup-plugin-terser": "^7.0.2",
|
||||
"rollup-plugin-ts": "^1.3.10",
|
||||
"rollup-plugin-ts": "^2.0.7",
|
||||
"sinon": "^9.2.4",
|
||||
"ts-node": "^9.1.1",
|
||||
"tsd": "^0.18.0",
|
||||
"tsd": "^0.28.1",
|
||||
"typedoc": "^0.17.8",
|
||||
"typedoc-plugin-markdown": "^2.3.1",
|
||||
"typescript": "^4.0.0-beta"
|
||||
"typescript": "~4.6"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint './src/**/*.ts'",
|
||||
|
||||
@ -5,35 +5,55 @@ import filesize from 'rollup-plugin-filesize';
|
||||
import typescript from 'rollup-plugin-ts';
|
||||
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
||||
import commonjs from '@rollup/plugin-commonjs';
|
||||
import declarationTransformer from './build-utils/declarationTransformer';
|
||||
import factoryConstructorTransformer from './build-utils/factoryConstructorTransformer';
|
||||
|
||||
import licenseBanner from './build-utils/license';
|
||||
|
||||
|
||||
function buildTransformerPipeline(...transformers) {
|
||||
transformers = transformers.map(t => t({}))
|
||||
|
||||
return {
|
||||
before: transformers
|
||||
.map(t => t.before)
|
||||
.filter(Boolean),
|
||||
after: transformers
|
||||
.map(t => t.after)
|
||||
.filter(Boolean),
|
||||
afterDeclarations: transformers
|
||||
.map(t => t.afterDeclarations)
|
||||
.filter(Boolean),
|
||||
}
|
||||
}
|
||||
|
||||
export default [
|
||||
{
|
||||
src: 'src/index.ts',
|
||||
dest: 'dist/planck.js',
|
||||
minimize: false,
|
||||
declaration: true,
|
||||
includeFactoryConstructors: true,
|
||||
},
|
||||
{
|
||||
src: 'src/index.ts',
|
||||
dest: 'dist/planck.min.js',
|
||||
minimize: true,
|
||||
declaration: false,
|
||||
includeFactoryConstructors: true,
|
||||
},
|
||||
{
|
||||
src: 'testbed/index.ts',
|
||||
dest: 'dist/planck-with-testbed.js',
|
||||
minimize: false,
|
||||
declaration: true,
|
||||
includeFactoryConstructors: true,
|
||||
},
|
||||
{
|
||||
src: 'testbed/index.ts',
|
||||
dest: 'dist/planck-with-testbed.min.js',
|
||||
minimize: true,
|
||||
declaration: false,
|
||||
includeFactoryConstructors: true,
|
||||
}
|
||||
].map(options => {
|
||||
const config = {
|
||||
@ -61,36 +81,9 @@ export default [
|
||||
...resolvedConfig,
|
||||
declaration: options.declaration
|
||||
}),
|
||||
transformers: {
|
||||
afterDeclarations: [
|
||||
declarationTransformer({
|
||||
classes: [
|
||||
'Vec2',
|
||||
'Vec3',
|
||||
'Rot',
|
||||
'Transform',
|
||||
'AABB',
|
||||
'World',
|
||||
'BoxShape',
|
||||
'CircleShape',
|
||||
'ChainShape',
|
||||
'EdgeShape',
|
||||
'PolygonShape',
|
||||
'DistanceJoint',
|
||||
'FrictionJoint',
|
||||
'GearJoint',
|
||||
'MotorJoint',
|
||||
'MouseJoint',
|
||||
'PrismaticJoint',
|
||||
'PulleyJoint',
|
||||
'RevoluteJoint',
|
||||
'RopeJoint',
|
||||
'WeldJoint',
|
||||
'WheelJoint',
|
||||
]
|
||||
})
|
||||
]
|
||||
},
|
||||
transformers: buildTransformerPipeline(
|
||||
factoryConstructorTransformer({ enabled: options.includeFactoryConstructors })
|
||||
),
|
||||
}),
|
||||
license({
|
||||
banner: licenseBanner,
|
||||
|
||||
@ -25,6 +25,7 @@
|
||||
import common from '../util/common';
|
||||
import Math from '../common/Math';
|
||||
import Vec2 from '../common/Vec2';
|
||||
import factoryConstructor from '../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
|
||||
@ -51,15 +52,12 @@ export interface RayCastOutput {
|
||||
fraction: number;
|
||||
}
|
||||
|
||||
@factoryConstructor
|
||||
export default class AABB {
|
||||
lowerBound: Vec2;
|
||||
upperBound: Vec2;
|
||||
|
||||
constructor(lower?: Vec2, upper?: Vec2) {
|
||||
if (!(this instanceof AABB)) {
|
||||
return new AABB(lower, upper);
|
||||
}
|
||||
|
||||
this.lowerBound = Vec2.zero();
|
||||
this.upperBound = Vec2.zero();
|
||||
|
||||
|
||||
@ -24,19 +24,16 @@
|
||||
|
||||
import type Vec2 from '../../common/Vec2';
|
||||
import PolygonShape from './PolygonShape';
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
/**
|
||||
* A rectangle polygon which extend PolygonShape.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class BoxShape extends PolygonShape {
|
||||
static TYPE = 'polygon' as const;
|
||||
|
||||
constructor(hx: number, hy: number, center?: Vec2, angle?: number) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof BoxShape)) {
|
||||
return new BoxShape(hx, hy, center, angle);
|
||||
}
|
||||
|
||||
super();
|
||||
|
||||
this._setAsBox(hx, hy, center, angle);
|
||||
|
||||
@ -31,6 +31,7 @@ import Vec2 from '../../common/Vec2';
|
||||
import Settings from '../../Settings';
|
||||
import Shape from '../Shape';
|
||||
import EdgeShape from './EdgeShape';
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -44,6 +45,7 @@ const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
*
|
||||
* WARNING: The chain will not collide properly if there are self-intersections.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class ChainShape extends Shape {
|
||||
static TYPE = 'chain' as const;
|
||||
|
||||
@ -57,11 +59,6 @@ export default class ChainShape extends Shape {
|
||||
m_isLoop: boolean;
|
||||
|
||||
constructor(vertices?: Vec2[], loop?: boolean) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof ChainShape)) {
|
||||
return new ChainShape(vertices, loop);
|
||||
}
|
||||
|
||||
super();
|
||||
|
||||
this.m_type = ChainShape.TYPE;
|
||||
|
||||
@ -31,11 +31,13 @@ import AABB, { RayCastInput, RayCastOutput } from '../AABB';
|
||||
import Transform from '../../common/Transform';
|
||||
import { MassData } from '../../dynamics/Body';
|
||||
import { DistanceProxy } from '../Distance';
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
|
||||
|
||||
@factoryConstructor
|
||||
export default class CircleShape extends Shape {
|
||||
static TYPE = 'circle' as const;
|
||||
|
||||
@ -45,11 +47,6 @@ export default class CircleShape extends Shape {
|
||||
constructor(radius?: number);
|
||||
// tslint:disable-next-line:typedef
|
||||
constructor(a, b?) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof CircleShape)) {
|
||||
return new CircleShape(a, b);
|
||||
}
|
||||
|
||||
super();
|
||||
|
||||
this.m_type = CircleShape.TYPE;
|
||||
|
||||
@ -30,12 +30,14 @@ import Vec2 from '../../common/Vec2';
|
||||
import AABB, { RayCastInput, RayCastOutput } from '../AABB';
|
||||
import { MassData } from '../../dynamics/Body';
|
||||
import { DistanceProxy } from '../Distance';
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
/**
|
||||
* A line segment (edge) shape. These can be connected in chains or loops to
|
||||
* other edge shapes. The connectivity information is used to ensure correct
|
||||
* contact normals.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class EdgeShape extends Shape {
|
||||
static TYPE = 'edge' as const;
|
||||
|
||||
@ -51,11 +53,6 @@ export default class EdgeShape extends Shape {
|
||||
m_hasVertex3: boolean;
|
||||
|
||||
constructor(v1?: Vec2, v2?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof EdgeShape)) {
|
||||
return new EdgeShape(v1, v2);
|
||||
}
|
||||
|
||||
super();
|
||||
|
||||
this.m_type = EdgeShape.TYPE;
|
||||
|
||||
@ -32,6 +32,7 @@ import Rot from '../../common/Rot';
|
||||
import Vec2 from '../../common/Vec2';
|
||||
import Settings from '../../Settings';
|
||||
import Shape from '../Shape';
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -43,6 +44,7 @@ const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
* Settings.maxPolygonVertices. In most cases you should not need many vertices
|
||||
* for a convex polygon. extends Shape
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class PolygonShape extends Shape {
|
||||
static TYPE = 'polygon' as const;
|
||||
|
||||
@ -53,11 +55,6 @@ export default class PolygonShape extends Shape {
|
||||
|
||||
// @ts-ignore
|
||||
constructor(vertices?: Vec2[]) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof PolygonShape)) {
|
||||
return new PolygonShape(vertices);
|
||||
}
|
||||
|
||||
super();
|
||||
|
||||
this.m_type = PolygonShape.TYPE;
|
||||
|
||||
@ -25,21 +25,20 @@
|
||||
import common from '../util/common';
|
||||
import Vec2 from './Vec2';
|
||||
import Math from './Math';
|
||||
import factoryConstructor from '../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
|
||||
|
||||
@factoryConstructor
|
||||
export default class Rot {
|
||||
s: number;
|
||||
c: number;
|
||||
|
||||
/** Initialize from an angle in radians. */
|
||||
constructor(angle?: number | Rot) {
|
||||
if (!(this instanceof Rot)) {
|
||||
return new Rot(angle);
|
||||
}
|
||||
if (typeof angle === 'number') {
|
||||
this.setAngle(angle);
|
||||
} else if (typeof angle === 'object') {
|
||||
|
||||
@ -25,6 +25,7 @@
|
||||
import common from '../util/common';
|
||||
import Vec2 from './Vec2';
|
||||
import Rot from './Rot';
|
||||
import factoryConstructor from '../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
|
||||
@ -36,6 +37,7 @@ const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
* position and orientation of rigid frames. Initialize using a position vector
|
||||
* and a rotation.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class Transform {
|
||||
/** position */
|
||||
p: Vec2;
|
||||
@ -44,9 +46,6 @@ export default class Transform {
|
||||
q: Rot;
|
||||
|
||||
constructor(position?: Vec2, rotation?: number) {
|
||||
if (!(this instanceof Transform)) {
|
||||
return new Transform(position, rotation);
|
||||
}
|
||||
this.p = Vec2.zero();
|
||||
this.q = Rot.identity();
|
||||
if (typeof position !== 'undefined') {
|
||||
|
||||
@ -24,12 +24,14 @@
|
||||
|
||||
import common from '../util/common';
|
||||
import Math from './Math';
|
||||
import factoryConstructor from '../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
|
||||
|
||||
@factoryConstructor
|
||||
export default class Vec2 {
|
||||
x: number;
|
||||
y: number;
|
||||
@ -39,9 +41,6 @@ export default class Vec2 {
|
||||
constructor();
|
||||
// tslint:disable-next-line:typedef
|
||||
constructor(x?, y?) {
|
||||
if (!(this instanceof Vec2)) {
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
if (typeof x === 'undefined') {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
|
||||
@ -24,12 +24,14 @@
|
||||
|
||||
import common from '../util/common';
|
||||
import Math from './Math';
|
||||
import factoryConstructor from '../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
|
||||
|
||||
@factoryConstructor
|
||||
export default class Vec3 {
|
||||
x: number;
|
||||
y: number;
|
||||
@ -40,9 +42,6 @@ export default class Vec3 {
|
||||
constructor();
|
||||
// tslint:disable-next-line:typedef
|
||||
constructor(x?, y?, z?) {
|
||||
if (!(this instanceof Vec3)) {
|
||||
return new Vec3(x, y, z);
|
||||
}
|
||||
if (typeof x === 'undefined') {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
|
||||
23
src/decorators/factoryConstructor.ts
Normal file
23
src/decorators/factoryConstructor.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Adds a factory constructor by transforming a class
|
||||
* ```
|
||||
* class A {
|
||||
* constructor(param: number) {
|
||||
* // ...
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* into
|
||||
* ```
|
||||
* class A {
|
||||
* constructor(param: number) {
|
||||
* if(!(this instaceof A)) {
|
||||
* return new A(param);
|
||||
* }
|
||||
* // ...
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* **Note**: Only supported when `target` is ES5 or lower.
|
||||
*/
|
||||
export default function factoryConstructor(target: Function) { }
|
||||
@ -33,6 +33,7 @@ import Contact from './Contact';
|
||||
import AABB, { RayCastInput, RayCastOutput } from "../collision/AABB";
|
||||
import Fixture, { FixtureProxy } from "./Fixture";
|
||||
import Manifold from "../collision/Manifold";
|
||||
import factoryConstructor from '../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -92,6 +93,7 @@ export type WorldRayCastCallback = (fixture: Fixture, point: Vec2, normal: Vec2,
|
||||
*/
|
||||
export type WorldAABBQueryCallback = (fixture: Fixture) => boolean;
|
||||
|
||||
@factoryConstructor
|
||||
export default class World {
|
||||
/** @internal */ m_solver: Solver;
|
||||
/** @internal */ m_broadPhase: BroadPhase;
|
||||
@ -124,10 +126,6 @@ export default class World {
|
||||
* @param def World definition or gravity vector.
|
||||
*/
|
||||
constructor(def?: WorldDef | Vec2 | null) {
|
||||
if (!(this instanceof World)) {
|
||||
return new World(def);
|
||||
}
|
||||
|
||||
if (def && Vec2.isValid(def)) {
|
||||
def = { gravity: def as Vec2 };
|
||||
}
|
||||
|
||||
@ -30,6 +30,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
/**
|
||||
* Distance joint definition. This requires defining an anchor point on both
|
||||
@ -82,6 +83,7 @@ const DEFAULTS = {
|
||||
* @param anchorA Anchor A in global coordination.
|
||||
* @param anchorB Anchor B in global coordination.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class DistanceJoint extends Joint {
|
||||
static TYPE = 'distance-joint' as const;
|
||||
|
||||
@ -110,16 +112,11 @@ export default class DistanceJoint extends Joint {
|
||||
constructor(def: DistanceJointDef);
|
||||
constructor(def: DistanceJointOpt, bodyA: Body, bodyB: Body, anchorA: Vec2, anchorB: Vec2);
|
||||
constructor(def: DistanceJointDef, bodyA?: Body, bodyB?: Body, anchorA?: Vec2, anchorB?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof DistanceJoint)) {
|
||||
return new DistanceJoint(def, bodyA, bodyB, anchorA, anchorB);
|
||||
}
|
||||
|
||||
// order of constructor arguments is changed in v0.2
|
||||
if (bodyB && anchorA && ('m_type' in anchorA) && ('x' in bodyB) && ('y' in bodyB)) {
|
||||
const temp = bodyB;
|
||||
bodyB = anchorA;
|
||||
anchorA = temp;
|
||||
bodyB = anchorA as any;
|
||||
anchorA = temp as any;
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
|
||||
@ -31,6 +31,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -74,6 +75,7 @@ const DEFAULTS = {
|
||||
*
|
||||
* @param anchor Anchor in global coordination.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class FrictionJoint extends Joint {
|
||||
static TYPE = 'friction-joint' as const;
|
||||
|
||||
@ -103,11 +105,6 @@ export default class FrictionJoint extends Joint {
|
||||
constructor(def: FrictionJointDef);
|
||||
constructor(def: FrictionJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
|
||||
constructor(def: FrictionJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof FrictionJoint)) {
|
||||
return new FrictionJoint(def, bodyA, bodyB, anchor);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -33,6 +33,7 @@ import Body from '../Body';
|
||||
import RevoluteJoint from './RevoluteJoint';
|
||||
import PrismaticJoint from './PrismaticJoint';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -78,6 +79,7 @@ const DEFAULTS = {
|
||||
* This definition requires two existing revolute or prismatic joints (any
|
||||
* combination will work).
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class GearJoint extends Joint {
|
||||
static TYPE = 'gear-joint' as const;
|
||||
|
||||
@ -124,11 +126,6 @@ export default class GearJoint extends Joint {
|
||||
constructor(def: GearJointDef);
|
||||
constructor(def: GearJointOpt, bodyA: Body, bodyB: Body, joint1: RevoluteJoint | PrismaticJoint, joint2: RevoluteJoint | PrismaticJoint, ratio?: number);
|
||||
constructor(def: GearJointDef, bodyA?: Body, bodyB?: Body, joint1?: RevoluteJoint | PrismaticJoint, joint2?: RevoluteJoint | PrismaticJoint, ratio?: number) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof GearJoint)) {
|
||||
return new GearJoint(def, bodyA, bodyB, joint1, joint2, ratio);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -31,6 +31,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -78,6 +79,7 @@ const DEFAULTS = {
|
||||
* typical usage is to control the movement of a dynamic body with respect to
|
||||
* the ground.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class MotorJoint extends Joint {
|
||||
static TYPE = 'motor-joint' as const;
|
||||
|
||||
@ -107,11 +109,6 @@ export default class MotorJoint extends Joint {
|
||||
constructor(def: MotorJointDef);
|
||||
constructor(def: MotorJointOpt, bodyA: Body, bodyB: Body);
|
||||
constructor(def: MotorJointDef | MotorJointOpt, bodyA?: Body, bodyB?: Body) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof MotorJoint)) {
|
||||
return new MotorJoint(def, bodyA, bodyB);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -32,6 +32,7 @@ import Transform from '../../common/Transform';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -85,6 +86,7 @@ const DEFAULTS = {
|
||||
* be used in the testbed. If you want to learn how to use the mouse joint, look
|
||||
* at the testbed.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class MouseJoint extends Joint {
|
||||
static TYPE = 'mouse-joint' as const;
|
||||
|
||||
@ -108,11 +110,6 @@ export default class MouseJoint extends Joint {
|
||||
constructor(def: MouseJointDef);
|
||||
constructor(def: MouseJointOpt, bodyA: Body, bodyB: Body, target: Vec2);
|
||||
constructor(def: MouseJointDef, bodyA?: Body, bodyB?: Body, target?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof MouseJoint)) {
|
||||
return new MouseJoint(def, bodyA, bodyB, target);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -34,6 +34,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -121,6 +122,7 @@ const DEFAULTS = {
|
||||
* joint limit to restrict the range of motion and a joint motor to drive the
|
||||
* motion or to model joint friction.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class PrismaticJoint extends Joint {
|
||||
static TYPE = 'prismatic-joint' as const;
|
||||
|
||||
@ -158,11 +160,6 @@ export default class PrismaticJoint extends Joint {
|
||||
constructor(def: PrismaticJointDef);
|
||||
constructor(def: PrismaticJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2, axis: Vec2);
|
||||
constructor(def: PrismaticJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2, axis?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof PrismaticJoint)) {
|
||||
return new PrismaticJoint(def, bodyA, bodyB, anchor, axis);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -31,6 +31,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -93,6 +94,7 @@ const DEFAULTS = {
|
||||
* anchor points with static shapes to prevent one side from going to zero
|
||||
* length.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class PulleyJoint extends Joint {
|
||||
static TYPE = 'pulley-joint' as const;
|
||||
// static MIN_PULLEY_LENGTH: number = 2.0; // TODO where this is used?
|
||||
@ -124,11 +126,6 @@ export default class PulleyJoint extends Joint {
|
||||
constructor(def: PulleyJointDef);
|
||||
constructor(def: PulleyJointOpt, bodyA: Body, bodyB: Body, groundA: Vec2, groundB: Vec2, anchorA: Vec2, anchorB: Vec2, ratio: number);
|
||||
constructor(def: PulleyJointDef, bodyA?: Body, bodyB?: Body, groundA?: Vec2, groundB?: Vec2, anchorA?: Vec2, anchorB?: Vec2, ratio?: number) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof PulleyJoint)) {
|
||||
return new PulleyJoint(def, bodyA, bodyB, groundA, groundB, anchorA, anchorB, ratio);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -34,6 +34,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
||||
@ -127,6 +128,7 @@ const DEFAULTS = {
|
||||
* relative rotation about the shared point. A maximum motor torque is provided
|
||||
* so that infinite forces are not generated.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class RevoluteJoint extends Joint {
|
||||
static TYPE = 'revolute-joint' as const;
|
||||
|
||||
@ -162,11 +164,6 @@ export default class RevoluteJoint extends Joint {
|
||||
constructor(def: RevoluteJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
|
||||
// @ts-ignore
|
||||
constructor(def: RevoluteJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof RevoluteJoint)) {
|
||||
return new RevoluteJoint(def, bodyA, bodyB, anchor);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -30,6 +30,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
const inactiveLimit = 0;
|
||||
const atLowerLimit = 1;
|
||||
@ -79,6 +80,7 @@ const DEFAULTS = {
|
||||
* sponginess, so I chose not to implement it that way. See {@link DistanceJoint} if you
|
||||
* want to dynamically control length.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class RopeJoint extends Joint {
|
||||
static TYPE = 'rope-joint' as const;
|
||||
|
||||
@ -107,11 +109,6 @@ export default class RopeJoint extends Joint {
|
||||
constructor(def: RopeJointDef);
|
||||
constructor(def: RopeJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
|
||||
constructor(def: RopeJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof RopeJoint)) {
|
||||
return new RopeJoint(def, bodyA, bodyB, anchor);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -32,6 +32,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
/**
|
||||
@ -86,6 +87,7 @@ const DEFAULTS = {
|
||||
* A weld joint essentially glues two bodies together. A weld joint may distort
|
||||
* somewhat because the island constraint solver is approximate.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class WeldJoint extends Joint {
|
||||
static TYPE = 'weld-joint' as const
|
||||
|
||||
@ -116,11 +118,6 @@ export default class WeldJoint extends Joint {
|
||||
constructor(def: WeldJointDef);
|
||||
constructor(def: WeldJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
|
||||
constructor(def: WeldJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof WeldJoint)) {
|
||||
return new WeldJoint(def, bodyA, bodyB, anchor);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -30,6 +30,7 @@ import Rot from '../../common/Rot';
|
||||
import Joint, { JointOpt, JointDef } from '../Joint';
|
||||
import Body from '../Body';
|
||||
import { TimeStep } from "../Solver";
|
||||
import factoryConstructor from '../../decorators/factoryConstructor';
|
||||
|
||||
|
||||
/**
|
||||
@ -99,6 +100,7 @@ const DEFAULTS = {
|
||||
* point to line constraint with a rotational motor and a linear spring/damper.
|
||||
* This joint is designed for vehicle suspensions.
|
||||
*/
|
||||
@factoryConstructor
|
||||
export default class WheelJoint extends Joint {
|
||||
static TYPE = 'wheel-joint' as const;
|
||||
|
||||
@ -144,11 +146,6 @@ export default class WheelJoint extends Joint {
|
||||
constructor(def: WheelJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2, axis: Vec2);
|
||||
// @ts-ignore
|
||||
constructor(def: WheelJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2, axis?: Vec2) {
|
||||
// @ts-ignore
|
||||
if (!(this instanceof WheelJoint)) {
|
||||
return new WheelJoint(def, bodyA, bodyB, anchor, axis);
|
||||
}
|
||||
|
||||
def = options(def, DEFAULTS);
|
||||
super(def, bodyA, bodyB);
|
||||
bodyA = this.m_bodyA;
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
// "noImplicitAny": true,
|
||||
"lib": ["ES2015", "DOM"],
|
||||
"stripInternal": true,
|
||||
"target": "ES5"
|
||||
"target": "ES5",
|
||||
"experimentalDecorators": true
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user