@gg-web-engine/rapier2d 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,10 +1,11 @@
1
- import { Body2DOptions, CollisionGroup, Entity2d, Gg2dWorld, IRigidBody2dComponent, Point2, VisualTypeDocRepo2D } from '@gg-web-engine/core';
1
+ import { Body2DOptions, CollisionGroup, DebugBody2DSettings, Entity2d, Gg2dWorld, IRigidBody2dComponent, Point2, Shape2DDescriptor, VisualTypeDocRepo2D } from '@gg-web-engine/core';
2
2
  import { Collider, ColliderDesc, InteractionGroups, RigidBody, RigidBodyDesc } from '@dimforge/rapier2d-compat';
3
3
  import { Rapier2dWorldComponent } from './rapier-2d-world.component';
4
4
  import { Rapier2dPhysicsTypeDocRepo } from '../types';
5
5
  export declare class Rapier2dRigidBodyComponent implements IRigidBody2dComponent<Rapier2dPhysicsTypeDocRepo> {
6
6
  protected readonly world: Rapier2dWorldComponent;
7
7
  protected _colliderDescr: ColliderDesc[];
8
+ readonly shape: Shape2DDescriptor;
8
9
  protected _bodyDescr: RigidBodyDesc;
9
10
  protected _colliderOptions: Omit<Omit<Body2DOptions, 'dynamic'>, 'mass'>;
10
11
  entity: Entity2d | null;
@@ -21,8 +22,14 @@ export declare class Rapier2dRigidBodyComponent implements IRigidBody2dComponent
21
22
  get nativeBody(): RigidBody | null;
22
23
  set nativeBody(value: RigidBody | null);
23
24
  name: string;
24
- get factoryProps(): [ColliderDesc[], RigidBodyDesc, Omit<Omit<Body2DOptions, 'dynamic'>, 'mass'>];
25
- constructor(world: Rapier2dWorldComponent, _colliderDescr: ColliderDesc[], _bodyDescr: RigidBodyDesc, _colliderOptions: Omit<Omit<Body2DOptions, 'dynamic'>, 'mass'>);
25
+ get factoryProps(): [
26
+ ColliderDesc[],
27
+ Shape2DDescriptor,
28
+ RigidBodyDesc,
29
+ Omit<Omit<Body2DOptions, 'dynamic'>, 'mass'>
30
+ ];
31
+ get debugBodySettings(): DebugBody2DSettings;
32
+ constructor(world: Rapier2dWorldComponent, _colliderDescr: ColliderDesc[], shape: Shape2DDescriptor, _bodyDescr: RigidBodyDesc, _colliderOptions: Omit<Omit<Body2DOptions, 'dynamic'>, 'mass'>);
26
33
  protected collisionGroups: InteractionGroups;
27
34
  get interactWithCollisionGroups(): CollisionGroup[];
28
35
  set interactWithCollisionGroups(value: CollisionGroup[] | 'all');
@@ -1,9 +1,10 @@
1
1
  import { BitMask, Pnt2, } from '@gg-web-engine/core';
2
2
  import { Vector2, } from '@dimforge/rapier2d-compat';
3
3
  export class Rapier2dRigidBodyComponent {
4
- constructor(world, _colliderDescr, _bodyDescr, _colliderOptions) {
4
+ constructor(world, _colliderDescr, shape, _bodyDescr, _colliderOptions) {
5
5
  this.world = world;
6
6
  this._colliderDescr = _colliderDescr;
7
+ this.shape = shape;
7
8
  this._bodyDescr = _bodyDescr;
8
9
  this._colliderOptions = _colliderOptions;
9
10
  this.entity = null;
@@ -62,7 +63,16 @@ export class Rapier2dRigidBodyComponent {
62
63
  this._nativeBody = value;
63
64
  }
64
65
  get factoryProps() {
65
- return [this._colliderDescr, this._bodyDescr, this._colliderOptions];
66
+ return [this._colliderDescr, this.shape, this._bodyDescr, this._colliderOptions];
67
+ }
68
+ get debugBodySettings() {
69
+ var _a;
70
+ if (this._bodyDescr.mass > 0) {
71
+ return { shape: this.shape, type: 'RIGID_DYNAMIC', sleeping: !!((_a = this._nativeBody) === null || _a === void 0 ? void 0 : _a.isSleeping()) };
72
+ }
73
+ else {
74
+ return { shape: this.shape, type: 'RIGID_STATIC' };
75
+ }
66
76
  }
67
77
  get interactWithCollisionGroups() {
68
78
  return BitMask.unpack(this.collisionGroups, 16);
@@ -124,6 +134,7 @@ export class Rapier2dRigidBodyComponent {
124
134
  return col;
125
135
  });
126
136
  this.world.handleIdEntityMap.set(this._nativeBody.handle, this);
137
+ this.world.added$.next(this);
127
138
  }
128
139
  removeFromWorld(world) {
129
140
  if (world.physicsWorld != this.world) {
@@ -138,6 +149,7 @@ export class Rapier2dRigidBodyComponent {
138
149
  this._nativeBody = null;
139
150
  this._nativeBodyColliders = null;
140
151
  }
152
+ this.world.removed$.next(this);
141
153
  }
142
154
  resetMotion() {
143
155
  this._nativeBody.setAngvel(0, false);
@@ -1,18 +1,20 @@
1
1
  import { Observable, Subject } from 'rxjs';
2
2
  import { ColliderDesc, RigidBodyDesc } from '@dimforge/rapier2d-compat';
3
3
  import { Rapier2dRigidBodyComponent } from './rapier-2d-rigid-body.component';
4
- import { Gg2dWorld, ITrigger2dComponent, VisualTypeDocRepo2D } from '@gg-web-engine/core';
4
+ import { DebugBody2DSettings, Gg2dWorld, ITrigger2dComponent, Shape2DDescriptor, VisualTypeDocRepo2D } from '@gg-web-engine/core';
5
5
  import { Rapier2dWorldComponent } from './rapier-2d-world.component';
6
6
  import { Rapier2dPhysicsTypeDocRepo } from '../types';
7
7
  export declare class Rapier2dTriggerComponent extends Rapier2dRigidBodyComponent implements ITrigger2dComponent<Rapier2dPhysicsTypeDocRepo> {
8
8
  protected readonly world: Rapier2dWorldComponent;
9
9
  protected _colliderDescr: ColliderDesc[];
10
+ readonly shape: Shape2DDescriptor;
10
11
  protected _bodyDescr: RigidBodyDesc;
11
12
  get onEntityEntered(): Observable<Rapier2dRigidBodyComponent>;
12
13
  get onEntityLeft(): Observable<Rapier2dRigidBodyComponent>;
13
14
  protected readonly onEnter$: Subject<Rapier2dRigidBodyComponent>;
14
15
  protected readonly onLeft$: Subject<Rapier2dRigidBodyComponent>;
15
- constructor(world: Rapier2dWorldComponent, _colliderDescr: ColliderDesc[], _bodyDescr: RigidBodyDesc);
16
+ get debugBodySettings(): DebugBody2DSettings;
17
+ constructor(world: Rapier2dWorldComponent, _colliderDescr: ColliderDesc[], shape: Shape2DDescriptor, _bodyDescr: RigidBodyDesc);
16
18
  addToWorld(world: Gg2dWorld<VisualTypeDocRepo2D, Rapier2dPhysicsTypeDocRepo>): void;
17
19
  checkOverlaps(): void;
18
20
  clone(): Rapier2dTriggerComponent;
@@ -1,10 +1,11 @@
1
1
  import { Subject } from 'rxjs';
2
2
  import { Rapier2dRigidBodyComponent } from './rapier-2d-rigid-body.component';
3
3
  export class Rapier2dTriggerComponent extends Rapier2dRigidBodyComponent {
4
- constructor(world, _colliderDescr, _bodyDescr) {
5
- super(world, _colliderDescr, _bodyDescr, null);
4
+ constructor(world, _colliderDescr, shape, _bodyDescr) {
5
+ super(world, _colliderDescr, shape, _bodyDescr, null);
6
6
  this.world = world;
7
7
  this._colliderDescr = _colliderDescr;
8
+ this.shape = shape;
8
9
  this._bodyDescr = _bodyDescr;
9
10
  this.onEnter$ = new Subject();
10
11
  this.onLeft$ = new Subject();
@@ -15,6 +16,9 @@ export class Rapier2dTriggerComponent extends Rapier2dRigidBodyComponent {
15
16
  get onEntityLeft() {
16
17
  return this.onLeft$.asObservable();
17
18
  }
19
+ get debugBodySettings() {
20
+ return { shape: this.shape, type: 'TRIGGER' };
21
+ }
18
22
  addToWorld(world) {
19
23
  if (world.physicsWorld != this.world) {
20
24
  throw new Error('Rapier2D bodies cannot be shared between different worlds');
@@ -22,6 +26,7 @@ export class Rapier2dTriggerComponent extends Rapier2dRigidBodyComponent {
22
26
  this._nativeBody = this.world.nativeWorld.createRigidBody(this._bodyDescr);
23
27
  this._nativeBodyColliders = this._colliderDescr.map(c => this.world.nativeWorld.createCollider(c, this._nativeBody));
24
28
  this.world.handleIdEntityMap.set(this._nativeBody.handle, this);
29
+ this.world.added$.next(this);
25
30
  }
26
31
  checkOverlaps() {
27
32
  this.world.eventQueue.drainCollisionEvents((h1, h2, started) => {
@@ -39,7 +44,7 @@ export class Rapier2dTriggerComponent extends Rapier2dRigidBodyComponent {
39
44
  });
40
45
  }
41
46
  clone() {
42
- const [colliderDescr, bd] = super.factoryProps;
43
- return new Rapier2dTriggerComponent(this.world, colliderDescr, bd);
47
+ const [colliderDescr, sd, bd] = super.factoryProps;
48
+ return new Rapier2dTriggerComponent(this.world, colliderDescr, sd, bd);
44
49
  }
45
50
  }
@@ -1,30 +1,29 @@
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 { EventQueue, World } from '@dimforge/rapier2d-compat';
3
3
  import { Rapier2dRigidBodyComponent } from './rapier-2d-rigid-body.component';
4
4
  import { Rapier2dFactory } from '../rapier-2d-factory';
5
5
  import { Rapier2dPhysicsTypeDocRepo } from '../types';
6
+ import { Subject } from 'rxjs';
6
7
  export declare class Rapier2dWorldComponent implements IPhysicsWorld2dComponent<Rapier2dPhysicsTypeDocRepo> {
7
8
  private _factory;
8
9
  get factory(): Rapier2dFactory;
10
+ readonly added$: Subject<Rapier2dRigidBodyComponent>;
11
+ readonly removed$: Subject<Rapier2dRigidBodyComponent>;
12
+ readonly children: Rapier2dRigidBodyComponent[];
9
13
  private readonly unitScale;
10
14
  private _gravity;
11
15
  get gravity(): Point2;
12
16
  set gravity(value: Point2);
13
- private _timeScale;
14
- get timeScale(): number;
15
- set timeScale(value: number);
16
- get physicsDebugViewActive(): boolean;
17
17
  protected _nativeWorld: World | null;
18
18
  get nativeWorld(): World;
19
19
  private _eventQueue;
20
20
  get eventQueue(): EventQueue;
21
21
  readonly handleIdEntityMap: Map<number, Rapier2dRigidBodyComponent>;
22
+ constructor();
22
23
  init(): Promise<void>;
23
24
  simulate(delta: number): void;
24
25
  protected lockedCollisionGroups: number[];
25
26
  registerCollisionGroup(): CollisionGroup;
26
27
  deregisterCollisionGroup(group: CollisionGroup): void;
27
- startDebugger(world: Gg2dWorld, drawer: IDebugPhysicsDrawer<Point2, number>): void;
28
- stopDebugger(): void;
29
28
  dispose(): void;
30
29
  }
@@ -7,19 +7,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { Pnt2, } from '@gg-web-engine/core';
10
+ import { Pnt2 } from '@gg-web-engine/core';
11
11
  import { EventQueue, init, Vector2, World } from '@dimforge/rapier2d-compat';
12
12
  import { Rapier2dFactory } from '../rapier-2d-factory';
13
+ import { Subject } from 'rxjs';
13
14
  export class Rapier2dWorldComponent {
14
15
  constructor() {
15
16
  this._factory = null;
17
+ this.added$ = new Subject();
18
+ this.removed$ = new Subject();
19
+ this.children = [];
16
20
  this.unitScale = 100; // TODO abstractize somehow, hardcoded now
17
21
  this._gravity = Pnt2.scalarMult({ x: 0, y: 9.82 }, this.unitScale);
18
- this._timeScale = 1;
19
22
  this._nativeWorld = null;
20
23
  this._eventQueue = null;
21
24
  this.handleIdEntityMap = new Map();
22
25
  this.lockedCollisionGroups = [];
26
+ this.added$.subscribe(c => this.children.push(c));
27
+ this.removed$.subscribe(c => this.children.splice(this.children.indexOf(c), 1));
23
28
  }
24
29
  get factory() {
25
30
  if (!this._factory) {
@@ -37,15 +42,6 @@ export class Rapier2dWorldComponent {
37
42
  this.nativeWorld.gravity.y = this._gravity.y;
38
43
  }
39
44
  }
40
- get timeScale() {
41
- return this._timeScale;
42
- }
43
- set timeScale(value) {
44
- this._timeScale = value;
45
- }
46
- get physicsDebugViewActive() {
47
- return false;
48
- }
49
45
  get nativeWorld() {
50
46
  if (!this._nativeWorld) {
51
47
  throw new Error('Rapier2d world not initialized');
@@ -68,7 +64,7 @@ export class Rapier2dWorldComponent {
68
64
  }
69
65
  simulate(delta) {
70
66
  var _a;
71
- this._nativeWorld.timestep = (this.timeScale * delta) / 1000;
67
+ this._nativeWorld.timestep = delta / 1000;
72
68
  (_a = this._nativeWorld) === null || _a === void 0 ? void 0 : _a.step(this.eventQueue);
73
69
  }
74
70
  registerCollisionGroup() {
@@ -83,14 +79,6 @@ export class Rapier2dWorldComponent {
83
79
  deregisterCollisionGroup(group) {
84
80
  this.lockedCollisionGroups = this.lockedCollisionGroups.filter(x => x !== group);
85
81
  }
86
- startDebugger(world, drawer) {
87
- // TODO
88
- throw new Error('rapier-2d DebugDrawer not implemented');
89
- }
90
- stopDebugger() {
91
- // TODO
92
- throw new Error('rapier-2d DebugDrawer not implemented');
93
- }
94
82
  dispose() {
95
83
  var _a;
96
84
  (_a = this._nativeWorld) === null || _a === void 0 ? void 0 : _a.free();
@@ -7,7 +7,7 @@ export class Rapier2dFactory {
7
7
  this.world = world;
8
8
  }
9
9
  createRigidBody(descriptor, transform) {
10
- return new Rapier2dRigidBodyComponent(this.world, this.createColliderDescr(descriptor.shape), this.createRigidBodyDescr(descriptor.body, transform), Object.assign({ friction: 0.5, restitution: 0.1, ownCollisionGroups: 'all', interactWithCollisionGroups: 'all' }, descriptor.body));
10
+ return new Rapier2dRigidBodyComponent(this.world, this.createColliderDescr(descriptor.shape), descriptor.shape, this.createRigidBodyDescr(descriptor.body, transform), Object.assign({ friction: 0.5, restitution: 0.1, ownCollisionGroups: 'all', interactWithCollisionGroups: 'all' }, descriptor.body));
11
11
  }
12
12
  createTrigger(descriptor, transform) {
13
13
  const colliderDescr = this.createColliderDescr(descriptor);
@@ -15,7 +15,7 @@ export class Rapier2dFactory {
15
15
  c.isSensor = true;
16
16
  c.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
17
17
  });
18
- return new Rapier2dTriggerComponent(this.world, colliderDescr, this.createRigidBodyDescr({ dynamic: false }, transform));
18
+ return new Rapier2dTriggerComponent(this.world, colliderDescr, descriptor, this.createRigidBodyDescr({ dynamic: false }, transform));
19
19
  }
20
20
  createColliderDescr(descriptor) {
21
21
  switch (descriptor.shape) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gg-web-engine/rapier2d",
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",
@@ -28,7 +28,7 @@
28
28
  "homepage": "https://github.com/AndyGura/gg-web-engine#readme",
29
29
  "devDependencies": {
30
30
  "@dimforge/rapier2d-compat": "0.0.0-98015b1-20240128",
31
- "@gg-web-engine/core": "0.0.38",
31
+ "@gg-web-engine/core": "0.0.39",
32
32
  "@types/jest": "^29.5.0",
33
33
  "jest": "^29.5.0",
34
34
  "jest-environment-jsdom": "^29.5.0",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "peerDependencies": {
41
41
  "@dimforge/rapier2d-compat": "0.0.0-98015b1-20240128",
42
- "@gg-web-engine/core": "0.0.38",
42
+ "@gg-web-engine/core": "0.0.39",
43
43
  "rxjs": "7.8.1"
44
44
  },
45
45
  "jest": {