From c814f7715e2179f21e6688c178c413728bad581b Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Tue, 6 Jul 2021 22:51:48 +0300 Subject: [PATCH] backgound music --- TODO.md | 5 ++++- app/.gitignore | 1 + app/config.json | 3 ++- js/index.js | 5 +++++ js/music.js | 19 +++++++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 js/music.js diff --git a/TODO.md b/TODO.md index b03c8d0..2743701 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,12 @@ -- several correct answer +- finish game image +- desktop (landscape) orientation +- background music - change quest animation - button hover animation - make docs/ ## done +- several correct answer [498ab55] - loading all quest images with loading [b8aa4b6] - splash (loading) screen [b8aa4b6] - add quest progress [aac38ec] diff --git a/app/.gitignore b/app/.gitignore index 72cc074..3bd626b 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1,2 +1,3 @@ engine.js assets/images/*.jpg +assets/sfx/*.mp3 diff --git a/app/config.json b/app/config.json index 47d01bf..a5eb6d4 100644 --- a/app/config.json +++ b/app/config.json @@ -2,5 +2,6 @@ "gameName": "quizEngine", "gameVersion": [0, 0, 1], "debug": true, - "loaderWidth": 200 + "loaderWidth": 200, + "music": "music.mp3" } diff --git a/js/index.js b/js/index.js index d963967..f2cbe1a 100644 --- a/js/index.js +++ b/js/index.js @@ -5,6 +5,7 @@ import gameData from '../app/gameData.json'; // game data import { getMousePos, isInside } from './buttons.js'; import { clearContext, placeImage, getCenterH, getCenterV } from './draw.js'; +import { playMusic } from './music.js'; import { imagePreloader, checkAnswer, shuffle, restartGame } from './game.js'; // Engine variables ------------------------------------- @@ -18,6 +19,7 @@ let game = {}; // main game variable let areas = { game: {}, finish: {} }; let images = {}; let buttons = {}; +let music = {}; // Init ------------------------------------------------- window.onload = function() { @@ -47,6 +49,9 @@ window.onload = function() { imagePreloader(questImages, function() { game.loadedState = true; }); + music.music = new AudioContext() || new webkitAudioContext(); + playMusic(config, music.music); + game.loadedState = false; game.finish = false; game.currentQuest = 0; diff --git a/js/music.js b/js/music.js new file mode 100644 index 0000000..e1d49f8 --- /dev/null +++ b/js/music.js @@ -0,0 +1,19 @@ +export function playMusic(config, music) { + let request = new XMLHttpRequest(); + + request.open("GET", `assets/sfx/${config.music}`, true); + request.responseType = "arraybuffer"; + request.onload = function(){ + music.decodeAudioData(request.response, onDecoded); + } + + function onDecoded(buffer) { + var bufferSource = music.createBufferSource(); + bufferSource.buffer = buffer; + bufferSource.connect(music.destination); + bufferSource.loop = true; + bufferSource.start(); + } + + request.send(); +}