From 62ecfa90582752c0c7aa4fba616de5d1b43ba59e Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Wed, 7 Jul 2021 00:04:35 +0300 Subject: [PATCH] ui buttons --- TODO.md | 2 ++ app/config.json | 2 +- js/index.js | 43 ++++++++++++++++++++++++++++++++++++++----- js/music.js | 10 ++++------ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/TODO.md b/TODO.md index f7d26f8..0466968 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,11 @@ ## TODO: +- info widget - finish game image - desktop (landscape) orientation - change quest animation - button hover animation - make docs/ +- make quests editor ## Done - background music [c814f77] diff --git a/app/config.json b/app/config.json index c733da0..a5eb6d4 100644 --- a/app/config.json +++ b/app/config.json @@ -3,5 +3,5 @@ "gameVersion": [0, 0, 1], "debug": true, "loaderWidth": 200, - "music": { "src": "music.mp3", "autoplay": true } + "music": "music.mp3" } diff --git a/js/index.js b/js/index.js index f2cbe1a..909ff4d 100644 --- a/js/index.js +++ b/js/index.js @@ -16,9 +16,10 @@ let cW = null; // canvas with let cH = null; // canvas height let landscape_orientation = null; // canvas orientation let game = {}; // main game variable -let areas = { game: {}, finish: {} }; +let areas = { splash: {}, game: {}, finish: {} }; let images = {}; let buttons = {}; +let buttonsUi = {}; let music = {}; // Init ------------------------------------------------- @@ -56,10 +57,11 @@ window.onload = function() { game.finish = false; game.currentQuest = 0; - shuffle(gameData.questions); // shuffle quests - shuffle(gameData.questions[game.currentQuest].answer); // shuffle first quest answers + // shuffle quests and first quest answers + shuffle(gameData.questions); + shuffle(gameData.questions[game.currentQuest].answer); - // присваем всем квестам статус не выполнен + // set all quest status 'not answered' gameData.questions.forEach(element => element.status = null); // set areas sizes @@ -91,10 +93,25 @@ window.onload = function() { // TODO: add areas for landscape mode } - // click by buttons?! + // click by buttons canvas.addEventListener('click', function(evt) { let mousePos = getMousePos(canvas, evt); + if (game.loadedState) { + for (const [key, value] of Object.entries(buttonsUi)) { + if (isInside(mousePos, value)) { + // Music button + if (key == "uiMusic") { + music.music.suspend(); + + if (music.music.state == "suspended") { + music.music.resume(); + } + } + } + } + } + for (const [key, value] of Object.entries(buttons)) { if (!game.finish && key != undefined) { if (isInside(mousePos, value)) { @@ -164,6 +181,11 @@ function update() { answerButtonsArray.forEach(function callback(value, index) { value.data = gameData.questions[game.currentQuest].answer[index]; }); + + buttonsUi.uiMusic = { x: areas.game.btnUi.x, y: areas.game.btnUi.y, w: areas.game.btnUi.h, h: areas.game.btnUi }; + buttonsUi.uiInfo = { x: areas.game.btnUi.x, y: areas.game.btnUi.y, w: areas.game.btnUi.h, h: areas.game.btnUi.h }; + buttonsUi.uiMusic = { x: areas.game.btnUi.w + areas.game.btnUi.x - areas.game.btnUi.h, y: areas.game.btnUi.y, w: areas.game.btnUi.h, h: areas.game.btnUi.h }; + buttonsUi.uiSfx = { x: areas.game.btnUi.w + areas.game.btnUi.x - (areas.game.btnUi.h * 2) - 10, y: areas.game.btnUi.y, w: areas.game.btnUi.h, h: areas.game.btnUi.h }; } if (game.finish) { @@ -254,6 +276,16 @@ function draw() { answerButtonsArray.forEach(function callback(value) { context.fillText(value.data, cW / 2, value.y + 35); }); + + // draw UI buttons + context.fillStyle = "purple"; + context.strokeStyle = "navy"; + context.lineWidth = 2; + + for (const [key, value] of Object.entries(buttonsUi)) { + context.fillRect(value.x, value.y, value.w, value.h); + context.strokeRect(value.x, value.y, value.w, value.h); + } } // render result game ------------------------------- @@ -300,6 +332,7 @@ function draw() { context.fillStyle = "white"; context.fillText(buttons.btnRestart.data, cW / 2, buttons.btnRestart.y + 43); } + // draw game areas ---------------------------------- if (DEBUG && !game.finish && game.loadedState) { context.strokeStyle = "red"; diff --git a/js/music.js b/js/music.js index 4802401..c54716f 100644 --- a/js/music.js +++ b/js/music.js @@ -1,20 +1,18 @@ export function playMusic(config, music) { let request = new XMLHttpRequest(); - request.open("GET", `assets/sfx/${config.music.src}`, true); + request.open("GET", `assets/sfx/${config.music}`, true); request.responseType = "arraybuffer"; - request.onload = function(){ + request.onload = function() { music.decodeAudioData(request.response, onDecoded); } function onDecoded(buffer) { - var bufferSource = music.createBufferSource(); + let bufferSource = music.createBufferSource(); bufferSource.buffer = buffer; bufferSource.connect(music.destination); bufferSource.loop = true; - - if (config.music.autoplay) - bufferSource.start(); + bufferSource.start(); } request.send();