quizEngine/app/js/index.js

249 lines
7.2 KiB
JavaScript
Raw Normal View History

2021-07-04 18:07:44 +03:00
'use strict';
2021-07-04 19:42:03 +03:00
import config from '../config.json'; // game configuration
import gameData from '../gameData.json'; // game data
2021-07-04 18:07:44 +03:00
2021-07-04 19:42:03 +03:00
import { getMousePos, isInside } from './buttons.js';
2021-07-04 21:07:22 +03:00
import { clearContext, getCenterH, getCenterV } from './draw.js';
2021-07-04 22:59:09 +03:00
import { nextQuest } from './game.js';
2021-07-04 18:07:44 +03:00
2021-07-04 19:42:03 +03:00
// Engine variables -------------------------------------
let DEBUG = true;
let canvas = null; // canvas id
let context = null; // context id
let cW = null; // canvas with
let cH = null; // canvas height
let orientation = null; // screen orientation
let button = null; // buttons array
let area = null; // game areas (buttons, images, etc.)
2021-07-04 21:28:50 +03:00
let game = null;
2021-07-04 19:42:03 +03:00
// Init -------------------------------------------------
2021-07-04 18:07:44 +03:00
window.onload = function() {
2021-07-04 19:42:03 +03:00
// init canvas id and sizes
2021-07-04 18:07:44 +03:00
canvas = document.getElementById('game');
context = canvas.getContext('2d');
cW = canvas.width;
cH = canvas.height;
2021-07-04 19:42:03 +03:00
// set screen orientation by carculate canvas width&height
if (cW >= cH) { orientation = true; }
else { orientation = false; }
2021-07-04 18:07:44 +03:00
2021-07-04 22:12:04 +03:00
if (DEBUG) {
console.log(`Loaded ${gameData.length} quests.`);
}
2021-07-04 21:28:50 +03:00
game = {
// TODO: change quest by script
2021-07-04 22:12:04 +03:00
questIndex: 0,
quest: null,
2021-07-04 22:59:09 +03:00
totalRightAnswers: 0, // количество правильных ответов
2021-07-04 21:28:50 +03:00
};
2021-07-04 22:12:04 +03:00
game.quest = gameData[game.questIndex];
2021-07-04 21:28:50 +03:00
2021-07-04 22:59:09 +03:00
// присваем всем квестам статус не выполнен
gameData.forEach(element => element.status = null);
if (!orientation) {
area = {
answerButtons: { x: 10, y: cH - 340, w: cW - 20, h: 250 },
questionLabel: { x: 10, y: cH - 340 - 80, w: cW - 20, h: 70 },
uiButtons: { x: 10, y: cH - 80, w: cW - 20, h: 70 },
questProgress: { x: 10, y: 10, w: cW - 20, h: 20 },
}
2021-07-04 21:07:22 +03:00
}
2021-07-04 22:59:09 +03:00
// TODO: add areas for landscape mode
2021-07-04 21:07:22 +03:00
2021-07-04 18:07:44 +03:00
button = {
info: { x: 10, y: cH - 80, w: 70, h: 70 },
sfx: { x: cW - 80, y: cH - 80, w: 70, h: 70 },
2021-07-04 21:07:22 +03:00
// TODO: change data: to null
answerButtons: [
2021-07-04 21:28:50 +03:00
{ x: getCenterH(cW, cW / 1.5), y: 0, w: cW / 1.5, h: 50, data: null },
{ x: getCenterH(cW, cW / 1.5), y: 0, w: cW / 1.5, h: 50, data: null },
{ x: getCenterH(cW, cW / 1.5), y: 0, w: cW / 1.5, h: 50, data: null },
{ x: getCenterH(cW, cW / 1.5), y: 0, w: cW / 1.5, h: 50, data: null },
2021-07-04 21:07:22 +03:00
],
2021-07-04 18:07:44 +03:00
}
2021-07-04 21:07:22 +03:00
button.answerButtons.forEach(function callback(value, index, array) {
if (index == 0)
array[index].y = area.answerButtons.y;
else
array[index].y = array[index - 1].y + value.h + 15;
});
2021-07-04 19:42:03 +03:00
canvas.addEventListener('click', function(evt) {
let mousePos = getMousePos(canvas, evt);
// bet inscrease
if (isInside(mousePos, button.info)) {
console.log("info");
}
2021-07-04 21:37:21 +03:00
// click by first answer button
if (isInside(mousePos, button.answerButtons[0])) {
2021-07-04 22:59:09 +03:00
if (nextQuest(gameData, game.questIndex, button.answerButtons[0].data)) {
game.quest = gameData[game.questIndex += 1];
game.totalRightAnswers += 1;
if (DEBUG) {
console.log("Wow, right answer!!");
console.log(`Total right answers ${game.totalRightAnswers}.`)
}
2021-07-04 22:12:04 +03:00
}
2021-07-04 22:59:09 +03:00
else
console.log("quest ALL end");
2021-07-04 21:37:21 +03:00
}
// click by second answer button
if (isInside(mousePos, button.answerButtons[1])) {
2021-07-04 22:59:09 +03:00
if (nextQuest(gameData, game.questIndex, button.answerButtons[1].data)) {
game.quest = gameData[game.questIndex += 1];
game.totalRightAnswers += 1;
if (DEBUG) {
console.log("Wow, right answer!!");
console.log(`Total right answers ${game.totalRightAnswers}.`)
}
}
else
console.log("quest ALL end");
2021-07-04 21:37:21 +03:00
}
// click by third answer button
if (isInside(mousePos, button.answerButtons[2])) {
2021-07-04 22:59:09 +03:00
if (nextQuest(gameData, game.questIndex, button.answerButtons[2].data)) {
game.quest = gameData[game.questIndex += 1];
game.totalRightAnswers += 1;
if (DEBUG) {
console.log("Wow, right answer!!");
console.log(`Total right answers ${game.totalRightAnswers}.`)
}
}
else
console.log("quest ALL end");
2021-07-04 21:37:21 +03:00
}
// click by four answer button
if (isInside(mousePos, button.answerButtons[3])) {
2021-07-04 22:59:09 +03:00
if (nextQuest(gameData, game.questIndex, button.answerButtons[3].data)) {
game.quest = gameData[game.questIndex += 1];
game.totalRightAnswers += 1;
if (DEBUG) {
console.log("Wow, right answer!!");
console.log(`Total right answers ${game.totalRightAnswers}.`)
}
}
else
console.log("quest ALL end");
2021-07-04 21:37:21 +03:00
}
2021-07-04 19:42:03 +03:00
}, false);
2021-07-04 18:07:44 +03:00
window.requestAnimationFrame(gameLoop);
};
2021-07-04 19:42:03 +03:00
// GameLoop ---------------------------------------------
2021-07-04 18:07:44 +03:00
function gameLoop(timeStamp) {
update();
draw();
window.requestAnimationFrame(gameLoop);
}
2021-07-04 19:42:03 +03:00
// Game update func -------------------------------------
2021-07-04 18:07:44 +03:00
function update() {
2021-07-04 21:28:50 +03:00
// Update answer buttons label
button.answerButtons.forEach(function callback(value, index) {
value.data = game.quest.answer[index];
});
2021-07-04 18:07:44 +03:00
}
2021-07-04 19:42:03 +03:00
// Draw to canvas func ----------------------------------
2021-07-04 18:07:44 +03:00
function draw() {
2021-07-04 19:42:03 +03:00
clearContext(canvas, config); // flush?! canvas
// draw question label
2021-07-04 18:07:44 +03:00
context.font = "32px Ubuntu";
context.textAlign = "center";
context.fillStyle = "white";
2021-07-04 22:12:04 +03:00
context.fillText(game.quest.question, cW / 2, area.questionLabel.y + 30);
2021-07-04 18:07:44 +03:00
// draw answer buttons
2021-07-04 21:28:50 +03:00
context.fillStyle = "purple";
context.strokeStyle = "navy";
context.lineWidth = 2;
2021-07-04 18:07:44 +03:00
2021-07-04 21:07:22 +03:00
for (let i = 0; i <= button.answerButtons.length - 1; i++) {
context.fillRect(button.answerButtons[i].x, button.answerButtons[i].y,
button.answerButtons[i].w, button.answerButtons[i].h);
2021-07-04 21:28:50 +03:00
context.strokeRect(button.answerButtons[i].x, button.answerButtons[i].y,
button.answerButtons[i].w, button.answerButtons[i].h);
2021-07-04 18:07:44 +03:00
}
2021-07-04 21:28:50 +03:00
// draw answer button label
2021-07-04 18:07:44 +03:00
context.font = "32px Ubuntu";
context.textAlign = "center";
context.fillStyle = "white";
2021-07-04 21:07:22 +03:00
button.answerButtons.forEach(function callback(value) {
context.fillText(value.data, cW / 2, value.y + 35);
});
2021-07-04 18:07:44 +03:00
2021-07-04 22:59:09 +03:00
// draw progress bar
let sizeProgressItem = area.questProgress.w / gameData.length;
context.strokeStyle = "black";
// for (const [key, value] of Object.entries(area)) {
// context.strokeRect(value.x, value.y, value.w, value.h);
// }
for (let i = 0; i < gameData.length; i++) {
// change progress item color by status answer
switch (gameData[i].status) {
case null:
context.fillStyle = "gray";
break;
case true:
context.fillStyle = "green";
break;
case false:
context.fillStyle = "red";
break;
}
context.fillRect(10 + (i * sizeProgressItem), 10, sizeProgressItem, 20);
context.strokeRect(10 + (i * sizeProgressItem), 10, sizeProgressItem, 20);
}
2021-07-04 18:07:44 +03:00
// UI ------------------------------------------
2021-07-04 21:28:50 +03:00
// TODO: переписать это всё г*
2021-07-04 18:07:44 +03:00
context.fillStyle = "red";
context.strokeStyle = "navy";
2021-07-04 21:28:50 +03:00
context.lineWidth = 2;
2021-07-04 18:07:44 +03:00
context.fillRect(button.info.x, button.info.y, button.info.w, button.info.h); // info button
context.strokeRect(button.info.x, button.info.y, button.info.w, button.info.h); // info button
context.fillRect(button.sfx.x, button.sfx.y, button.sfx.w, button.sfx.h); // sfx button
context.strokeRect(button.sfx.x, button.sfx.y, button.sfx.w, button.sfx.h); // sfx button
2021-07-04 22:59:09 +03:00
// draw game areas
if (DEBUG) {
context.strokeStyle = "red";
context.lineWidth = 2;
// answer buttons area
if (orientation)
// TODO: draw answer buttons area by landscape
console.log('TODO: draw answer buttons area by landscape');
else
for (const [key, value] of Object.entries(area)) {
context.strokeRect(value.x, value.y, value.w, value.h);
}
}
2021-07-04 18:07:44 +03:00
}