@safe-engine/cocos 1.5.5 → 1.5.8
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/box2d-wasm/ContactListener.d.ts.map +1 -1
- package/dist/box2d-wasm/ContactListener.js +49 -17
- package/dist/box2d-wasm/PhysicsComponent.d.ts +15 -21
- package/dist/box2d-wasm/PhysicsComponent.d.ts.map +1 -1
- package/dist/box2d-wasm/PhysicsComponent.js +5 -7
- package/dist/box2d-wasm/PhysicsSprite.js +3 -3
- package/dist/box2d-wasm/PhysicsSystem.d.ts +3 -0
- package/dist/box2d-wasm/PhysicsSystem.d.ts.map +1 -1
- package/dist/box2d-wasm/PhysicsSystem.js +120 -22
- package/dist/gworld/systems/GUISystem.d.ts.map +1 -1
- package/dist/gworld/systems/GUISystem.js +3 -2
- package/dist/gworld/systems/RenderSystem.js +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContactListener.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/ContactListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAA;AAG7B,KAAK,IAAI,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAAA;CAAE,CAAA;AACvC,eAAO,MAAM,mBAAmB,GAAI,OAAO,KAAK,CAAC,OAAO,EAAE,UAAU,IAAI,EAAE,OAAO,OAAO,KAAK;0BAWnE,KAAK,CAAC,SAAS,GAAG,MAAM,GAAG,IAAI;wBAwBjC,KAAK,CAAC,SAAS,GAAG,MAAM,GAAG,IAAI;sBAmBjC,KAAK,CAAC,SAAS,GAAG,MAAM,eAAe,KAAK,CAAC,UAAU,GAAG,MAAM,GAAG,IAAI;
|
|
1
|
+
{"version":3,"file":"ContactListener.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/ContactListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAA;AAG7B,KAAK,IAAI,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAAA;CAAE,CAAA;AACvC,eAAO,MAAM,mBAAmB,GAAI,OAAO,KAAK,CAAC,OAAO,EAAE,UAAU,IAAI,EAAE,OAAO,OAAO,KAAK;0BAWnE,KAAK,CAAC,SAAS,GAAG,MAAM,GAAG,IAAI;wBAwBjC,KAAK,CAAC,SAAS,GAAG,MAAM,GAAG,IAAI;sBAmBjC,KAAK,CAAC,SAAS,GAAG,MAAM,eAAe,KAAK,CAAC,UAAU,GAAG,MAAM,GAAG,IAAI;uBAmBtE,KAAK,CAAC,SAAS,GAAG,MAAM,WAAW,KAAK,CAAC,gBAAgB,GAAG,MAAM,GAAG,IAAI;CAqB/F,CAAA"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PhysicsCollider } from './PhysicsComponent';
|
|
2
2
|
export const makeContactListener = (world, metadata, box2D) => {
|
|
3
3
|
const { JSContactListener, getPointer, NULL } = box2D;
|
|
4
4
|
function getContactById(contact) {
|
|
5
5
|
for (let contactList = world.GetContactList(); getPointer(contactList) !== getPointer(NULL); contactList = contactList.GetNext()) {
|
|
6
6
|
if (getPointer(contactList) === contact) {
|
|
7
|
-
console.log('contactBegin', contact, getPointer(contactList), getPointer(NULL))
|
|
7
|
+
// console.log('contactBegin', contact, getPointer(contactList), getPointer(NULL))
|
|
8
8
|
return contactList;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
@@ -17,20 +17,20 @@ export const makeContactListener = (world, metadata, box2D) => {
|
|
|
17
17
|
const ett1 = metadata[getPointer(contact.GetFixtureA().GetBody())];
|
|
18
18
|
const ett2 = metadata[getPointer(contact.GetFixtureB().GetBody())];
|
|
19
19
|
// world.addPostStepCallback(() => {
|
|
20
|
-
// // log('addPostStepCallback');
|
|
21
20
|
// listRemoveShape.forEach((s) => world.removeShape(s))
|
|
22
21
|
// listRemoveBody.forEach((b) => world.removeBody(b))
|
|
23
22
|
// listRemoveBody = []
|
|
24
23
|
// listRemoveShape = []
|
|
25
24
|
// })
|
|
26
|
-
const phys1 = ett1.getComponent(
|
|
27
|
-
const phys2 = ett2.getComponent(
|
|
25
|
+
const phys1 = ett1.getComponent(PhysicsCollider);
|
|
26
|
+
const phys2 = ett2.getComponent(PhysicsCollider);
|
|
27
|
+
// console.log('addPostStepCallback', ett1, phys1.props.onBeginContact, phys2.props)
|
|
28
28
|
if (phys1 && phys2) {
|
|
29
|
-
if (Object.prototype.hasOwnProperty.call(phys1, '
|
|
30
|
-
phys1.props.
|
|
29
|
+
if (Object.prototype.hasOwnProperty.call(phys1.props, 'onBeginContact')) {
|
|
30
|
+
phys1.props.onBeginContact(phys2);
|
|
31
31
|
}
|
|
32
|
-
if (Object.prototype.hasOwnProperty.call(phys2, '
|
|
33
|
-
phys2.props.
|
|
32
|
+
if (Object.prototype.hasOwnProperty.call(phys2.props, 'onBeginContact')) {
|
|
33
|
+
phys2.props.onBeginContact(phys1);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
},
|
|
@@ -41,23 +41,55 @@ export const makeContactListener = (world, metadata, box2D) => {
|
|
|
41
41
|
const ett1 = metadata[getPointer(contact.GetFixtureA().GetBody())];
|
|
42
42
|
const ett2 = metadata[getPointer(contact.GetFixtureB().GetBody())];
|
|
43
43
|
// const event1 = ett1.getComponent(NodeComp)
|
|
44
|
-
const phys1 = ett1.getComponent(
|
|
45
|
-
const phys2 = ett2.getComponent(
|
|
44
|
+
const phys1 = ett1.getComponent(PhysicsCollider);
|
|
45
|
+
const phys2 = ett2.getComponent(PhysicsCollider);
|
|
46
46
|
// const event2 = ett2.getComponent(NodeComp)
|
|
47
47
|
if (phys1 && phys2) {
|
|
48
|
-
if (Object.prototype.hasOwnProperty.call(phys1, '
|
|
49
|
-
phys1.props.
|
|
48
|
+
if (Object.prototype.hasOwnProperty.call(phys1.props, 'onEndContact')) {
|
|
49
|
+
phys1.props.onEndContact(phys2);
|
|
50
50
|
}
|
|
51
|
-
if (Object.prototype.hasOwnProperty.call(phys2, '
|
|
52
|
-
phys2.props.
|
|
51
|
+
if (Object.prototype.hasOwnProperty.call(phys2.props, 'onEndContact')) {
|
|
52
|
+
phys2.props.onEndContact(phys1);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
},
|
|
56
56
|
PreSolve(contact, oldManifold) {
|
|
57
|
-
|
|
57
|
+
if (typeof contact === 'number') {
|
|
58
|
+
contact = getContactById(contact);
|
|
59
|
+
}
|
|
60
|
+
const ett1 = metadata[getPointer(contact.GetFixtureA().GetBody())];
|
|
61
|
+
const ett2 = metadata[getPointer(contact.GetFixtureB().GetBody())];
|
|
62
|
+
// const event1 = ett1.getComponent(NodeComp)
|
|
63
|
+
const phys1 = ett1.getComponent(PhysicsCollider);
|
|
64
|
+
const phys2 = ett2.getComponent(PhysicsCollider);
|
|
65
|
+
// const event2 = ett2.getComponent(NodeComp)
|
|
66
|
+
if (phys1 && phys2) {
|
|
67
|
+
if (Object.prototype.hasOwnProperty.call(phys1.props, 'onPostSolve')) {
|
|
68
|
+
phys1.props.onPostSolve(phys2, oldManifold);
|
|
69
|
+
}
|
|
70
|
+
if (Object.prototype.hasOwnProperty.call(phys2.props, 'onPostSolve')) {
|
|
71
|
+
phys2.props.onPostSolve(phys1, oldManifold);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
58
74
|
},
|
|
59
75
|
PostSolve(contact, impulse) {
|
|
60
|
-
|
|
76
|
+
if (typeof contact === 'number') {
|
|
77
|
+
contact = getContactById(contact);
|
|
78
|
+
}
|
|
79
|
+
const ett1 = metadata[getPointer(contact.GetFixtureA().GetBody())];
|
|
80
|
+
const ett2 = metadata[getPointer(contact.GetFixtureB().GetBody())];
|
|
81
|
+
// const event1 = ett1.getComponent(NodeComp)
|
|
82
|
+
const phys1 = ett1.getComponent(PhysicsCollider);
|
|
83
|
+
const phys2 = ett2.getComponent(PhysicsCollider);
|
|
84
|
+
// const event2 = ett2.getComponent(NodeComp)
|
|
85
|
+
if (phys1 && phys2) {
|
|
86
|
+
if (Object.prototype.hasOwnProperty.call(phys1.props, 'onPostSolve')) {
|
|
87
|
+
phys1.props.onPostSolve(phys2, impulse);
|
|
88
|
+
}
|
|
89
|
+
if (Object.prototype.hasOwnProperty.call(phys2.props, 'onPostSolve')) {
|
|
90
|
+
phys2.props.onPostSolve(phys1, impulse);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
61
93
|
},
|
|
62
94
|
});
|
|
63
95
|
return listener;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { NoRenderComponentX } from '../gworld/core/decorator';
|
|
2
2
|
import { Vec2 } from '../polyfills';
|
|
3
|
+
import { BaseComponentProps } from '../safex';
|
|
3
4
|
import { PhysicsSprite } from './PhysicsSprite';
|
|
4
5
|
interface RigidBodyProps {
|
|
5
|
-
type?:
|
|
6
|
+
type?: 0 | 1 | 2;
|
|
6
7
|
density?: Float;
|
|
7
8
|
restitution?: Float;
|
|
8
9
|
friction?: Float;
|
|
@@ -11,40 +12,33 @@ interface RigidBodyProps {
|
|
|
11
12
|
export declare class RigidBody extends NoRenderComponentX<RigidBodyProps> {
|
|
12
13
|
body: Box2D.b2Body;
|
|
13
14
|
}
|
|
14
|
-
interface PhysicsMaterialProps {
|
|
15
|
-
friction?: number;
|
|
16
|
-
restitution?: number;
|
|
17
|
-
density?: number;
|
|
18
|
-
}
|
|
19
|
-
export declare class PhysicsMaterial extends NoRenderComponentX<PhysicsMaterialProps> {
|
|
20
|
-
}
|
|
21
15
|
interface ColliderPhysicsProps {
|
|
22
16
|
tag?: number;
|
|
23
|
-
group?: number;
|
|
24
17
|
offset?: Vec2;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
onBeginContact?: (other: PhysicsCollider) => void;
|
|
19
|
+
onEndContact?: (other: PhysicsCollider) => void;
|
|
20
|
+
onPreSolve?: (other: PhysicsCollider, impulse?: any) => void;
|
|
21
|
+
onPostSolve?: (other: PhysicsCollider, oldManifold?: any) => void;
|
|
28
22
|
}
|
|
29
|
-
export declare class
|
|
23
|
+
export declare class PhysicsCollider extends NoRenderComponentX<ColliderPhysicsProps & BaseComponentProps<PhysicsCollider>> {
|
|
30
24
|
enabled: boolean;
|
|
31
|
-
|
|
25
|
+
physicSprite: PhysicsSprite;
|
|
32
26
|
}
|
|
33
|
-
interface BoxColliderPhysicsProps
|
|
27
|
+
interface BoxColliderPhysicsProps {
|
|
34
28
|
width: number;
|
|
35
29
|
height: number;
|
|
36
30
|
}
|
|
37
|
-
export declare class
|
|
31
|
+
export declare class PhysicsBoxCollider extends NoRenderComponentX<BoxColliderPhysicsProps & BaseComponentProps<PhysicsCollider>> {
|
|
38
32
|
}
|
|
39
|
-
interface CircleColliderPhysicsProps
|
|
33
|
+
interface CircleColliderPhysicsProps {
|
|
40
34
|
radius: number;
|
|
41
35
|
}
|
|
42
|
-
export declare class
|
|
36
|
+
export declare class PhysicsCircleCollider extends NoRenderComponentX<CircleColliderPhysicsProps & BaseComponentProps<PhysicsCollider>> {
|
|
43
37
|
}
|
|
44
|
-
interface PolygonColliderPhysicsProps
|
|
45
|
-
points: Array<Vec2
|
|
38
|
+
interface PolygonColliderPhysicsProps {
|
|
39
|
+
points: Array<Vec2> | [number, number][];
|
|
46
40
|
}
|
|
47
|
-
export declare class
|
|
41
|
+
export declare class PhysicsPolygonCollider extends NoRenderComponentX<PolygonColliderPhysicsProps & BaseComponentProps<PhysicsCollider>> {
|
|
48
42
|
}
|
|
49
43
|
export {};
|
|
50
44
|
//# sourceMappingURL=PhysicsComponent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PhysicsComponent.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/PhysicsComponent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAC7D,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE/C,UAAU,cAAc;IACtB,IAAI,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"PhysicsComponent.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/PhysicsComponent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAC7D,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACnC,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE/C,UAAU,cAAc;IACtB,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChB,OAAO,CAAC,EAAE,KAAK,CAAA;IACf,WAAW,CAAC,EAAE,KAAK,CAAA;IACnB,QAAQ,CAAC,EAAE,KAAK,CAAA;IAChB,YAAY,CAAC,EAAE,KAAK,CAAA;CACrB;AAED,qBAAa,SAAU,SAAQ,kBAAkB,CAAC,cAAc,CAAC;IAC/D,IAAI,EAAE,KAAK,CAAC,MAAM,CAAA;CAmBnB;AASD,UAAU,oBAAoB;IAC5B,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,IAAI,CAAA;IACb,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAA;IACjD,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAA;IAC/C,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,EAAE,OAAO,CAAC,KAAA,KAAK,IAAI,CAAA;IACvD,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,EAAE,WAAW,CAAC,KAAA,KAAK,IAAI,CAAA;CAC7D;AAED,qBAAa,eAAgB,SAAQ,kBAAkB,CAAC,oBAAoB,GAAG,kBAAkB,CAAC,eAAe,CAAC,CAAC;IACjH,OAAO,UAAO;IACd,YAAY,EAAE,aAAa,CAAA;CAC5B;AAED,UAAU,uBAAuB;IAC/B,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf;AACD,qBAAa,kBAAmB,SAAQ,kBAAkB,CAAC,uBAAuB,GAAG,kBAAkB,CAAC,eAAe,CAAC,CAAC;CASxH;AACD,UAAU,0BAA0B;IAClC,MAAM,EAAE,MAAM,CAAA;CACf;AACD,qBAAa,qBAAsB,SAAQ,kBAAkB,CAAC,0BAA0B,GAAG,kBAAkB,CAAC,eAAe,CAAC,CAAC;CAAG;AAClI,UAAU,2BAA2B;IACnC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAA;CACzC;AACD,qBAAa,sBAAuB,SAAQ,kBAAkB,CAAC,2BAA2B,GAAG,kBAAkB,CAAC,eAAe,CAAC,CAAC;CAAG"}
|
|
@@ -2,15 +2,13 @@ import { NoRenderComponentX } from '../gworld/core/decorator';
|
|
|
2
2
|
export class RigidBody extends NoRenderComponentX {
|
|
3
3
|
body;
|
|
4
4
|
}
|
|
5
|
-
export class
|
|
6
|
-
}
|
|
7
|
-
export class ColliderPhysics extends NoRenderComponentX {
|
|
5
|
+
export class PhysicsCollider extends NoRenderComponentX {
|
|
8
6
|
enabled = true;
|
|
9
|
-
|
|
7
|
+
physicSprite;
|
|
10
8
|
}
|
|
11
|
-
export class
|
|
9
|
+
export class PhysicsBoxCollider extends NoRenderComponentX {
|
|
12
10
|
}
|
|
13
|
-
export class
|
|
11
|
+
export class PhysicsCircleCollider extends NoRenderComponentX {
|
|
14
12
|
}
|
|
15
|
-
export class
|
|
13
|
+
export class PhysicsPolygonCollider extends NoRenderComponentX {
|
|
16
14
|
}
|
|
@@ -16,7 +16,7 @@ export class PhysicsSprite {
|
|
|
16
16
|
const pos = Vec2(cc.lerp(this.node.x, this.physicsBody.GetPosition().x, dt * 10), cc.lerp(this.node.y, this.physicsBody.GetPosition().y, dt * 10));
|
|
17
17
|
this.node.setPosition(pos.x, pos.y);
|
|
18
18
|
// lerp the rotation
|
|
19
|
-
this.node.setRotation(cc.lerp(this.node.rotation, cc.radiansToDegrees(this.physicsBody.GetAngle()), dt * 10));
|
|
19
|
+
this.node.setRotation(cc.lerp(this.node.rotation, cc.radiansToDegrees(-this.physicsBody.GetAngle()), dt * 10));
|
|
20
20
|
// this.node.setRotation(cc.radiansToDegrees(this.physicsBody.GetAngle()))
|
|
21
21
|
// this.node.setScale(1 / pixelsPerMeter)
|
|
22
22
|
// this.node.setScale(1 / this.physicsBody.GetFixtureList().GetShape().GetRadius())
|
|
@@ -37,13 +37,13 @@ export class PhysicsSprite {
|
|
|
37
37
|
return this.physicsBody.GetPosition().x;
|
|
38
38
|
}
|
|
39
39
|
get y() {
|
|
40
|
-
return this.physicsBody.GetPosition().
|
|
40
|
+
return this.physicsBody.GetPosition().y;
|
|
41
41
|
}
|
|
42
42
|
// set angle(val: number) {
|
|
43
43
|
// this.physicsBody.setAngle(val)
|
|
44
44
|
// }
|
|
45
45
|
get angle() {
|
|
46
|
-
return this.physicsBody.GetAngle();
|
|
46
|
+
return -this.physicsBody.GetAngle();
|
|
47
47
|
}
|
|
48
48
|
addChild(child) {
|
|
49
49
|
this.node.addChild(child);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { EntityManager, EventManager, System } from 'entityx-ts';
|
|
2
|
+
export declare const DynamicBody = 2;
|
|
3
|
+
export declare const KinematicBody = 1;
|
|
4
|
+
export declare const StaticBody = 0;
|
|
2
5
|
export declare let box2D: typeof Box2D;
|
|
3
6
|
export declare function initBox2d(cb: any): void;
|
|
4
7
|
export declare function setColliderMatrix(colliderMatrix?: boolean[][]): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PhysicsSystem.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/PhysicsSystem.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAc,MAAM,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"PhysicsSystem.d.ts","sourceRoot":"","sources":["../../src/box2d-wasm/PhysicsSystem.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAc,MAAM,EAAE,MAAM,YAAY,CAAA;AAQ5E,eAAO,MAAM,WAAW,IAAI,CAAA;AAC5B,eAAO,MAAM,aAAa,IAAI,CAAA;AAC9B,eAAO,MAAM,UAAU,IAAI,CAAA;AAC3B,eAAO,IAAI,KAAK,EAAE,OAAO,KAAK,CAAA;AAE9B,wBAAgB,SAAS,CAAC,EAAE,KAAA,QAK3B;AASD,wBAAgB,iBAAiB,CAAC,cAAc,cAAW,QAG1D;AAOD,qBAAa,aAAc,YAAW,MAAM;IAC1C,KAAK,EAAE,KAAK,CAAC,OAAO,CAAA;IACpB,cAAc,EAAE,KAAK,CAAC,MAAM,EAAE,CAAK;IACnC,eAAe,EAAE,KAAK,CAAC,OAAO,EAAE,CAAK;IACrC,cAAc,cAAW;IACzB,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAA;IAErB,SAAS,CAAC,aAAa,EAAE,YAAY;IA8KrC,MAAM,CAAC,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM;IAwBhE,IAAI,OAAO,CAAC,GAAG,KAAA,EAMd;CACF"}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import Box2DFactory from 'box2d-wasm';
|
|
2
2
|
import { EventTypes } from 'entityx-ts';
|
|
3
|
-
import { GameWorld, instantiate, NodeComp } from '..';
|
|
3
|
+
import { GameWorld, instantiate, NodeComp, Vec2 } from '..';
|
|
4
4
|
import { makeContactListener } from './ContactListener';
|
|
5
5
|
import { makeDebugDraw } from './debugDraw';
|
|
6
|
-
import {
|
|
6
|
+
import { PhysicsBoxCollider, PhysicsCircleCollider, PhysicsCollider, PhysicsPolygonCollider, RigidBody } from './PhysicsComponent';
|
|
7
7
|
import { PhysicsSprite } from './PhysicsSprite';
|
|
8
|
+
export const DynamicBody = 2;
|
|
9
|
+
export const KinematicBody = 1;
|
|
10
|
+
export const StaticBody = 0;
|
|
8
11
|
export let box2D;
|
|
9
12
|
export function initBox2d(cb) {
|
|
10
13
|
Box2DFactory().then((b2) => {
|
|
@@ -34,7 +37,7 @@ export class PhysicsSystem {
|
|
|
34
37
|
colliderMatrix = [[true]];
|
|
35
38
|
graphics;
|
|
36
39
|
configure(event_manager) {
|
|
37
|
-
const { b2BodyDef,
|
|
40
|
+
const { b2BodyDef, b2FixtureDef, b2PolygonShape, b2CircleShape, b2Vec2, b2World, pointsToVec2Array, getPointer } = box2D;
|
|
38
41
|
const gravity = new b2Vec2(0, -10);
|
|
39
42
|
this.world = new b2World(gravity);
|
|
40
43
|
console.log('configure PhysicsSystem world', this.world);
|
|
@@ -47,26 +50,27 @@ export class PhysicsSystem {
|
|
|
47
50
|
const debugDraw = makeDebugDraw(graphics, pixelsPerMeter, box2D);
|
|
48
51
|
this.world.SetDebugDraw(debugDraw);
|
|
49
52
|
// event_manager.subscribe(ComponentAddedEvent(RigidBody), this);
|
|
50
|
-
event_manager.subscribe(EventTypes.ComponentAdded,
|
|
51
|
-
console.log('ComponentAddedEvent
|
|
53
|
+
event_manager.subscribe(EventTypes.ComponentAdded, PhysicsBoxCollider, ({ entity, component }) => {
|
|
54
|
+
console.log('ComponentAddedEvent PhysicsBoxCollider', component);
|
|
52
55
|
let rigidBody = entity.getComponent(RigidBody);
|
|
53
56
|
if (!rigidBody) {
|
|
54
57
|
rigidBody = instantiate(RigidBody);
|
|
55
58
|
entity.assign(rigidBody);
|
|
56
59
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
let physicsCollide = entity.getComponent(PhysicsCollider);
|
|
61
|
+
if (!physicsCollide) {
|
|
62
|
+
physicsCollide = instantiate(PhysicsCollider);
|
|
63
|
+
entity.assign(physicsCollide);
|
|
64
|
+
}
|
|
65
|
+
const { type = StaticBody, gravityScale = 1, density = 1, friction = 0.5, restitution = 0.3 } = rigidBody.props;
|
|
60
66
|
const box = component;
|
|
61
67
|
const node = entity.getComponent(NodeComp);
|
|
62
|
-
const { width, height
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
const zero = new b2Vec2(0, 0);
|
|
68
|
+
const { width, height } = box.props;
|
|
69
|
+
const { x = 0, y = 0 } = physicsCollide.props.offset || {};
|
|
70
|
+
const zero = new b2Vec2(x, y);
|
|
66
71
|
const position = new b2Vec2(node.posX, node.posY);
|
|
67
|
-
const offset = new b2Vec2(x, y);
|
|
68
72
|
const bd = new b2BodyDef();
|
|
69
|
-
bd.set_type(type
|
|
73
|
+
bd.set_type(type);
|
|
70
74
|
bd.set_position(zero);
|
|
71
75
|
bd.set_gravityScale(gravityScale);
|
|
72
76
|
const body = this.world.CreateBody(bd);
|
|
@@ -87,18 +91,112 @@ export class PhysicsSystem {
|
|
|
87
91
|
body.SetAwake(true);
|
|
88
92
|
body.SetEnabled(true);
|
|
89
93
|
metadata[getPointer(body)] = node;
|
|
90
|
-
|
|
91
|
-
physicsCollide.instance = physicsNode;
|
|
94
|
+
physicsCollide.physicSprite = physicsNode;
|
|
92
95
|
physicsCollide.node = node;
|
|
93
96
|
box.node = node;
|
|
94
97
|
});
|
|
95
|
-
event_manager.subscribe(EventTypes.ComponentAdded,
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
98
|
+
event_manager.subscribe(EventTypes.ComponentAdded, PhysicsCircleCollider, ({ entity, component }) => {
|
|
99
|
+
console.log('ComponentAddedEvent PhysicsCircleCollider', component);
|
|
100
|
+
let rigidBody = entity.getComponent(RigidBody);
|
|
101
|
+
if (!rigidBody) {
|
|
102
|
+
rigidBody = instantiate(RigidBody);
|
|
103
|
+
entity.assign(rigidBody);
|
|
104
|
+
}
|
|
105
|
+
let physicsCollide = entity.getComponent(PhysicsCollider);
|
|
106
|
+
if (!physicsCollide) {
|
|
107
|
+
physicsCollide = instantiate(PhysicsCollider);
|
|
108
|
+
entity.assign(physicsCollide);
|
|
109
|
+
}
|
|
110
|
+
const { type = StaticBody, gravityScale = 1, density = 1, friction = 0.5, restitution = 0.3 } = rigidBody.props;
|
|
99
111
|
const node = entity.getComponent(NodeComp);
|
|
100
|
-
|
|
101
|
-
|
|
112
|
+
const { radius } = component.props;
|
|
113
|
+
const { x = 0, y = 0 } = physicsCollide.props.offset || {};
|
|
114
|
+
const zero = new b2Vec2(x, y);
|
|
115
|
+
const position = new b2Vec2(node.posX, node.posY);
|
|
116
|
+
const bd = new b2BodyDef();
|
|
117
|
+
bd.set_type(type);
|
|
118
|
+
bd.set_position(zero);
|
|
119
|
+
bd.set_gravityScale(gravityScale);
|
|
120
|
+
const body = this.world.CreateBody(bd);
|
|
121
|
+
rigidBody.body = body;
|
|
122
|
+
// console.log('body', type, b2_dynamicBody, b2_staticBody, getPointer(body));
|
|
123
|
+
// body.setMassData({ mass: 1 } as any)
|
|
124
|
+
const physicsNode = new PhysicsSprite(node.instance, body);
|
|
125
|
+
const circleShape = new b2CircleShape();
|
|
126
|
+
circleShape.set_m_radius(radius);
|
|
127
|
+
const fixtureDef = new b2FixtureDef();
|
|
128
|
+
fixtureDef.set_shape(circleShape);
|
|
129
|
+
fixtureDef.set_density(density);
|
|
130
|
+
fixtureDef.set_friction(friction);
|
|
131
|
+
fixtureDef.set_restitution(restitution);
|
|
132
|
+
body.CreateFixture(fixtureDef);
|
|
133
|
+
body.SetTransform(position, 0);
|
|
134
|
+
body.SetLinearVelocity(zero);
|
|
135
|
+
body.SetAwake(true);
|
|
136
|
+
body.SetEnabled(true);
|
|
137
|
+
metadata[getPointer(body)] = node;
|
|
138
|
+
physicsCollide.physicSprite = physicsNode;
|
|
139
|
+
physicsCollide.node = node;
|
|
140
|
+
component.node = node;
|
|
141
|
+
});
|
|
142
|
+
event_manager.subscribe(EventTypes.ComponentAdded, PhysicsPolygonCollider, ({ entity, component }) => {
|
|
143
|
+
console.log('ComponentAddedEvent PhysicsPolygonCollider', component);
|
|
144
|
+
let rigidBody = entity.getComponent(RigidBody);
|
|
145
|
+
if (!rigidBody) {
|
|
146
|
+
rigidBody = instantiate(RigidBody);
|
|
147
|
+
entity.assign(rigidBody);
|
|
148
|
+
}
|
|
149
|
+
let physicsCollide = entity.getComponent(PhysicsCollider);
|
|
150
|
+
if (!physicsCollide) {
|
|
151
|
+
physicsCollide = instantiate(PhysicsCollider);
|
|
152
|
+
entity.assign(physicsCollide);
|
|
153
|
+
}
|
|
154
|
+
const { type = StaticBody, gravityScale = 1, density = 1, friction = 0.5, restitution = 0.3 } = rigidBody.props;
|
|
155
|
+
const node = entity.getComponent(NodeComp);
|
|
156
|
+
const { points } = component.props;
|
|
157
|
+
// ett.assign(instantiate(PhysicsCollider, { tag, offset }))
|
|
158
|
+
const { x = 0, y = 0 } = physicsCollide.props.offset || {};
|
|
159
|
+
const zero = new b2Vec2(0, 0);
|
|
160
|
+
const position = new b2Vec2(node.posX, node.posY);
|
|
161
|
+
const { width, height } = node.getContentSize();
|
|
162
|
+
const { scaleX, scaleY, anchorX, anchorY } = node;
|
|
163
|
+
const bd = new b2BodyDef();
|
|
164
|
+
bd.set_type(type);
|
|
165
|
+
bd.set_position(zero);
|
|
166
|
+
bd.set_gravityScale(gravityScale);
|
|
167
|
+
const body = this.world.CreateBody(bd);
|
|
168
|
+
rigidBody.body = body;
|
|
169
|
+
// console.log('body', type, b2_dynamicBody, b2_staticBody, getPointer(body));
|
|
170
|
+
// body.setMassData({ mass: 1 } as any)
|
|
171
|
+
const physicsNode = new PhysicsSprite(node.instance, body);
|
|
172
|
+
const polygonShape = new b2PolygonShape();
|
|
173
|
+
const fixedPoints = points.map((p) => {
|
|
174
|
+
if (p.x)
|
|
175
|
+
return Vec2(p.x + x - width * anchorX * scaleX, p.y + y - height * scaleY * anchorY);
|
|
176
|
+
return Vec2(p[0] + x - width * anchorX * scaleX, p[1] + y - height * scaleY * anchorY);
|
|
177
|
+
});
|
|
178
|
+
const [vecArr, destroyVecArr] = pointsToVec2Array(fixedPoints);
|
|
179
|
+
polygonShape.Set(vecArr, points.length);
|
|
180
|
+
destroyVecArr();
|
|
181
|
+
const fixtureDef = new b2FixtureDef();
|
|
182
|
+
fixtureDef.set_shape(polygonShape);
|
|
183
|
+
fixtureDef.set_density(density);
|
|
184
|
+
fixtureDef.set_friction(friction);
|
|
185
|
+
fixtureDef.set_restitution(restitution);
|
|
186
|
+
body.CreateFixture(fixtureDef);
|
|
187
|
+
body.SetTransform(position, 0);
|
|
188
|
+
body.SetLinearVelocity(zero);
|
|
189
|
+
body.SetAwake(true);
|
|
190
|
+
body.SetEnabled(true);
|
|
191
|
+
metadata[getPointer(body)] = node;
|
|
192
|
+
physicsCollide.physicSprite = physicsNode;
|
|
193
|
+
physicsCollide.node = node;
|
|
194
|
+
component.node = node;
|
|
195
|
+
});
|
|
196
|
+
event_manager.subscribe(EventTypes.ComponentRemoved, PhysicsCollider, ({ component }) => {
|
|
197
|
+
// console.log('ComponentRemovedEvent NodeComp', component)
|
|
198
|
+
if (component.physicSprite instanceof PhysicsSprite) {
|
|
199
|
+
const body = component.physicSprite.getBody();
|
|
102
200
|
// this.listRemoveShape.push(...body.shapeList)
|
|
103
201
|
this.listRemoveBody.push(body);
|
|
104
202
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GUISystem.d.ts","sourceRoot":"","sources":["../../../src/gworld/systems/GUISystem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAoC,MAAM,EAAE,MAAM,YAAY,CAAA;AAiBlG,qBAAa,SAAU,YAAW,MAAM;IACtC,WAAW,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,aAAa,EAAE,YAAY;IAWrC,OAAO,CAAC,eAAe,CAuBtB;IAED,OAAO,CAAC,sBAAsB,
|
|
1
|
+
{"version":3,"file":"GUISystem.d.ts","sourceRoot":"","sources":["../../../src/gworld/systems/GUISystem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAoC,MAAM,EAAE,MAAM,YAAY,CAAA;AAiBlG,qBAAa,SAAU,YAAW,MAAM;IACtC,WAAW,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,aAAa,EAAE,YAAY;IAWrC,OAAO,CAAC,eAAe,CAuBtB;IAED,OAAO,CAAC,sBAAsB,CAc7B;IAED,OAAO,CAAC,cAAc,CAMrB;IAED,OAAO,CAAC,qBAAqB,CAM5B;IAED,OAAO,CAAC,oBAAoB,CAM3B;IAED,OAAO,CAAC,mBAAmB,CAU1B;IAED,OAAO,CAAC,yBAAyB,CAOhC;IAED,OAAO,CAAC,cAAc,CAWrB;IAED,MAAM,CAAC,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM;CAIjE"}
|
|
@@ -40,8 +40,9 @@ export class GUISystem {
|
|
|
40
40
|
touchComp.props.onTouchCancel = touchComp.props.onTouchEnd;
|
|
41
41
|
};
|
|
42
42
|
onAddProgressTimerComp = ({ entity, component: bar }) => {
|
|
43
|
-
const { spriteFrame, fillType = FillType.HORIZONTAL, fillRange = 1, fillCenter = Vec2(
|
|
44
|
-
const
|
|
43
|
+
const { spriteFrame, fillType = FillType.HORIZONTAL, fillRange = 1, fillCenter = Vec2(0, 0) } = bar.props;
|
|
44
|
+
const frame = cc.spriteFrameCache.getSpriteFrame(spriteFrame);
|
|
45
|
+
const sprite = new cc.Sprite(frame || spriteFrame);
|
|
45
46
|
const pTimer = new cc.ProgressTimer(sprite);
|
|
46
47
|
const ptt = fillType === FillType.RADIAL ? cc.ProgressTimer.TYPE_RADIAL : cc.ProgressTimer.TYPE_BAR;
|
|
47
48
|
pTimer.setType(ptt);
|
|
@@ -31,7 +31,7 @@ export class RenderSystem {
|
|
|
31
31
|
const { spriteFrame } = spriteComp.props;
|
|
32
32
|
const frame = cc.spriteFrameCache.getSpriteFrame(spriteFrame);
|
|
33
33
|
// console.log('frame', spriteFrame, frame)
|
|
34
|
-
const node = new cc.Sprite(frame);
|
|
34
|
+
const node = new cc.Sprite(frame || spriteFrame);
|
|
35
35
|
const ett = entity;
|
|
36
36
|
spriteComp.node = ett.assign(new NodeComp(node, ett));
|
|
37
37
|
};
|