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

0001 /* GCompris - positions.js
0002  *
0003  * SPDX-FileCopyrightText: 2021 Mariam Fahmy <mariamfahmy66@gmail.com>
0004  *
0005  * Authors:
0006  *   Mariam Fahmy <mariamfahmy66@gmail.com>
0007  *
0008  *   SPDX-License-Identifier: GPL-3.0-or-later
0009  */
0010 .pragma library
0011 .import QtQuick 2.12 as Quick
0012 .import "qrc:/gcompris/src/core/core.js" as Core
0013 
0014 
0015 var numberOfLevel;
0016 var items;
0017 var position;
0018 var dataset;
0019 var questionList = [];
0020 
0021 var rightPosition = 0x0000;
0022 var leftPosition = 0x0001;
0023 var abovePosition = 0x0002;
0024 var underPosition = 0x0004;
0025 var insidePosition = 0x0008;
0026 var behindPosition = 0x0010;
0027 var inFrontOfPosition = 0x0020;
0028 var currentQuestionIndex;
0029 
0030 function start(items_) {
0031     items = items_;
0032     dataset = items.levels;
0033     numberOfLevel = dataset.length;
0034     items.currentLevel = Core.getInitialLevel(numberOfLevel);
0035     initLevel();
0036 }
0037 
0038 function stop() {
0039 }
0040 
0041 function initLevel() {
0042     items.score.stopWinAnimation();
0043     items.errorRectangle.resetState();
0044     questionList = [];
0045 
0046     questionList = dataset[items.currentLevel].questions;
0047     items.score.currentSubLevel = 0;
0048     items.score.numberOfSubLevels = questionList.length;
0049     currentQuestionIndex = -1;
0050 
0051     if(dataset[items.currentLevel].generateRandomPositions) {
0052         questionList = Core.shuffle(questionList);
0053     }
0054 
0055     nextSubLevel();
0056 }
0057 
0058 function nextLevel() {
0059     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0060     initLevel();
0061 }
0062 
0063 function previousLevel() {
0064     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0065     initLevel();
0066 }
0067 
0068 function verifyAnswer() {
0069     items.buttonsBlocked = true;
0070     if(items.selectedPosition === items.checkState) {
0071         items.score.currentSubLevel += 1;
0072         items.score.playWinAnimation();
0073         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav");
0074     }
0075     else {
0076         items.errorRectangle.startAnimation();
0077         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav");
0078     }
0079 }
0080 
0081 function nextSubLevel() {
0082     currentQuestionIndex++;
0083     if(currentQuestionIndex >= questionList.length) {
0084         items.bonus.good("flower");
0085         return;
0086     }
0087     items.positionModels.clear();
0088     items.checkState = questionList[currentQuestionIndex]["id"];
0089     if(questionList[currentQuestionIndex].text !== undefined) {
0090         items.questionText = questionList[currentQuestionIndex]["text"];
0091     }
0092     items.view.currentIndex = -1;
0093     getRandomPositions();
0094     items.buttonsBlocked = false;
0095 }
0096 
0097 function getRandomPositions() {
0098     var randomPositions = [];
0099     var correctAnswer = questionList[currentQuestionIndex]["id"];
0100     randomPositions.push(questionList[currentQuestionIndex]);
0101     for(var i = 1 ; i < questionList.length / 2 ; i++ ) {
0102         getRandomElement(randomPositions);
0103     }
0104     randomPositions = Core.shuffle(randomPositions);
0105     for(var j = 0; j < randomPositions.length ; j++) {
0106         items.positionModels.append( {"stateId" : randomPositions[j].id,
0107                                       "stateName" : randomPositions[j].position } );
0108     }
0109 }
0110 
0111 function getRandomElement(randomPositions) {
0112     var randomElement = questionList[Math.floor(Math.random() * questionList.length)];
0113     while(randomPositions.indexOf(randomElement) !== -1) {
0114         randomElement = questionList[Math.floor(Math.random() * questionList.length)];
0115     }
0116     randomPositions.push(randomElement);
0117 }