diff --git a/public/music/background.mp3 b/public/music/background.mp3 new file mode 100644 index 0000000..eabf89d Binary files /dev/null and b/public/music/background.mp3 differ diff --git a/src/constants/index.ts b/src/constants/index.ts index b42c70b..39c9940 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,3 +1,4 @@ +export * from './music' export * from './scenes' export * from './sounds' export * from './sprites' diff --git a/src/constants/music.ts b/src/constants/music.ts new file mode 100644 index 0000000..e655663 --- /dev/null +++ b/src/constants/music.ts @@ -0,0 +1,3 @@ +export enum Music { + Background = 'Background', +} diff --git a/src/gameobjects/index.ts b/src/gameobjects/index.ts index 0120b53..334e1c8 100644 --- a/src/gameobjects/index.ts +++ b/src/gameobjects/index.ts @@ -1,4 +1,5 @@ export * from './bullet' export * from './enemy' export * from './health' +export * from './music' export * from './player' diff --git a/src/gameobjects/music.ts b/src/gameobjects/music.ts new file mode 100644 index 0000000..d186642 --- /dev/null +++ b/src/gameobjects/music.ts @@ -0,0 +1,20 @@ +import type { AudioPlay } from 'kaplay' + +import { Music } from '../constants' + +let music: AudioPlay + +export function playMusic() { + if (!music) { + music = play(Music.Background, { + loop: true, + paused: true, + }) + music.volume = 0.5 + } + music.play() +} + +export function stopMusic() { + music.stop() +} diff --git a/src/gameobjects/player.ts b/src/gameobjects/player.ts index ef5576c..3d3b39a 100644 --- a/src/gameobjects/player.ts +++ b/src/gameobjects/player.ts @@ -1,5 +1,6 @@ import { Scene, Sprite, Tag } from '../constants' import { addAttack, addCursorKeys } from '../events' +import { stopMusic } from '../gameobjects' const HEALTH = 100 @@ -24,6 +25,7 @@ export function addPlayer(x = center().x, y = center().y) { player.onHurt(() => { if (player.hp() <= 0) { + stopMusic() go(Scene.Lose) } }) diff --git a/src/scenes/game.ts b/src/scenes/game.ts index bdfd411..8261266 100644 --- a/src/scenes/game.ts +++ b/src/scenes/game.ts @@ -1,10 +1,12 @@ import { Scene } from '../constants' -import { addEnemy, addHealth, addPlayer } from '../gameobjects' +import { addEnemy, addHealth, addPlayer, playMusic } from '../gameobjects' import { outsideCoordinates } from '../helpers' scene(Scene.Game, () => { add([text('Press arrow keys', { width: width() / 2 }), pos(12, 12)]) + playMusic() + const player = addPlayer() addHealth(player) diff --git a/src/scenes/preload.ts b/src/scenes/preload.ts index ef78d71..e008cb9 100644 --- a/src/scenes/preload.ts +++ b/src/scenes/preload.ts @@ -1,4 +1,4 @@ -import { Scene, Sound, Sprite } from '../constants' +import { Music, Scene, Sound, Sprite } from '../constants' scene(Scene.Preload, () => { const sprites = [ @@ -20,5 +20,11 @@ scene(Scene.Preload, () => { loadSound(name, src) }) + const music = [[Music.Background, 'music/background.mp3']] + + music.forEach(([name, src]) => { + loadMusic(name, src) + }) + go(Scene.Game) })