/
Events.ts
547 lines (471 loc) · 14.7 KB
/
Events.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import { Scene } from './Scene';
import { Vector } from './Math/vector';
import { Actor } from './Actor';
import { Trigger } from './Trigger';
import { FrameStats } from './Debug';
import { Engine } from './Engine';
import { TileMap } from './TileMap';
import { Side } from './Collision/Side';
import * as Input from './Input/Index';
import { CollisionContact } from './Collision/Detection/CollisionContact';
import { Collider } from './Collision/Colliders/Collider';
import { Entity } from './EntityComponentSystem/Entity';
import { OnInitialize, OnPreUpdate, OnPostUpdate, SceneActivationContext } from './Interfaces/LifecycleEvents';
import { BodyComponent } from './Collision/BodyComponent';
import { ExcaliburGraphicsContext } from './Graphics';
export enum EventTypes {
Kill = 'kill',
PreKill = 'prekill',
PostKill = 'postkill',
PreDraw = 'predraw',
PostDraw = 'postdraw',
PreDebugDraw = 'predebugdraw',
PostDebugDraw = 'postdebugdraw',
PreUpdate = 'preupdate',
PostUpdate = 'postupdate',
PreFrame = 'preframe',
PostFrame = 'postframe',
PreCollision = 'precollision',
CollisionStart = 'collisionstart',
CollisionEnd = 'collisionend',
PostCollision = 'postcollision',
Initialize = 'initialize',
Activate = 'activate',
Deactivate = 'deactivate',
ExitViewport = 'exitviewport',
EnterViewport = 'enterviewport',
ExitTrigger = 'exit',
EnterTrigger = 'enter',
Connect = 'connect',
Disconnect = 'disconnect',
Button = 'button',
Axis = 'axis',
Visible = 'visible',
Hidden = 'hidden',
Start = 'start',
Stop = 'stop',
PointerUp = 'pointerup',
PointerDown = 'pointerdown',
PointerMove = 'pointermove',
PointerEnter = 'pointerenter',
PointerLeave = 'pointerleave',
PointerCancel = 'pointercancel',
PointerWheel = 'pointerwheel',
Up = 'up',
Down = 'down',
Move = 'move',
Enter = 'enter',
Leave = 'leave',
Cancel = 'cancel',
Wheel = 'wheel',
Press = 'press',
Release = 'release',
Hold = 'hold',
PointerDragStart = 'pointerdragstart',
PointerDragEnd = 'pointerdragend',
PointerDragEnter = 'pointerdragenter',
PointerDragLeave = 'pointerdragleave',
PointerDragMove = 'pointerdragmove'
}
/* istanbul ignore next */
/* compiler only: these are internal to lib */
export type kill = 'kill';
export type prekill = 'prekill';
export type postkill = 'postkill';
export type predraw = 'predraw';
export type postdraw = 'postdraw';
export type predebugdraw = 'predebugdraw';
export type postdebugdraw = 'postdebugdraw';
export type preupdate = 'preupdate';
export type postupdate = 'postupdate';
export type preframe = 'preframe';
export type postframe = 'postframe';
export type precollision = 'precollision';
export type collisionstart = 'collisionstart';
export type collisionend = 'collisionend';
export type postcollision = 'postcollision';
export type initialize = 'initialize';
export type activate = 'activate';
export type deactivate = 'deactivate';
export type exitviewport = 'exitviewport';
export type enterviewport = 'enterviewport';
export type exittrigger = 'exit';
export type entertrigger = 'enter';
export type connect = 'connect';
export type disconnect = 'disconnect';
export type button = 'button';
export type axis = 'axis';
export type subscribe = 'subscribe';
export type unsubscribe = 'unsubscribe';
export type visible = 'visible';
export type hidden = 'hidden';
export type start = 'start';
export type stop = 'stop';
export type pointerup = 'pointerup';
export type pointerdown = 'pointerdown';
export type pointermove = 'pointermove';
export type pointerenter = 'pointerenter';
export type pointerleave = 'pointerleave';
export type pointercancel = 'pointercancel';
export type pointerwheel = 'pointerwheel';
export type up = 'up';
export type down = 'down';
export type move = 'move';
export type enter = 'enter';
export type leave = 'leave';
export type cancel = 'cancel';
export type wheel = 'wheel';
export type press = 'press';
export type release = 'release';
export type hold = 'hold';
export type pointerdragstart = 'pointerdragstart';
export type pointerdragend = 'pointerdragend';
export type pointerdragenter = 'pointerdragenter';
export type pointerdragleave = 'pointerdragleave';
export type pointerdragmove = 'pointerdragmove';
/**
* Base event type in Excalibur that all other event types derive from. Not all event types are thrown on all Excalibur game objects,
* some events are unique to a type, others are not.
*
*/
export class GameEvent<T, U = T> {
/**
* Target object for this event.
*/
public target: T;
/**
* Other target object for this event
*/
public other: U | null;
/**
* If set to false, prevents event from propagating to other actors. If true it will be propagated
* to all actors that apply.
*/
public get bubbles(): boolean {
return this._bubbles;
}
public set bubbles(value: boolean) {
this._bubbles = value;
}
private _bubbles: boolean = true;
/**
* Prevents event from bubbling
*/
public stopPropagation() {
this.bubbles = false;
}
}
/**
* The 'kill' event is emitted on actors when it is killed. The target is the actor that was killed.
*/
export class KillEvent extends GameEvent<Actor> {
constructor(public target: Actor) {
super();
}
}
/**
* The 'prekill' event is emitted directly before an actor is killed.
*/
export class PreKillEvent extends GameEvent<Actor> {
constructor(public target: Actor) {
super();
}
}
/**
* The 'postkill' event is emitted directly after the actor is killed.
*/
export class PostKillEvent extends GameEvent<Actor> {
constructor(public target: Actor) {
super();
}
}
/**
* The 'start' event is emitted on engine when has started and is ready for interaction.
*/
export class GameStartEvent extends GameEvent<Engine> {
constructor(public target: Engine) {
super();
}
}
/**
* The 'stop' event is emitted on engine when has been stopped and will no longer take input, update or draw.
*/
export class GameStopEvent extends GameEvent<Engine> {
constructor(public target: Engine) {
super();
}
}
/**
* The 'predraw' event is emitted on actors, scenes, and engine before drawing starts. Actors' predraw happens inside their graphics
* transform so that all drawing takes place with the actor as the origin.
*
*/
export class PreDrawEvent extends GameEvent<Entity | Scene | Engine | TileMap> {
constructor(public ctx: ExcaliburGraphicsContext, public delta: number, public target: Entity | Scene | Engine | TileMap) {
super();
}
}
/**
* The 'postdraw' event is emitted on actors, scenes, and engine after drawing finishes. Actors' postdraw happens inside their graphics
* transform so that all drawing takes place with the actor as the origin.
*
*/
export class PostDrawEvent extends GameEvent<Entity | Scene | Engine | TileMap> {
constructor(public ctx: ExcaliburGraphicsContext, public delta: number, public target: Entity | Scene | Engine | TileMap) {
super();
}
}
/**
* The 'predebugdraw' event is emitted on actors, scenes, and engine before debug drawing starts.
*/
export class PreDebugDrawEvent extends GameEvent<Entity | Actor | Scene | Engine> {
constructor(public ctx: ExcaliburGraphicsContext, public target: Entity | Actor | Scene | Engine) {
super();
}
}
/**
* The 'postdebugdraw' event is emitted on actors, scenes, and engine after debug drawing starts.
*/
export class PostDebugDrawEvent extends GameEvent<Entity | Actor | Scene | Engine> {
constructor(public ctx: ExcaliburGraphicsContext, public target: Entity | Actor | Scene | Engine) {
super();
}
}
/**
* The 'preupdate' event is emitted on actors, scenes, camera, and engine before the update starts.
*/
export class PreUpdateEvent<T extends OnPreUpdate = Entity> extends GameEvent<T> {
constructor(public engine: Engine, public delta: number, public target: T) {
super();
}
}
/**
* The 'postupdate' event is emitted on actors, scenes, camera, and engine after the update ends.
*/
export class PostUpdateEvent<T extends OnPostUpdate = Entity> extends GameEvent<T> {
constructor(public engine: Engine, public delta: number, public target: T) {
super();
}
}
/**
* The 'preframe' event is emitted on the engine, before the frame begins.
*/
export class PreFrameEvent extends GameEvent<Engine> {
constructor(public engine: Engine, public prevStats: FrameStats) {
super();
this.target = engine;
}
}
/**
* The 'postframe' event is emitted on the engine, after a frame ends.
*/
export class PostFrameEvent extends GameEvent<Engine> {
constructor(public engine: Engine, public stats: FrameStats) {
super();
this.target = engine;
}
}
/**
* Event received when a gamepad is connected to Excalibur. [[Gamepads]] receives this event.
*/
export class GamepadConnectEvent extends GameEvent<Input.Gamepad> {
constructor(public index: number, public gamepad: Input.Gamepad) {
super();
this.target = gamepad;
}
}
/**
* Event received when a gamepad is disconnected from Excalibur. [[Gamepads]] receives this event.
*/
export class GamepadDisconnectEvent extends GameEvent<Input.Gamepad> {
constructor(public index: number, public gamepad: Input.Gamepad) {
super();
this.target = gamepad;
}
}
/**
* Gamepad button event. See [[Gamepads]] for information on responding to controller input. [[Gamepad]] instances receive this event;
*/
export class GamepadButtonEvent extends GameEvent<Input.Gamepad> {
/**
* @param button The Gamepad button
* @param value A numeric value between 0 and 1
*/
constructor(public button: Input.Buttons, public value: number, public target: Input.Gamepad) {
super();
}
}
/**
* Gamepad axis event. See [[Gamepads]] for information on responding to controller input. [[Gamepad]] instances receive this event;
*/
export class GamepadAxisEvent extends GameEvent<Input.Gamepad> {
/**
* @param axis The Gamepad axis
* @param value A numeric value between -1 and 1
*/
constructor(public axis: Input.Axes, public value: number, public target: Input.Gamepad) {
super();
}
}
/**
* Event received by the [[Engine]] when the browser window is visible on a screen.
*/
export class VisibleEvent extends GameEvent<Engine> {
constructor(public target: Engine) {
super();
}
}
/**
* Event received by the [[Engine]] when the browser window is hidden from all screens.
*/
export class HiddenEvent extends GameEvent<Engine> {
constructor(public target: Engine) {
super();
}
}
/**
* Event thrown on an [[Actor|actor]] when a collision will occur this frame if it resolves
*/
export class PreCollisionEvent<T extends BodyComponent | Collider | Entity = Actor> extends GameEvent<T> {
/**
* @param actor The actor the event was thrown on
* @param other The actor that will collided with the current actor
* @param side The side that will be collided with the current actor
* @param intersection Intersection vector
*/
constructor(actor: T, public other: T, public side: Side, public intersection: Vector) {
super();
this.target = actor;
}
}
/**
* Event thrown on an [[Actor|actor]] when a collision has been resolved (body reacted) this frame
*/
export class PostCollisionEvent<T extends Collider | Entity = Actor> extends GameEvent<T> {
/**
* @param actor The actor the event was thrown on
* @param other The actor that did collide with the current actor
* @param side The side that did collide with the current actor
* @param intersection Intersection vector
*/
constructor(actor: T, public other: T, public side: Side, public intersection: Vector) {
super();
this.target = actor;
}
public get actor() {
return this.target;
}
public set actor(actor: T) {
this.target = actor;
}
}
export class ContactStartEvent<T> {
constructor(public target: T, public other: T, public contact: CollisionContact) {}
}
export class ContactEndEvent<T> {
constructor(public target: T, public other: T) {}
}
export class CollisionPreSolveEvent<T> {
constructor(public target: T, public other: T, public side: Side, public intersection: Vector, public contact: CollisionContact) {}
}
export class CollisionPostSolveEvent<T> {
constructor(public target: T, public other: T, public side: Side, public intersection: Vector, public contact: CollisionContact) {}
}
/**
* Event thrown the first time an [[Actor|actor]] collides with another, after an actor is in contact normal collision events are fired.
*/
export class CollisionStartEvent<T extends BodyComponent | Collider | Entity = Actor> extends GameEvent<T> {
/**
*
* @param actor
* @param other
* @param contact
*/
constructor(actor: T, public other: T, public contact: CollisionContact) {
super();
this.target = actor;
}
public get actor() {
return this.target;
}
public set actor(actor: T) {
this.target = actor;
}
}
/**
* Event thrown when the [[Actor|actor]] is no longer colliding with another
*/
export class CollisionEndEvent<T extends BodyComponent | Collider | Entity = Actor> extends GameEvent<T> {
/**
*
*/
constructor(actor: T, public other: T) {
super();
this.target = actor;
}
public get actor() {
return this.target;
}
public set actor(actor: T) {
this.target = actor;
}
}
/**
* Event thrown on an [[Actor]] and a [[Scene]] only once before the first update call
*/
export class InitializeEvent<T extends OnInitialize = Entity> extends GameEvent<T> {
/**
* @param engine The reference to the current engine
*/
constructor(public engine: Engine, public target: T) {
super();
}
}
/**
* Event thrown on a [[Scene]] on activation
*/
export class ActivateEvent<TData = undefined> extends GameEvent<Scene> {
/**
* @param context The context for the scene activation
*/
constructor(public context: SceneActivationContext<TData>, public target: Scene) {
super();
}
}
/**
* Event thrown on a [[Scene]] on deactivation
*/
export class DeactivateEvent extends GameEvent<Scene> {
/**
* @param context The context for the scene deactivation
*/
constructor(public context: SceneActivationContext<never>, public target: Scene) {
super();
}
}
/**
* Event thrown on an [[Actor]] when it completely leaves the screen.
*/
export class ExitViewPortEvent extends GameEvent<Entity> {
constructor(public target: Entity) {
super();
}
}
/**
* Event thrown on an [[Actor]] when it completely leaves the screen.
*/
export class EnterViewPortEvent extends GameEvent<Entity> {
constructor(public target: Entity) {
super();
}
}
export class EnterTriggerEvent extends GameEvent<Actor> {
constructor(public target: Trigger, public actor: Actor) {
super();
}
}
export class ExitTriggerEvent extends GameEvent<Actor> {
constructor(public target: Trigger, public actor: Actor) {
super();
}
}