@gg-web-engine/matter 0.0.38 → 0.0.39

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.
@@ -1,9 +1,10 @@
1
- import { CollisionGroup, Entity2d, Gg2dWorld, IRigidBody2dComponent, IVisualScene2dComponent, Point2, VisualTypeDocRepo2D } from '@gg-web-engine/core';
1
+ import { CollisionGroup, DebugBody2DSettings, Entity2d, Gg2dWorld, IRigidBody2dComponent, IVisualScene2dComponent, Point2, Shape2DDescriptor, VisualTypeDocRepo2D } from '@gg-web-engine/core';
2
2
  import { Body } from 'matter-js';
3
3
  import { MatterPhysicsTypeDocRepo } from '../types';
4
4
  import { MatterWorldComponent } from './matter-world.component';
5
5
  export declare class MatterRigidBodyComponent implements IRigidBody2dComponent<MatterPhysicsTypeDocRepo> {
6
6
  nativeBody: Body;
7
+ readonly shape: Shape2DDescriptor;
7
8
  get position(): Point2;
8
9
  set position(value: Point2);
9
10
  get rotation(): number;
@@ -14,7 +15,8 @@ export declare class MatterRigidBodyComponent implements IRigidBody2dComponent<M
14
15
  set angularVelocity(value: number);
15
16
  name: string;
16
17
  entity: Entity2d | null;
17
- constructor(nativeBody: Body);
18
+ get debugBodySettings(): DebugBody2DSettings;
19
+ constructor(nativeBody: Body, shape: Shape2DDescriptor);
18
20
  get interactWithCollisionGroups(): CollisionGroup[];
19
21
  set interactWithCollisionGroups(value: CollisionGroup[] | 'all');
20
22
  get ownCollisionGroups(): CollisionGroup[];
@@ -1,8 +1,9 @@
1
1
  import { Pnt2, } from '@gg-web-engine/core';
2
2
  import { Body, Composite, Vector } from 'matter-js';
3
3
  export class MatterRigidBodyComponent {
4
- constructor(nativeBody) {
4
+ constructor(nativeBody, shape) {
5
5
  this.nativeBody = nativeBody;
6
+ this.shape = shape;
6
7
  this.name = '';
7
8
  this.entity = null;
8
9
  }
@@ -30,6 +31,14 @@ export class MatterRigidBodyComponent {
30
31
  set angularVelocity(value) {
31
32
  Body.setAngularVelocity(this.nativeBody, value);
32
33
  }
34
+ get debugBodySettings() {
35
+ if (isFinite(this.nativeBody.mass)) {
36
+ return { shape: this.shape, type: 'RIGID_DYNAMIC', sleeping: this.nativeBody.isSleeping };
37
+ }
38
+ else {
39
+ return { shape: this.shape, type: 'RIGID_STATIC' };
40
+ }
41
+ }
33
42
  get interactWithCollisionGroups() {
34
43
  throw new Error('Collision groups not implemented for Matter.js');
35
44
  }
@@ -48,9 +57,11 @@ export class MatterRigidBodyComponent {
48
57
  }
49
58
  addToWorld(world) {
50
59
  Composite.add(world.physicsWorld.matterWorld, this.nativeBody);
60
+ world.physicsWorld.added$.next(this);
51
61
  }
52
62
  removeFromWorld(world) {
53
63
  Composite.remove(world.physicsWorld.matterWorld, this.nativeBody);
64
+ world.physicsWorld.removed$.next(this);
54
65
  }
55
66
  dispose() { }
56
67
  resetMotion() {
@@ -1,23 +1,23 @@
1
- import { CollisionGroup, Gg2dWorld, IDebugPhysicsDrawer, IPhysicsWorld2dComponent, Point2 } from '@gg-web-engine/core';
1
+ import { CollisionGroup, IPhysicsWorld2dComponent, Point2 } from '@gg-web-engine/core';
2
2
  import { Engine, World } from 'matter-js';
3
3
  import { MatterFactory } from '../matter-factory';
4
4
  import { MatterPhysicsTypeDocRepo } from '../types';
5
+ import { Subject } from 'rxjs';
6
+ import { MatterRigidBodyComponent } from './matter-rigid-body.component';
5
7
  export declare class MatterWorldComponent implements IPhysicsWorld2dComponent<MatterPhysicsTypeDocRepo> {
6
8
  protected matterEngine: Engine | null;
7
9
  get matterWorld(): World | null;
8
10
  readonly factory: MatterFactory;
11
+ readonly added$: Subject<MatterRigidBodyComponent>;
12
+ readonly removed$: Subject<MatterRigidBodyComponent>;
13
+ readonly children: MatterRigidBodyComponent[];
9
14
  private _gravity;
10
15
  get gravity(): Point2;
11
16
  set gravity(value: Point2);
12
- private _timeScale;
13
- get timeScale(): number;
14
- set timeScale(value: number);
15
- get physicsDebugViewActive(): boolean;
17
+ constructor();
16
18
  init(): Promise<void>;
17
19
  registerCollisionGroup(): CollisionGroup;
18
20
  deregisterCollisionGroup(group: CollisionGroup): void;
19
21
  simulate(delta: number): void;
20
- startDebugger(world: Gg2dWorld, drawer: IDebugPhysicsDrawer<Point2, number>): void;
21
- stopDebugger(): void;
22
22
  dispose(): void;
23
23
  }
@@ -9,13 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { Engine } from 'matter-js';
11
11
  import { MatterFactory } from '../matter-factory';
12
+ import { Subject } from 'rxjs';
12
13
  // TODO implement bindings for collision groups. Matter.js has elegant solution for that, read body.collisionFilter
13
14
  export class MatterWorldComponent {
14
15
  constructor() {
15
16
  this.matterEngine = null;
16
17
  this.factory = new MatterFactory();
18
+ this.added$ = new Subject();
19
+ this.removed$ = new Subject();
20
+ this.children = [];
17
21
  this._gravity = { x: 0, y: 9.82 };
18
- this._timeScale = 1;
22
+ this.added$.subscribe(c => this.children.push(c));
23
+ this.removed$.subscribe(c => this.children.splice(this.children.indexOf(c), 1));
19
24
  }
20
25
  get matterWorld() {
21
26
  return this.matterEngine && this.matterEngine.world;
@@ -30,15 +35,6 @@ export class MatterWorldComponent {
30
35
  this.matterEngine.gravity.y = this._gravity.y;
31
36
  }
32
37
  }
33
- get timeScale() {
34
- return this._timeScale;
35
- }
36
- set timeScale(value) {
37
- this._timeScale = value;
38
- }
39
- get physicsDebugViewActive() {
40
- return false;
41
- }
42
38
  init() {
43
39
  return __awaiter(this, void 0, void 0, function* () {
44
40
  this.matterEngine = Engine.create({ gravity: Object.assign(Object.assign({}, this._gravity), { scale: 0.0001 }) });
@@ -51,15 +47,7 @@ export class MatterWorldComponent {
51
47
  throw new Error('Collision groups not implemented for Matter.js');
52
48
  }
53
49
  simulate(delta) {
54
- Engine.update(this.matterEngine, this._timeScale * delta);
55
- }
56
- startDebugger(world, drawer) {
57
- // TODO
58
- throw new Error('Matter.js DebugDrawer not implemented');
59
- }
60
- stopDebugger() {
61
- // TODO
62
- throw new Error('Matter.js DebugDrawer not implemented');
50
+ Engine.update(this.matterEngine, delta);
63
51
  }
64
52
  dispose() {
65
53
  Engine.clear(this.matterEngine);
@@ -17,7 +17,7 @@ export class MatterFactory {
17
17
  }
18
18
  nativeBody.position = Vector.create(((_a = transform === null || transform === void 0 ? void 0 : transform.position) === null || _a === void 0 ? void 0 : _a.x) || 0, ((_b = transform === null || transform === void 0 ? void 0 : transform.position) === null || _b === void 0 ? void 0 : _b.y) || 0);
19
19
  nativeBody.angle = (transform === null || transform === void 0 ? void 0 : transform.rotation) || 0;
20
- return new MatterRigidBodyComponent(nativeBody);
20
+ return new MatterRigidBodyComponent(nativeBody, descriptor.shape);
21
21
  }
22
22
  createTrigger(descriptor, transform) {
23
23
  throw new Error('Triggers not implemented for Matter.js');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gg-web-engine/matter",
3
- "version": "0.0.38",
3
+ "version": "0.0.39",
4
4
  "description": "An attempt to create open source game engine for browser",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,13 +30,15 @@
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/matter-js": "0.19.6",
33
- "@gg-web-engine/core": "0.0.38",
33
+ "@gg-web-engine/core": "0.0.39",
34
34
  "matter-js": "0.19.0",
35
35
  "prettier": "^2.8.6",
36
+ "rxjs": "7.8.1",
36
37
  "typescript": "~4.7.2"
37
38
  },
38
39
  "peerDependencies": {
39
- "@gg-web-engine/core": "0.0.38",
40
- "matter-js": "0.19.0"
40
+ "@gg-web-engine/core": "0.0.39",
41
+ "matter-js": "0.19.0",
42
+ "rxjs": "7.8.1"
41
43
  }
42
44
  }