quizEngine/js/index.js

166 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-07-04 18:07:44 +03:00
'use strict';
// Import game configs and questsData -------------------
2021-07-08 00:45:43 +03:00
import config from './config.json'; // game configuration
import gameData from './gameData.json'; // game data
2021-07-04 18:07:44 +03:00
// Import engine libs -----------------------------------
import * as Engine from './engine.js';
import * as Buttons from './buttons.js';
import * as Draw from './draw.js';
import * as Music from './music.js';
import * as Game from './game.js';
2021-07-04 18:07:44 +03:00
2021-07-04 19:42:03 +03:00
// Engine variables -------------------------------------
2021-07-06 05:27:07 +03:00
const DEBUG = config.debug;
2021-07-04 19:42:03 +03:00
let canvas = null; // canvas id
let context = null; // context id
let cW = null; // canvas with
let cH = null; // canvas height
2021-07-06 05:27:07 +03:00
let game = {}; // main game variable
let images = {};
2021-07-06 22:51:48 +03:00
let music = {};
2021-07-06 05:27:07 +03:00
// Engine init ------------------------------------------
2021-07-11 17:39:11 +03:00
// ------------------------------------------------------
2021-07-04 18:07:44 +03:00
window.onload = function() {
2021-07-11 17:58:43 +03:00
Engine.Init(game);
2021-07-11 17:39:11 +03:00
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-06 05:27:07 +03:00
if (DEBUG)
console.log(`Canvas size ${cW} x ${cH}`);
2021-07-11 17:39:11 +03:00
game.orientation = Engine.setOrientation(canvas);
2021-07-04 18:07:44 +03:00
2021-07-11 17:39:11 +03:00
// loading Logo image
let imageLogo = new Image();
imageLogo.src = "assets/logo.png";
images.logo = imageLogo;
2021-07-11 17:39:11 +03:00
// set game loading progress bar
game.loader = { x: Draw.getCenterH(cW, config.loader.w),
y: cH - config.loader.h - 100 };
game.loader.pointer = 0;
game.loader.direction = true;
2021-07-07 14:55:10 +03:00
let loadingImages = [];
2021-07-08 01:38:10 +03:00
loadingImages.push(gameData.result.notPassed, gameData.result.passed);
for (const [key, value] of Object.entries(gameData.questions)) {
2021-07-07 14:55:10 +03:00
loadingImages.push(value.image);
}
Engine.imagePreloader(loadingImages, function() {
2021-07-07 01:06:16 +03:00
game.loadedState = true;
game.showAlpha = 1;
});
2021-07-04 22:12:04 +03:00
// TODO: rewrite playMusic() func
2021-07-06 22:51:48 +03:00
music.music = new AudioContext() || new webkitAudioContext();
Music.playMusic(config, music.music);
2021-07-06 22:51:48 +03:00
2021-07-07 00:04:35 +03:00
// shuffle quests and first quest answers
Engine.shuffle(gameData.questions);
gameData.questions.forEach(quest => {
Engine.shuffle(quest.answer);
});
2021-07-07 00:04:35 +03:00
// set all quest status 'not answered'
gameData.questions.forEach(element => element.status = null);
2021-07-04 22:59:09 +03:00
2021-07-07 01:06:16 +03:00
// hover by buttons
canvas.addEventListener('mousemove', e => {
let mousePos = Buttons.getMousePos(canvas, e);
2021-07-07 01:06:16 +03:00
2021-07-11 17:58:43 +03:00
// NOTE: put your code here
}, false);
2021-07-07 01:06:16 +03:00
2021-07-07 00:04:35 +03:00
// click by buttons
2021-07-07 01:06:16 +03:00
canvas.addEventListener('click', e => {
let mousePos = Buttons.getMousePos(canvas, e);
2021-07-04 19:42:03 +03:00
2021-07-11 17:58:43 +03:00
// NOTE: put your code here
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-11 17:39:11 +03:00
// ------------------------------------------------------
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-11 17:39:11 +03:00
// ------------------------------------------------------
2021-07-04 18:07:44 +03:00
function update() {
2021-07-11 17:39:11 +03:00
// appearance effect --------------------------------
if (game.appearanceAplha >= 0) {
game.appearanceAplha -= 0.01;
2021-07-07 01:06:16 +03:00
}
2021-07-11 17:58:43 +03:00
// game loading -------------------------------------
if (!game.loadedState) {
2021-07-11 17:39:11 +03:00
if (game.loader.pointer < config.loader.w - 50 && game.loader.direction)
game.loader.pointer += 1;
else game.loader.direction = false;
2021-07-11 17:39:11 +03:00
if (!game.loader.direction) {
if (game.loader.pointer <= 0) game.loader.direction = true;
2021-07-11 17:39:11 +03:00
game.loader.pointer -= 1;
}
2021-07-06 05:27:07 +03:00
}
2021-07-11 17:39:11 +03:00
// game ---------------------------------------------
2021-07-06 05:27:07 +03:00
if (game.loadedState && !game.finish) {
2021-07-11 17:39:11 +03:00
// NOTE: put your code here
2021-07-06 05:27:07 +03:00
}
2021-07-11 17:39:11 +03:00
// finish game --------------------------------------
if (game.finish) {
2021-07-11 17:39:11 +03:00
// NOTE: put your code here
}
2021-07-04 18:07:44 +03:00
}
2021-07-04 19:42:03 +03:00
// Draw to canvas func ----------------------------------
2021-07-11 17:39:11 +03:00
// ------------------------------------------------------
2021-07-04 18:07:44 +03:00
function draw() {
Draw.clearContext(canvas, config.colors.back); // clean canvas
2021-07-04 18:07:44 +03:00
2021-07-06 05:27:07 +03:00
// render splash screen -----------------------------
if (!game.loadedState) {
context.drawImage(images.logo, Draw.getCenterH(cW, images.logo.width), Draw.getCenterV(cH, images.logo.height));
2021-07-04 18:07:44 +03:00
2021-07-07 13:39:22 +03:00
context.fillStyle = config.colors.buttonFill;
context.strokeStyle = config.colors.buttonStroke;
context.lineWidth = config.colors.strokeSize;
2021-07-06 05:27:07 +03:00
2021-07-11 17:39:11 +03:00
context.fillRect(game.loader.x + game.loader.pointer, game.loader.y, 50, config.loader.h);
context.strokeRect(game.loader.x, game.loader.y, config.loader.w, config.loader.h);
}
2021-07-07 00:04:35 +03:00
2021-07-11 17:39:11 +03:00
// render game --------------------------------------
if (!game.finish && game.loadedState) {
// NOTE: put your code here
2021-07-04 22:59:09 +03:00
}
2021-07-04 18:07:44 +03:00
2021-07-06 05:27:07 +03:00
// render result game -------------------------------
if (game.finish) {
2021-07-11 17:39:11 +03:00
// NOTE: put your code here
}
2021-07-07 01:06:16 +03:00
// draw alpha animation -----------------------------
2021-07-11 17:39:11 +03:00
if (game.appearanceAplha != null) {
context.fillStyle = `rgba(0,0,0,${game.appearanceAplha})`;
2021-07-07 01:06:16 +03:00
context.fillRect(0, 0, cW, cH);
}
2021-07-04 18:07:44 +03:00
}