Skip to content

Commit 35bc525

Browse files
committed
Add T-Rex
1 parent 576cf8e commit 35bc525

File tree

2 files changed

+296
-39
lines changed

2 files changed

+296
-39
lines changed

game/Runner.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export default class Runner {
4141
distanceRan = 0
4242
highestScore = 0
4343

44+
tRex!: Trex
45+
4446
constructor(outerContainerId: string, optConfig?: ConfigDict) {
4547
this.outerContainerEl = document.querySelector(outerContainerId)!
4648
this.config = optConfig || Object.assign(Runner.config, Runner.normalConfig)
@@ -91,6 +93,7 @@ export default class Runner {
9193
this.horizon = new Horizon(this.canvas, this.spriteDef, this.dimensions, this.config.GAP_COEFFICIENT)
9294

9395
this.distanceMeter = new DistanceMeter(this.canvas, this.spriteDef.TEXT_SPRITE, this.dimensions.WIDTH)
96+
this.tRex = new Trex(this.canvas, this.spriteDef.TREX)
9497

9598
this.startListening()
9699
this.update()
@@ -155,10 +158,14 @@ export default class Runner {
155158
if (this.playing) {
156159
this.clearCanvas()
157160

161+
if (this.tRex.jumping) {
162+
this.tRex.updateJump(deltaTime)
163+
}
164+
158165
this.runningTime += deltaTime
159166
let hasObstacles = this.runningTime > Runner.config.CLEAR_TIME
160167

161-
if (!this.playingIntro) {
168+
if (!this.playingIntro && this.tRex.jumpCount === 1) {
162169
this.playIntro()
163170
}
164171

@@ -203,7 +210,9 @@ export default class Runner {
203210
}
204211
}
205212

206-
if (this.playing) {
213+
if (this.playing || (!this.activated && this.tRex.blinkCount < Runner.config.MAX_BLINK_COUNT)) {
214+
this.tRex.update(deltaTime)
215+
207216
this.scheduleNextUpdate()
208217
}
209218
}
@@ -225,6 +234,7 @@ export default class Runner {
225234
playIntro() {
226235
if (!this.activated && !this.crashed) {
227236
this.playingIntro = true
237+
this.tRex.playingIntro = true
228238

229239
let keyframes = `@-webkit-keyframes intro {
230240
from { width: ${Trex.config.WIDTH}px }
@@ -249,6 +259,8 @@ export default class Runner {
249259
if (this.isArcadeMode()) {
250260
this.setArcadeMode()
251261
}
262+
263+
this.tRex.playingIntro = false
252264
this.runningTime = 0
253265
this.playingIntro = false
254266
this.containerEl.style.webkitAnimation = ""
@@ -363,27 +375,53 @@ export default class Runner {
363375
handleEvent = (e: KeyboardEvent) => {
364376
const evtType = e.type
365377
switch (evtType) {
366-
case Runner.events.KEYDOWN:
367-
case Runner.events.TOUCHSTART:
368378
case Runner.events.KEYDOWN:
369379
this.onKeydown(e)
370380
break
381+
case Runner.events.KEYUP:
382+
this.onKeyUp(e)
383+
break
371384
}
372385
}
373386

374387
onKeydown(e: KeyboardEvent) {
375388
if (!this.crashed && !this.paused) {
376-
if (Runner.keycodes.JUMP[e.code]) {
389+
if (Runner.keycodes.JUMP[e.keyCode]) {
377390
e.preventDefault()
378391

379392
if (!this.playing) {
380393
this.setPlayStatus(true)
381394
this.update()
382395
}
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+
}
383408
}
384409
}
385410
}
386411

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+
387425
stop() {
388426
this.setPlayStatus(false)
389427
this.paused = true
@@ -397,6 +435,7 @@ export default class Runner {
397435
this.paused = false
398436
this.time = Date.now()
399437
this.update()
438+
this.tRex.reset()
400439
}
401440
}
402441

@@ -481,9 +520,9 @@ export default class Runner {
481520
}
482521

483522
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
487526
}
488527

489528
/**
@@ -521,7 +560,7 @@ export default class Runner {
521560
AUDIOCUE_PROXIMITY_THRESHOLD: 190,
522561
AUDIOCUE_PROXIMITY_THRESHOLD_MOBILE_A11Y: 250,
523562
GAP_COEFFICIENT: 0.6,
524-
INVERT_DISTANCE: 100,
563+
INVERT_DISTANCE: 700,
525564
MAX_SPEED: 13,
526565
MOBILE_SPEED_COEFFICIENT: 1.2,
527566
SPEED: 6

0 commit comments

Comments
 (0)