@eva/plugin-matterjs 2.0.1-beta.27 → 2.0.1-beta.28

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.
@@ -10864,10 +10864,39 @@ exports.PhysicsType = void 0;
10864
10864
  PhysicsType["CIRCLE"] = "circle";
10865
10865
  PhysicsType["POLYGON"] = "polygon";
10866
10866
  })(exports.PhysicsType || (exports.PhysicsType = {}));
10867
+ /**
10868
+ * 物理组件
10869
+ *
10870
+ * Physics 组件为游戏对象提供 Matter.js 物理引擎支持。
10871
+ * 可以创建不同类型的刚体(矩形、圆形、多边形),
10872
+ * 并自动同步物理引擎的位置和旋转到游戏对象的 Transform。
10873
+ *
10874
+ * @example
10875
+ * ```typescript
10876
+ * const ball = new GameObject('ball');
10877
+ * ball.addComponent(new Physics({
10878
+ * type: PhysicsType.CIRCLE,
10879
+ * radius: 25,
10880
+ * bodyOptions: {
10881
+ * restitution: 0.8, // 弹性
10882
+ * density: 0.01 // 密度
10883
+ * }
10884
+ * }));
10885
+ * ```
10886
+ */
10867
10887
  class Physics extends eva_js.Component {
10888
+ /**
10889
+ * 初始化物理组件
10890
+ * @param params - 物理体参数
10891
+ */
10868
10892
  init(params) {
10869
10893
  this.bodyParams = params;
10870
10894
  }
10895
+ /**
10896
+ * 每帧更新物理状态
10897
+ *
10898
+ * 将物理引擎计算的位置和旋转同步到游戏对象的 Transform 组件。
10899
+ */
10871
10900
  update() {
10872
10901
  if (this.body && this.gameObject) {
10873
10902
  this.gameObject.transform.anchor.x = 0;
@@ -10879,10 +10908,16 @@ class Physics extends eva_js.Component {
10879
10908
  }
10880
10909
  }
10881
10910
  }
10911
+ /**
10912
+ * 组件销毁时调用
10913
+ *
10914
+ * 从物理世界中移除物理体。
10915
+ */
10882
10916
  onDestroy() {
10883
10917
  Matter$1.World.remove(this.PhysicsEngine.world, this.body, true);
10884
10918
  }
10885
10919
  }
10920
+ /** 组件名称 */
10886
10921
  Physics.componentName = 'Physics';
10887
10922
 
10888
10923
  class BodiesFactory {
@@ -10938,6 +10973,7 @@ class PhysicsEngine {
10938
10973
  this.options = options;
10939
10974
  this.runner = this.Runner.create({
10940
10975
  fps: this.options.fps || 60,
10976
+ // Eva.js设置fps30也可能导致deltaTime为16,导致经过matterjs采样后的一段时间的delta都是很低
10941
10977
  deltaSampleSize: this.options.deltaSampleSize || 1,
10942
10978
  });
10943
10979
  }
@@ -10968,6 +11004,7 @@ class PhysicsEngine {
10968
11004
  }
10969
11005
  update(e) {
10970
11006
  if (!this.options.isTest) {
11007
+ // @ts-ignore
10971
11008
  this.Runner.tick(this.runner, this.engine, e.currentTime);
10972
11009
  }
10973
11010
  }
@@ -11046,15 +11083,82 @@ class PhysicsEngine {
11046
11083
  }
11047
11084
  }
11048
11085
 
11086
+ /**
11087
+ * 物理系统(基于 Matter.js)
11088
+ *
11089
+ * PhysicsSystem 集成了 Matter.js 2D 物理引擎,为游戏提供完整的刚体物理模拟。
11090
+ * 它管理物理世界、物理引擎的运行,并自动同步物理引擎的状态到游戏对象。
11091
+ *
11092
+ * 主要功能:
11093
+ * - 集成 Matter.js 物理引擎
11094
+ * - 管理物理世界和物理体
11095
+ * - 自动同步物理状态到游戏对象
11096
+ * - 支持鼠标交互约束
11097
+ * - 可配置物理引擎参数和渲染调试
11098
+ *
11099
+ * @example
11100
+ * ```typescript
11101
+ * // 基础配置
11102
+ * game.addSystem(new PhysicsSystem({
11103
+ * resolution: 2,
11104
+ * fps: 60,
11105
+ * world: {
11106
+ * gravity: { x: 0, y: 1 } // 重力方向
11107
+ * }
11108
+ * }));
11109
+ *
11110
+ * // 开启调试渲染和鼠标交互
11111
+ * game.addSystem(new PhysicsSystem({
11112
+ * isTest: true, // 显示物理调试绘制
11113
+ * canvas: debugCanvas,
11114
+ * mouse: {
11115
+ * open: true // 启用鼠标拖拽物理体
11116
+ * },
11117
+ * world: {
11118
+ * gravity: { x: 0, y: 1 }
11119
+ * }
11120
+ * }));
11121
+ * ```
11122
+ */
11049
11123
  let PhysicsSystem = class PhysicsSystem extends eva_js.System {
11124
+ /**
11125
+ * 初始化物理系统
11126
+ *
11127
+ * 配置物理引擎参数、创建物理世界、设置渲染分辨率等。
11128
+ *
11129
+ * @param param - 物理系统配置参数
11130
+ * @param param.resolution - 渲染分辨率,默认 1
11131
+ * @param param.fps - 物理引擎更新帧率,默认 60
11132
+ * @param param.isTest - 是否开启调试渲染模式
11133
+ * @param param.element - 物理调试渲染的容器元素
11134
+ * @param param.canvas - 物理调试渲染的画布
11135
+ * @param param.deltaSampleSize - 时间步长采样大小
11136
+ * @param param.mouse - 鼠标交互配置
11137
+ * @param param.world - Matter.js 世界配置(重力、边界等)
11138
+ */
11050
11139
  init(param) {
11051
11140
  this.engine = new PhysicsEngine(this.game, param);
11052
11141
  this.game.canvas.setAttribute('data-pixel-ratio', (param.resolution || '1'));
11053
11142
  }
11143
+ /**
11144
+ * System 被安装的时候,如果游戏还没有开始,那么会在游戏开始的时候调用。用于前置操作,初始化数据等。
11145
+ *
11146
+ * Called while the System installed, if game is not begain, it will be called while begain. use to pre operation, init data.
11147
+ */
11054
11148
  awake() { }
11149
+ /**
11150
+ * System 被安装后,所有的 awake 执行完后
11151
+ *
11152
+ * Called while the System installed, after all of systems' awake been called
11153
+ */
11055
11154
  start() {
11056
11155
  this.engine.start();
11057
11156
  }
11157
+ /**
11158
+ * 每一次游戏循环调用,可以做一些游戏操作,控制改变一些组件属性。
11159
+ *
11160
+ * Called by every loop, can do some operation, change some property or other component property.
11161
+ */
11058
11162
  update(e) {
11059
11163
  const changes = this.componentObserver.clear();
11060
11164
  for (const changed of changes) {
@@ -11099,17 +11203,37 @@ let PhysicsSystem = class PhysicsSystem extends eva_js.System {
11099
11203
  }
11100
11204
  }
11101
11205
  }
11206
+ /**
11207
+ * 和 update?() 类似,在所有System和组件的 update?() 执行以后调用。
11208
+ *
11209
+ * Like update, called all of gameobject update.
11210
+ */
11102
11211
  lateUpdate() { }
11212
+ /**
11213
+ * 游戏开始和游戏暂停后开始播放的时候调用。
11214
+ *
11215
+ * Called while the game to play when game pause.
11216
+ */
11103
11217
  onResume() {
11104
11218
  if (!this.engine.enabled) {
11105
11219
  this.engine.awake();
11106
11220
  }
11107
11221
  }
11222
+ /**
11223
+ * 游戏暂停的时候调用。
11224
+ *
11225
+ * Called while the game paused.
11226
+ */
11108
11227
  onPause() {
11109
11228
  this.engine.stop();
11110
11229
  }
11230
+ /**
11231
+ * System 被销毁的时候调用。
11232
+ * Called while the system be destroyed.
11233
+ */
11111
11234
  onDestroy() { }
11112
11235
  };
11236
+ /** 系统名称 */
11113
11237
  PhysicsSystem.systemName = 'PhysicsSystem';
11114
11238
  PhysicsSystem = __decorate([
11115
11239
  eva_js.decorators.componentObserver({
@@ -6,13 +6,51 @@ declare type DeepPartial<T> = {
6
6
  [P in keyof T]?: T[P] extends Object ? DeepPartial<T[P]> : T[P];
7
7
  };
8
8
 
9
+ /**
10
+ * 物理组件
11
+ *
12
+ * Physics 组件为游戏对象提供 Matter.js 物理引擎支持。
13
+ * 可以创建不同类型的刚体(矩形、圆形、多边形),
14
+ * 并自动同步物理引擎的位置和旋转到游戏对象的 Transform。
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const ball = new GameObject('ball');
19
+ * ball.addComponent(new Physics({
20
+ * type: PhysicsType.CIRCLE,
21
+ * radius: 25,
22
+ * bodyOptions: {
23
+ * restitution: 0.8, // 弹性
24
+ * density: 0.01 // 密度
25
+ * }
26
+ * }));
27
+ * ```
28
+ */
9
29
  export declare class Physics extends Component<PhysicsParams> {
30
+ /** 组件名称 */
10
31
  static componentName: string;
32
+ /** 物理体参数配置 */
11
33
  bodyParams: PhysicsParams;
34
+ /** Matter.js 物理体实例 */
12
35
  body: Matter.Body;
36
+ /** Matter.js 物理引擎实例 */
13
37
  private PhysicsEngine;
38
+ /**
39
+ * 初始化物理组件
40
+ * @param params - 物理体参数
41
+ */
14
42
  init(params: PhysicsParams): void;
43
+ /**
44
+ * 每帧更新物理状态
45
+ *
46
+ * 将物理引擎计算的位置和旋转同步到游戏对象的 Transform 组件。
47
+ */
15
48
  update(): void;
49
+ /**
50
+ * 组件销毁时调用
51
+ *
52
+ * 从物理世界中移除物理体。
53
+ */
16
54
  onDestroy(): void;
17
55
  }
18
56
 
@@ -33,17 +71,105 @@ declare interface PhysicsParams {
33
71
  stopRotation?: boolean;
34
72
  }
35
73
 
74
+ /**
75
+ * 物理系统(基于 Matter.js)
76
+ *
77
+ * PhysicsSystem 集成了 Matter.js 2D 物理引擎,为游戏提供完整的刚体物理模拟。
78
+ * 它管理物理世界、物理引擎的运行,并自动同步物理引擎的状态到游戏对象。
79
+ *
80
+ * 主要功能:
81
+ * - 集成 Matter.js 物理引擎
82
+ * - 管理物理世界和物理体
83
+ * - 自动同步物理状态到游戏对象
84
+ * - 支持鼠标交互约束
85
+ * - 可配置物理引擎参数和渲染调试
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * // 基础配置
90
+ * game.addSystem(new PhysicsSystem({
91
+ * resolution: 2,
92
+ * fps: 60,
93
+ * world: {
94
+ * gravity: { x: 0, y: 1 } // 重力方向
95
+ * }
96
+ * }));
97
+ *
98
+ * // 开启调试渲染和鼠标交互
99
+ * game.addSystem(new PhysicsSystem({
100
+ * isTest: true, // 显示物理调试绘制
101
+ * canvas: debugCanvas,
102
+ * mouse: {
103
+ * open: true // 启用鼠标拖拽物理体
104
+ * },
105
+ * world: {
106
+ * gravity: { x: 0, y: 1 }
107
+ * }
108
+ * }));
109
+ * ```
110
+ */
36
111
  export declare class PhysicsSystem extends System<PhysicsSystemParams> {
112
+ /** 系统名称 */
37
113
  static systemName: string;
114
+ /** 物理引擎实例 */
38
115
  private engine;
116
+ /**
117
+ * 初始化物理系统
118
+ *
119
+ * 配置物理引擎参数、创建物理世界、设置渲染分辨率等。
120
+ *
121
+ * @param param - 物理系统配置参数
122
+ * @param param.resolution - 渲染分辨率,默认 1
123
+ * @param param.fps - 物理引擎更新帧率,默认 60
124
+ * @param param.isTest - 是否开启调试渲染模式
125
+ * @param param.element - 物理调试渲染的容器元素
126
+ * @param param.canvas - 物理调试渲染的画布
127
+ * @param param.deltaSampleSize - 时间步长采样大小
128
+ * @param param.mouse - 鼠标交互配置
129
+ * @param param.world - Matter.js 世界配置(重力、边界等)
130
+ */
39
131
  init(param?: PhysicsSystemParams): void;
132
+ /**
133
+ * System 被安装的时候,如果游戏还没有开始,那么会在游戏开始的时候调用。用于前置操作,初始化数据等。
134
+ *
135
+ * Called while the System installed, if game is not begain, it will be called while begain. use to pre operation, init data.
136
+ */
40
137
  awake(): void;
138
+ /**
139
+ * System 被安装后,所有的 awake 执行完后
140
+ *
141
+ * Called while the System installed, after all of systems' awake been called
142
+ */
41
143
  start(): void;
144
+ /**
145
+ * 每一次游戏循环调用,可以做一些游戏操作,控制改变一些组件属性。
146
+ *
147
+ * Called by every loop, can do some operation, change some property or other component property.
148
+ */
42
149
  update(e: any): void;
43
150
  componentChanged(changed: ComponentChanged): void;
151
+ /**
152
+ * 和 update?() 类似,在所有System和组件的 update?() 执行以后调用。
153
+ *
154
+ * Like update, called all of gameobject update.
155
+ */
44
156
  lateUpdate(): void;
157
+ /**
158
+ * 游戏开始和游戏暂停后开始播放的时候调用。
159
+ *
160
+ * Called while the game to play when game pause.
161
+ */
45
162
  onResume(): void;
163
+ /**
164
+ * 游戏暂停的时候调用。
165
+ *
166
+ * Called while the game paused.
167
+ */
46
168
  onPause(): void;
169
+ /**
170
+ * System 被销毁的时候调用。
171
+ * Called while the system be destroyed.
172
+ */
47
173
  onDestroy(): void;
48
174
  }
49
175
 
@@ -10860,10 +10860,39 @@ var PhysicsType;
10860
10860
  PhysicsType["CIRCLE"] = "circle";
10861
10861
  PhysicsType["POLYGON"] = "polygon";
10862
10862
  })(PhysicsType || (PhysicsType = {}));
10863
+ /**
10864
+ * 物理组件
10865
+ *
10866
+ * Physics 组件为游戏对象提供 Matter.js 物理引擎支持。
10867
+ * 可以创建不同类型的刚体(矩形、圆形、多边形),
10868
+ * 并自动同步物理引擎的位置和旋转到游戏对象的 Transform。
10869
+ *
10870
+ * @example
10871
+ * ```typescript
10872
+ * const ball = new GameObject('ball');
10873
+ * ball.addComponent(new Physics({
10874
+ * type: PhysicsType.CIRCLE,
10875
+ * radius: 25,
10876
+ * bodyOptions: {
10877
+ * restitution: 0.8, // 弹性
10878
+ * density: 0.01 // 密度
10879
+ * }
10880
+ * }));
10881
+ * ```
10882
+ */
10863
10883
  class Physics extends Component {
10884
+ /**
10885
+ * 初始化物理组件
10886
+ * @param params - 物理体参数
10887
+ */
10864
10888
  init(params) {
10865
10889
  this.bodyParams = params;
10866
10890
  }
10891
+ /**
10892
+ * 每帧更新物理状态
10893
+ *
10894
+ * 将物理引擎计算的位置和旋转同步到游戏对象的 Transform 组件。
10895
+ */
10867
10896
  update() {
10868
10897
  if (this.body && this.gameObject) {
10869
10898
  this.gameObject.transform.anchor.x = 0;
@@ -10875,10 +10904,16 @@ class Physics extends Component {
10875
10904
  }
10876
10905
  }
10877
10906
  }
10907
+ /**
10908
+ * 组件销毁时调用
10909
+ *
10910
+ * 从物理世界中移除物理体。
10911
+ */
10878
10912
  onDestroy() {
10879
10913
  Matter$1.World.remove(this.PhysicsEngine.world, this.body, true);
10880
10914
  }
10881
10915
  }
10916
+ /** 组件名称 */
10882
10917
  Physics.componentName = 'Physics';
10883
10918
 
10884
10919
  class BodiesFactory {
@@ -10934,6 +10969,7 @@ class PhysicsEngine {
10934
10969
  this.options = options;
10935
10970
  this.runner = this.Runner.create({
10936
10971
  fps: this.options.fps || 60,
10972
+ // Eva.js设置fps30也可能导致deltaTime为16,导致经过matterjs采样后的一段时间的delta都是很低
10937
10973
  deltaSampleSize: this.options.deltaSampleSize || 1,
10938
10974
  });
10939
10975
  }
@@ -10964,6 +11000,7 @@ class PhysicsEngine {
10964
11000
  }
10965
11001
  update(e) {
10966
11002
  if (!this.options.isTest) {
11003
+ // @ts-ignore
10967
11004
  this.Runner.tick(this.runner, this.engine, e.currentTime);
10968
11005
  }
10969
11006
  }
@@ -11042,15 +11079,82 @@ class PhysicsEngine {
11042
11079
  }
11043
11080
  }
11044
11081
 
11082
+ /**
11083
+ * 物理系统(基于 Matter.js)
11084
+ *
11085
+ * PhysicsSystem 集成了 Matter.js 2D 物理引擎,为游戏提供完整的刚体物理模拟。
11086
+ * 它管理物理世界、物理引擎的运行,并自动同步物理引擎的状态到游戏对象。
11087
+ *
11088
+ * 主要功能:
11089
+ * - 集成 Matter.js 物理引擎
11090
+ * - 管理物理世界和物理体
11091
+ * - 自动同步物理状态到游戏对象
11092
+ * - 支持鼠标交互约束
11093
+ * - 可配置物理引擎参数和渲染调试
11094
+ *
11095
+ * @example
11096
+ * ```typescript
11097
+ * // 基础配置
11098
+ * game.addSystem(new PhysicsSystem({
11099
+ * resolution: 2,
11100
+ * fps: 60,
11101
+ * world: {
11102
+ * gravity: { x: 0, y: 1 } // 重力方向
11103
+ * }
11104
+ * }));
11105
+ *
11106
+ * // 开启调试渲染和鼠标交互
11107
+ * game.addSystem(new PhysicsSystem({
11108
+ * isTest: true, // 显示物理调试绘制
11109
+ * canvas: debugCanvas,
11110
+ * mouse: {
11111
+ * open: true // 启用鼠标拖拽物理体
11112
+ * },
11113
+ * world: {
11114
+ * gravity: { x: 0, y: 1 }
11115
+ * }
11116
+ * }));
11117
+ * ```
11118
+ */
11045
11119
  let PhysicsSystem = class PhysicsSystem extends System {
11120
+ /**
11121
+ * 初始化物理系统
11122
+ *
11123
+ * 配置物理引擎参数、创建物理世界、设置渲染分辨率等。
11124
+ *
11125
+ * @param param - 物理系统配置参数
11126
+ * @param param.resolution - 渲染分辨率,默认 1
11127
+ * @param param.fps - 物理引擎更新帧率,默认 60
11128
+ * @param param.isTest - 是否开启调试渲染模式
11129
+ * @param param.element - 物理调试渲染的容器元素
11130
+ * @param param.canvas - 物理调试渲染的画布
11131
+ * @param param.deltaSampleSize - 时间步长采样大小
11132
+ * @param param.mouse - 鼠标交互配置
11133
+ * @param param.world - Matter.js 世界配置(重力、边界等)
11134
+ */
11046
11135
  init(param) {
11047
11136
  this.engine = new PhysicsEngine(this.game, param);
11048
11137
  this.game.canvas.setAttribute('data-pixel-ratio', (param.resolution || '1'));
11049
11138
  }
11139
+ /**
11140
+ * System 被安装的时候,如果游戏还没有开始,那么会在游戏开始的时候调用。用于前置操作,初始化数据等。
11141
+ *
11142
+ * Called while the System installed, if game is not begain, it will be called while begain. use to pre operation, init data.
11143
+ */
11050
11144
  awake() { }
11145
+ /**
11146
+ * System 被安装后,所有的 awake 执行完后
11147
+ *
11148
+ * Called while the System installed, after all of systems' awake been called
11149
+ */
11051
11150
  start() {
11052
11151
  this.engine.start();
11053
11152
  }
11153
+ /**
11154
+ * 每一次游戏循环调用,可以做一些游戏操作,控制改变一些组件属性。
11155
+ *
11156
+ * Called by every loop, can do some operation, change some property or other component property.
11157
+ */
11054
11158
  update(e) {
11055
11159
  const changes = this.componentObserver.clear();
11056
11160
  for (const changed of changes) {
@@ -11095,17 +11199,37 @@ let PhysicsSystem = class PhysicsSystem extends System {
11095
11199
  }
11096
11200
  }
11097
11201
  }
11202
+ /**
11203
+ * 和 update?() 类似,在所有System和组件的 update?() 执行以后调用。
11204
+ *
11205
+ * Like update, called all of gameobject update.
11206
+ */
11098
11207
  lateUpdate() { }
11208
+ /**
11209
+ * 游戏开始和游戏暂停后开始播放的时候调用。
11210
+ *
11211
+ * Called while the game to play when game pause.
11212
+ */
11099
11213
  onResume() {
11100
11214
  if (!this.engine.enabled) {
11101
11215
  this.engine.awake();
11102
11216
  }
11103
11217
  }
11218
+ /**
11219
+ * 游戏暂停的时候调用。
11220
+ *
11221
+ * Called while the game paused.
11222
+ */
11104
11223
  onPause() {
11105
11224
  this.engine.stop();
11106
11225
  }
11226
+ /**
11227
+ * System 被销毁的时候调用。
11228
+ * Called while the system be destroyed.
11229
+ */
11107
11230
  onDestroy() { }
11108
11231
  };
11232
+ /** 系统名称 */
11109
11233
  PhysicsSystem.systemName = 'PhysicsSystem';
11110
11234
  PhysicsSystem = __decorate([
11111
11235
  decorators.componentObserver({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eva/plugin-matterjs",
3
- "version": "2.0.1-beta.27",
3
+ "version": "2.0.1-beta.28",
4
4
  "description": "@eva/plugin-matterjs",
5
5
  "main": "index.js",
6
6
  "module": "dist/plugin-matterjs.esm.js",
@@ -18,7 +18,7 @@
18
18
  "license": "MIT",
19
19
  "homepage": "https://eva.js.org",
20
20
  "dependencies": {
21
- "@eva/eva.js": "2.0.1-beta.27",
21
+ "@eva/eva.js": "2.0.1-beta.28",
22
22
  "@types/matter-js": "^0.17.5",
23
23
  "poly-decomp": "^0.3.0"
24
24
  }