@@ -41,6 +41,8 @@ export default class Runner {
41
41
distanceRan = 0
42
42
highestScore = 0
43
43
44
+ tRex ! : Trex
45
+
44
46
constructor ( outerContainerId : string , optConfig ?: ConfigDict ) {
45
47
this . outerContainerEl = document . querySelector ( outerContainerId ) !
46
48
this . config = optConfig || Object . assign ( Runner . config , Runner . normalConfig )
@@ -91,6 +93,7 @@ export default class Runner {
91
93
this . horizon = new Horizon ( this . canvas , this . spriteDef , this . dimensions , this . config . GAP_COEFFICIENT )
92
94
93
95
this . distanceMeter = new DistanceMeter ( this . canvas , this . spriteDef . TEXT_SPRITE , this . dimensions . WIDTH )
96
+ this . tRex = new Trex ( this . canvas , this . spriteDef . TREX )
94
97
95
98
this . startListening ( )
96
99
this . update ( )
@@ -155,10 +158,14 @@ export default class Runner {
155
158
if ( this . playing ) {
156
159
this . clearCanvas ( )
157
160
161
+ if ( this . tRex . jumping ) {
162
+ this . tRex . updateJump ( deltaTime )
163
+ }
164
+
158
165
this . runningTime += deltaTime
159
166
let hasObstacles = this . runningTime > Runner . config . CLEAR_TIME
160
167
161
- if ( ! this . playingIntro ) {
168
+ if ( ! this . playingIntro && this . tRex . jumpCount === 1 ) {
162
169
this . playIntro ( )
163
170
}
164
171
@@ -203,7 +210,9 @@ export default class Runner {
203
210
}
204
211
}
205
212
206
- if ( this . playing ) {
213
+ if ( this . playing || ( ! this . activated && this . tRex . blinkCount < Runner . config . MAX_BLINK_COUNT ) ) {
214
+ this . tRex . update ( deltaTime )
215
+
207
216
this . scheduleNextUpdate ( )
208
217
}
209
218
}
@@ -225,6 +234,7 @@ export default class Runner {
225
234
playIntro ( ) {
226
235
if ( ! this . activated && ! this . crashed ) {
227
236
this . playingIntro = true
237
+ this . tRex . playingIntro = true
228
238
229
239
let keyframes = `@-webkit-keyframes intro {
230
240
from { width: ${ Trex . config . WIDTH } px }
@@ -249,6 +259,8 @@ export default class Runner {
249
259
if ( this . isArcadeMode ( ) ) {
250
260
this . setArcadeMode ( )
251
261
}
262
+
263
+ this . tRex . playingIntro = false
252
264
this . runningTime = 0
253
265
this . playingIntro = false
254
266
this . containerEl . style . webkitAnimation = ""
@@ -363,27 +375,53 @@ export default class Runner {
363
375
handleEvent = ( e : KeyboardEvent ) => {
364
376
const evtType = e . type
365
377
switch ( evtType ) {
366
- case Runner . events . KEYDOWN :
367
- case Runner . events . TOUCHSTART :
368
378
case Runner . events . KEYDOWN :
369
379
this . onKeydown ( e )
370
380
break
381
+ case Runner . events . KEYUP :
382
+ this . onKeyUp ( e )
383
+ break
371
384
}
372
385
}
373
386
374
387
onKeydown ( e : KeyboardEvent ) {
375
388
if ( ! this . crashed && ! this . paused ) {
376
- if ( Runner . keycodes . JUMP [ e . code ] ) {
389
+ if ( Runner . keycodes . JUMP [ e . keyCode ] ) {
377
390
e . preventDefault ( )
378
391
379
392
if ( ! this . playing ) {
380
393
this . setPlayStatus ( true )
381
394
this . update ( )
382
395
}
396
+
397
+ if ( ! this . tRex . jumping && ! this . tRex . ducking ) {
398
+ this . tRex . startJump ( this . currentSpeed )
399
+ }
400
+ } else if ( this . playing && Runner . keycodes . DUCK [ e . keyCode ] ) {
401
+ e . preventDefault ( )
402
+
403
+ if ( this . tRex . jumping ) {
404
+ this . tRex . setSpeedDrop ( )
405
+ } else if ( ! this . tRex . jumping && ! this . tRex . ducking ) {
406
+ this . tRex . setDuck ( true )
407
+ }
383
408
}
384
409
}
385
410
}
386
411
412
+ onKeyUp ( e : KeyboardEvent ) {
413
+ if ( this . isRunning ( ) && Runner . keycodes . JUMP [ e . keyCode ] ) {
414
+ this . tRex . endJump ( )
415
+ } else if ( Runner . keycodes . DUCK [ e . keyCode ] ) {
416
+ this . tRex . speedDrop = false
417
+ this . tRex . setDuck ( false )
418
+ }
419
+ }
420
+
421
+ isRunning ( ) {
422
+ return ! ! this . raqId
423
+ }
424
+
387
425
stop ( ) {
388
426
this . setPlayStatus ( false )
389
427
this . paused = true
@@ -397,6 +435,7 @@ export default class Runner {
397
435
this . paused = false
398
436
this . time = Date . now ( )
399
437
this . update ( )
438
+ this . tRex . reset ( )
400
439
}
401
440
}
402
441
@@ -481,9 +520,9 @@ export default class Runner {
481
520
}
482
521
483
522
static keycodes = {
484
- JUMP : { ArrowUp : 1 , Space : 1 } as any , // Up, spacebar
485
- DUCK : { ArrowDown : 1 } as any , // Down
486
- RESTART : { Enter : 1 } as any // Enter
523
+ JUMP : { 38 : 1 , 32 : 1 } as any , // Up, spacebar
524
+ DUCK : { 40 : 1 } as any , // Down
525
+ RESTART : { 13 : 1 } as any // Enter
487
526
}
488
527
489
528
/**
@@ -521,7 +560,7 @@ export default class Runner {
521
560
AUDIOCUE_PROXIMITY_THRESHOLD : 190 ,
522
561
AUDIOCUE_PROXIMITY_THRESHOLD_MOBILE_A11Y : 250 ,
523
562
GAP_COEFFICIENT : 0.6 ,
524
- INVERT_DISTANCE : 100 ,
563
+ INVERT_DISTANCE : 700 ,
525
564
MAX_SPEED : 13 ,
526
565
MOBILE_SPEED_COEFFICIENT : 1.2 ,
527
566
SPEED : 6
0 commit comments