diff --git a/src/helpers/events.ts b/src/helpers/events.ts index 3a694ba..9fb19fb 100644 --- a/src/helpers/events.ts +++ b/src/helpers/events.ts @@ -1,3 +1,10 @@ +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', @@ -5,7 +12,10 @@ export function addEventListeners(callback: () => void) { 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) }) } diff --git a/src/levels/11.ts b/src/levels/11.ts index 8c17b57..1a85440 100644 --- a/src/levels/11.ts +++ b/src/levels/11.ts @@ -12,6 +12,7 @@ export const prescript = ` ${loadPlayer()} ${loadExit()} loadSprite('spike', 'sprites/spike.png') +loadSound('explode', 'sounds/hit.mp3') add([ sprite('player'), @@ -56,6 +57,7 @@ map.forEach((row, rowIndex) => { }) onCollide('player', 'spike', (player, spike) => { + play('explode') spike.opacity = 1 player.destroy() addKaboom(player.pos) @@ -63,13 +65,11 @@ onCollide('player', 'spike', (player, spike) => { 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) } diff --git a/src/templates/events.ts b/src/templates/events.ts index ccb0821..26cbd0b 100644 --- a/src/templates/events.ts +++ b/src/templates/events.ts @@ -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}, + }) }) }) ` diff --git a/src/templates/layouts.ts b/src/templates/layouts.ts index a53ed01..229281b 100644 --- a/src/templates/layouts.ts +++ b/src/templates/layouts.ts @@ -23,6 +23,7 @@ body {