diff --git a/app/js/game.js b/app/js/game.js index b9b0597..6009879 100644 --- a/app/js/game.js +++ b/app/js/game.js @@ -1,13 +1,16 @@ // функции игры -// передаёт следующий квест, либо возвращает окончание кввестов -export function nextQuest(questions, questIndex, answer) { - if (questions[questIndex].rightAnswer.toLowerCase() == answer.toLowerCase()) - questions[questIndex].status = true; +export function clickAnswer(questions, game, answer) { + if (questions[game.questIndex].rightAnswer.toLowerCase() == answer.toLowerCase()) { + questions[game.questIndex].status = true; + game.totalRightAnswers += 1; + } else - questions[questIndex].status = false; + questions[game.questIndex].status = false; - if (questIndex < questions.length - 1) { + if (game.questIndex < questions.length - 1) { + game.quest = questions[game.questIndex += 1]; // костыль + shuffleQuestAnswer(questions[game.questIndex].answer); return true; } else { diff --git a/app/js/index.js b/app/js/index.js index 6c372ae..0301917 100644 --- a/app/js/index.js +++ b/app/js/index.js @@ -5,7 +5,7 @@ import gameData from '../gameData.json'; // game data import { getMousePos, isInside } from './buttons.js'; import { clearContext, getCenterH, getCenterV } from './draw.js'; -import { nextQuest, shuffleQuestAnswer } from './game.js'; +import { clickAnswer, shuffleQuestAnswer } from './game.js'; // Engine variables ------------------------------------- let DEBUG = true; @@ -39,6 +39,7 @@ window.onload = function() { questIndex: 0, quest: null, totalRightAnswers: 0, // количество правильных ответов + scene: null, }; game.quest = gameData[game.questIndex]; shuffleQuestAnswer(gameData[game.questIndex].answer); @@ -85,66 +86,22 @@ window.onload = function() { // click by first answer button if (isInside(mousePos, button.answerButtons[0])) { - if (nextQuest(gameData, game.questIndex, button.answerButtons[0].data)) { - game.quest = gameData[game.questIndex += 1]; // костыль - game.totalRightAnswers += 1; - shuffleQuestAnswer(gameData[game.questIndex].answer); - - if (DEBUG) { - console.log("Wow, right answer!!"); - console.log(`Total right answers ${game.totalRightAnswers}.`) - } - } - else - console.log("quest ALL end"); + clickAnswer(gameData, game, button.answerButtons[0].data); } // click by second answer button if (isInside(mousePos, button.answerButtons[1])) { - if (nextQuest(gameData, game.questIndex, button.answerButtons[1].data)) { - game.quest = gameData[game.questIndex += 1]; - game.totalRightAnswers += 1; - shuffleQuestAnswer(gameData[game.questIndex].answer); - - if (DEBUG) { - console.log("Wow, right answer!!"); - console.log(`Total right answers ${game.totalRightAnswers}.`) - } - } - else - console.log("quest ALL end"); + clickAnswer(gameData, game, button.answerButtons[1].data); } // click by third answer button if (isInside(mousePos, button.answerButtons[2])) { - if (nextQuest(gameData, game.questIndex, button.answerButtons[2].data)) { - game.quest = gameData[game.questIndex += 1]; - game.totalRightAnswers += 1; - shuffleQuestAnswer(gameData[game.questIndex].answer); - - if (DEBUG) { - console.log("Wow, right answer!!"); - console.log(`Total right answers ${game.totalRightAnswers}.`) - } - } - else - console.log("quest ALL end"); + clickAnswer(gameData, game, button.answerButtons[2].data); } // click by four answer button if (isInside(mousePos, button.answerButtons[3])) { - if (nextQuest(gameData, game.questIndex, button.answerButtons[3].data)) { - game.quest = gameData[game.questIndex += 1]; - game.totalRightAnswers += 1; - shuffleQuestAnswer(gameData[game.questIndex].answer); - - if (DEBUG) { - console.log("Wow, right answer!!"); - console.log(`Total right answers ${game.totalRightAnswers}.`) - } - } - else - console.log("quest ALL end"); + clickAnswer(gameData, game, button.answerButtons[3].data); } }, false);