Skip to content

Commit

Permalink
feat(levels): add level 14
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 28, 2024
1 parent 494c4c7 commit 3d31bf5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/helpers/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ export function clearAllIntervals() {
clearInterval(id)
}
}

/**
* Clears all `setTimeout()` and `setInterval()` timers.
*/
export function clearAllTimers() {
clearAllTimeouts()
clearAllIntervals()
}
66 changes: 66 additions & 0 deletions src/levels/14.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { addCursorKeys, clearAllTimers, initLevel } from '../helpers'
import { Cleanup, Sprite } from '../types'

export const level = 14
export const title = 'Timer 3'

const cleanups: Cleanup[] = [clearAllTimers]

let keys: number

function getMessage() {
return `Collect ${keys} more key${keys !== 1 ? 's' : ''}`
}

function addKey() {
add([
sprite(Sprite.key),
pos(randi(width()), randi(height())),
area(),
anchor('center'),
Sprite.key,
])
}

export function prescript() {
keys = 420
initLevel(level, cleanups)
loadSprite(Sprite.key, 'sprites/key.png')

addCursorKeys(
add([sprite(Sprite.player), pos(center()), area(), Sprite.player]),
)

const message = add([text(getMessage())])

addKey()

cleanups.push(
onCollide(Sprite.key, Sprite.player, (key) => {
keys--
key.destroy()
message.text = getMessage()

if (keys) {
addKey()
} else {
add([sprite(Sprite.exit), pos(center()), area(), Sprite.exit])
}
}).cancel,
)

cleanups.push(
onAdd(Sprite.exit, () => {
if (keys) {
destroyAll(Sprite.exit)
}
}).cancel,
)
}

export const script = `
const player = get('player')[0]
const key = get('key')[0]
`

export function postscript() {}

0 comments on commit 3d31bf5

Please sign in to comment.