logo
By Lawrence

13 minutes

Police Chases

The download writes the scene only, not src/main.ts. If you start here, add the debug keys from lessons 2.4 and 2.5 back to src/main.ts, so T calls the weapons truck and B calls the bomber:

src/main.ts
// At the top, with the other imports:
import { SupplyTruckInputEvents } from './components/SupplyTruck/SupplyTruck.events'
import { AutoRunnerBomberInputEvents } from './components/AutoRunnerBomber/AutoRunnerBomber.events'

// At the bottom. Debug keys: lesson 2.7 deletes them.
window.addEventListener('keydown', (e) => {
  if (e.code === 'KeyT' && !e.repeat) game.eventBus.emit(SupplyTruckInputEvents.DISPATCH, {})
})
window.addEventListener('keydown', (e) => {
  if (e.code === 'KeyB' && !e.repeat) game.eventBus.emit(AutoRunnerBomberInputEvents.DISPATCH, {})
})

Hold W and your car tops out at 52 m/s. You weave past commuters doing 20 to 31, and nobody minds. Speed costs you nothing, because the road has no law.

This lesson adds the law: black patrol cars that chase anyone over the limit. Starting a chase is the easy part. The hard part is knowing when to stop.

Two rules

A patrol starts a chase on one rule. You go faster than 36 m/s, about 130 km/h, close enough for it to see you. Close enough means within 45 m, ahead or behind. Ramming it or shooting it starts a chase too.

The obvious way to stop is that same rule turned around: chase while you are over the limit, and give up the moment you are not. That rule flickers. Your speed wobbles around the line, and each time it crosses, the chase flips on or off.

So the patrol stops on a second, slower rule. It keeps a clock called calm. Each frame you behave, calm grows by that frame's time. Each frame you speed, it drops back to 0. When it reaches calmSeconds, 3 s in this scene, the chase ends.

Continue reading

Unlock the Full Course

Every lesson, the runnable examples, and the finished build — yours to keep.

$9one-time

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search