File indexing completed on 2024-04-28 15:07:47

0001 /* GCompris - clockgame.js
0002  *
0003  * SPDX-FileCopyrightText: 2014 Stephane Mankowski <stephane@mankowski.fr>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Stephane Mankowski <stephane@mankowski.fr> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 
0012 .import "qrc:/gcompris/src/core/core.js" as Core
0013 
0014 var numberOfLevel = 10;
0015 var items;
0016 var selectedArrow;
0017 var pastQuestionsH = [];
0018 var pastQuestionsM = [];
0019 var pastQuestionsS = [];
0020 var targetHour;
0021 
0022 function start(items_) {
0023     items = items_;
0024     numberOfLevel = items.levels.length;
0025     items.currentLevel = Core.getInitialLevel(numberOfLevel);
0026     pastQuestionsH = [];
0027     pastQuestionsM = [];
0028     pastQuestionsS = [];
0029     initLevel();
0030 }
0031 
0032 function stop() {}
0033 
0034 function initLevel() {
0035     items.errorRectangle.resetState();
0036     items.score.numberOfSubLevels = items.levels[items.currentLevel].numberOfSubLevels;
0037     items.score.currentSubLevel = 0;
0038     initQuestion();
0039 }
0040 
0041 function initQuestion() {
0042     differentTargetH();
0043     differentCurrentH();
0044 
0045     items.minutesHandVisible = items.levels[items.currentLevel].displayMinutesHand;
0046     if(!items.minutesHandVisible) {
0047         items.currentM = 0;
0048         items.targetM = 0;
0049     }
0050     else if(items.levels[items.currentLevel].fixedMinutes !== undefined) {
0051         items.targetM = items.levels[items.currentLevel].fixedMinutes;
0052         differentCurrentM();
0053     }
0054     else {
0055         differentTargetM();
0056         differentCurrentM();
0057     }
0058 
0059     items.secondsHandVisible = items.levels[items.currentLevel].displaySecondsHand;
0060     if(!items.secondsHandVisible) {
0061         items.currentS = 0;
0062         items.targetS = 0;
0063     }
0064     else if(items.levels[items.currentLevel].fixedSeconds !== undefined) {
0065         items.targetS = items.levels[items.currentLevel].fixedSeconds;
0066         differentCurrentS();
0067     }
0068     else {
0069         differentTargetS();
0070         differentCurrentS();
0071     }
0072 
0073     if(items.levels[items.currentLevel].zonesVisible !== undefined) {
0074         items.zonesVisible = items.levels[items.currentLevel].zonesVisible;
0075     }
0076     else {
0077         items.zonesVisible = true;
0078     }
0079 
0080     if(items.levels[items.currentLevel].hoursMarksVisible !== undefined) {
0081         items.hoursMarksVisible = items.levels[items.currentLevel].hoursMarksVisible;
0082     }
0083     else {
0084         items.hoursMarksVisible = true;
0085     }
0086 
0087     if(items.levels[items.currentLevel].hoursVisible !== undefined) {
0088         items.hoursVisible = items.levels[items.currentLevel].hoursVisible;
0089     }
0090     else {
0091         items.hoursVisible = true;
0092     }
0093 
0094     if(items.levels[items.currentLevel].minutesVisible !== undefined) {
0095         items.minutesVisible = items.levels[items.currentLevel].minutesVisible;
0096     }
0097     else {
0098         items.minutesVisible = true;
0099     }
0100 
0101     if(items.levels[items.currentLevel].noHint !== undefined) {
0102         items.noHint = items.levels[items.currentLevel].noHint;
0103     }
0104     else {
0105         items.noHint = false;
0106     }
0107     items.buttonsBlocked = false;
0108 }
0109 
0110 function differentTargetH() {
0111     items.targetH = Math.floor(Math.random() * 12);
0112     while(pastQuestionsH.indexOf(items.targetH) != -1) {
0113         items.targetH = Math.floor(Math.random() * 12);
0114     }
0115     pastQuestionsH.push(items.targetH);
0116 
0117     // converting 12-hour system to 24-hour system
0118     if(!items.useTwelveHourFormat) {
0119         items.targetH += 12;
0120     }
0121 }
0122 
0123 function differentTargetM() {
0124     items.targetM = Math.floor(Math.random() * 60);
0125     while(pastQuestionsM.indexOf(items.targetM) != -1) {
0126         items.targetM = Math.floor(Math.random() * 60);
0127     }
0128     pastQuestionsM.push(items.targetM);
0129 }
0130 
0131 function differentTargetS() {
0132     items.targetS = Math.floor(Math.random() * 60);
0133     while(pastQuestionsS.indexOf(items.targetS) != -1) {
0134         items.targetS = Math.floor(Math.random() * 60);
0135     }
0136     pastQuestionsS.push(items.targetS);
0137 }
0138 
0139 function differentCurrentH() {
0140     targetHour = items.targetH % 12;
0141     items.currentH = Math.floor(Math.random() * 12);
0142     while(items.currentH === targetHour) {
0143         items.currentH = Math.floor(Math.random() * 12);
0144     }
0145 }
0146 
0147 function differentCurrentM() {
0148     items.currentM = Math.floor(Math.random() * 60);
0149     while(items.currentM === items.targetM) {
0150         items.currentM = Math.floor(Math.random() * 60);
0151     }
0152 }
0153 
0154 function differentCurrentS() {
0155     items.currentS = Math.floor(Math.random() * 60);
0156     while(items.currentS === items.targetS) {
0157         items.currentS = Math.floor(Math.random() * 60);
0158     }
0159 }
0160 
0161 function nextSubLevel() {
0162     if (items.score.currentSubLevel >= items.score.numberOfSubLevels) {
0163         items.bonus.good("gnu");
0164     } else {
0165         initQuestion();
0166     }
0167 }
0168 
0169 function checkAnswer() {
0170     items.buttonsBlocked = true;
0171     if (items.currentH === targetHour
0172                     && items.currentM === items.targetM
0173                     && items.currentS === items.targetS) {
0174         items.score.currentSubLevel++;
0175         items.score.playWinAnimation();
0176         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav");
0177     }
0178     else {
0179         items.errorRectangle.startAnimation();
0180         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav");
0181     }
0182 }
0183 
0184 function nextLevel() {
0185     items.score.stopWinAnimation();
0186     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0187     pastQuestionsH = [];
0188     pastQuestionsM = [];
0189     pastQuestionsS = [];
0190     initLevel();
0191 }
0192 
0193 function previousLevel() {
0194     items.score.stopWinAnimation();
0195     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0196     pastQuestionsH = [];
0197     pastQuestionsM = [];
0198     pastQuestionsS = [];
0199     initLevel();
0200 }
0201 
0202 function get2CharValue(i) {
0203     if (String(i).length === 1)
0204         return "0" + i;
0205     return i;
0206 }