Skip to content

Commit

Permalink
chore(music): play background music
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 23, 2025
1 parent 3f94498 commit 30db74a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 2 deletions.
Binary file added public/music/background.mp3
Binary file not shown.
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './music'
export * from './scenes'
export * from './sounds'
export * from './sprites'
Expand Down
3 changes: 3 additions & 0 deletions src/constants/music.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum Music {
Background = 'Background',
}
1 change: 1 addition & 0 deletions src/gameobjects/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './bullet'
export * from './enemy'
export * from './health'
export * from './music'
export * from './player'
20 changes: 20 additions & 0 deletions src/gameobjects/music.ts
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 2 additions & 0 deletions src/gameobjects/player.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Scene, Sprite, Tag } from '../constants'
import { addAttack, addCursorKeys } from '../events'
import { stopMusic } from '../gameobjects'

const HEALTH = 100

Expand All @@ -24,6 +25,7 @@ export function addPlayer(x = center().x, y = center().y) {

player.onHurt(() => {
if (player.hp() <= 0) {
stopMusic()
go(Scene.Lose)
}
})
Expand Down
4 changes: 3 additions & 1 deletion src/scenes/game.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
8 changes: 7 additions & 1 deletion src/scenes/preload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Scene, Sound, Sprite } from '../constants'
import { Music, Scene, Sound, Sprite } from '../constants'

scene(Scene.Preload, () => {
const sprites = [
Expand All @@ -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)
})

0 comments on commit 30db74a

Please sign in to comment.