Workshop › Pool › Step 13 of 33
The game has two moods: 'aiming', waiting for a shot, and 'rolling', while anything moves. A shot is only allowed while
aiming. When every ball is still again, it is back to aiming.
```js
let state // 'aiming', 'rolling' or 'won'
state = 'aiming'
function shoot() {
if (state !== 'aiming') return
cue.vx = Math.cos(aim) * power
cue.vy = Math.sin(aim) * power
state = 'rolling'
}
function update() {
if (state !== 'rolling') return
step()
let moving = false
for (const b of balls) {
b.vx *= FRICTION
b.vy *= FRICTION
if (Math.hypot(b.vx, b.vy) < 0.05) b.vx = b.vy = 0
else moving = true
}
if (moving) return
state = 'aiming'
}
```shoot does nothing unless aiming, and starts the rolling.update only moves balls while rolling.moving starts false; any ball that is not stopped sets it to true. If nothing moved, the game goes back to aiming.let power write let state ...; in reset, under power = 8, write state = 'aiming'.shoot, add the first line and the last line.update, add the first line, let moving = false above the loop, else moving = true under the stop line, and the
last two lines after the loop.Skills: Game states
← Cushions Hide the aim while rolling →