Skip to content

Commit

Permalink
refactor(gameobjects): export music
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 28, 2025
1 parent 65fa6da commit 67f9cfe
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
23 changes: 4 additions & 19 deletions src/gameobjects/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,12 @@ import type { AudioPlay } from 'kaplay'

import { Music } from '../constants'

let music: AudioPlay
export let music: AudioPlay

export function playMusic() {
if (music) {
return music.play()
}

const currentMusic = play(Music.Background, {
export function addMusic() {
music = play(Music.Background, {
loop: true,
paused: true,
})

currentMusic.volume = 0.5
setMusic(currentMusic)
playMusic()
}

function setMusic(currentMusic: AudioPlay) {
music = currentMusic
}

export function stopMusic() {
music?.stop()
music.volume = 0.5
}
4 changes: 2 additions & 2 deletions src/gameobjects/pause.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TweenController } from 'kaplay'

import { Scene } from '../constants'
import { addButton, game, stopMusic } from '.'
import { addButton, game, music } from '.'

export function addPause() {
let currentTween: TweenController
Expand Down Expand Up @@ -50,7 +50,7 @@ export function addPause() {
text: 'Exit',
onClick() {
go(Scene.Title)
stopMusic()
music.stop()
},
parent: pauseMenu,
})
Expand Down
4 changes: 2 additions & 2 deletions src/gameobjects/player.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Expression, Scene, Sound, Sprite, Tag } from '../constants'
import { addAttack, addCursorKeys } from '../events'
import { getAvatar, getChildBubble, stopMusic } from '../gameobjects'
import { getAvatar, getChildBubble, music } from '../gameobjects'
import { gameState } from '../helpers'
import type { Player } from '../types'
import { game } from '.'
Expand Down Expand Up @@ -48,7 +48,7 @@ export function addPlayer(x = center().x, y = center().y) {
player.destroy()

wait(3, () => {
stopMusic()
music.stop()
play(Sound.Whoosh)
go(Scene.Lose)
})
Expand Down
9 changes: 6 additions & 3 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import {
addEnemy,
addGame,
addHealth,
addMusic,
addPause,
addPlayer,
addScore,
addText,
game,
playMusic,
music,
} from '../gameobjects'
import { gameState, levels } from '../helpers'

scene(Scene.Game, () => {
addGame()
gameState.init()

addMusic()
music.play()

addText({
width: 600,
height: 100,
Expand All @@ -27,7 +30,7 @@ scene(Scene.Game, () => {
})

addScore()
playMusic()
addGame()
addPause()

const player = addPlayer()
Expand Down

0 comments on commit 67f9cfe

Please sign in to comment.