-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMingine.Engine.JavaScript.d.ts
355 lines (306 loc) · 9.2 KB
/
Mingine.Engine.JavaScript.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
declare module "mingine-engine";
/** An object that works like a vec2 in most functions */
interface Vec2Like {
x: number;
y: number;
}
// the Types.fs Vec2 type as used by ALL internal code and
// as returned by mp.v()
/** a 2D vector as used in the engine, with helpful methods */
interface Vec2 extends Vec2Like {
/** creates the vector of this one plus another */
add(other: Vec2Like): Vec2
/** creates the vector of this one subtract another */
sub(other: Vec2Like): Vec2
/** creates the opposing vector of this one */
neg(): Vec2
/** gets the dot product of this vector plus another */
dot(other: Vec2Like): number
/** gets the (magnitude of the) cross product of this vector plus another */
cross(other: Vec2Like): number
/** creates the vector of this one multiplied by some scalar */
scale(scalar: number): Vec2
/** creates the vector of this one divided by some scalar */
scdiv(scalar: number): Vec2
/** returns the length of this vector */
len(): number;
/** rotates this vector by the given angle about the given origin */
rotate(angle: number, origin: Vec2Like): Vec2;
/** returns this vector normalized (with length of 1) */
norm(): Vec2;
/** returns the angle between this vector and another */
angleTo(other: Vec2Like): number;
/** gets a perpendicular vector to this one - often useful with neg() */
perp(): Vec2;
}
/** An object as seen and operated on by the physics engine */
interface PhysicsObject {
/** how heavy the object is in kilograms */
mass: number;
/** the position of the object in meters */
pos: Vec2;
/** the velocity of the object in meters/second */
velocity: Vec2;
/** the acceleration of the object in meters/second^2 */
accel: Vec2;
/** force calculators having an effect on this object */
forces: ForceCalculator[]
/** the moment of inertia of this object - see wikipedia! */
momentOfInertia: number;
/** the angle CLOCKWISE of the object in radians - be careful with equations that expect opposite! */
angle: number;
/** angular velocity in radians/second */
angVelocity: number;
/** angular acceleration in radians/second^2 */
angAccel: number;
/** the coefficient of restitution (how bouncy an object is!) */
restitutionCoeff: number;
}
/** Represents a renderable object in the game */
interface GameObject {
/** the unique ID of this game object */
id: string;
/** the physics object for this game object */
physicsObj: PhysicsObject;
/** the render layer (z-index) of this object */
layer: number;
/** the vector from the bottom left to the center of the object */
blOffset: Vec2;
/** the CSS on this object */
styles: object;
/** the collider this object is using */
collider: Collider;
}
interface WrappedGO extends GameObject {
o: GameObject;
}
// just about enough to make it unique
// i do not intend to write typedefs for fable unions
interface Collider {
"___DONT TOUCH THIS OBJECT___": "defos an accurate collider type"
}
/** the scene - the top level container of the game */
interface Scene {
/** how many pixels per meter */
scale: number;
/** the styles to place on the container */
rootStyles: object;
/**
* a Fable HashSet of objects in the scene - dont attempt to use this
* @internal
*/
objects: unknown;
/** where the camera is placed */
renderOffset: Vec2;
/** how many metres in each direction the canvas is */
canvasSize: Vec2;
/** hooks to run after every tick */
postTickHooks: PostTickHook[];
/** gets all game objects in scene */
getObjects(): WrappedGO[];
/** adds a game object to the scene */
addObject(go: WrappedGO): void;
/** removes a game object from the scene */
removeObject(go: WrappedGO): void;
}
/** options used to start an engine running */
type EngineStartOpts = undefined | {
/**
* the rate to update the engine at
* @default 200
*/
physicsHz?: number;
/**
* a timestep cap, avoids engine instability at the cost of slowdowns
* set to <0 to disable
* @default
*/
tsCap?: number;
/**
* locks the physics tick to happen on each draw frame - overrides physicsHz
* @default false
*/
lockPhysicsToRender?: boolean;
}
/** the object containing the engine and controlling it */
interface Engine {
/** the scene being shown and simulated */
scene: Scene;
/** if the engine is or is not currently started */
running: boolean;
/** the element in which the engine is mounted, if it is */
mounted: HTMLElement | undefined;
/**
* used for timing, do not touch
* @internal
*/
lastTick: number;
/**
* used for render ticks, do not touch
* Fable Dictionary<WrappedGO, HTMLElement>
* @internal
*/
gObjMountedCache: unknown;
/**
* used for collision, do not touch
* Fable Dictionary<WrappedGO, WrappedGO list>
* @internal
*/
collisionCache: unknown
/** checks if two objects collided in this scene last tick */
queryCollision(o1: WrappedGO, o2: WrappedGO): boolean;
/** starts the engine with given options */
start(opts?: EngineStartOpts): void;
/** stops a running engine */
stop(): void;
/** mounts the engine in the given element */
mount(elem: HTMLElement): void;
/** unmounts the engine and returns the element */
unmount(): HTMLElement;
}
type PostTickHook = (x: [Scene, number]) => void;
type ForceCalculator = (x: [PhysicsObject, number]) => [Vec2, number];
/** creates a 2d vector */
export function v(x: number, y: number): Vec2;
/** gets the origin vector */
export function vo(): Vec2;
/** inits an engine with the given scene */
export function createEngine(scene: Scene): Engine;
/** creates a scene from a partial one */
export function createScene(partial: {
/** the amount of pixels per meter */
scale?: number;
/** the CSS for the canvas */
rootStyles?: object;
/** the objects in the scene */
objects?: WrappedGO[];
/** the position of the camera */
renderOffset?: Vec2Like
/** how big the canvas is */
canvasSize?: Vec2Like
/** hooks to run after each tick*/
postTickHooks?: PostTickHook[]
}): Scene;
type CustomObjectCreator = {
/** the unique id of this object */
id: string;
/**
* the render layer of this object
* @default 1
*/
layer?: number;
/** the vector from the bottom left to the center */
blOffset: Vec2Like;
/**
* styles to apply to the element
* @default {}
*/
styles?: object;
/**
* the collider used for collision response
* @default NullCollider
*/
collider?: Collider;
/**
* position of the object
* @default (0, 0)
*/
pos?: Vec2Like;
/**
* velocity of the object
* @default (0, 0)
*/
velocity?: Vec2Like;
/**
* acceleration of the object
* @default (0, 0)
*/
accel?: Vec2Like;
/** mass of the object in kg*/
mass: number;
/**
* functions that model forces
* @default []
*/
forces?: ForceCalculator[];
/** moment of inertia (see wikipedia) */
momentOfInertia: number;
/**
* the rotation of this object
* @default 0
*/
angle?: number;
/**
* the angular velocity of this object
* @default 0
*/
angVelocity?: number;
/**
* the angular acceleration of this object
* @default 0
*/
angAccel?: number;
/**
* the coefficient of restitution (how bouncy is it!)
* @default 1
*/
restitutionCoeff?: number;
}
/** creates a custom game object, intended to be abstracted by the user */
export function createObject(obj: CustomObjectCreator): WrappedGO;
type CircleCreator =
Omit<CustomObjectCreator, "collider" | "blOffset">
& {
/** the radius, in meters, of the circle */
radius: number;
/**
* when true, adds a suitable collider
* @default false
*/
collide?: boolean;
};
/** creates a circle game object */
export function createCircle(obj: CircleCreator): WrappedGO;
type RectCreator =
Omit<CustomObjectCreator, "collider" | "blOffset">
& {
/** the width, in meters, of the rectangle */
width: number;
/** the height, in meters, of the rectangle */
height: number;
/**
* when true, adds a suitable collider
* @default false
*/
collide?: boolean;
};
/** creates a rectangular game object */
export function createRect(obj: RectCreator): WrappedGO;
/** creates a "collider" that cannot collide with anything */
export function createColliderNull(): Collider;
/** creates a collider that combines two colliders into one */
export function createColliderComposite(a: Collider, b: Collider): Collider;
/** creates a collider for a circle */
export function createColliderCircle(radius: number, center: Vec2Like): Collider;
/** creates a collider for a rectangle */
export function createColliderRect(bottomLeft: Vec2Like, topRight: Vec2Like): Collider;
/** contains models of real forces */
export const forceModels: {
/** models gravity */
weight(gravity: Vec2Like): ForceCalculator;
/** models a spring */
spring(springConst: number, restPos: Vec2Like, connectionOset: Vec2Like): ForceCalculator;
/** models air resistance */
airDrag(absoluteFlowVelocity: Vec2Like, density: number, csArea: number, dragCoeff: number): ForceCalculator;
/** models air resistance in still air */
stillAirDrag(density: number, csArea: number, dragCoeff: number): ForceCalculator;
/** a super simple damping force (not a real force model!) */
simpleDamping(positionRatio: number, angleRatio: number): ForceCalculator
};
/** physical constants of earth :) */
export const consts: {
/** the gravity of earth */
earthGravity: Vec2;
/** the air density of earth */
earthAirDensity: number;
};