File indexing completed on 2024-04-28 15:08:00

0001 /* GCompris - reversecount.js
0002  *
0003  * SPDX-FileCopyrightText: 2014 Emmanuel Charruau
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Emmanuel Charruau <echarruau@gmail.com> (Qt Quick port)
0008  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (Major rework)
0009  *
0010  *   SPDX-License-Identifier: GPL-3.0-or-later
0011  */
0012 .pragma library
0013 .import QtQuick 2.12 as Quick
0014 .import GCompris 1.0 as GCompris //for ApplicationInfo
0015 .import "qrc:/gcompris/src/core/core.js" as Core
0016 
0017 var iceBlocksLayout = [[0, 0],[1, 0],[2, 0],[3, 0],[4, 0],
0018                        [4, 1],[4, 2],[4, 3],[4, 4],[3, 4],
0019                        [2, 4],[1, 4],[0, 4],[0, 3],[0, 2],
0020                        [0, 1]]
0021 
0022 var tuxIceBlockNumber = 0
0023 var tuxIceBlockNumberGoal = 0
0024 var placeFishToReachBool = false
0025 
0026 var level = null;
0027 
0028 var fishes = [
0029             "Benzfish.svg",
0030             "blue-fish.svg",
0031             "drunken_duck_cartoon_globefish_kugelfisch.svg",
0032             "Fish02.svg",
0033             "molumen_Codfish.svg",
0034             "mystica_Aquarium_fish_-_Amphiprion_percula.svg",
0035             "pepinux_Pez_dorado.svg",
0036             "The_Whale-Fish.svg",
0037             "Benzfish.svg",
0038             "blue-fish.svg",
0039             "drunken_duck_cartoon_globefish_kugelfisch.svg",
0040             "Fish02.svg"
0041         ]
0042 
0043 var numberOfFish
0044 var fishIndex = -1
0045 
0046 var numberOfLevel = 0
0047 var items
0048 
0049 function start(items_) {
0050     items = items_
0051     numberOfLevel = items.levels.length
0052     items.currentLevel = Core.getInitialLevel(numberOfLevel)
0053     initLevel()
0054 }
0055 
0056 function stop() {
0057     fishIndex = -1
0058 }
0059 
0060 function initLevel() {
0061     items.tuxIsMoving = false
0062     items.tuxCanMove = true
0063     items.chooseDiceBar.value1 = 0
0064     items.chooseDiceBar.value2 = 0
0065     items.chooseDiceBar.valueMax = items.levels[items.currentLevel].maxNumber
0066     numberOfFish = items.levels[items.currentLevel].numberOfFish
0067 
0068     fishIndex = 0
0069     tuxIceBlockNumber = 0
0070     items.tux.init()
0071 
0072     calculateNextPlaceFishToReach()
0073     placeFishToReach()
0074     moveTuxToIceBlock()
0075     items.clockPosition = 4
0076 }
0077 
0078 function moveTux(numberOfMovesToDo) {
0079     calculateTuxIceBlockNextPos(numberOfMovesToDo)
0080 
0081     if(items.chooseDiceBar.value1 === 0 && items.chooseDiceBar.value2 === 0) {
0082         return
0083     }
0084     else if (tuxIceBlockNumberGoal != fishIndex) {
0085         items.clockPosition--
0086         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/darken.wav")
0087         if (items.clockPosition === 0) {
0088             lost()
0089             return
0090         }
0091     }
0092     else {
0093         items.tuxCanMove = false
0094         moveTuxToNextIceBlock()
0095     }
0096 }
0097 
0098 function moveTuxToNextIceBlock() {
0099     items.tuxIsMoving = false
0100     tuxIceBlockNumber++
0101     tuxIceBlockNumber = tuxIceBlockNumber % iceBlocksLayout.length
0102 
0103     if (tuxIceBlockNumber > 0 && tuxIceBlockNumber <= 4)
0104         items.tux.rotation = -90
0105     else if (tuxIceBlockNumber >= 5 && tuxIceBlockNumber <= 8)
0106         items.tux.rotation = 0
0107     else if (tuxIceBlockNumber >= 9 && tuxIceBlockNumber <= 12)
0108         items.tux.rotation = 90
0109     else if (tuxIceBlockNumber >= 13 && tuxIceBlockNumber <= 15)
0110         items.tux.rotation = 180
0111 
0112     moveTuxToIceBlock()
0113 
0114     var fishPos = fishIndex % iceBlocksLayout.length
0115     //if tux reaches its position + dice number
0116     if (tuxIceBlockNumber == fishPos) {
0117         items.tuxIsMoving = false;
0118 
0119         // if last fish reached
0120         if (--numberOfFish == 0) {
0121             won()
0122             items.fishToReach.showParticles()
0123             items.clockPosition++
0124             return
0125         }
0126 
0127         items.audioEffects.play('qrc:/gcompris/src/activities/gnumch-equality/resource/eat.wav')
0128         calculateNextPlaceFishToReach()
0129         placeFishToReachBool = true
0130         return
0131     }
0132 
0133     items.audioEffects.play(items.resourceUrl + 'icy_walk.wav')
0134     //if tux reaches its position + dice number before reaching the fish, calculation was wrong
0135     if (tuxIceBlockNumber == tuxIceBlockNumberGoal) {
0136         items.clockPosition--
0137         if (items.clockPosition === 0) {
0138             lost()
0139             return
0140         }
0141         items.tuxIsMoving = false;
0142         return
0143     }
0144     items.tuxIsMoving = true
0145 }
0146 
0147 function moveTuxToIceBlock() {
0148     items.tux.x = iceBlocksLayout[tuxIceBlockNumber % iceBlocksLayout.length][0] *
0149             items.widthBase + (items.widthBase - items.tux.width) / 2
0150     items.tux.y = iceBlocksLayout[tuxIceBlockNumber % iceBlocksLayout.length][1] *
0151             items.heightBase + (items.heightBase - items.tux.height) / 2
0152 }
0153 
0154 function tuxRunningChanged() {
0155     if (items.tuxIsMoving) {
0156         moveTuxToNextIceBlock()
0157     } else {
0158         if (placeFishToReachBool == true) {
0159             placeFishToReach(fishIndex)
0160             placeFishToReachBool = false
0161         }
0162     }
0163 }
0164 
0165 function calculateTuxIceBlockNextPos(numberOfMovesToDo) {
0166     tuxIceBlockNumberGoal = (tuxIceBlockNumber + numberOfMovesToDo) % iceBlocksLayout.length
0167     // Increase Tux's speed depending on the number of blocks to move
0168     items.tux.duration = 1000 - numberOfMovesToDo * 40
0169 }
0170 
0171 var previousFishIndex = 0
0172 function calculateNextPlaceFishToReach() {
0173     var index, newFishIndex
0174     do {
0175         index = Math.floor(Math.random() * (items.levels[items.currentLevel].values.length))
0176         newFishIndex = items.levels[items.currentLevel].values[index]
0177     } while(items.levels[items.currentLevel].values.length > 2 &&     /* Allow repetition for array size 2 */
0178         ((newFishIndex === previousFishIndex) || (newFishIndex >= iceBlocksLayout.length)))
0179     previousFishIndex = newFishIndex
0180 
0181     fishIndex = (tuxIceBlockNumber + newFishIndex) % iceBlocksLayout.length
0182 }
0183 
0184 function placeFishToReach() {
0185     // placeFishToReach can be called when the opacity is 0.
0186     // In this case, this does not trigger the onOpacityChanged of the fish Image (meaning the fish will not be displayed) so we directly set the opacity to 1.
0187     if(items.fishToReach.opacity == 0)
0188         items.fishToReach.opacity = 1
0189     else
0190         items.fishToReach.opacity = 0
0191 
0192     items.fishToReach.nextX = iceBlocksLayout[fishIndex % iceBlocksLayout.length][0] *
0193             items.widthBase + (items.widthBase - items.fishToReach.width) / 2
0194     items.fishToReach.nextY = iceBlocksLayout[fishIndex % iceBlocksLayout.length][1] *
0195             items.heightBase + (items.heightBase - items.fishToReach.height) / 2
0196 }
0197 
0198 function nextLevel() {
0199     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0200     initLevel();
0201 }
0202 
0203 function previousLevel() {
0204     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0205     initLevel();
0206 }
0207 
0208 function lost() {
0209     items.bonus.bad("tux")
0210 }
0211 
0212 function won() {
0213     items.bonus.good("flower")
0214 }