@safe-engine/cocos 2.3.10 → 2.4.0

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.
@@ -0,0 +1,49 @@
1
+ import { BaseComponentProps } from '..';
2
+ import { ComponentX } from '../core/decorator';
3
+ import { Vec2 } from '../polyfills';
4
+ import { PhysicsSprite } from './PhysicsSprite';
5
+ interface RigidBodyProps {
6
+ type?: 0 | 1 | 2;
7
+ density?: Float;
8
+ restitution?: Float;
9
+ friction?: Float;
10
+ gravityScale?: Float;
11
+ isSensor?: boolean;
12
+ tag?: number;
13
+ onBeginContact?: (other: RigidBody) => void;
14
+ onEndContact?: (other: RigidBody) => void;
15
+ onPreSolve?: (other: RigidBody, impulse?: any) => void;
16
+ onPostSolve?: (other: RigidBody, oldManifold?: any) => void;
17
+ }
18
+ export declare class RigidBody extends ComponentX<RigidBodyProps> {
19
+ body: cp.Body;
20
+ physicSprite: PhysicsSprite;
21
+ set linearVelocity(vel: Vec2);
22
+ get linearVelocity(): Vec2;
23
+ applyForceTo(vel: Vec2, pos?: Vec2): void;
24
+ applyLinearImpulse(vel: Vec2, pos?: Vec2): void;
25
+ applyTorque(torque: Float): void;
26
+ set position(pos: Vec2);
27
+ get position(): Vec2;
28
+ }
29
+ interface BoxColliderPhysicsProps {
30
+ width: number;
31
+ height: number;
32
+ offset?: [number, number];
33
+ }
34
+ export declare class PhysicsBoxCollider extends ComponentX<BoxColliderPhysicsProps & BaseComponentProps<PhysicsBoxCollider>> {
35
+ }
36
+ interface CircleColliderPhysicsProps {
37
+ radius: number;
38
+ offset?: [number, number];
39
+ }
40
+ export declare class PhysicsCircleCollider extends ComponentX<CircleColliderPhysicsProps & BaseComponentProps<PhysicsCircleCollider>> {
41
+ }
42
+ interface PolygonColliderPhysicsProps {
43
+ points: Array<Vec2> | [number, number][];
44
+ offset?: [number, number];
45
+ }
46
+ export declare class PhysicsPolygonCollider extends ComponentX<PolygonColliderPhysicsProps & BaseComponentProps<PhysicsPolygonCollider>> {
47
+ }
48
+ export {};
49
+ //# sourceMappingURL=PhysicsComponent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PhysicsComponent.d.ts","sourceRoot":"","sources":["../../src/chipmunk/PhysicsComponent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,IAAI,CAAA;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACnC,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;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAA;IAC3C,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAA;IACzC,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,KAAA,KAAK,IAAI,CAAA;IACjD,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,WAAW,CAAC,KAAA,KAAK,IAAI,CAAA;CACvD;AAED,qBAAa,SAAU,SAAQ,UAAU,CAAC,cAAc,CAAC;IACvD,IAAI,EAAE,EAAE,CAAC,IAAI,CAAA;IACb,YAAY,EAAE,aAAa,CAAA;IAC3B,IAAI,cAAc,CAAC,GAAG,EAAE,IAAI,EAM3B;IAED,IAAI,cAAc,IARM,IAAI,CAc3B;IAED,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,CAAC,EAAE,IAAI;IAWlC,kBAAkB,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,CAAC,EAAE,IAAI;IAaxC,WAAW,CAAC,MAAM,EAAE,KAAK;IAOzB,IAAI,QAAQ,CAAC,GAAG,EAAE,IAAI,EASrB;IAED,IAAI,QAAQ,IAXM,IAAI,CAarB;CACF;AAED,UAAU,uBAAuB;IAC/B,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC1B;AACD,qBAAa,kBAAmB,SAAQ,UAAU,CAAC,uBAAuB,GAAG,kBAAkB,CAAC,kBAAkB,CAAC,CAAC;CASnH;AACD,UAAU,0BAA0B;IAClC,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC1B;AACD,qBAAa,qBAAsB,SAAQ,UAAU,CAAC,0BAA0B,GAAG,kBAAkB,CAAC,qBAAqB,CAAC,CAAC;CAAG;AAChI,UAAU,2BAA2B;IACnC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAA;IACxC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC1B;AACD,qBAAa,sBAAuB,SAAQ,UAAU,CAAC,2BAA2B,GAAG,kBAAkB,CAAC,sBAAsB,CAAC,CAAC;CAAG"}
@@ -0,0 +1,69 @@
1
+ import { ComponentX } from '../core/decorator';
2
+ import { Vec2 } from '../polyfills';
3
+ export class RigidBody extends ComponentX {
4
+ body;
5
+ physicSprite;
6
+ set linearVelocity(vel) {
7
+ // console.log('set linearVelocity(', vel)
8
+ if (!this.node) {
9
+ return;
10
+ }
11
+ this.body.setVel(new cp.Vect(vel.x, vel.y));
12
+ }
13
+ get linearVelocity() {
14
+ if (!this.node) {
15
+ return Vec2.ZERO;
16
+ }
17
+ const vel = this.body.getVel();
18
+ return Vec2(vel);
19
+ }
20
+ applyForceTo(vel, pos) {
21
+ if (!this.node) {
22
+ return;
23
+ }
24
+ if (pos) {
25
+ this.body.applyForce(new cp.Vect(vel.x, vel.y), new cp.Vect(pos.x, pos.y));
26
+ }
27
+ else {
28
+ this.body.applyForce(new cp.Vect(vel.x, vel.y), new cp.Vect(0, 0));
29
+ }
30
+ }
31
+ applyLinearImpulse(vel, pos) {
32
+ if (!this.node) {
33
+ return;
34
+ }
35
+ // console.log('applyLinearImpulse', vel, pos)
36
+ // this.body.activate()
37
+ if (pos) {
38
+ this.body.applyImpulse(new cp.Vect(vel.x, vel.y), new cp.Vect(pos.x, pos.y));
39
+ }
40
+ else {
41
+ this.body.applyImpulse(new cp.Vect(vel.x, vel.y), new cp.Vect(0, 0));
42
+ }
43
+ }
44
+ applyTorque(torque) {
45
+ if (!this.node) {
46
+ return;
47
+ }
48
+ this.body.setAngVel(torque);
49
+ }
50
+ set position(pos) {
51
+ this.physicSprite.node.setPosition(pos.x, pos.y);
52
+ const physicsPos = new cp.Vect(pos.x, pos.y);
53
+ // console.log('SetTransform', pos, physicsPos)
54
+ const body = this.body;
55
+ body.setVel(new cp.Vect(0, 0));
56
+ body.setAngVel(0);
57
+ // body.SetAwake(true)
58
+ body.setPos(physicsPos);
59
+ }
60
+ get position() {
61
+ return Vec2(this.physicSprite.getBody().getPos());
62
+ }
63
+ }
64
+ export class PhysicsBoxCollider extends ComponentX {
65
+ }
66
+ export class PhysicsCircleCollider extends ComponentX {
67
+ }
68
+ export class PhysicsPolygonCollider extends ComponentX {
69
+ }
@@ -0,0 +1,16 @@
1
+ export declare class PhysicsSprite {
2
+ node: cc.Node;
3
+ physicsBody: cp.Body;
4
+ constructor(node: cc.Node, body: cp.Body);
5
+ update(dt: number): void;
6
+ getBody(): cp.Body;
7
+ set position(val: cp.Vect);
8
+ set x(val: number);
9
+ set y(val: number);
10
+ get x(): number;
11
+ get y(): number;
12
+ set rotation(val: number);
13
+ get rotation(): number;
14
+ addChild(child: cc.Node): void;
15
+ }
16
+ //# sourceMappingURL=PhysicsSprite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PhysicsSprite.d.ts","sourceRoot":"","sources":["../../src/chipmunk/PhysicsSprite.ts"],"names":[],"mappings":"AAEA,qBAAa,aAAa;IACxB,IAAI,EAAE,EAAE,CAAC,IAAI,CAAA;IACb,WAAW,EAAE,EAAE,CAAC,IAAI,CAAA;gBAER,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI;IAKxC,MAAM,CAAC,EAAE,EAAE,MAAM;IAcjB,OAAO;IAIP,IAAI,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,EAExB;IAED,IAAI,CAAC,CAAC,GAAG,QAAA,EAER;IACD,IAAI,CAAC,CAAC,GAAG,QAAA,EAER;IAED,IAAI,CAAC,WAEJ;IAED,IAAI,CAAC,WAEJ;IAED,IAAI,QAAQ,CAAC,GAAG,EAAE,MAAM,EAEvB;IAED,IAAI,QAAQ,IAJM,MAAM,CAMvB;IAED,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI;CAGxB"}
@@ -0,0 +1,49 @@
1
+ import { Vec2 } from '../polyfills';
2
+ export class PhysicsSprite {
3
+ node;
4
+ physicsBody;
5
+ constructor(node, body) {
6
+ this.node = node;
7
+ this.physicsBody = body;
8
+ }
9
+ update(dt) {
10
+ if (!this.physicsBody) {
11
+ return;
12
+ }
13
+ const { x, y } = this.physicsBody.getPos();
14
+ // use cc.lerp to smooth the position update
15
+ // const pos = Vec2(cc.lerp(this.node.x, x, dt * 10), cc.lerp(this.node.y, y, dt * 10))
16
+ this.node.setPosition(x, y);
17
+ // lerp the rotation
18
+ // this.node.setRotation(cc.lerp(this.node.rotation, -cc.radiansToDegrees(this.physicsBody.a), dt * 10))
19
+ this.node.setRotation(-cc.radiansToDegrees(this.physicsBody.a));
20
+ // this.node.setScale(1 / this.physicsBody.GetFixtureList().GetShape().GetRadius())
21
+ }
22
+ getBody() {
23
+ return this.physicsBody;
24
+ }
25
+ set position(val) {
26
+ this.physicsBody.setPos(val);
27
+ }
28
+ set x(val) {
29
+ this.physicsBody.setPos(Vec2(val, this.y));
30
+ }
31
+ set y(val) {
32
+ this.physicsBody.setPos(Vec2(this.x, val));
33
+ }
34
+ get x() {
35
+ return this.physicsBody.getPos().x;
36
+ }
37
+ get y() {
38
+ return this.physicsBody.getPos().y;
39
+ }
40
+ set rotation(val) {
41
+ this.physicsBody.setAngle(-cc.degreesToRadians(val));
42
+ }
43
+ get rotation() {
44
+ return -cc.radiansToDegrees(this.physicsBody.a);
45
+ }
46
+ addChild(child) {
47
+ this.node.addChild(child);
48
+ }
49
+ }
@@ -0,0 +1,20 @@
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;
5
+ export declare function setColliderMatrix(colliderMatrix?: boolean[][]): void;
6
+ export declare class PhysicsSystem implements System {
7
+ space: cp.Space;
8
+ _debugNode: cc.PhysicsDebugNode;
9
+ listRemoveBody: cp.Body[];
10
+ listRemoveShape: cp.Shape[];
11
+ colliderMatrix: boolean[][];
12
+ configure(event_manager: EventManager): void;
13
+ update(entities: EntityManager, events: EventManager, dt: number): void;
14
+ collisionBegin(arbiter: cp.Arbiter, space: cp.Space): boolean;
15
+ collisionPre(arbiter: cp.Arbiter, space: cp.Space): boolean;
16
+ collisionPost(arbiter: cp.Arbiter, space: cp.Space): boolean;
17
+ collisionSeparate(arbiter: cp.Arbiter, space: cp.Space): boolean;
18
+ addDebug(): void;
19
+ }
20
+ //# sourceMappingURL=PhysicsSystem.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PhysicsSystem.d.ts","sourceRoot":"","sources":["../../src/chipmunk/PhysicsSystem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAc,MAAM,EAAE,MAAM,YAAY,CAAA;AAO5E,eAAO,MAAM,WAAW,IAAI,CAAA;AAC5B,eAAO,MAAM,aAAa,IAAI,CAAA;AAC9B,eAAO,MAAM,UAAU,IAAI,CAAA;AAE3B,wBAAgB,iBAAiB,CAAC,cAAc,cAAW,QAG1D;AAED,qBAAa,aAAc,YAAW,MAAM;IAC1C,KAAK,EAAE,EAAE,CAAC,KAAK,CAAA;IACf,UAAU,EAAE,EAAE,CAAC,gBAAgB,CAAA;IAC/B,cAAc,EAAE,EAAE,CAAC,IAAI,EAAE,CAAK;IAC9B,eAAe,EAAE,EAAE,CAAC,KAAK,EAAE,CAAK;IAChC,cAAc,cAAW;IAEzB,SAAS,CAAC,aAAa,EAAE,YAAY;IAiKrC,MAAM,CAAC,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM;IA2BhE,cAAc,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK;IA2BnD,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK;IAKjD,aAAa,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK;IAKlD,iBAAiB,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK;IAkBtD,QAAQ;CAMT"}
@@ -0,0 +1,253 @@
1
+ import { EventTypes } from 'entityx-ts';
2
+ import { NodeComp } from '../core/NodeComp';
3
+ import { GameWorld } from '../gworld';
4
+ import { instantiate } from '../helper';
5
+ import { PhysicsBoxCollider, PhysicsCircleCollider, PhysicsPolygonCollider, RigidBody } from './PhysicsComponent';
6
+ import { PhysicsSprite } from './PhysicsSprite';
7
+ export const DynamicBody = 2;
8
+ export const KinematicBody = 1;
9
+ export const StaticBody = 0;
10
+ export function setColliderMatrix(colliderMatrix = [[true]]) {
11
+ const physicsSystem = GameWorld.Instance.systems.get(PhysicsSystem);
12
+ physicsSystem.colliderMatrix = colliderMatrix;
13
+ }
14
+ export class PhysicsSystem {
15
+ space;
16
+ _debugNode;
17
+ listRemoveBody = [];
18
+ listRemoveShape = [];
19
+ colliderMatrix = [[true]];
20
+ configure(event_manager) {
21
+ this.space = new cp.Space();
22
+ this.space.gravity = cp.v(0, -98);
23
+ this.space.iterations = 60;
24
+ this.space.collisionSlop = 0.5;
25
+ this.space.setDefaultCollisionHandler(this.collisionBegin.bind(this), this.collisionPre.bind(this), this.collisionPost.bind(this), this.collisionSeparate.bind(this));
26
+ event_manager.subscribe(EventTypes.ComponentAdded, PhysicsBoxCollider, ({ entity, component: box }) => {
27
+ // console.log('ComponentAddedEvent PhysicsBoxCollider', box)
28
+ let rigidBody = entity.getComponent(RigidBody);
29
+ if (!rigidBody) {
30
+ rigidBody = instantiate(RigidBody);
31
+ entity.assign(rigidBody);
32
+ }
33
+ const { type = StaticBody, gravityScale = 1, density = 1, friction = 0.5, restitution = 0.3, isSensor, tag = 0 } = rigidBody.props;
34
+ const { width, height, offset = [] } = box.props;
35
+ const [x = 0, y = 0] = offset;
36
+ const node = entity.getComponent(NodeComp);
37
+ const hw = width * 0.5;
38
+ const hh = height * 0.5;
39
+ let shape;
40
+ let body;
41
+ if (type === DynamicBody) {
42
+ body = new cp.Body(density * gravityScale, cp.momentForBox(density, width, height));
43
+ shape = new cp.BoxShape2(body, new cp.BB(-hw + x, -hh + y, hw + x, hh + y));
44
+ this.space.addShape(shape);
45
+ this.space.addBody(body);
46
+ }
47
+ else {
48
+ body = new cp.Body(Infinity, Infinity);
49
+ body.nodeIdleTime = Infinity;
50
+ shape = new cp.BoxShape2(body, new cp.BB(-hw + x, -hh + y, hw + x, hh + y));
51
+ this.space.addStaticShape(shape);
52
+ }
53
+ shape.setElasticity(restitution);
54
+ shape.setFriction(friction);
55
+ shape.setSensor(isSensor);
56
+ const physicsNode = new PhysicsSprite(node.instance, body);
57
+ rigidBody.physicSprite = physicsNode;
58
+ rigidBody.node = node;
59
+ rigidBody.body = body;
60
+ body.data = node;
61
+ shape.group = tag;
62
+ box.node = node;
63
+ body.setPos(node.position);
64
+ });
65
+ event_manager.subscribe(EventTypes.ComponentAdded, PhysicsCircleCollider, ({ entity, component }) => {
66
+ // console.log('ComponentAddedEvent PhysicsCircleCollider', component)
67
+ let rigidBody = entity.getComponent(RigidBody);
68
+ if (!rigidBody) {
69
+ rigidBody = instantiate(RigidBody);
70
+ entity.assign(rigidBody);
71
+ }
72
+ const { type = StaticBody, gravityScale = 1, density = 1, friction = 0.5, restitution = 0.3, isSensor = false, tag = 0, } = rigidBody.props;
73
+ const node = entity.getComponent(NodeComp);
74
+ const { radius, offset = [] } = component.props;
75
+ const [x = 0, y = 0] = offset;
76
+ const { width, height } = node.contentSize;
77
+ const { scaleX, scaleY, anchorX, anchorY } = node;
78
+ const offVect = new cp.Vect(x - width * anchorX * scaleX, y - height * scaleY * anchorY);
79
+ let shape;
80
+ let body;
81
+ if (type === DynamicBody) {
82
+ body = new cp.Body(density * gravityScale, cp.momentForCircle(density, radius, radius, offVect));
83
+ shape = new cp.CircleShape(body, radius, offVect);
84
+ this.space.addShape(shape);
85
+ this.space.addBody(body);
86
+ }
87
+ else {
88
+ body = new cp.Body(Infinity, Infinity);
89
+ body.nodeIdleTime = Infinity;
90
+ shape = new cp.CircleShape(body, radius, offVect);
91
+ this.space.addStaticShape(shape);
92
+ }
93
+ shape.setElasticity(restitution);
94
+ shape.setFriction(friction);
95
+ shape.setSensor(isSensor);
96
+ const physicsNode = new PhysicsSprite(node.instance, body);
97
+ // console.log(position instanceof b2Vec2, zero instanceof b2Vec2)
98
+ rigidBody.physicSprite = physicsNode;
99
+ rigidBody.node = node;
100
+ rigidBody.body = body;
101
+ body.data = node;
102
+ shape.group = tag;
103
+ component.node = node;
104
+ body.setPos(node.position);
105
+ });
106
+ event_manager.subscribe(EventTypes.ComponentAdded, PhysicsPolygonCollider, ({ entity, component }) => {
107
+ // console.log('ComponentAddedEvent PhysicsPolygonCollider', component)
108
+ let rigidBody = entity.getComponent(RigidBody);
109
+ if (!rigidBody) {
110
+ rigidBody = instantiate(RigidBody);
111
+ entity.assign(rigidBody);
112
+ }
113
+ const { type = StaticBody, gravityScale = 1, density = 1, friction = 0.5, restitution = 0.3, isSensor, tag = 0 } = rigidBody.props;
114
+ const node = entity.getComponent(NodeComp);
115
+ const { points, offset = [] } = component.props;
116
+ const [x = 0, y = 0] = offset;
117
+ const { width, height } = node.contentSize;
118
+ const { scaleX, scaleY, anchorX, anchorY } = node;
119
+ const offVect = new cp.Vect(x - width * anchorX * scaleX, y - height * scaleY * anchorY);
120
+ const fixedPoints = [];
121
+ points.forEach((p) => {
122
+ const px = p.x || p[0];
123
+ const py = p.y || p[1];
124
+ fixedPoints.push(px);
125
+ fixedPoints.push(py);
126
+ });
127
+ let shape;
128
+ let body;
129
+ if (type === DynamicBody) {
130
+ body = new cp.Body(density * gravityScale, cp.momentForPoly(density, fixedPoints, offVect));
131
+ shape = new cp.PolyShape(body, fixedPoints, offVect);
132
+ this.space.addShape(shape);
133
+ this.space.addBody(body);
134
+ }
135
+ else {
136
+ body = new cp.Body(Infinity, Infinity);
137
+ body.nodeIdleTime = Infinity;
138
+ shape = new cp.PolyShape(body, fixedPoints, offVect);
139
+ this.space.addStaticShape(shape);
140
+ }
141
+ shape.setElasticity(restitution);
142
+ shape.setFriction(friction);
143
+ shape.setSensor(isSensor);
144
+ const physicsNode = new PhysicsSprite(node.instance, body);
145
+ rigidBody.physicSprite = physicsNode;
146
+ rigidBody.node = node;
147
+ rigidBody.body = body;
148
+ body.data = node;
149
+ shape.group = tag;
150
+ component.node = node;
151
+ body.setPos(node.position);
152
+ });
153
+ event_manager.subscribe(EventTypes.ComponentRemoved, RigidBody, ({ component }) => {
154
+ // console.log('ComponentRemovedEvent RigidBody', component)
155
+ if (component.enabled && component.physicSprite instanceof PhysicsSprite) {
156
+ const body = component.physicSprite.getBody();
157
+ if (body && this.space.containsBody(body)) {
158
+ body.shapeList.forEach((shape) => {
159
+ if (shape && this.space.containsShape(shape)) {
160
+ if (!this.listRemoveShape.includes(shape)) {
161
+ this.listRemoveShape.push(shape);
162
+ }
163
+ }
164
+ });
165
+ this.listRemoveBody.push(body);
166
+ }
167
+ }
168
+ });
169
+ }
170
+ update(entities, events, dt) {
171
+ if (this.space) {
172
+ this.space.step(dt);
173
+ }
174
+ // this.space.addPostStepCallback(() => {
175
+ // cc.log('addPostStepCallback');
176
+ this.listRemoveShape.forEach((shape) => {
177
+ if (shape && this.space.containsShape(shape)) {
178
+ this.space.removeShape(shape);
179
+ }
180
+ });
181
+ this.listRemoveBody.forEach((body) => {
182
+ if (body && this.space.containsBody(body)) {
183
+ this.space.removeBody(body);
184
+ }
185
+ });
186
+ this.listRemoveBody = [];
187
+ this.listRemoveShape = [];
188
+ // })
189
+ for (const entt of entities.entities_with_components(RigidBody)) {
190
+ const comp = entt.getComponent(RigidBody);
191
+ if (comp.node && comp.node.active && comp.enabled) {
192
+ comp.physicSprite.update(dt);
193
+ }
194
+ }
195
+ }
196
+ collisionBegin(arbiter, space) {
197
+ const shapes = arbiter.getShapes();
198
+ // cc.log(arbiter);
199
+ const node1 = arbiter.body_a.data;
200
+ const node2 = arbiter.body_b.data;
201
+ const groupA = shapes[0].group;
202
+ const groupB = shapes[1].group;
203
+ // cc.log(groupA, groupB, this.colliderMatrix[groupA][groupB])
204
+ if (!this.colliderMatrix[groupA][groupB]) {
205
+ return false;
206
+ }
207
+ const phys1 = node1.getComponent(RigidBody);
208
+ const phys2 = node2.getComponent(RigidBody);
209
+ if (node1 && node1.active) {
210
+ if (phys1 && phys2 && phys1.props.onBeginContact) {
211
+ phys1.props.onBeginContact(phys2);
212
+ }
213
+ }
214
+ if (node2 && node2.active) {
215
+ if (phys1 && phys2 && phys2.props.onBeginContact) {
216
+ phys2.props.onBeginContact(phys1);
217
+ }
218
+ }
219
+ return true;
220
+ }
221
+ collisionPre(arbiter, space) {
222
+ // cc.log('collisionPre');
223
+ return true;
224
+ }
225
+ collisionPost(arbiter, space) {
226
+ // cc.log('collisionPost');
227
+ return true;
228
+ }
229
+ collisionSeparate(arbiter, space) {
230
+ // cc.log('collisionSeparate');
231
+ const node1 = arbiter.body_a.data;
232
+ const node2 = arbiter.body_b.data;
233
+ const phys1 = node1.getComponent(RigidBody);
234
+ const phys2 = node2.getComponent(RigidBody);
235
+ if (node1 && node1.active) {
236
+ if (phys1 && phys2 && phys1.props.onEndContact) {
237
+ phys1.props.onEndContact(phys2);
238
+ }
239
+ }
240
+ if (node2 && node2.active) {
241
+ if (phys1 && phys2 && phys2.props.onEndContact) {
242
+ phys2.props.onEndContact(phys1);
243
+ }
244
+ }
245
+ return true;
246
+ }
247
+ addDebug() {
248
+ const currentScene = cc.director.getRunningScene();
249
+ this._debugNode = new cc.PhysicsDebugNode(this.space);
250
+ this._debugNode.visible = true;
251
+ currentScene.addChild(this._debugNode, 100);
252
+ }
253
+ }
@@ -0,0 +1,24 @@
1
+ import { GameWorld, Vec2 } from '..';
2
+ export * from './PhysicsComponent';
3
+ export * from './PhysicsSprite';
4
+ export * from './PhysicsSystem';
5
+ export declare function setupPhysics(world?: GameWorld, isDebugDraw?: boolean, gravity?: {
6
+ x: number;
7
+ y: number;
8
+ equals(other: /*elided*/ any): boolean;
9
+ add(value: cc.Point | Vec2): /*elided*/ any;
10
+ addSelf(value: cc.Point | Vec2): /*elided*/ any;
11
+ sub(value: cc.Point | Vec2): /*elided*/ any;
12
+ mul(multiply: number): /*elided*/ any;
13
+ mulSelf(multiply: number): /*elided*/ any;
14
+ mag(): number;
15
+ normalizeSelf(): /*elided*/ any;
16
+ normalize(): /*elided*/ any;
17
+ cross(other: Vec2): number;
18
+ signAngle(other: Vec2): number;
19
+ lengthSqr(): number;
20
+ dot(other: Vec2): number;
21
+ angle(other: Vec2): number;
22
+ distance(other: /*elided*/ any): number;
23
+ }): void;
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/chipmunk/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,IAAI,CAAA;AAGpC,cAAc,oBAAoB,CAAA;AAClC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAE/B,wBAAgB,YAAY,CAAC,KAAK,YAAqB,EAAE,WAAW,UAAQ,EAAE,OAAO;;;;;;;;;;;;;;;;;;CAAe,QAMnG"}
@@ -0,0 +1,12 @@
1
+ import { GameWorld, Vec2 } 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, isDebugDraw = false, gravity = Vec2(0, -98)) {
7
+ const physicsSystem = world.addSystemAndUpdate(PhysicsSystem);
8
+ if (isDebugDraw) {
9
+ physicsSystem.addDebug();
10
+ }
11
+ physicsSystem.space.gravity = gravity;
12
+ }
@@ -15,7 +15,7 @@ export class NoRenderSystem {
15
15
  swallowTouches: true,
16
16
  onTouchBegan: function (touch) {
17
17
  const { onTouchStart } = touchComp.props;
18
- if (!nodeComp.active)
18
+ if (!nodeComp.active || !touchComp.enabled)
19
19
  return false;
20
20
  // console.log('onTouchBegan', onTouchStart)
21
21
  if (!nodeComp.parent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@safe-engine/cocos",
3
- "version": "2.3.10",
3
+ "version": "2.4.0",
4
4
  "description": "safe-engine with cocos renderer support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "@cocos/dragonbones-js": "^1.0.2",
25
25
  "@esotericsoftware/spine-core": "^4.2.93",
26
26
  "box2d-wasm": "^7.0.0",
27
- "cocos-html5-ts": "^2.0.3",
27
+ "cocos-html5-ts": "^2.1.1",
28
28
  "entityx-ts": "^2.3.1"
29
29
  },
30
30
  "author": "",