@safe-engine/cocos 1.5.2 → 1.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/animation/AnimationComponent.d.ts +63 -0
- package/dist/animation/AnimationComponent.d.ts.map +1 -0
- package/dist/animation/AnimationComponent.js +119 -0
- package/dist/animation/AnimationSystem.d.ts +6 -0
- package/dist/animation/AnimationSystem.d.ts.map +1 -0
- package/dist/animation/AnimationSystem.js +22 -0
- package/dist/animation/index.d.ts +2 -0
- package/dist/animation/index.d.ts.map +1 -0
- package/dist/animation/index.js +7 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +40 -65
- package/dist/box2d-wasm/ContactListener.d.ts +12 -0
- package/dist/box2d-wasm/ContactListener.d.ts.map +1 -0
- package/dist/box2d-wasm/ContactListener.js +64 -0
- package/dist/box2d-wasm/PhysicsComponent.d.ts +50 -0
- package/dist/box2d-wasm/PhysicsComponent.d.ts.map +1 -0
- package/dist/box2d-wasm/PhysicsComponent.js +16 -0
- package/dist/box2d-wasm/PhysicsSprite.d.ts +12 -0
- package/dist/box2d-wasm/PhysicsSprite.d.ts.map +1 -0
- package/dist/box2d-wasm/PhysicsSprite.js +51 -0
- package/dist/box2d-wasm/PhysicsSystem.d.ts +15 -0
- package/dist/box2d-wasm/PhysicsSystem.d.ts.map +1 -0
- package/dist/box2d-wasm/PhysicsSystem.js +139 -0
- package/dist/box2d-wasm/debugDraw.d.ts +66 -0
- package/dist/box2d-wasm/debugDraw.d.ts.map +1 -0
- package/dist/box2d-wasm/debugDraw.js +210 -0
- package/dist/box2d-wasm/index.d.ts +6 -0
- package/dist/box2d-wasm/index.d.ts.map +1 -0
- package/dist/box2d-wasm/index.js +10 -0
- package/dist/collider/CollideComponent.js +98 -159
- package/dist/collider/CollideSystem.js +63 -79
- package/dist/collider/index.d.ts +1 -1
- package/dist/collider/index.d.ts.map +1 -1
- package/dist/collider/index.js +11 -9
- package/dist/dragonbones/PixiDragonBonesSprite.js +15 -19
- package/dist/dragonbones/index.js +38 -68
- package/dist/gworld/components/AnimationComponent.d.ts +2 -2
- package/dist/gworld/components/CollideComponent.d.ts +3 -3
- package/dist/gworld/components/EnhancedComponent.js +29 -36
- package/dist/gworld/components/GUIComponent.js +50 -118
- package/dist/gworld/components/NoRenderComponent.js +25 -65
- package/dist/gworld/components/NodeComp.d.ts.map +1 -1
- package/dist/gworld/components/NodeComp.js +237 -317
- package/dist/gworld/components/RenderComponent.js +46 -104
- package/dist/gworld/core/NodePool.js +13 -20
- package/dist/gworld/core/Scene.js +11 -35
- package/dist/gworld/core/decorator.js +12 -42
- package/dist/gworld/index.js +13 -42
- package/dist/gworld/systems/CollideSystem.d.ts +1 -2
- package/dist/gworld/systems/CollideSystem.d.ts.map +1 -1
- package/dist/gworld/systems/CollideSystem.js +2 -12
- package/dist/gworld/systems/GUISystem.js +105 -120
- package/dist/gworld/systems/NoRenderSystem.d.ts.map +1 -1
- package/dist/gworld/systems/NoRenderSystem.js +70 -80
- package/dist/gworld/systems/RenderSystem.js +64 -78
- package/dist/helper/Intersection.js +33 -40
- package/dist/helper/action.js +8 -21
- package/dist/helper/director.js +2 -6
- package/dist/helper/math.js +9 -15
- package/dist/helper/utils.js +28 -37
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -31
- package/dist/polyfills.js +69 -97
- package/dist/richtext/RichTextComp.js +27 -55
- package/dist/richtext/RichTextSystem.js +17 -25
- package/dist/richtext/html-text-parser.js +36 -40
- package/dist/richtext/index.js +5 -8
- package/dist/safex.js +1 -2
- package/dist/spine/CCSkeleton.d.ts.map +1 -1
- package/dist/spine/CCSkeleton.js +25 -61
- package/dist/spine/CCSkeletonAnimation.js +18 -54
- package/dist/spine/CCSkeletonCanvasRenderCmd.d.ts +1 -1
- package/dist/spine/CCSkeletonCanvasRenderCmd.d.ts.map +1 -1
- package/dist/spine/CCSkeletonCanvasRenderCmd.js +39 -43
- package/dist/spine/CCSkeletonTexture.js +4 -40
- package/dist/spine/CCSkeletonWebGLRenderCmd.js +56 -92
- package/dist/spine/index.js +37 -63
- package/package.json +4 -1
- package/dist/dragonbones/cocos/CocosArmatureDisplay.d.ts +0 -77
- package/dist/dragonbones/cocos/CocosArmatureDisplay.d.ts.map +0 -1
- package/dist/dragonbones/cocos/CocosArmatureDisplay.js +0 -227
- package/dist/dragonbones/cocos/CocosFactory.d.ts +0 -128
- package/dist/dragonbones/cocos/CocosFactory.d.ts.map +0 -1
- package/dist/dragonbones/cocos/CocosFactory.js +0 -231
- package/dist/dragonbones/cocos/CocosSlot.d.ts +0 -49
- package/dist/dragonbones/cocos/CocosSlot.d.ts.map +0 -1
- package/dist/dragonbones/cocos/CocosSlot.js +0 -385
- package/dist/dragonbones/cocos/CocosTextureAtlasData.d.ts +0 -53
- package/dist/dragonbones/cocos/CocosTextureAtlasData.d.ts.map +0 -1
- package/dist/dragonbones/cocos/CocosTextureAtlasData.js +0 -158
- package/dist/dragonbones/dragonBones.d.ts +0 -3
- package/dist/dragonbones/dragonBones.d.ts.map +0 -1
- package/dist/dragonbones/dragonBones.js +0 -4788
- package/dist/helper/NodePool.d.ts +0 -9
- package/dist/helper/NodePool.d.ts.map +0 -1
- package/dist/helper/NodePool.js +0 -29
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import Box2DFactory from 'box2d-wasm';
|
|
2
|
+
import { EventTypes } from 'entityx-ts';
|
|
3
|
+
import { GameWorld, instantiate, NodeComp } from '..';
|
|
4
|
+
import { makeContactListener } from './ContactListener';
|
|
5
|
+
import { makeDebugDraw } from './debugDraw';
|
|
6
|
+
import { BoxColliderPhysics, CircleColliderPhysics, ColliderPhysics, PhysicsMaterial, PolygonColliderPhysics, RigidBody, } from './PhysicsComponent';
|
|
7
|
+
import { PhysicsSprite } from './PhysicsSprite';
|
|
8
|
+
export let box2D;
|
|
9
|
+
export function initBox2d(cb) {
|
|
10
|
+
Box2DFactory().then((b2) => {
|
|
11
|
+
box2D = b2;
|
|
12
|
+
cb();
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
// Box2D.b2Fixture.prototype.shouldCollide = function (other) {
|
|
16
|
+
// const nodeThis: NodeComp = this.getBody().getUserData()
|
|
17
|
+
// const nodeOther = other.getBody().getUserData() as NodeComp
|
|
18
|
+
// const { colliderMatrix } = GameWorld.Instance.systems.get(PhysicsSystem)
|
|
19
|
+
// return colliderMatrix[nodeOther.group][nodeThis.group]
|
|
20
|
+
// }
|
|
21
|
+
export function setColliderMatrix(colliderMatrix = [[true]]) {
|
|
22
|
+
const physicsSystem = GameWorld.Instance.systems.get(PhysicsSystem);
|
|
23
|
+
physicsSystem.colliderMatrix = colliderMatrix;
|
|
24
|
+
}
|
|
25
|
+
const maxTimeStep = 1 / 60;
|
|
26
|
+
const velocityIterations = 1;
|
|
27
|
+
const positionIterations = 1;
|
|
28
|
+
const metadata = {};
|
|
29
|
+
const pixelsPerMeter = 1;
|
|
30
|
+
export class PhysicsSystem {
|
|
31
|
+
world;
|
|
32
|
+
listRemoveBody = [];
|
|
33
|
+
listRemoveShape = [];
|
|
34
|
+
colliderMatrix = [[true]];
|
|
35
|
+
graphics;
|
|
36
|
+
configure(event_manager) {
|
|
37
|
+
const { b2BodyDef, b2_dynamicBody, b2_staticBody, b2FixtureDef, b2PolygonShape, b2Vec2, b2World, getPointer, b2ContactListener } = box2D;
|
|
38
|
+
const gravity = new b2Vec2(0, -10);
|
|
39
|
+
this.world = new b2World(gravity);
|
|
40
|
+
console.log('configure PhysicsSystem world', this.world);
|
|
41
|
+
// event_manager.world.physicsManager = this
|
|
42
|
+
const graphics = new cc.DrawNode();
|
|
43
|
+
this.graphics = graphics;
|
|
44
|
+
graphics.zIndex = 1000;
|
|
45
|
+
const scene = cc.director.getRunningScene();
|
|
46
|
+
scene.addChild(graphics);
|
|
47
|
+
const debugDraw = makeDebugDraw(graphics, pixelsPerMeter, box2D);
|
|
48
|
+
this.world.SetDebugDraw(debugDraw);
|
|
49
|
+
// event_manager.subscribe(ComponentAddedEvent(RigidBody), this);
|
|
50
|
+
event_manager.subscribe(EventTypes.ComponentAdded, BoxColliderPhysics, ({ entity, component }) => {
|
|
51
|
+
console.log('ComponentAddedEvent BoxColliderPhysics', component);
|
|
52
|
+
let rigidBody = entity.getComponent(RigidBody);
|
|
53
|
+
if (!rigidBody) {
|
|
54
|
+
rigidBody = instantiate(RigidBody);
|
|
55
|
+
entity.assign(rigidBody);
|
|
56
|
+
}
|
|
57
|
+
const { type = 'static', gravityScale = 1 } = rigidBody.props;
|
|
58
|
+
const physicsMaterial = entity.getComponent(PhysicsMaterial);
|
|
59
|
+
const { density = 1, friction = 0.5, restitution = 0.3 } = physicsMaterial?.props || {};
|
|
60
|
+
const box = component;
|
|
61
|
+
const node = entity.getComponent(NodeComp);
|
|
62
|
+
const { width, height, ...colliderProps } = box.props;
|
|
63
|
+
// ett.assign(instantiate(ColliderPhysics, { tag, offset }))
|
|
64
|
+
const { x = 0, y = 0 } = colliderProps.offset || {};
|
|
65
|
+
const zero = new b2Vec2(0, 0);
|
|
66
|
+
const position = new b2Vec2(node.posX, node.posY);
|
|
67
|
+
const offset = new b2Vec2(x, y);
|
|
68
|
+
const bd = new b2BodyDef();
|
|
69
|
+
bd.set_type(type === 'dynamic' ? b2_dynamicBody : b2_staticBody);
|
|
70
|
+
bd.set_position(zero);
|
|
71
|
+
bd.set_gravityScale(gravityScale);
|
|
72
|
+
const body = this.world.CreateBody(bd);
|
|
73
|
+
rigidBody.body = body;
|
|
74
|
+
// console.log('body', type, b2_dynamicBody, b2_staticBody, getPointer(body));
|
|
75
|
+
// body.setMassData({ mass: 1 } as any)
|
|
76
|
+
const physicsNode = new PhysicsSprite(node.instance, body);
|
|
77
|
+
const square = new b2PolygonShape();
|
|
78
|
+
square.SetAsBox(width / 2, height / 2);
|
|
79
|
+
const fixtureDef = new b2FixtureDef();
|
|
80
|
+
fixtureDef.set_shape(square);
|
|
81
|
+
fixtureDef.set_density(density);
|
|
82
|
+
fixtureDef.set_friction(friction);
|
|
83
|
+
fixtureDef.set_restitution(restitution);
|
|
84
|
+
body.CreateFixture(fixtureDef);
|
|
85
|
+
body.SetTransform(position, 0);
|
|
86
|
+
body.SetLinearVelocity(zero);
|
|
87
|
+
body.SetAwake(true);
|
|
88
|
+
body.SetEnabled(true);
|
|
89
|
+
metadata[getPointer(body)] = node;
|
|
90
|
+
const physicsCollide = entity.assign(instantiate(ColliderPhysics, colliderProps));
|
|
91
|
+
physicsCollide.instance = physicsNode;
|
|
92
|
+
physicsCollide.node = node;
|
|
93
|
+
box.node = node;
|
|
94
|
+
});
|
|
95
|
+
event_manager.subscribe(EventTypes.ComponentAdded, CircleColliderPhysics, () => { });
|
|
96
|
+
event_manager.subscribe(EventTypes.ComponentAdded, PolygonColliderPhysics, () => { });
|
|
97
|
+
event_manager.subscribe(EventTypes.ComponentRemoved, NodeComp, ({ entity }) => {
|
|
98
|
+
// log('ComponentRemovedEvent NodeComp', event);
|
|
99
|
+
const node = entity.getComponent(NodeComp);
|
|
100
|
+
if (node.instance instanceof PhysicsSprite) {
|
|
101
|
+
const body = node.instance.getBody();
|
|
102
|
+
// this.listRemoveShape.push(...body.shapeList)
|
|
103
|
+
this.listRemoveBody.push(body);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
const listener = makeContactListener(this.world, metadata, box2D);
|
|
107
|
+
this.world.SetContactListener(listener);
|
|
108
|
+
}
|
|
109
|
+
update(entities, events, dt) {
|
|
110
|
+
if (this.world) {
|
|
111
|
+
const { getPointer } = box2D;
|
|
112
|
+
// remove bodies and shapes
|
|
113
|
+
this.listRemoveBody.forEach((body) => {
|
|
114
|
+
if (body) {
|
|
115
|
+
this.world.DestroyBody(getPointer(body));
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
// this.listRemoveShape.forEach((shape) => {
|
|
119
|
+
// if (shape) {
|
|
120
|
+
// this.world.DestroyShape(shape)
|
|
121
|
+
// }
|
|
122
|
+
// })
|
|
123
|
+
this.listRemoveBody = [];
|
|
124
|
+
this.listRemoveShape = [];
|
|
125
|
+
const clampedDelta = Math.min(dt, maxTimeStep);
|
|
126
|
+
this.world.Step(clampedDelta, velocityIterations, positionIterations);
|
|
127
|
+
this.graphics.clear();
|
|
128
|
+
this.world.DebugDraw();
|
|
129
|
+
// console.log('GetBodyCount', this.world.GetBodyCount())
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
set enabled(val) {
|
|
133
|
+
if (val) {
|
|
134
|
+
this.world.SetGravity(new Box2D.b2Vec2(0, -9.8));
|
|
135
|
+
// this.world.iterations = 60
|
|
136
|
+
// this.world.collisionSlop = 0.5
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forked from Box2D.js
|
|
3
|
+
* @see https://github.com/kripken/box2d.js/blob/f75077b/helpers/embox2d-helpers.js
|
|
4
|
+
* @author dmagunov + Huy Nguyen + fork contributions from Alex Birch
|
|
5
|
+
* @see https://github.com/kripken/box2d.js/blob/49dddd6/helpers/embox2d-html5canvas-debugDraw.js
|
|
6
|
+
* @author dmagunov + fork contributions from Alex Birch
|
|
7
|
+
* @license Zlib https://opensource.org/licenses/Zlib
|
|
8
|
+
* License evidence: https://github.com/kripken/box2d.js/blob/master/README.markdown#box2djs
|
|
9
|
+
* "box2d.js is zlib licensed, just like Box2D."
|
|
10
|
+
*
|
|
11
|
+
* @typedef {import('box2d-wasm')} Box2DFactory
|
|
12
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
13
|
+
* @param {number} pixelsPerMeter
|
|
14
|
+
* @param {typeof Box2D & EmscriptenModule} box2D
|
|
15
|
+
*/
|
|
16
|
+
export declare const makeDebugDraw: (graphics: cc.DrawNode, pixelsPerMeter: any, box2D: typeof Box2D) => Box2D.JSDraw & {
|
|
17
|
+
/**
|
|
18
|
+
* @param {number} vert1_p pointer to {@link Box2D.b2Vec2}
|
|
19
|
+
* @param {number} vert2_p pointer to {@link Box2D.b2Vec2}
|
|
20
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
21
|
+
* @returns {void}
|
|
22
|
+
*/
|
|
23
|
+
DrawSegment(vert1_p: any, vert2_p: any, color_p: any): void;
|
|
24
|
+
/**
|
|
25
|
+
* @param {number} vertices_p pointer to Array<{@link Box2D.b2Vec2}>
|
|
26
|
+
* @param {number} vertexCount
|
|
27
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
28
|
+
* @returns {void}
|
|
29
|
+
*/
|
|
30
|
+
DrawPolygon(vertices_p: any, vertexCount: any, color_p: any): void;
|
|
31
|
+
/**
|
|
32
|
+
* @param {number} vertices_p pointer to Array<{@link Box2D.b2Vec2}>
|
|
33
|
+
* @param {number} vertexCount
|
|
34
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*/
|
|
37
|
+
DrawSolidPolygon(vertices_p: any, vertexCount: any, color_p: any): void;
|
|
38
|
+
/**
|
|
39
|
+
* @param {number} center_p pointer to {@link Box2D.b2Vec2}
|
|
40
|
+
* @param {number} radius
|
|
41
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
42
|
+
* @returns {void}
|
|
43
|
+
*/
|
|
44
|
+
DrawCircle(center_p: any, radius: any, color_p: any): void;
|
|
45
|
+
/**
|
|
46
|
+
* @param {number} center_p pointer to {@link Box2D.b2Vec2}
|
|
47
|
+
* @param {number} radius
|
|
48
|
+
* @param {number} axis_p pointer to {@link Box2D.b2Vec2}
|
|
49
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
50
|
+
* @returns {void}
|
|
51
|
+
*/
|
|
52
|
+
DrawSolidCircle(center_p: any, radius: any, axis_p: any, color_p: any): void;
|
|
53
|
+
/**
|
|
54
|
+
* @param {number} transform_p pointer to {@link Box2D.b2Transform}
|
|
55
|
+
* @returns {void}
|
|
56
|
+
*/
|
|
57
|
+
DrawTransform(transform_p: any): void;
|
|
58
|
+
/**
|
|
59
|
+
* @param {number} vertex_p pointer to {@link Box2D.b2Vec2}
|
|
60
|
+
* @param {number} sizeMetres
|
|
61
|
+
* @param {number} pointer to {@link Box2D.b2Color}
|
|
62
|
+
* @returns {void}
|
|
63
|
+
*/
|
|
64
|
+
DrawPoint(vertex_p: any, sizeMetres: any, color_p: any): void;
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=debugDraw.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debugDraw.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/debugDraw.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,aAAa,GAAI,UAAU,EAAE,CAAC,QAAQ,EAAE,mBAAc,EAAE,OAAO,OAAO,KAAK;IA6HpF;;;;;OAKG;;IAQH;;;;;OAKG;;IAQH;;;;;OAKG;;IAOH;;;;;OAKG;;IAQH;;;;;;OAMG;;IAQH;;;OAGG;;IAKH;;;;;OAKG;;CAUN,CAAA"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { Color4B, Vec2 } from '../polyfills';
|
|
2
|
+
/**
|
|
3
|
+
* Forked from Box2D.js
|
|
4
|
+
* @see https://github.com/kripken/box2d.js/blob/f75077b/helpers/embox2d-helpers.js
|
|
5
|
+
* @author dmagunov + Huy Nguyen + fork contributions from Alex Birch
|
|
6
|
+
* @see https://github.com/kripken/box2d.js/blob/49dddd6/helpers/embox2d-html5canvas-debugDraw.js
|
|
7
|
+
* @author dmagunov + fork contributions from Alex Birch
|
|
8
|
+
* @license Zlib https://opensource.org/licenses/Zlib
|
|
9
|
+
* License evidence: https://github.com/kripken/box2d.js/blob/master/README.markdown#box2djs
|
|
10
|
+
* "box2d.js is zlib licensed, just like Box2D."
|
|
11
|
+
*
|
|
12
|
+
* @typedef {import('box2d-wasm')} Box2DFactory
|
|
13
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
14
|
+
* @param {number} pixelsPerMeter
|
|
15
|
+
* @param {typeof Box2D & EmscriptenModule} box2D
|
|
16
|
+
*/
|
|
17
|
+
export const makeDebugDraw = (graphics, pixelsPerMeter, box2D) => {
|
|
18
|
+
const { b2Color, b2Draw: { e_shapeBit }, b2Transform, b2Vec2, JSDraw, wrapPointer, } = box2D;
|
|
19
|
+
/**
|
|
20
|
+
* to replace original C++ operator =
|
|
21
|
+
* @param {Box2D.b2Vec2} vec
|
|
22
|
+
* @returns {Box2D.b2Vec2}
|
|
23
|
+
*/
|
|
24
|
+
// const copyVec2 = (vec: Box2D.b2Vec2) => new b2Vec2(vec.get_x(), vec.get_y())
|
|
25
|
+
/**
|
|
26
|
+
* to replace original C++ operator *= (float)
|
|
27
|
+
* @param {Box2D.b2Vec2} vec
|
|
28
|
+
* @param {number} scale
|
|
29
|
+
* @returns {Box2D.b2Vec2}
|
|
30
|
+
*/
|
|
31
|
+
// const scaledVec2 = (vec: Box2D.b2Vec2, scale: number) => new b2Vec2(scale * vec.get_x(), scale * vec.get_y())
|
|
32
|
+
/**
|
|
33
|
+
* @param {Box2D.b2Color} color
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
const getRgbStr = (color) => {
|
|
37
|
+
const red = (color.get_r() * 255) | 0;
|
|
38
|
+
const green = (color.get_g() * 255) | 0;
|
|
39
|
+
const blue = (color.get_b() * 255) | 0;
|
|
40
|
+
return Color4B(red, green, blue, 100);
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* @param {string} rgbStr
|
|
44
|
+
* @returns {void}
|
|
45
|
+
*/
|
|
46
|
+
const setCtxColor = (rgbStr) => {
|
|
47
|
+
graphics.color = rgbStr;
|
|
48
|
+
// graphics.strokeStyle = { color: rgbStr }
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* @param {Box2D.b2Vec2[]} vertices
|
|
52
|
+
* @param {boolean} fill
|
|
53
|
+
* @returns {void}
|
|
54
|
+
*/
|
|
55
|
+
const drawPolygon = (vertices, fill) => {
|
|
56
|
+
// console.log("drawPolygon", vertices[0].y, fill);
|
|
57
|
+
graphics.drawPoly(vertices, Color4B(255, 0, 0, 50), fill ? 1 : 0);
|
|
58
|
+
// graphics.drawPoly(vertices[vertices.length - 1].x * pixelsPerMeter, vertices[vertices.length - 1].y * pixelsPerMeter)
|
|
59
|
+
// vertices.forEach((v) => {
|
|
60
|
+
// graphics.lineTo(v.x * pixelsPerMeter, v.y * pixelsPerMeter)
|
|
61
|
+
// })
|
|
62
|
+
// graphics.fill()
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* @param {Box2D.b2Vec2} center
|
|
66
|
+
* @param {number} radius
|
|
67
|
+
* @param {Box2D.b2Vec2} axis
|
|
68
|
+
* @param {boolean} fill
|
|
69
|
+
* @returns {void}
|
|
70
|
+
*/
|
|
71
|
+
const drawCircle = (center, radius, axis, fill) => {
|
|
72
|
+
graphics.drawCircle(center, radius, 0, 64, true, 6, fill);
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* @param {Box2D.b2Vec2} vert1
|
|
76
|
+
* @param {Box2D.b2Vec2} vert2
|
|
77
|
+
* @returns {void}
|
|
78
|
+
*/
|
|
79
|
+
const drawSegment = (vert1, vert2) => {
|
|
80
|
+
// console.log("drawSegment", vert1, vert2)
|
|
81
|
+
graphics.drawSegment(Vec2(vert1.get_x(), vert1.get_y()), Vec2(vert2.get_x(), vert2.get_y()));
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* @param {Box2D.b2Vec2} vertex
|
|
85
|
+
* @param {number} sizeMetres
|
|
86
|
+
* @returns {void}
|
|
87
|
+
*/
|
|
88
|
+
const drawPoint = (vertex, sizeMetres) => {
|
|
89
|
+
const sizePixels = sizeMetres / pixelsPerMeter;
|
|
90
|
+
graphics.drawDot(Vec2(vertex.get_x() - sizePixels / 2, vertex.get_y() - sizePixels / 2), sizePixels);
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* @param {Box2D.b2Transform} transform
|
|
94
|
+
* @param {number} sizeMetres
|
|
95
|
+
* @returns {void}
|
|
96
|
+
*/
|
|
97
|
+
const drawTransform = (transform) => {
|
|
98
|
+
const pos = transform.get_p();
|
|
99
|
+
const rot = transform.get_q();
|
|
100
|
+
graphics.drawSegment(Vec2(pos.get_x(), pos.get_y()), Vec2(pos.get_x() + rot.GetXAxis().get_x(), pos.get_y() + rot.GetXAxis().get_y()));
|
|
101
|
+
// graphics.setTransform()
|
|
102
|
+
// graphics.save()
|
|
103
|
+
// graphics.translateTransform(pos.get_x(), pos.get_y())
|
|
104
|
+
// graphics.scaleTransform(0.5, 0.5)
|
|
105
|
+
// graphics.rotateTransform(rot.GetAngle())
|
|
106
|
+
// graphics.lineWidth *= 2;
|
|
107
|
+
// graphics.restore()
|
|
108
|
+
};
|
|
109
|
+
/** {@link Box2D.b2Vec2} is a struct of `float x, y` */
|
|
110
|
+
const sizeOfB2Vec = Float32Array.BYTES_PER_ELEMENT * 2;
|
|
111
|
+
/**
|
|
112
|
+
* @param {number} array_p pointer to {@link Box2D.b2Vec2}
|
|
113
|
+
* @param {number} numElements length of array
|
|
114
|
+
* @param {number} sizeOfElement size of an instance of the array element
|
|
115
|
+
* @param {typeof Box2D.b2Vec2} ctor constructor for the array element
|
|
116
|
+
* @return {Box2D.b2Vec2[]}
|
|
117
|
+
*/
|
|
118
|
+
const reifyArray = (array_p, numElements, sizeOfElement, ctor) => Array(numElements)
|
|
119
|
+
.fill(undefined)
|
|
120
|
+
.map((_, index) => wrapPointer(array_p + index * sizeOfElement, ctor));
|
|
121
|
+
const debugDraw = Object.assign(new JSDraw(), {
|
|
122
|
+
/**
|
|
123
|
+
* @param {number} vert1_p pointer to {@link Box2D.b2Vec2}
|
|
124
|
+
* @param {number} vert2_p pointer to {@link Box2D.b2Vec2}
|
|
125
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
126
|
+
* @returns {void}
|
|
127
|
+
*/
|
|
128
|
+
DrawSegment(vert1_p, vert2_p, color_p) {
|
|
129
|
+
const color = wrapPointer(color_p, b2Color);
|
|
130
|
+
setCtxColor(getRgbStr(color));
|
|
131
|
+
const vert1 = wrapPointer(vert1_p, b2Vec2);
|
|
132
|
+
const vert2 = wrapPointer(vert2_p, b2Vec2);
|
|
133
|
+
drawSegment(vert1, vert2);
|
|
134
|
+
},
|
|
135
|
+
/**
|
|
136
|
+
* @param {number} vertices_p pointer to Array<{@link Box2D.b2Vec2}>
|
|
137
|
+
* @param {number} vertexCount
|
|
138
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
139
|
+
* @returns {void}
|
|
140
|
+
*/
|
|
141
|
+
DrawPolygon(vertices_p, vertexCount, color_p) {
|
|
142
|
+
const color = wrapPointer(color_p, b2Color);
|
|
143
|
+
setCtxColor(getRgbStr(color));
|
|
144
|
+
const vertices = reifyArray(vertices_p, vertexCount, sizeOfB2Vec, b2Vec2);
|
|
145
|
+
drawPolygon(vertices, color);
|
|
146
|
+
// console.log(`DrawPolygon`, getRgbStr(color).green, vertices[0].y)
|
|
147
|
+
},
|
|
148
|
+
/**
|
|
149
|
+
* @param {number} vertices_p pointer to Array<{@link Box2D.b2Vec2}>
|
|
150
|
+
* @param {number} vertexCount
|
|
151
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
152
|
+
* @returns {void}
|
|
153
|
+
*/
|
|
154
|
+
DrawSolidPolygon(vertices_p, vertexCount, color_p) {
|
|
155
|
+
const color = wrapPointer(color_p, b2Color);
|
|
156
|
+
setCtxColor(getRgbStr(color));
|
|
157
|
+
const vertices = reifyArray(vertices_p, vertexCount, sizeOfB2Vec, b2Vec2);
|
|
158
|
+
drawPolygon(vertices, true);
|
|
159
|
+
},
|
|
160
|
+
/**
|
|
161
|
+
* @param {number} center_p pointer to {@link Box2D.b2Vec2}
|
|
162
|
+
* @param {number} radius
|
|
163
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
164
|
+
* @returns {void}
|
|
165
|
+
*/
|
|
166
|
+
DrawCircle(center_p, radius, color_p) {
|
|
167
|
+
const color = wrapPointer(color_p, b2Color);
|
|
168
|
+
setCtxColor(getRgbStr(color));
|
|
169
|
+
const center = wrapPointer(center_p, b2Vec2);
|
|
170
|
+
const dummyAxis = new b2Vec2(0, 0);
|
|
171
|
+
drawCircle(center, radius, dummyAxis, false);
|
|
172
|
+
},
|
|
173
|
+
/**
|
|
174
|
+
* @param {number} center_p pointer to {@link Box2D.b2Vec2}
|
|
175
|
+
* @param {number} radius
|
|
176
|
+
* @param {number} axis_p pointer to {@link Box2D.b2Vec2}
|
|
177
|
+
* @param {number} color_p pointer to {@link Box2D.b2Color}
|
|
178
|
+
* @returns {void}
|
|
179
|
+
*/
|
|
180
|
+
DrawSolidCircle(center_p, radius, axis_p, color_p) {
|
|
181
|
+
const color = wrapPointer(color_p, b2Color);
|
|
182
|
+
setCtxColor(getRgbStr(color));
|
|
183
|
+
const center = wrapPointer(center_p, b2Vec2);
|
|
184
|
+
const axis = wrapPointer(axis_p, b2Vec2);
|
|
185
|
+
drawCircle(center, radius, axis, true);
|
|
186
|
+
},
|
|
187
|
+
/**
|
|
188
|
+
* @param {number} transform_p pointer to {@link Box2D.b2Transform}
|
|
189
|
+
* @returns {void}
|
|
190
|
+
*/
|
|
191
|
+
DrawTransform(transform_p) {
|
|
192
|
+
const transform = wrapPointer(transform_p, b2Transform);
|
|
193
|
+
drawTransform(transform);
|
|
194
|
+
},
|
|
195
|
+
/**
|
|
196
|
+
* @param {number} vertex_p pointer to {@link Box2D.b2Vec2}
|
|
197
|
+
* @param {number} sizeMetres
|
|
198
|
+
* @param {number} pointer to {@link Box2D.b2Color}
|
|
199
|
+
* @returns {void}
|
|
200
|
+
*/
|
|
201
|
+
DrawPoint(vertex_p, sizeMetres, color_p) {
|
|
202
|
+
const color = wrapPointer(color_p, b2Color);
|
|
203
|
+
setCtxColor(getRgbStr(color));
|
|
204
|
+
const vertex = wrapPointer(vertex_p, b2Vec2);
|
|
205
|
+
drawPoint(vertex, sizeMetres);
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
debugDraw.SetFlags(e_shapeBit);
|
|
209
|
+
return debugDraw;
|
|
210
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAA;AAG9B,cAAc,oBAAoB,CAAA;AAClC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAE/B,wBAAgB,YAAY,CAAC,KAAK,YAAqB,QAItD"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GameWorld } from '..';
|
|
2
|
+
import { PhysicsSystem } from './PhysicsSystem';
|
|
3
|
+
export * from './PhysicsComponent';
|
|
4
|
+
export * from './PhysicsSprite';
|
|
5
|
+
export * from './PhysicsSystem';
|
|
6
|
+
export function setupPhysics(world = GameWorld.Instance) {
|
|
7
|
+
world.systems.add(PhysicsSystem);
|
|
8
|
+
world.systems.configureOnce(PhysicsSystem);
|
|
9
|
+
world.listUpdate.push(PhysicsSystem);
|
|
10
|
+
}
|