From 67f9cfef4c738404d31e6ec11e4716024c7c212a Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 27 Jan 2025 21:53:26 -0500 Subject: [PATCH] refactor(gameobjects): export music --- src/gameobjects/music.ts | 23 ++++------------------- src/gameobjects/pause.ts | 4 ++-- src/gameobjects/player.ts | 4 ++-- src/scenes/game.ts | 9 ++++++--- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/gameobjects/music.ts b/src/gameobjects/music.ts index bffb161..4328645 100644 --- a/src/gameobjects/music.ts +++ b/src/gameobjects/music.ts @@ -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 } diff --git a/src/gameobjects/pause.ts b/src/gameobjects/pause.ts index 8381777..db79952 100644 --- a/src/gameobjects/pause.ts +++ b/src/gameobjects/pause.ts @@ -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 @@ -50,7 +50,7 @@ export function addPause() { text: 'Exit', onClick() { go(Scene.Title) - stopMusic() + music.stop() }, parent: pauseMenu, }) diff --git a/src/gameobjects/player.ts b/src/gameobjects/player.ts index f4406b8..173b576 100644 --- a/src/gameobjects/player.ts +++ b/src/gameobjects/player.ts @@ -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 '.' @@ -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) }) diff --git a/src/scenes/game.ts b/src/scenes/game.ts index 857ad3e..4295ff4 100644 --- a/src/scenes/game.ts +++ b/src/scenes/game.ts @@ -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, @@ -27,7 +30,7 @@ scene(Scene.Game, () => { }) addScore() - playMusic() + addGame() addPause() const player = addPlayer()