Skip to content

Commit

Permalink
feat: add sounds to levels
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 30, 2024
1 parent 4badb46 commit c5ef10c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/helpers/events.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
const audio = new Audio('sounds/score.mp3')

/**
* Attaches click listeners to "Run" buttons.
*
* @param callback - Callback.
*/
export function addEventListeners(callback: () => void) {
const buttons = document.querySelectorAll(
'main button',
) as NodeListOf<HTMLButtonElement>

buttons.forEach((oldButton) => {
const newButton = oldButton.cloneNode(true) as HTMLButtonElement
newButton.addEventListener('click', callback)
newButton.addEventListener('click', () => {
audio.play()
callback()
})
oldButton.parentNode?.replaceChild(newButton, oldButton)
})
}
4 changes: 2 additions & 2 deletions src/levels/11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const prescript = `
${loadPlayer()}
${loadExit()}
loadSprite('spike', 'sprites/spike.png')
loadSound('explode', 'sounds/hit.mp3')
add([
sprite('player'),
Expand Down Expand Up @@ -56,20 +57,19 @@ map.forEach((row, rowIndex) => {
})
onCollide('player', 'spike', (player, spike) => {
play('explode')
spike.opacity = 1
player.destroy()
addKaboom(player.pos)
})
onUpdate(() => {
const player = get('player')[0]
if (!player) {
return
}
const { x, y } = player.pos
if (x < 0 || y < 0 || x > width() || y > height()) {
player.moveTo(40, 80)
}
Expand Down
12 changes: 9 additions & 3 deletions src/templates/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ onKeyDown((key) => {
* @returns - Game script.
*/
export const registerWinCondition = (level: number) => `
loadSound('exit', 'sounds/wooosh.mp3')
onCollide('player', 'exit', () => {
parent.postMessage({
source: '${GAME_ID}',
level: ${level + 1},
play('exit')
wait(0.2, () => {
window.parent.postMessage({
source: '${GAME_ID}',
level: ${level + 1},
})
})
})
`
2 changes: 2 additions & 0 deletions src/templates/layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ body {
<script>
kaboom()
window.onerror = (message) => {
debug.log(message)
}
Expand All @@ -31,6 +32,7 @@ console.log = (...args) => {
log(...args)
debug.log(args.join(' '))
}
${code}
</script>
</body>
Expand Down

0 comments on commit c5ef10c

Please sign in to comment.