2021-07-04 18:07:44 +03:00
'use strict' ;
2021-07-06 03:31:10 +03:00
import config from '../app/config.json' ; // game configuration
import gameData from '../app/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-06 09:45:08 +03:00
import { clearContext , placeImage , getCenterH , getCenterV } from './draw.js' ;
import { imagePreloader , checkAnswer , shuffle , restartGame } 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 landscape _orientation = null ; // canvas orientation
let game = { } ; // main game variable
2021-07-06 05:36:07 +03:00
let areas = { game : { } , finish : { } } ;
2021-07-06 05:27:07 +03:00
let images = { } ;
let buttons = { } ;
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-06 05:27:07 +03:00
if ( DEBUG )
console . log ( ` Canvas size ${ cW } x ${ cH } ` ) ;
2021-07-04 18:07:44 +03:00
2021-07-06 05:27:07 +03:00
// set screen orientation by carculate canvas width & height
if ( cW >= cH ) { landscape _orientation = true ; }
else { landscape _orientation = false ; }
if ( DEBUG )
console . log ( landscape _orientation ? "Canvas orientation set to landscape" : "Canvas orientation set to portrait" ) ;
2021-07-04 18:07:44 +03:00
2021-07-06 09:45:08 +03:00
let imageLogo = new Image ( ) ;
imageLogo . src = "assets/logo.png" ;
images . logo = imageLogo ;
let questImages = [ ] ;
for ( const [ key , value ] of Object . entries ( gameData . questions ) ) {
questImages . push ( value . image ) ;
}
imagePreloader ( questImages , function ( ) { game . loadedState = true ; } ) ;
2021-07-04 22:12:04 +03:00
2021-07-06 05:27:07 +03:00
game . loadedState = false ;
2021-07-06 05:36:07 +03:00
game . finish = false ;
2021-07-06 05:27:07 +03:00
game . currentQuest = 0 ;
2021-07-06 09:45:08 +03:00
shuffle ( gameData . questions ) ; // shuffle quests
2021-07-06 05:36:07 +03:00
shuffle ( gameData . questions [ game . currentQuest ] . answer ) ; // shuffle first quest answers
2021-07-04 21:28:50 +03:00
2021-07-04 22:59:09 +03:00
// присваем всем квестам статус не выполнен
2021-07-06 05:36:07 +03:00
gameData . questions . forEach ( element => element . status = null ) ;
2021-07-04 22:59:09 +03:00
2021-07-06 05:36:07 +03:00
// set areas sizes
2021-07-06 05:27:07 +03:00
if ( ! landscape _orientation ) {
2021-07-06 05:36:07 +03:00
areas . game = {
2021-07-06 04:09:59 +03:00
// { x: 0, y: 0, w: 0, h: 0 }
2021-07-06 05:27:07 +03:00
btnAnswer : { x : 10 , y : cH - 340 , w : cW - 20 , h : 250 } ,
labelQuestion : { x : 10 , y : cH - 340 - 80 , w : cW - 20 , h : 70 } ,
btnUi : { x : 10 , y : cH - 80 , w : cW - 20 , h : 70 } ,
2021-07-04 22:59:09 +03:00
questProgress : { x : 10 , y : 10 , w : cW - 20 , h : 20 } ,
}
2021-07-06 05:36:07 +03:00
areas . game . questImage = { x : 10 , y : areas . game . questProgress . y + areas . game . questProgress . h + 10 ,
w : cW - 20 , h : areas . game . labelQuestion . y - areas . game . questProgress . y - ( areas . game . questProgress . h * 2 ) } ;
2021-07-06 09:45:08 +03:00
let progressBarHeight = cH - ( getCenterV ( cH , images . logo . height ) + images . logo . height ) > 301 ?
getCenterV ( cH , images . logo . height ) + images . logo . height + 70 : cH - 70 ;
areas . splash = {
border : { x : getCenterH ( cW , config . loaderWidth ) , y : progressBarHeight , w : config . loaderWidth , h : 20 } ,
pointer : { position : 0 , direction : true } ,
}
2021-07-06 05:36:07 +03:00
areas . finish . labelFinishGameName = { x : 10 , y : 60 , w : cW - 20 , h : 30 } ;
areas . finish . labelTotalAnswerPercent = { x : 10 , y : getCenterV ( cH , 80 ) , w : cW - 20 , h : 80 } ;
areas . finish . labelTotalAnswerRight = { x : 10 , y : areas . finish . labelTotalAnswerPercent . y - 70 , w : cW - 20 , h : 60 } ;
areas . finish . labelTotalInfo = { x : 10 , y : areas . finish . labelTotalAnswerPercent . y + areas . finish . labelTotalAnswerPercent . h + 10 , w : cW - 20 , h : 90 } ;
}
else {
// TODO: add areas for landscape mode
2021-07-04 21:07:22 +03:00
}
2021-07-06 05:36:07 +03:00
// click by buttons?!
2021-07-04 19:42:03 +03:00
canvas . addEventListener ( 'click' , function ( evt ) {
let mousePos = getMousePos ( canvas , evt ) ;
2021-07-06 05:27:07 +03:00
for ( const [ key , value ] of Object . entries ( buttons ) ) {
2021-07-06 06:10:22 +03:00
if ( ! game . finish && key != undefined ) {
if ( isInside ( mousePos , value ) ) {
checkAnswer ( gameData . questions [ game . currentQuest ] , value . data ) ;
if ( game . currentQuest < gameData . questions . length - 1 ) {
game . currentQuest += 1 ;
}
else game . finish = true ;
2021-07-06 04:09:59 +03:00
}
2021-07-06 06:10:22 +03:00
}
}
if ( game . finish && buttons . btnRestart != undefined ) {
if ( isInside ( mousePos , buttons . btnRestart ) ) {
restartGame ( game , gameData . questions ) ;
delete buttons . btnRestart ;
2021-07-06 05:27:07 +03:00
}
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-06 09:45:08 +03:00
if ( ! game . loadedState ) {
if ( areas . splash . pointer . position < areas . splash . border . w - 50 && areas . splash . pointer . direction ) {
areas . splash . pointer . position += 1 ;
}
else areas . splash . pointer . direction = false ;
if ( ! areas . splash . pointer . direction ) {
if ( areas . splash . pointer . position <= 0 )
areas . splash . pointer . direction = true ;
areas . splash . pointer . position -= 1 ;
}
2021-07-06 05:27:07 +03:00
}
if ( game . loadedState && ! game . finish ) {
buttons . answerButton0 = { x : getCenterH ( cW , cW / 1.5 ) , y : 0 , w : cW / 1.5 , h : 50 , data : null } ;
buttons . answerButton1 = { x : getCenterH ( cW , cW / 1.5 ) , y : 0 , w : cW / 1.5 , h : 50 , data : null } ;
buttons . answerButton2 = { x : getCenterH ( cW , cW / 1.5 ) , y : 0 , w : cW / 1.5 , h : 50 , data : null } ;
buttons . answerButton3 = { x : getCenterH ( cW , cW / 1.5 ) , y : 0 , w : cW / 1.5 , h : 50 , data : null } ;
let answerButtonsArray = [ buttons . answerButton0 , buttons . answerButton1 ,
buttons . answerButton2 , buttons . answerButton3 ] ;
answerButtonsArray . forEach ( function callback ( value , index , array ) {
if ( index == 0 )
2021-07-06 05:36:07 +03:00
array [ index ] . y = areas . game . btnAnswer . y ;
2021-07-06 05:27:07 +03:00
else
array [ index ] . y = array [ index - 1 ] . y + value . h + 15 ;
} ) ;
// Update answer buttons label
answerButtonsArray . forEach ( function callback ( value , index ) {
2021-07-06 05:36:07 +03:00
value . data = gameData . questions [ game . currentQuest ] . answer [ index ] ;
2021-07-06 05:27:07 +03:00
} ) ;
}
2021-07-06 05:36:07 +03:00
if ( game . finish ) {
2021-07-06 06:10:22 +03:00
delete buttons . answerButton0 ;
delete buttons . answerButton1 ;
delete buttons . answerButton2 ;
delete buttons . answerButton3 ;
2021-07-06 05:36:07 +03:00
buttons . btnRestart = { x : getCenterH ( cW , cW / 1.5 ) , y : areas . finish . labelTotalInfo . y + areas . finish . labelTotalInfo . h + 20 , w : cW / 1.5 , h : 70 , data : "Ответить на вопросы заново" } ;
}
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-06 05:27:07 +03:00
clearContext ( canvas ) ; // 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 , getCenterH ( cW , images . logo . width ) , getCenterV ( cH , images . logo . height ) ) ;
2021-07-04 18:07:44 +03:00
2021-07-06 09:45:08 +03:00
context . font = "32px Yanone Kaffeesatz" ;
context . textAlign = "center" ;
context . fillStyle = "white" ;
context . fillRect ( areas . splash . pointer . position + areas . splash . border . x , areas . splash . border . y , 50 , areas . splash . border . h ) ;
context . strokeRect ( areas . splash . border . x , areas . splash . border . y ,
areas . splash . border . w , areas . splash . border . h ) ;
2021-07-06 05:27:07 +03:00
context . strokeStyle = "black" ;
context . lineWidth = 2 ;
context . fillStyle = "yellow" ;
2021-07-05 00:35:00 +03:00
}
2021-07-06 05:27:07 +03:00
// render game --------------------------------------
if ( ! game . finish && game . loadedState ) {
// draw progress bar
2021-07-06 05:36:07 +03:00
let sizeProgressItem = areas . game . questProgress . w / gameData . questions . length ;
2021-07-06 05:27:07 +03:00
context . strokeStyle = "black" ;
2021-07-06 05:36:07 +03:00
for ( let i = 0 ; i < gameData . questions . length ; i ++ ) {
2021-07-06 05:27:07 +03:00
// change progress item color by status answer
2021-07-06 05:36:07 +03:00
switch ( gameData . questions [ i ] . status ) {
2021-07-06 05:27:07 +03:00
case null :
context . fillStyle = "gray" ;
break ;
case true :
context . fillStyle = "green" ;
break ;
case false :
context . fillStyle = "red" ;
break ;
}
2021-07-05 00:35:00 +03:00
2021-07-06 05:27:07 +03:00
context . fillRect ( 10 + ( i * sizeProgressItem ) , 10 , sizeProgressItem , 20 ) ;
context . strokeRect ( 10 + ( i * sizeProgressItem ) , 10 , sizeProgressItem , 20 ) ;
2021-07-04 22:59:09 +03:00
}
2021-07-06 09:45:08 +03:00
// draw quest image
let i = new Image ( ) ;
i . src = ` assets/images/ ${ gameData . questions [ game . currentQuest ] . image } ` ;
placeImage ( canvas , areas . game . questImage , i ) ;
2021-07-06 05:27:07 +03:00
// draw question label
2021-07-06 05:36:07 +03:00
context . font = "32px Yanone Kaffeesatz" ;
2021-07-06 05:27:07 +03:00
context . textAlign = "center" ;
context . fillStyle = "white" ;
2021-07-06 05:36:07 +03:00
context . fillText ( gameData . questions [ game . currentQuest ] . question , cW / 2 , areas . game . labelQuestion . y + 30 ) ;
2021-07-06 05:27:07 +03:00
// draw answer buttons
context . fillStyle = "purple" ;
context . strokeStyle = "navy" ;
context . lineWidth = 2 ;
let answerButtonsArray = [ buttons . answerButton0 , buttons . answerButton1 ,
buttons . answerButton2 , buttons . answerButton3 ] ;
answerButtonsArray . forEach ( function ( btn ) {
context . fillRect ( btn . x , btn . y , btn . w , btn . h ) ;
context . strokeRect ( btn . x , btn . y , btn . w , btn . h ) ;
} ) ;
// draw answer button label
2021-07-06 05:36:07 +03:00
context . font = "32px Yanone Kaffeesatz" ;
2021-07-06 05:27:07 +03:00
context . textAlign = "center" ;
context . fillStyle = "white" ;
answerButtonsArray . forEach ( function callback ( value ) {
context . fillText ( value . data , cW / 2 , value . y + 35 ) ;
} ) ;
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-06 05:36:07 +03:00
// draw game name label
context . font = "32px Yanone Kaffeesatz" ;
context . textAlign = "center" ;
context . fillStyle = "white" ;
context . fillText ( config [ 'gameName' ] , cW / 2 , areas . finish . labelFinishGameName . y + 25 ) ;
2021-07-06 09:45:08 +03:00
// draw labelTotalAnswerRight
2021-07-06 05:36:07 +03:00
let rightAnswer = 0 ;
gameData . questions . forEach ( element => element . status ? rightAnswer += 1 : null ) ;
context . font = "50px Yanone Kaffeesatz" ;
context . fillText ( ` Вы ответили правильно на ${ rightAnswer } из ${ gameData . questions . length } вопросов ` , cW / 2 , areas . finish . labelTotalAnswerRight . y + 45 ) ;
2021-07-06 05:27:07 +03:00
2021-07-06 05:36:07 +03:00
// draw labelTotalAnswerPercent
let rightAnswerPercentage = Math . ceil ( rightAnswer / gameData . questions . length * 100 ) ;
context . font = "75px Yanone Kaffeesatz" ;
context . fillText ( ` ${ rightAnswerPercentage } % ` , cW / 2 , areas . finish . labelTotalAnswerPercent . y + 65 ) ;
// labelTotalInfo
context . font = "50px Yanone Kaffeesatz" ;
let resultInfo = null ;
for ( const [ key , value ] of Object . entries ( gameData . answerResult ) ) {
if ( key <= rightAnswerPercentage ) {
resultInfo = value ;
}
}
context . fillText ( resultInfo , cW / 2 , areas . finish . labelTotalInfo . y + 55 ) ;
// draw btnRestart
context . fillStyle = "purple" ;
context . strokeStyle = "navy" ;
context . fillRect ( buttons . btnRestart . x , buttons . btnRestart . y , buttons . btnRestart . w , buttons . btnRestart . h ) ;
context . strokeRect ( buttons . btnRestart . x , buttons . btnRestart . y , buttons . btnRestart . w , buttons . btnRestart . h ) ;
context . font = "32px Yanone Kaffeesatz" ;
context . textAlign = "center" ;
context . fillStyle = "white" ;
context . fillText ( buttons . btnRestart . data , cW / 2 , buttons . btnRestart . y + 43 ) ;
}
2021-07-06 05:27:07 +03:00
// draw game areas ----------------------------------
if ( DEBUG && ! game . finish && game . loadedState ) {
2021-07-04 22:59:09 +03:00
context . strokeStyle = "red" ;
2021-07-06 05:27:07 +03:00
context . lineWidth = 1 ;
2021-07-04 22:59:09 +03:00
2021-07-06 05:27:07 +03:00
if ( landscape _orientation )
2021-07-06 04:09:59 +03:00
// TODO: draw areas by landscape
2021-07-04 22:59:09 +03:00
console . log ( 'TODO: draw answer buttons area by landscape' ) ;
else
2021-07-06 05:36:07 +03:00
for ( const [ key , value ] of Object . entries ( areas . game ) ) {
2021-07-04 22:59:09 +03:00
context . strokeRect ( value . x , value . y , value . w , value . h ) ;
}
}
2021-07-06 04:09:59 +03:00
else if ( DEBUG && game . finish ) {
context . strokeStyle = "red" ;
context . lineWidth = 1 ;
if ( landscape _orientation )
// TODO: draw areas by landscape
console . log ( 'TODO: draw answer buttons area by landscape' ) ;
else
2021-07-06 05:36:07 +03:00
for ( const [ key , value ] of Object . entries ( areas . finish ) ) {
context . strokeRect ( value . x , value . y , value . w , value . h ) ;
}
2021-07-06 04:09:59 +03:00
}
2021-07-04 18:07:44 +03:00
}