@combos-fun/engine 0.0.1
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/README.md +3 -0
- package/dist/engine.cjs.js +1682 -0
- package/dist/engine.cjs.js.map +1 -0
- package/dist/engine.cjs.prod.js +1 -0
- package/dist/engine.d.ts +825 -0
- package/dist/engine.esm.js +1664 -0
- package/dist/engine.esm.js.map +1 -0
- package/index.js +7 -0
- package/package.json +29 -0
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,825 @@
|
|
|
1
|
+
import EventEmitter from 'eventemitter3';
|
|
2
|
+
import { AbstractLoadStrategy, AudioLoadStrategy, ImageLoadStrategy, XhrResponseType, MediaElementLoadStrategy, VideoLoadStrategy, XhrLoadStrategy, Loader, Resource as Resource$1, ResourceType, ResourceState } from 'resource-loader';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Two dimensional vector
|
|
6
|
+
*/
|
|
7
|
+
interface Vector2 {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Two dimensional size
|
|
13
|
+
*/
|
|
14
|
+
interface Size2$1 {
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Radiation transformation martix
|
|
20
|
+
*
|
|
21
|
+
* {@link https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform-function/matrix() }
|
|
22
|
+
*/
|
|
23
|
+
interface TransformMatrix {
|
|
24
|
+
a: number;
|
|
25
|
+
b: number;
|
|
26
|
+
c: number;
|
|
27
|
+
d: number;
|
|
28
|
+
tx: number;
|
|
29
|
+
ty: number;
|
|
30
|
+
array?: number[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Transform propterty
|
|
34
|
+
*/
|
|
35
|
+
interface TransformParams extends ComponentParams {
|
|
36
|
+
position?: Vector2;
|
|
37
|
+
size?: Size2$1;
|
|
38
|
+
origin?: Vector2;
|
|
39
|
+
anchor?: Vector2;
|
|
40
|
+
scale?: Vector2;
|
|
41
|
+
skew?: Vector2;
|
|
42
|
+
rotation?: number;
|
|
43
|
+
}
|
|
44
|
+
/** Basic component for gameObject, See {@link TransformParams} */
|
|
45
|
+
declare class Transform extends Component<TransformParams> {
|
|
46
|
+
/**
|
|
47
|
+
* component's name
|
|
48
|
+
* @readonly
|
|
49
|
+
*/
|
|
50
|
+
static componentName: string;
|
|
51
|
+
readonly name: string;
|
|
52
|
+
private _parent;
|
|
53
|
+
/** Whether this transform in a scene object */
|
|
54
|
+
inScene: boolean;
|
|
55
|
+
/** World coordinate system transformation matrix */
|
|
56
|
+
worldTransform: TransformMatrix;
|
|
57
|
+
/** Child transform components */
|
|
58
|
+
children: Transform[];
|
|
59
|
+
/**
|
|
60
|
+
* Init component
|
|
61
|
+
* @param params - Transform init data
|
|
62
|
+
*/
|
|
63
|
+
init(params?: TransformParams): void;
|
|
64
|
+
position: Vector2;
|
|
65
|
+
size: Size2$1;
|
|
66
|
+
origin: Vector2;
|
|
67
|
+
anchor: Vector2;
|
|
68
|
+
scale: Vector2;
|
|
69
|
+
skew: Vector2;
|
|
70
|
+
rotation: number;
|
|
71
|
+
set parent(val: Transform);
|
|
72
|
+
/**
|
|
73
|
+
* Get parent of this component
|
|
74
|
+
*/
|
|
75
|
+
get parent(): Transform;
|
|
76
|
+
/**
|
|
77
|
+
* Add Child Transform
|
|
78
|
+
* @remarks
|
|
79
|
+
* If `child` is already a child of this component, `child` will removed to the last of children list
|
|
80
|
+
* If `child` is already a child of other component, `child` will removed from its parent first
|
|
81
|
+
* @param child - child gameObject's transform component
|
|
82
|
+
*/
|
|
83
|
+
addChild(child: Transform): void;
|
|
84
|
+
/**
|
|
85
|
+
* Remove child transform
|
|
86
|
+
* @param child - child gameObject's transform component
|
|
87
|
+
*/
|
|
88
|
+
removeChild(child: Transform): void;
|
|
89
|
+
/** Clear all child transform */
|
|
90
|
+
clearChildren(): void;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Scene is a gameObject container
|
|
95
|
+
*/
|
|
96
|
+
declare class Scene extends GameObject {
|
|
97
|
+
gameObjects: GameObject[];
|
|
98
|
+
canvas: HTMLCanvasElement;
|
|
99
|
+
constructor(name: any, obj?: TransformParams);
|
|
100
|
+
/**
|
|
101
|
+
* Add gameObject
|
|
102
|
+
* @param gameObject - game object
|
|
103
|
+
*/
|
|
104
|
+
addGameObject(gameObject: GameObject): void;
|
|
105
|
+
/**
|
|
106
|
+
* Remove gameObject
|
|
107
|
+
* @param gameObject - game object
|
|
108
|
+
*/
|
|
109
|
+
removeGameObject(gameObject: GameObject): void;
|
|
110
|
+
/**
|
|
111
|
+
* Destroy scene
|
|
112
|
+
*/
|
|
113
|
+
destroy(): void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* GameObject is a general purpose object. It consists of a unique id and components.
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
declare class GameObject {
|
|
121
|
+
/** Name of this gameObject */
|
|
122
|
+
private _name;
|
|
123
|
+
/** Scene is an abstraction, represent a canvas layer */
|
|
124
|
+
private _scene;
|
|
125
|
+
/** A key-value map for components on this gameObject */
|
|
126
|
+
private _componentCache;
|
|
127
|
+
/** Identifier of this gameObject */
|
|
128
|
+
id: number;
|
|
129
|
+
/** Components apply to this gameObject */
|
|
130
|
+
components: Component<ComponentParams>[];
|
|
131
|
+
/** GameObject has been destroyed */
|
|
132
|
+
destroyed: boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Consruct a new gameObject
|
|
135
|
+
* @param name - the name of this gameObject
|
|
136
|
+
* @param obj - optional transform parameters for default Transform component
|
|
137
|
+
*/
|
|
138
|
+
constructor(name: string, obj?: TransformParams);
|
|
139
|
+
/**
|
|
140
|
+
* Get default transform component
|
|
141
|
+
* @returns transform component on this gameObject
|
|
142
|
+
* @readonly
|
|
143
|
+
*/
|
|
144
|
+
get transform(): Transform;
|
|
145
|
+
/**
|
|
146
|
+
* Get parent gameObject
|
|
147
|
+
* @returns parent gameObject
|
|
148
|
+
* @readonly
|
|
149
|
+
*/
|
|
150
|
+
get parent(): GameObject;
|
|
151
|
+
/**
|
|
152
|
+
* Get the name of this gameObject
|
|
153
|
+
* @readonly
|
|
154
|
+
*/
|
|
155
|
+
get name(): string;
|
|
156
|
+
set scene(val: Scene);
|
|
157
|
+
/**
|
|
158
|
+
* Get the scene which this gameObject added on
|
|
159
|
+
* @returns scene
|
|
160
|
+
* @readonly
|
|
161
|
+
*/
|
|
162
|
+
get scene(): Scene;
|
|
163
|
+
/**
|
|
164
|
+
* Add child gameObject
|
|
165
|
+
* @param gameObject - child gameobject
|
|
166
|
+
*/
|
|
167
|
+
addChild(gameObject: GameObject): void;
|
|
168
|
+
/**
|
|
169
|
+
* Remove child gameObject
|
|
170
|
+
* @param gameObject - child gameobject
|
|
171
|
+
*/
|
|
172
|
+
removeChild(gameObject: GameObject): GameObject;
|
|
173
|
+
/**
|
|
174
|
+
* Add component to this gameObject
|
|
175
|
+
* @remarks
|
|
176
|
+
* If component has already been added on a gameObject, it will throw an error
|
|
177
|
+
* @param C - component instance or Component class
|
|
178
|
+
*/
|
|
179
|
+
addComponent<T extends Component<ComponentParams>>(C: T): T;
|
|
180
|
+
addComponent<T extends Component<ComponentParams>>(C: ComponentConstructor<T>, obj?: ComponentParams): T;
|
|
181
|
+
/**
|
|
182
|
+
* Remove component on this gameObject
|
|
183
|
+
* @remarks
|
|
184
|
+
* default Transform component can not be removed, if the paramter represent a transform component, an error will be thrown.
|
|
185
|
+
* @param c - one of the compnoentName, component instance, component Class
|
|
186
|
+
* @returns
|
|
187
|
+
*/
|
|
188
|
+
removeComponent<T extends Component<ComponentParams>>(c: string): T;
|
|
189
|
+
removeComponent<T extends Component<ComponentParams>>(c: T): T;
|
|
190
|
+
removeComponent<T extends Component<ComponentParams>>(c: ComponentConstructor<T>): T;
|
|
191
|
+
private _removeComponent;
|
|
192
|
+
/**
|
|
193
|
+
* Get component on this gameObject
|
|
194
|
+
* @param c - one of the compnoentName, component instance, component Class
|
|
195
|
+
* @returns
|
|
196
|
+
*/
|
|
197
|
+
getComponent<T extends Component<ComponentParams>>(c: ComponentConstructor<T>): T;
|
|
198
|
+
getComponent<T extends Component>(c: string): T;
|
|
199
|
+
/**
|
|
200
|
+
* Remove this gameObject on its parent
|
|
201
|
+
* @returns return this gameObject
|
|
202
|
+
*/
|
|
203
|
+
remove(): GameObject;
|
|
204
|
+
/** Destory this gameObject */
|
|
205
|
+
destroy(): void;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** frame info pass to `Component.update` method */
|
|
209
|
+
interface UpdateParams {
|
|
210
|
+
/** delta time from last frame */
|
|
211
|
+
deltaTime: number;
|
|
212
|
+
/** frame count since game begining */
|
|
213
|
+
frameCount: number;
|
|
214
|
+
/** current timestamp */
|
|
215
|
+
time: number;
|
|
216
|
+
/** current timestamp */
|
|
217
|
+
currentTime: number;
|
|
218
|
+
/** fps at current frame */
|
|
219
|
+
fps: number;
|
|
220
|
+
}
|
|
221
|
+
interface ComponentParams {
|
|
222
|
+
}
|
|
223
|
+
interface ComponentConstructor<T extends Component<ComponentParams>> {
|
|
224
|
+
componentName: string;
|
|
225
|
+
new (params?: ComponentParams): T;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Component contain raw data apply to gameObject and how it interacts with the world
|
|
229
|
+
* @public
|
|
230
|
+
*/
|
|
231
|
+
declare class Component<T extends ComponentParams = {}> extends EventEmitter {
|
|
232
|
+
/** Name of this component */
|
|
233
|
+
static componentName: string;
|
|
234
|
+
/** Name of this component */
|
|
235
|
+
readonly name: string;
|
|
236
|
+
/**
|
|
237
|
+
* Represents the status of the component, If component has started, the value is true
|
|
238
|
+
* @defaultValue false
|
|
239
|
+
*/
|
|
240
|
+
started: boolean;
|
|
241
|
+
/**
|
|
242
|
+
* gameObject which this component had added on
|
|
243
|
+
* @remarks
|
|
244
|
+
* Component can only be added on one gameObject, otherwise an error will be thrown,
|
|
245
|
+
* Component can only be attached to one game object at a time.
|
|
246
|
+
*/
|
|
247
|
+
gameObject: GameObject;
|
|
248
|
+
/** Default paramaters for this component */
|
|
249
|
+
__componentDefaultParams: T;
|
|
250
|
+
constructor(params?: T);
|
|
251
|
+
/**
|
|
252
|
+
* Called during component construction
|
|
253
|
+
* @param params - optional initial parameters
|
|
254
|
+
* @override
|
|
255
|
+
*/
|
|
256
|
+
init?(params?: T): void;
|
|
257
|
+
/**
|
|
258
|
+
* Called when component is added to a gameObject
|
|
259
|
+
* @override
|
|
260
|
+
*/
|
|
261
|
+
awake?(): void;
|
|
262
|
+
/**
|
|
263
|
+
* Called after all component's `awake` method has been called
|
|
264
|
+
* @override
|
|
265
|
+
*/
|
|
266
|
+
start?(): void;
|
|
267
|
+
/**
|
|
268
|
+
* Called in every tick, change self property or other component property
|
|
269
|
+
* @param frame - frame info about this tick
|
|
270
|
+
* @override
|
|
271
|
+
*/
|
|
272
|
+
update?(frame: UpdateParams): void;
|
|
273
|
+
/**
|
|
274
|
+
* Called after all gameObject's `update` method has been called
|
|
275
|
+
* @param frame - frame info about this tick
|
|
276
|
+
* @override
|
|
277
|
+
*/
|
|
278
|
+
lateUpdate?(frame: UpdateParams): void;
|
|
279
|
+
/**
|
|
280
|
+
* Called before game runing or every time game paused
|
|
281
|
+
* @virtual
|
|
282
|
+
* @override
|
|
283
|
+
*/
|
|
284
|
+
onResume?(): void;
|
|
285
|
+
/**
|
|
286
|
+
* Called while the game paused.
|
|
287
|
+
* @override
|
|
288
|
+
*/
|
|
289
|
+
onPause?(): void;
|
|
290
|
+
/**
|
|
291
|
+
* Called while component be destroyed.
|
|
292
|
+
* @override
|
|
293
|
+
*/
|
|
294
|
+
onDestroy?(): void;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
interface ObserverEventParams {
|
|
298
|
+
type: ObserverType;
|
|
299
|
+
component: Component;
|
|
300
|
+
componentName: string;
|
|
301
|
+
prop?: PureObserverProp;
|
|
302
|
+
}
|
|
303
|
+
interface ObserverEvent extends ObserverEventParams {
|
|
304
|
+
gameObject?: GameObject;
|
|
305
|
+
systemName?: string;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Management observe events
|
|
309
|
+
* @remarks
|
|
310
|
+
* See {@link System} for more details
|
|
311
|
+
* @public
|
|
312
|
+
*/
|
|
313
|
+
declare class ComponentObserver {
|
|
314
|
+
/**
|
|
315
|
+
* Component property change events
|
|
316
|
+
* @defaultValue []
|
|
317
|
+
*/
|
|
318
|
+
private events;
|
|
319
|
+
/**
|
|
320
|
+
* Add event
|
|
321
|
+
* @remarks
|
|
322
|
+
* The same event will be placed last
|
|
323
|
+
* @param component - changed component
|
|
324
|
+
* @param prop - changed property on `component`
|
|
325
|
+
* @param type - change event type
|
|
326
|
+
* @param componentName - `component.name` this parameter will deprecated
|
|
327
|
+
*/
|
|
328
|
+
add({ component, prop, type, componentName }: ObserverEventParams): void;
|
|
329
|
+
/** Return change events */
|
|
330
|
+
getChanged(): ObserverEvent[];
|
|
331
|
+
/**
|
|
332
|
+
* Return change events
|
|
333
|
+
* @readonly
|
|
334
|
+
*/
|
|
335
|
+
get changed(): ObserverEvent[];
|
|
336
|
+
/** Clear events */
|
|
337
|
+
clear(): ObserverEvent[];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
interface TickerOptions {
|
|
341
|
+
autoStart?: boolean;
|
|
342
|
+
frameRate?: number;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Timeline tool
|
|
346
|
+
*/
|
|
347
|
+
declare class Ticker {
|
|
348
|
+
/** Whether or not ticker should auto start */
|
|
349
|
+
autoStart: boolean;
|
|
350
|
+
/** FPS, The number of times that raf method is called per second */
|
|
351
|
+
frameRate: number;
|
|
352
|
+
/** Global Timeline **/
|
|
353
|
+
private timeline;
|
|
354
|
+
/** Time between two frame */
|
|
355
|
+
private _frameDuration;
|
|
356
|
+
/** Ticker is a function will called in each raf */
|
|
357
|
+
private _tickers;
|
|
358
|
+
/** raf handle id */
|
|
359
|
+
_requestId: number;
|
|
360
|
+
/** Last frame render time */
|
|
361
|
+
private _lastFrameTime;
|
|
362
|
+
/** Frame count since from ticker beigning */
|
|
363
|
+
private _frameCount;
|
|
364
|
+
/** Main ticker method handle */
|
|
365
|
+
private _ticker;
|
|
366
|
+
/** Represents the status of the Ticker, If ticker has started, the value is true */
|
|
367
|
+
private _started;
|
|
368
|
+
/**
|
|
369
|
+
* @param autoStart - auto start game
|
|
370
|
+
* @param frameRate - game frame rate
|
|
371
|
+
*/
|
|
372
|
+
constructor(options?: TickerOptions);
|
|
373
|
+
/** Main loop, all _tickers will called in this method */
|
|
374
|
+
update(): void;
|
|
375
|
+
/** Add ticker function */
|
|
376
|
+
add(fn: (params: UpdateParams) => void): void;
|
|
377
|
+
/** Remove ticker function */
|
|
378
|
+
remove(fn: (params: UpdateParams) => void): void;
|
|
379
|
+
/** Start main loop */
|
|
380
|
+
start(): void;
|
|
381
|
+
/** Pause main loop */
|
|
382
|
+
pause(): void;
|
|
383
|
+
setPlaybackRate(rate: number): void;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** Plugin registration shape */
|
|
387
|
+
interface PluginStruct {
|
|
388
|
+
Components?: typeof Component[];
|
|
389
|
+
Systems?: typeof System[];
|
|
390
|
+
}
|
|
391
|
+
interface GameParams {
|
|
392
|
+
/** isn't game will auto start */
|
|
393
|
+
autoStart?: boolean;
|
|
394
|
+
/** fps for this game */
|
|
395
|
+
frameRate?: number;
|
|
396
|
+
/** systems in this game */
|
|
397
|
+
systems?: System[];
|
|
398
|
+
/** whether or not need to create scene */
|
|
399
|
+
needScene?: boolean;
|
|
400
|
+
/**
|
|
401
|
+
* Called after async system bootstrap finishes (constructor path with non-empty `systems`),
|
|
402
|
+
* i.e. after all `addSystem`/`init` work and optional `loadScene` / `start`.
|
|
403
|
+
* Second argument is set if bootstrap threw (e.g. async `init` rejection).
|
|
404
|
+
*/
|
|
405
|
+
onSystemsBootstrapComplete?: (game: Game, error?: unknown) => void;
|
|
406
|
+
}
|
|
407
|
+
declare enum LOAD_SCENE_MODE {
|
|
408
|
+
SINGLE = "SINGLE",
|
|
409
|
+
MULTI_CANVAS = "MULTI_CANVAS"
|
|
410
|
+
}
|
|
411
|
+
interface LoadSceneParams {
|
|
412
|
+
scene: Scene;
|
|
413
|
+
mode?: LOAD_SCENE_MODE;
|
|
414
|
+
params?: {
|
|
415
|
+
width?: number;
|
|
416
|
+
height?: number;
|
|
417
|
+
canvas?: HTMLCanvasElement;
|
|
418
|
+
renderType?: number;
|
|
419
|
+
autoStart?: boolean;
|
|
420
|
+
sharedTicker?: boolean;
|
|
421
|
+
sharedLoader?: boolean;
|
|
422
|
+
transparent?: boolean;
|
|
423
|
+
antialias?: boolean;
|
|
424
|
+
preserveDrawingBuffer?: boolean;
|
|
425
|
+
resolution?: number;
|
|
426
|
+
backgroundColor?: number;
|
|
427
|
+
clearBeforeRender?: boolean;
|
|
428
|
+
roundPixels?: boolean;
|
|
429
|
+
forceFXAA?: boolean;
|
|
430
|
+
legacy?: boolean;
|
|
431
|
+
autoResize?: boolean;
|
|
432
|
+
powerPreference?: "high-performance";
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
declare class Game extends EventEmitter {
|
|
436
|
+
_scene: Scene;
|
|
437
|
+
canvas: HTMLCanvasElement;
|
|
438
|
+
/**
|
|
439
|
+
* State of game
|
|
440
|
+
* @defaultValue false
|
|
441
|
+
*/
|
|
442
|
+
playing: boolean;
|
|
443
|
+
started: boolean;
|
|
444
|
+
multiScenes: Scene[];
|
|
445
|
+
/**
|
|
446
|
+
* Ticker
|
|
447
|
+
*/
|
|
448
|
+
ticker: Ticker;
|
|
449
|
+
/** Systems alled to this game */
|
|
450
|
+
systems: System[];
|
|
451
|
+
constructor({ systems, frameRate, autoStart, needScene, onSystemsBootstrapComplete, }?: GameParams);
|
|
452
|
+
/** When `systems` is passed in the constructor, run async `init` for each before `loadScene` / `start`. */
|
|
453
|
+
private bootstrapSystemsAndMaybeStart;
|
|
454
|
+
/**
|
|
455
|
+
* Get scene on this game
|
|
456
|
+
*/
|
|
457
|
+
get scene(): Scene;
|
|
458
|
+
set scene(scene: Scene);
|
|
459
|
+
get gameObjects(): any[];
|
|
460
|
+
addSystem<T extends System>(S: T): Promise<T | undefined>;
|
|
461
|
+
addSystem<T extends System>(S: SystemConstructor<T>, obj?: ConstructorParameters<SystemConstructor<T>>): Promise<T | undefined>;
|
|
462
|
+
/**
|
|
463
|
+
* Remove system from this game
|
|
464
|
+
* @param system - one of system instance / system Class or system name
|
|
465
|
+
*/
|
|
466
|
+
removeSystem<S extends System>(system: S | SystemConstructor<S> | string): void;
|
|
467
|
+
/**
|
|
468
|
+
* Get system
|
|
469
|
+
* @param S - system class or system name
|
|
470
|
+
* @returns system instance
|
|
471
|
+
*/
|
|
472
|
+
getSystem<T extends System>(S: SystemConstructor<T> | string): T;
|
|
473
|
+
/** Pause game */
|
|
474
|
+
pause(): void;
|
|
475
|
+
/** Start game */
|
|
476
|
+
start(): void;
|
|
477
|
+
/** Resume game */
|
|
478
|
+
resume(): void;
|
|
479
|
+
/**
|
|
480
|
+
* add main render method to ticker
|
|
481
|
+
* @remarks
|
|
482
|
+
* the method added to ticker will called in each requestAnimationFrame,
|
|
483
|
+
* 1. call update method on all gameObject
|
|
484
|
+
* 2. call lastUpdate method on all gameObject
|
|
485
|
+
* 3. call update method on all system
|
|
486
|
+
* 4. call lastUpdate method on all system
|
|
487
|
+
*/
|
|
488
|
+
initTicker(): void;
|
|
489
|
+
/** Call onResume method on all gameObject's, and then call onResume method on all system */
|
|
490
|
+
triggerResume(): void;
|
|
491
|
+
/** Call onPause method on all gameObject */
|
|
492
|
+
triggerPause(): void;
|
|
493
|
+
/** remove all system on this game */
|
|
494
|
+
destroySystems(): void;
|
|
495
|
+
/** Destroy game instance */
|
|
496
|
+
destroy(): void;
|
|
497
|
+
loadScene({ scene, mode, params }: LoadSceneParams): void;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
interface SystemConstructor<T extends System = System> {
|
|
501
|
+
systemName: string;
|
|
502
|
+
observerInfo: PureObserverInfo;
|
|
503
|
+
new (params?: any): T;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Each System runs continuously and performs global actions on every Entity that possesses a Component of the same aspect as that System.
|
|
507
|
+
* @public
|
|
508
|
+
*/
|
|
509
|
+
declare class System<T extends {} = {}> {
|
|
510
|
+
/** System name */
|
|
511
|
+
static systemName: string;
|
|
512
|
+
name: string;
|
|
513
|
+
/**
|
|
514
|
+
* The collection of component properties observed by the System. System will respond to these changes
|
|
515
|
+
* @example
|
|
516
|
+
* ```typescript
|
|
517
|
+
* // TestSystem will respond to changes of `size` and `position` property of the Transform component
|
|
518
|
+
* class TestSystem extends System {
|
|
519
|
+
* static observerInfo = {
|
|
520
|
+
* Transform: [{ prop: 'size', deep: true }, { prop: 'position', deep: true }]
|
|
521
|
+
* }
|
|
522
|
+
* }
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
static observerInfo: PureObserverInfo;
|
|
526
|
+
/** Component Observer */
|
|
527
|
+
componentObserver: ComponentObserver;
|
|
528
|
+
/** Game instance */
|
|
529
|
+
game: Game;
|
|
530
|
+
/** Represents the status of the component, if component has started, the value is true */
|
|
531
|
+
started: boolean;
|
|
532
|
+
/** Default paramaters for this system */
|
|
533
|
+
__systemDefaultParams: T;
|
|
534
|
+
constructor(params?: T);
|
|
535
|
+
/**
|
|
536
|
+
* Called when system is added to a gameObject
|
|
537
|
+
* @remarks
|
|
538
|
+
* The difference between init and awake is that `init` method recieves params.
|
|
539
|
+
* Both of those methods are called early than `start` method.
|
|
540
|
+
* Use this method to prepare data, ect.
|
|
541
|
+
* @param param - optional params
|
|
542
|
+
* @override
|
|
543
|
+
*/
|
|
544
|
+
init?(param?: T): void | Promise<void>;
|
|
545
|
+
/**
|
|
546
|
+
* Calleen system installed
|
|
547
|
+
* @override
|
|
548
|
+
*/
|
|
549
|
+
awake?(): void;
|
|
550
|
+
/**
|
|
551
|
+
* Called after all system `awake` method has been called
|
|
552
|
+
* @override
|
|
553
|
+
*/
|
|
554
|
+
start?(): void;
|
|
555
|
+
/**
|
|
556
|
+
* Called in each tick
|
|
557
|
+
* @example
|
|
558
|
+
* ```typescript
|
|
559
|
+
* // run TWEEN `update` method in main requestAnimationFrame loop
|
|
560
|
+
* class TransitionSystem extends System {
|
|
561
|
+
* update() {
|
|
562
|
+
* TWEEN.update()
|
|
563
|
+
* }
|
|
564
|
+
* }
|
|
565
|
+
* ```
|
|
566
|
+
* @param e - info about this tick
|
|
567
|
+
* @override
|
|
568
|
+
*/
|
|
569
|
+
update?(e: UpdateParams): void;
|
|
570
|
+
/**
|
|
571
|
+
* Called after all system have called the `update` method
|
|
572
|
+
* @param e - info about this tick
|
|
573
|
+
* @override
|
|
574
|
+
*/
|
|
575
|
+
lateUpdate?(e: UpdateParams): void;
|
|
576
|
+
/**
|
|
577
|
+
* Called before game runing or every time game paused
|
|
578
|
+
* @override
|
|
579
|
+
*/
|
|
580
|
+
onResume?(): void;
|
|
581
|
+
/**
|
|
582
|
+
* Called while the game paused
|
|
583
|
+
* @override
|
|
584
|
+
*/
|
|
585
|
+
onPause?(): void;
|
|
586
|
+
/**
|
|
587
|
+
* Called while the system be destroyed.
|
|
588
|
+
* @override
|
|
589
|
+
*/
|
|
590
|
+
onDestroy?(): void;
|
|
591
|
+
/** Default destory method */
|
|
592
|
+
destroy(): void;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** Observer event type */
|
|
596
|
+
declare enum ObserverType {
|
|
597
|
+
ADD = "ADD",
|
|
598
|
+
REMOVE = "REMOVE",
|
|
599
|
+
CHANGE = "CHANGE"
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Observer property
|
|
603
|
+
* @remarks
|
|
604
|
+
* If `deep` is true then all descendants of `prop` will be observed
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* @observerComponent({
|
|
608
|
+
* Transform: [{ prop: 'size', deep: true }]
|
|
609
|
+
* })
|
|
610
|
+
* class TestSystem extends System {}
|
|
611
|
+
* ```
|
|
612
|
+
*/
|
|
613
|
+
interface PureObserverProp {
|
|
614
|
+
deep: boolean;
|
|
615
|
+
prop: string[];
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Observer Info
|
|
619
|
+
* @remarks
|
|
620
|
+
* The key of this map always be component's name, the value of this map is an array of `PureObserverProp`
|
|
621
|
+
*/
|
|
622
|
+
type PureObserverInfo = Record<string, PureObserverProp[]>;
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Collect property which react in Editor tooling
|
|
626
|
+
* @param target - component instance
|
|
627
|
+
* @param propertyKey - property name
|
|
628
|
+
*/
|
|
629
|
+
declare function IDEProp(target: any, propertyKey: any): void;
|
|
630
|
+
|
|
631
|
+
type observableKeys = string | string[];
|
|
632
|
+
interface ObserverProp {
|
|
633
|
+
deep: boolean;
|
|
634
|
+
prop: observableKeys;
|
|
635
|
+
}
|
|
636
|
+
type ObserverValue = observableKeys | ObserverProp;
|
|
637
|
+
type ComponentName = string;
|
|
638
|
+
/**
|
|
639
|
+
* Normailized observer info
|
|
640
|
+
* @example
|
|
641
|
+
* ```typescript
|
|
642
|
+
* {
|
|
643
|
+
* Transform: [{ prop: ['size'], deep: false }],
|
|
644
|
+
* OtherComponent: [{ prop: ['style', 'transform'], deep: true }]
|
|
645
|
+
* }
|
|
646
|
+
* ```
|
|
647
|
+
*/
|
|
648
|
+
type ObserverInfo = Record<ComponentName, ObserverValue[]>;
|
|
649
|
+
/**
|
|
650
|
+
* Normailize system observer info
|
|
651
|
+
* @param obj - system observer info
|
|
652
|
+
*/
|
|
653
|
+
declare function componentObserver(observerInfo?: ObserverInfo): (constructor: any) => void;
|
|
654
|
+
|
|
655
|
+
interface EventParam {
|
|
656
|
+
name: string;
|
|
657
|
+
resource: ResourceStruct;
|
|
658
|
+
success: boolean;
|
|
659
|
+
errMsg?: string;
|
|
660
|
+
}
|
|
661
|
+
declare class Progress extends EventEmitter {
|
|
662
|
+
progress: number;
|
|
663
|
+
resourceTotal: number;
|
|
664
|
+
resourceLoadedCount: number;
|
|
665
|
+
resource: Resource;
|
|
666
|
+
constructor({ resource, resourceTotal }: {
|
|
667
|
+
resource: any;
|
|
668
|
+
resourceTotal: any;
|
|
669
|
+
});
|
|
670
|
+
onStart(): void;
|
|
671
|
+
onProgress(param: EventParam): void;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
declare const resourceLoader: {
|
|
675
|
+
AbstractLoadStrategy: typeof AbstractLoadStrategy;
|
|
676
|
+
AudioLoadStrategy: typeof AudioLoadStrategy;
|
|
677
|
+
ImageLoadStrategy: typeof ImageLoadStrategy;
|
|
678
|
+
XhrResponseType: typeof XhrResponseType;
|
|
679
|
+
MediaElementLoadStrategy: typeof MediaElementLoadStrategy;
|
|
680
|
+
VideoLoadStrategy: typeof VideoLoadStrategy;
|
|
681
|
+
XhrLoadStrategy: typeof XhrLoadStrategy;
|
|
682
|
+
Loader: typeof Loader;
|
|
683
|
+
Resource: typeof Resource$1;
|
|
684
|
+
ResourceType: typeof ResourceType;
|
|
685
|
+
ResourceState: typeof ResourceState;
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
/** Load lifecycle events (decoupled from Resource/Progress to avoid barrel import cycles). */
|
|
689
|
+
declare enum LOAD_EVENT {
|
|
690
|
+
'START' = "start",
|
|
691
|
+
'PROGRESS' = "progress",
|
|
692
|
+
'LOADED' = "loaded",
|
|
693
|
+
'COMPLETE' = "complete",
|
|
694
|
+
'ERROR' = "error"
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/** Resource type */
|
|
698
|
+
declare enum RESOURCE_TYPE {
|
|
699
|
+
'IMAGE' = "IMAGE",
|
|
700
|
+
'SPRITE' = "SPRITE",
|
|
701
|
+
'SPRITE_ANIMATION' = "SPRITE_ANIMATION",
|
|
702
|
+
'AUDIO' = "AUDIO",
|
|
703
|
+
'VIDEO' = "VIDEO"
|
|
704
|
+
}
|
|
705
|
+
/** Resource item */
|
|
706
|
+
interface SrcBase {
|
|
707
|
+
type: string;
|
|
708
|
+
url?: string;
|
|
709
|
+
data?: any;
|
|
710
|
+
size?: Size2;
|
|
711
|
+
texture?: TextureBase[] | TextureBase;
|
|
712
|
+
}
|
|
713
|
+
interface Size2 {
|
|
714
|
+
width: number;
|
|
715
|
+
height: number;
|
|
716
|
+
}
|
|
717
|
+
interface TextureBase {
|
|
718
|
+
type: string;
|
|
719
|
+
url: string;
|
|
720
|
+
size?: Size2;
|
|
721
|
+
}
|
|
722
|
+
/** Resource base */
|
|
723
|
+
interface ResourceBase {
|
|
724
|
+
name: string;
|
|
725
|
+
type: RESOURCE_TYPE;
|
|
726
|
+
src: {
|
|
727
|
+
json?: SrcBase;
|
|
728
|
+
image?: SrcBase;
|
|
729
|
+
tex?: SrcBase;
|
|
730
|
+
ske?: SrcBase;
|
|
731
|
+
video?: SrcBase;
|
|
732
|
+
audio?: SrcBase;
|
|
733
|
+
[propName: string]: SrcBase;
|
|
734
|
+
};
|
|
735
|
+
complete?: boolean;
|
|
736
|
+
preload?: boolean;
|
|
737
|
+
}
|
|
738
|
+
/** Resource with entity */
|
|
739
|
+
interface ResourceStruct extends ResourceBase {
|
|
740
|
+
data?: {
|
|
741
|
+
json?: any;
|
|
742
|
+
image?: HTMLImageElement;
|
|
743
|
+
tex?: any;
|
|
744
|
+
ske?: any;
|
|
745
|
+
video?: HTMLVideoElement;
|
|
746
|
+
audio?: ArrayBuffer;
|
|
747
|
+
[propName: string]: any;
|
|
748
|
+
};
|
|
749
|
+
instance?: any;
|
|
750
|
+
}
|
|
751
|
+
declare const RESOURCE_TYPE_STRATEGY: {
|
|
752
|
+
[type: string]: new (...args: any[]) => AbstractLoadStrategy;
|
|
753
|
+
};
|
|
754
|
+
type ResourceName = string;
|
|
755
|
+
type ResourceProcessFn = (resource: ResourceStruct) => any;
|
|
756
|
+
type PreProcessResourceHandler = (res: ResourceBase) => void;
|
|
757
|
+
/**
|
|
758
|
+
* Resource manager
|
|
759
|
+
* @public
|
|
760
|
+
*/
|
|
761
|
+
declare class Resource extends EventEmitter {
|
|
762
|
+
/** load resource timeout */
|
|
763
|
+
timeout: number;
|
|
764
|
+
private preProcessResourceHandlers;
|
|
765
|
+
/** Resource cache */
|
|
766
|
+
resourcesMap: Record<ResourceName, ResourceStruct>;
|
|
767
|
+
/** Collection of make resource instance function */
|
|
768
|
+
private makeInstanceFunctions;
|
|
769
|
+
/** Collection of destroy resource instance function */
|
|
770
|
+
private destroyInstanceFunctions;
|
|
771
|
+
/** Resource load promise */
|
|
772
|
+
private promiseMap;
|
|
773
|
+
private loaders;
|
|
774
|
+
progress: Progress;
|
|
775
|
+
constructor(options?: {
|
|
776
|
+
timeout: number;
|
|
777
|
+
});
|
|
778
|
+
/** Add resource configs and then preload */
|
|
779
|
+
loadConfig(resources: ResourceBase[]): void;
|
|
780
|
+
/** Add single resource config and then preload */
|
|
781
|
+
loadSingle(resource: ResourceBase): Promise<ResourceStruct>;
|
|
782
|
+
/** Add resource configs */
|
|
783
|
+
addResource(resources: ResourceBase[]): void;
|
|
784
|
+
/** dd resource preprocesser*/
|
|
785
|
+
addPreProcessResourceHandler(handler: PreProcessResourceHandler): void;
|
|
786
|
+
removePreProcessResourceHandler(handler: PreProcessResourceHandler): void;
|
|
787
|
+
/** Start preload */
|
|
788
|
+
preload(): void;
|
|
789
|
+
/** Get resource by name */
|
|
790
|
+
getResource(name: string): Promise<ResourceStruct>;
|
|
791
|
+
/** Make resource instance by resource type */
|
|
792
|
+
private instance;
|
|
793
|
+
/** destory this resource manager */
|
|
794
|
+
destroy(name: string): Promise<void>;
|
|
795
|
+
private _destroy;
|
|
796
|
+
/**
|
|
797
|
+
* Register a custom resource type string on {@link RESOURCE_TYPE}.
|
|
798
|
+
* For TypeScript, extend `RESOURCE_TYPE` via `declare module "@combos-fun/engine"` in your app’s ambient `.d.ts`.
|
|
799
|
+
* Call this before {@link registerInstance} / {@link registerDestroy} for that type.
|
|
800
|
+
*/
|
|
801
|
+
registerResourceType(type: string, value?: string): void;
|
|
802
|
+
/** Add resource instance function */
|
|
803
|
+
registerInstance(type: RESOURCE_TYPE | string, callback: ResourceProcessFn): void;
|
|
804
|
+
/** Add resource destroy function */
|
|
805
|
+
registerDestroy(type: RESOURCE_TYPE | string, callback: ResourceProcessFn): void;
|
|
806
|
+
private loadResource;
|
|
807
|
+
doComplete(name: any, resolve: any, preload?: boolean): Promise<void>;
|
|
808
|
+
checkAllLoaded(name: any): boolean;
|
|
809
|
+
getLoader(preload?: boolean): Loader;
|
|
810
|
+
private onLoad;
|
|
811
|
+
private onError;
|
|
812
|
+
}
|
|
813
|
+
/** Resource manager single instance */
|
|
814
|
+
declare const resource: Resource;
|
|
815
|
+
|
|
816
|
+
/** Decorators util */
|
|
817
|
+
declare const decorators: {
|
|
818
|
+
IDEProp: typeof IDEProp;
|
|
819
|
+
componentObserver: typeof componentObserver;
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
declare const version = "__VERSION__";
|
|
823
|
+
|
|
824
|
+
export { Component, Game, GameObject, IDEProp, LOAD_EVENT, LOAD_SCENE_MODE, ObserverType as OBSERVER_TYPE, RESOURCE_TYPE, RESOURCE_TYPE_STRATEGY, Scene, System, Transform, componentObserver, decorators, resource, resourceLoader, version };
|
|
825
|
+
export type { ObserverEvent as ComponentChanged, ComponentParams, GameParams, ObserverInfo, PluginStruct, PureObserverInfo, ResourceBase, SystemConstructor, TransformParams, UpdateParams };
|