File indexing completed on 2024-04-28 15:07:59
0001 /* GCompris - railroad.js 0002 * 0003 * SPDX-FileCopyrightText: 2016 Utkarsh Tiwari <iamutkarshtiwari@kde.org> 0004 * SPDX-FileCopyrightText: 2018 Amit Sagtani <asagtani06@gmail.com> 0005 * 0006 * Authors: 0007 * <Pascal Georges> (GTK+ version) 0008 * Utkarsh Tiwari <iamutkarshtiwari@kde.org> (Qt Quick port) 0009 * Amit Sagtani <asagtani06@gmail.com> (Qt Quick port) 0010 * 0011 * SPDX-License-Identifier: GPL-3.0-or-later 0012 */ 0013 .pragma library 0014 .import QtQuick 2.12 as Quick 0015 .import GCompris 1.0 as GCompris 0016 .import "qrc:/gcompris/src/core/core.js" as Core 0017 0018 var numberOfLevel = 10 0019 var solutionArray = [] 0020 var backupListModel = [] 0021 var isNewLevel = true 0022 var resourceURL = "qrc:/gcompris/src/activities/railroad/resource/" 0023 var items 0024 0025 /** 0026 * Stores configuration for each level. 0027 * 'WagonsInCorrectAnswers' contains no. of wagons in correct answer. 0028 * 'memoryTime' contains time(in seconds) for memorizing the wagons. 0029 * 'numberOfSubLevels' contains no. of sublevels in each level. 0030 * 'columnsInHorizontalMode' contains no. of columns in a row of sampleZone in horizontal mode. 0031 * 'columnsInVerticalMode' contains no. of columns in a row of sampleZone in vertical mode. 0032 * 'noOfLocos' stores no. of locos to be displayed in sampleZone. 0033 * 'noOfWagons' stores no. of wagons to be displayed in sampleZone. 0034 */ 0035 var dataset = { 0036 "WagonsInCorrectAnswers": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5], 0037 "memoryTime": [4, 4, 6, 6, 7, 7, 8, 8, 10, 10], 0038 "numberOfSubLevels": 3, 0039 "columnsInHorizontalMode": [3, 5, 3, 5, 3, 5, 3, 5, 3, 5], 0040 "columsInVerticalMode": [3, 4, 3, 4, 3, 4, 3, 4, 3, 4], 0041 "noOfLocos": [8, 9, 4, 9, 4, 9, 4, 9, 4, 9], 0042 "noOfWagons": [4, 11, 8, 11, 8, 11, 8, 11, 8, 11] 0043 } 0044 0045 function start(items_) { 0046 items = items_; 0047 items.score.numberOfSubLevels = dataset["numberOfSubLevels"]; 0048 items.currentLevel = Core.getInitialLevel(numberOfLevel); 0049 items.score.currentSubLevel = 0; 0050 initLevel(); 0051 } 0052 0053 function stop() { 0054 items.trainAnimationTimer.stop(); 0055 } 0056 0057 function initLevel() { 0058 items.errorRectangle.resetState(); 0059 generateUniqueId(); 0060 items.mouseEnabled = true; 0061 items.memoryMode = false; 0062 items.trainAnimationTimer.stop(); 0063 items.animateFlow.stop(); // Stops any previous animations 0064 items.listModel.clear(); 0065 items.answerZone.currentIndex = 0; 0066 items.sampleList.currentIndex = 0; 0067 items.answerZone.selectedSwapIndex = -1; 0068 if(isNewLevel) { 0069 // Initiates a new level 0070 backupListModel = []; 0071 solutionArray = []; 0072 //Adds wagons to display in answerZone 0073 var identifier; 0074 var idLoco; 0075 // Adds a loco at the beginning 0076 idLoco = "loco" + Math.floor(Math.random() * dataset["noOfLocos"][items.currentLevel]) 0077 addWagon(idLoco, items.listModel.length); 0078 for(var i = 0; i < dataset["WagonsInCorrectAnswers"][items.currentLevel] - 1; i++) { 0079 do { 0080 identifier = "wagon" + Math.floor(Math.random() * dataset["noOfWagons"][items.currentLevel]) 0081 } while (solutionArray.indexOf(identifier) != -1) 0082 solutionArray.push(identifier); 0083 addWagon(identifier, i); 0084 } 0085 solutionArray.push(idLoco); 0086 0087 } else { 0088 // Re-setup the same level 0089 for(var i = 0; i < solutionArray.length; i++) { 0090 addWagon(solutionArray[i], i); 0091 } 0092 } 0093 if(items.introMessage.visible === false && isNewLevel) { 0094 items.trainAnimationTimer.start(); 0095 } 0096 items.trainAnimationTimer.interval = dataset["memoryTime"][items.currentLevel] * 1000; 0097 } 0098 0099 function nextLevel() { 0100 items.score.stopWinAnimation(); 0101 items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel); 0102 items.score.currentSubLevel = 0; 0103 isNewLevel = true; 0104 initLevel(); 0105 } 0106 0107 function previousLevel() { 0108 items.score.stopWinAnimation(); 0109 items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel); 0110 items.score.currentSubLevel = 0; 0111 isNewLevel = true; 0112 initLevel(); 0113 } 0114 0115 function restoreLevel() { 0116 backupListModel = []; 0117 for (var index = 0; index < items.listModel.count; index++) { 0118 backupListModel.push(items.listModel.get(index).id); 0119 } 0120 isNewLevel = false; 0121 initLevel(); 0122 } 0123 0124 function nextSubLevel() { 0125 /* Sets up the next sublevel */ 0126 if(items.score.currentSubLevel >= dataset["numberOfSubLevels"]) { 0127 items.bonus.good("flower"); 0128 } 0129 else { 0130 isNewLevel = true; 0131 initLevel(); 0132 } 0133 } 0134 0135 function checkAnswer() { 0136 items.mouseEnabled = false; // Disables the touch 0137 /* Checks if the top level setup equals the solutions */ 0138 if(items.listModel.count === solutionArray.length) { 0139 var isSolution = true; 0140 for (var index = 0; index < items.listModel.count; index++) { 0141 if(items.listModel.get(index).id !== solutionArray[index]) { 0142 isSolution = false; 0143 break; 0144 } 0145 } 0146 if(isSolution == true) { 0147 items.score.currentSubLevel += 1; 0148 items.score.playWinAnimation(); 0149 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav"); 0150 } 0151 else { 0152 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav"); 0153 items.errorRectangle.startAnimation(); 0154 } 0155 } 0156 else { 0157 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav"); 0158 items.errorRectangle.startAnimation(); 0159 } 0160 } 0161 0162 function addWagon(uniqueID, dropIndex) { 0163 /* Appends wagons to the display area */ 0164 items.listModel.insert(dropIndex, {"id": uniqueID}); 0165 } 0166 0167 function getDropIndex(x) { 0168 var count = items.listModel.count; 0169 for (var index = 0; index < count; index++) { 0170 var xVal = items.answerZone.cellWidth * index; 0171 var itemWidth = items.answerZone.cellWidth; 0172 if(x < xVal && index == 0) { 0173 return 0; 0174 } 0175 else if((xVal + itemWidth + items.background.width * 0.0025) <= x && index == (count - 1)) { 0176 return count; 0177 } 0178 else if(xVal <= x && x < (xVal + itemWidth + items.background.width * 0.0025)) { 0179 return index + 1; 0180 } 0181 } 0182 return 0; 0183 } 0184 0185 function generateUniqueId() { 0186 var uniqueIds = []; 0187 var index; 0188 for(index = 0; index < dataset["noOfLocos"][items.currentLevel]; index++) { 0189 uniqueIds.push("loco" + index); 0190 } 0191 for(index = 0; index < dataset["noOfWagons"][items.currentLevel]; index++) { 0192 uniqueIds.push("wagon" + index); 0193 } 0194 items.uniqueId = uniqueIds; 0195 }