Warning, file /education/gcompris/src/activities/lightsoff/lightsoff.js was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* GCompris - lightsoff.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 .import "qrc:/gcompris/src/core/core.js" as Core
0012 
0013 var levels
0014 var numberOfLevel
0015 var items
0016 var table
0017 var soluc
0018 var showSoluce = false
0019 var size = 5
0020 
0021 var url = "qrc:/gcompris/src/activities/lightsoff/resource/"
0022 
0023 function start(items_) {
0024     items = items_
0025     numberOfLevel = items.levels.length
0026     items.currentLevel = Core.getInitialLevel(numberOfLevel)
0027     initLevel()
0028 }
0029 
0030 function stop() {}
0031 
0032 function initLevel() {
0033 
0034     /* Is it a static or dynamic level ? */
0035     if (items.levels[items.currentLevel].dynamic) {
0036         /* Dynamic */
0037         size = items.levels[items.currentLevel].size
0038         table = new Array(size * size)
0039         soluc = new Array(size * size)
0040         for (var i = 0; i < size * size; ++i) {
0041             table[i] = 0
0042             soluc[i] = 0
0043         }
0044 
0045         for (var j = 0; j < items.currentLevel; ++j) {
0046             switchLightNoCheck(Math.floor(size * size * Math.random()))
0047         }
0048     } else {
0049         /* Static */
0050         size = items.levels[items.currentLevel].size
0051         table = items.levels[items.currentLevel].level.slice(0)
0052         soluc = items.levels[items.currentLevel].solution.slice(0)
0053     }
0054     showSoluce = false
0055     items.modelTable.clear()
0056     items.nbCell = size
0057     refreshModel()
0058     checkResult()
0059 }
0060 
0061 function nextLevel() {
0062     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0063     initLevel()
0064 }
0065 
0066 function previousLevel() {
0067     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0068     initLevel()
0069 }
0070 
0071 function switchLightNoCheck(index) {
0072     /* Switch the selected item */
0073     table[index] = 1 - table[index]
0074 
0075     /* Switch the soluce */
0076     soluc[index] = 1 - soluc[index]
0077 
0078     /* Switch neighbor left */
0079     if (index % size !== 0)
0080         table[index - 1] = 1 - table[index - 1]
0081 
0082     /* Switch neighbor right */
0083     if (index % size !== size - 1)
0084         table[index + 1] = 1 - table[index + 1]
0085 
0086     /* Switch neighbor up */
0087     if (index > size - 1)
0088         table[index - size] = 1 - table[index - size]
0089 
0090     /* Switch neighbor down */
0091     if (index < size * size -size)
0092         table[index + size] = 1 - table[index + size]
0093 }
0094 
0095 function refreshModel() {
0096     for (var i = 0; i < size * size; ++i) {
0097         items.modelTable.set(i, { 'lighton': table[i],  'soluce': showSoluce ? soluc[i] : 0})
0098     }
0099 }
0100 
0101 function switchLight(index) {
0102     /* Switch the selected item */
0103     switchLightNoCheck(index)
0104 
0105     /* Refresh the lights */
0106     refreshModel()
0107 
0108     /* Check the result */
0109     checkResult()
0110 }
0111 
0112 function checkResult() {
0113     /* Check the result */
0114     var nb = 0
0115     table.forEach(function (entry) {
0116         nb += entry
0117     })
0118 
0119     if (nb === 0) {
0120         items.blockClicks = true
0121         items.bonus.good("tux")
0122     }
0123 
0124     /* Check the soluce */
0125     items.nbCelToWin = nb
0126 }
0127 
0128 function solve() {
0129     showSoluce = !showSoluce
0130 
0131     /* Refresh the lights */
0132     refreshModel()
0133 }
0134 
0135 function windowPressed(index) {
0136     audioEffects.play('qrc:/gcompris/src/core/resource/sounds/scroll.wav')
0137     switchLight(index)
0138 }