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

0001 /* GCompris - football.js
0002  *
0003  * SPDX-FileCopyrightText: 2014 Bruno Coudoin
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (Qt Quick port)
0008  *   Bharath M S <brat.197@gmail.com> (Qt Quick port)
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
0015 .import "qrc:/gcompris/src/core/core.js" as Core
0016 
0017 var url = "qrc:/gcompris/src/activities/football/resource/"
0018 
0019 var numberOfLevel = 8
0020 var items
0021 var flag = 0
0022 var friction = 0.995
0023 var velocityX
0024 var velocityY
0025 var tuxCollision
0026 
0027 function start(items_) {
0028     items = items_
0029     items.currentLevel = Core.getInitialLevel(numberOfLevel)
0030     initLevel()
0031 }
0032 
0033 function stop() {
0034     items.timer.stop();
0035 }
0036 
0037 function initLevel() {
0038     items.ball.x = items.border.width * 0.2
0039     items.ball.y = items.border.height / 2 - items.ball.height / 2
0040     velocityX = 0
0041     velocityY = 0
0042     tuxCollision = false
0043     /* Increase size of TUX for each level */
0044     items.tux.sourceSize.height = 10 * (5 + 2 * items.bar.level) * GCompris.ApplicationInfo.ratio
0045     moveTux(items.background.height)
0046 }
0047 
0048 function nextLevel() {
0049     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel)
0050     initLevel();
0051 }
0052 
0053 function previousLevel() {
0054     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0055     initLevel();
0056 }
0057 
0058 function startMotion(x1, y1) {
0059     var velocity = Math.sqrt(Math.pow(x1, 2) +
0060                           Math.pow(y1, 2))
0061     velocity = Math.min(10, velocity * 0.5)
0062     /* Modify speed of ball here */
0063     velocityX = velocity * x1 * 0.01 * -1 / 5
0064     velocityY = velocity * y1 * 0.01 * -1 / 5
0065     tuxCollision = false
0066     items.timer.start()
0067 }
0068 
0069 /* Calculates the angle between the two points */
0070 function getAngle(sx1, sy1, sx2, sy2)
0071 {
0072         var dy, slope, angle;
0073         var dx = sx2 - sx1;
0074         if (dx === 0)
0075             return 0;
0076         dy = sy2 - sy1;
0077         slope = dy / dx;
0078         angle = Math.atan(slope) * 180 / Math.PI;
0079 
0080         if (dy < 0 && dx < 0){
0081             return angle;
0082         }
0083         else if (dy >= 0 && dx >= 0){
0084             return angle + 180;
0085         }
0086         else if (dy < 0 && dx >= 0){
0087             return angle + 180;
0088         }
0089         else if (dy >= 0 && dx < 0){
0090             return angle;
0091         }
0092         else
0093             return 0;
0094     }
0095 
0096 /* Draw a line dynamically to display the drag */
0097 function drawLine(x1, y1, x2, y2){
0098     items.line.height = 5
0099     items.line.rotation = getAngle(x1, y1, x2, y2);
0100     items.line.width = Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1- y2), 2));
0101     items.line.x = x2
0102     items.line.y = y2
0103 }
0104 
0105 /* Tux moves up and down, called in initlevel() */
0106 function moveTux(height){
0107     items.moveUp.to = 0
0108     items.moveDown.to = items.background.height * 0.75 - items.tux.height
0109     items.moveTux.restart()
0110 }
0111 
0112 
0113 function ballMotion() {
0114     items.ball.x += velocityX
0115     items.ball.y += velocityY
0116 
0117     if(items.ball.y > (items.border.height - items.ball.height)) { //bottom
0118         velocityY *= -1
0119         items.ball.y = items.border.height - items.ball.height
0120         tuxCollision = false
0121     } else if(items.ball.x > (items.border.width)) { //right ---- GOAL!
0122         velocityX *= 0
0123         velocityY *= 0
0124         items.ball.x = items.border.width// - items.ball.width
0125         items.bonus.good("smiley")
0126     } else if(items.ball.x < 0) { // left
0127         velocityX *= -1
0128         items.ball.x = 0
0129         tuxCollision = false
0130     } else if(items.ball.y < 0) { //top
0131         velocityY *= -1
0132         items.ball.y = 0
0133         tuxCollision = false
0134     }
0135     /* Collision with TUX */
0136     else if(items.ball.y > items.tux.y &&
0137             items.ball.y <= items.tux.y + items.tux.height/2 &&
0138             items.ball.x > items.tux.x &&
0139             items.ball.x <= items.tux.x + items.tux.width/2 &&
0140             !tuxCollision) {
0141         velocityY *= -2
0142         velocityX *= -2
0143         tuxCollision = true
0144         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/brick.wav")
0145     }
0146 
0147     // Friction
0148     velocityX *= friction
0149     velocityY *= friction
0150 
0151     // Ball stop
0152     if(Math.abs(velocityX) < 0.2 && Math.abs(velocityY) < 0.2) {
0153         velocityX = 0
0154         velocityY = 0
0155         items.timer.stop()
0156     }
0157 }