~baltix/gcompris-qt/0.97.1

« back to all changes in this revision

Viewing changes to src/activities/land_safe/land_safe.js

  • Committer: Mantas Kriaučiūnas
  • Date: 2020-07-06 18:07:11 UTC
  • Revision ID: baltix@gmail.com-20200706180711-g254osu02cn8bc8p
GCompris-QT 0.97.1 with Lithuanian translation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GCompris - land_safe.js
 
2
 *
 
3
 * Copyright (C) 2016 Holger Kaelberer <holger.k@elberer.de>
 
4
 *
 
5
 * Authors:
 
6
 *   Matilda Bernard <serah4291@gmail.com> (GTK+ version)
 
7
 *   Holger Kaelberer <holger.k@elberer.de> (Qt Quick port)
 
8
 *
 
9
 *   This program is free software; you can redistribute it and/or modify
 
10
 *   it under the terms of the GNU General Public License as published by
 
11
 *   the Free Software Foundation; either version 3 of the License, or
 
12
 *   (at your option) any later version.
 
13
 *
 
14
 *   This program is distributed in the hope that it will be useful,
 
15
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *   GNU General Public License for more details.
 
18
 *
 
19
 *   You should have received a copy of the GNU General Public License
 
20
 *   along with this program; if not, see <https://www.gnu.org/licenses/>.
 
21
 */
 
22
 
 
23
/* ToDo:
 
24
 * - check for shader availability
 
25
 * - test more generic on-screen controls
 
26
 * (- use polygon fixture for rocket)
 
27
 *
 
28
 * Some gravitational forces:
 
29
 * !- Ceres 0,27
 
30
 * !- Pluto: 0,62 m/s²
 
31
 * !- Titan: 1,352 m/s²
 
32
 * - Ganymed: 1,428
 
33
 * !- Moon: 1,622 m/s²
 
34
 * - Io: 1,796 m/s²
 
35
 * !- Mars: 3,711 m/s²
 
36
 * - Merkur: 3,7 m/s²
 
37
 * !- Venus: 8,87 m/s²
 
38
 * !- Earth: 9,807 m/s²
 
39
 * - Jupiter: 24,79 m/s²
 
40
 */
 
41
 
 
42
.pragma library
 
43
.import QtQuick 2.6 as Quick
 
44
.import GCompris 1.0 as GCompris
 
45
 
 
46
var levels = [
 
47
                                /**  simple  **/
 
48
            {   "planet": qsTr("Ceres"),    "gravity":  0.27,   "maxAccel": 0.054,
 
49
                "accelSteps": 2,            "alt": 75.0 ,       "mode": "simple",
 
50
                "fuel" : -1 },
 
51
            {   "planet": qsTr("Pluto"),    "gravity":  0.62,   "maxAccel": 0.186,
 
52
                "accelSteps": 3,            "alt": 100.0,       "mode": "simple",
 
53
                "fuel" : -1 },
 
54
            {   "planet": qsTr("Titan"),    "gravity": 1.352,   "maxAccel": 0.406,
 
55
                "accelSteps": 3,            "alt": 100.0,       "mode": "simple",
 
56
                "fuel" : -1 },
 
57
            {   "planet": qsTr("Moon"),     "gravity": 1.622,   "maxAccel": 0.324,
 
58
                "accelSteps": 4,            "alt": 150.0,       "mode": "simple",
 
59
                "fuel" : 10 },
 
60
            {   "planet": qsTr("Mars"),     "gravity": 3.711,   "maxAccel": 0.619,
 
61
                "accelSteps": 5,            "alt": 200.0,       "mode": "simple",
 
62
                "fuel" : 20 },
 
63
            {   "planet": qsTr("Venus"),    "gravity":  8.87,   "maxAccel": 1.331,
 
64
                "accelSteps": 6,            "alt": 300.0,       "mode": "simple",
 
65
                "fuel" : 70 },
 
66
            {   "planet": qsTr("Earth"),    "gravity": 9.807,   "maxAccel": 1.373,
 
67
                "accelSteps": 7,            "alt": 350.0,       "mode": "simple",
 
68
                "fuel" : 70 },
 
69
 
 
70
                                /**  rotation  **/
 
71
            {   "planet": qsTr("Ceres"),    "gravity":  0.27,   "maxAccel": 0.054,
 
72
                "accelSteps": 2,            "alt": 75.0 ,       "mode": "rotation",
 
73
                "fuel" : -1 },
 
74
            {   "planet": qsTr("Pluto"),    "gravity":  0.62,   "maxAccel": 0.186,
 
75
                "accelSteps": 3,            "alt": 100.0,       "mode": "rotation",
 
76
                "fuel" : -1 },
 
77
            {   "planet": qsTr("Titan"),    "gravity": 1.352,   "maxAccel": 0.406,
 
78
                "accelSteps": 3,            "alt": 100.0,       "mode": "rotation",
 
79
                "fuel" : -1 },
 
80
            {   "planet": qsTr("Moon"),     "gravity":  1.62,   "maxAccel": 0.324,
 
81
                "accelSteps": 4,            "alt": 150.0,       "mode": "rotation",
 
82
                "fuel" : 10 },
 
83
            {   "planet": qsTr("Mars"),     "gravity":  3.71,   "maxAccel": 0.619,
 
84
                "accelSteps": 5,            "alt": 200.0,       "mode": "rotation",
 
85
                "fuel" : 20 },
 
86
            {   "planet": qsTr("Venus"),    "gravity":  8.87,   "maxAccel": 1.331,
 
87
                "accelSteps": 5,            "alt": 300.0,       "mode": "rotation",
 
88
                "fuel" : 70 },
 
89
            {   "planet": qsTr("Earth"),    "gravity": 9.807,   "maxAccel": 1.373,
 
90
                "accelSteps": 7,            "alt": 350.0,       "mode": "rotation",
 
91
                "fuel" : 70 }
 
92
 
 
93
];
 
94
 
 
95
var introTextSimple = qsTr("Use the up and down keys to control the thrust."
 
96
                           + "<br/>Use the right and left keys to control direction."
 
97
                           + "<br/>You must drive Tux's ship towards the landing platform."
 
98
                           + "<br/>The landing platform turns green when the velocity is safe to land.")
 
99
 
 
100
var introTextRotate = qsTr("The up and down keys control the thrust of the rear engine."
 
101
                           + "<br/>The right and left keys now control the rotation of the ship."
 
102
                           + "<br/>To move the ship in horizontal direction you must first rotate and then accelerate it.")
 
103
 
 
104
var currentLevel = 0;
 
105
var numberOfLevel;
 
106
var items = null;
 
107
var baseUrl = "qrc:/gcompris/src/activities/land_safe/resource";
 
108
var startingAltitudeReal = 100.0;
 
109
var startingOffsetPx = 10;  // y-value for setting rocket initially
 
110
var maxLandingVelocity = 10;
 
111
var leftRightAccel = 0.1;   // accel force set on horizontal accel
 
112
//var minAccel = 0.1;
 
113
var maxAccel = 0.15;
 
114
var accelSteps = 3;
 
115
var dAccel = maxAccel / accelSteps;//- minAccel;
 
116
var barAtStart;
 
117
var maxFuel = 100.0;
 
118
var currentFuel = 0.0;
 
119
var lastLevel = -1;
 
120
var debugDraw = false;
 
121
var zoomStack = new Array;
 
122
 
 
123
function start(items_) {
 
124
    items = items_;
 
125
    currentLevel = 0;
 
126
    lastLevel = -1;
 
127
    numberOfLevel = levels.length;
 
128
    barAtStart = GCompris.ApplicationSettings.isBarHidden;
 
129
    GCompris.ApplicationSettings.isBarHidden = true;
 
130
    initLevel()
 
131
}
 
132
 
 
133
function stop() {
 
134
    GCompris.ApplicationSettings.isBarHidden = barAtStart;
 
135
}
 
136
 
 
137
function initLevel() {
 
138
    if (items === null)
 
139
        return;
 
140
 
 
141
    items.bar.level = currentLevel + 1
 
142
 
 
143
    items.zoom = 1;
 
144
    zoomStack = [];
 
145
    // init level:
 
146
    items.accelerometer.min = -levels[currentLevel].gravity;
 
147
    items.accelerometer.max = levels[currentLevel].maxAccel*10-levels[currentLevel].gravity;
 
148
    maxAccel = levels[currentLevel].maxAccel;
 
149
    accelSteps = levels[currentLevel].accelSteps;
 
150
    dAccel = maxAccel / accelSteps;//- minAccel;
 
151
    startingAltitudeReal = levels[currentLevel].alt;
 
152
    items.gravity = levels[currentLevel].gravity;
 
153
    items.mode = levels[currentLevel].mode;
 
154
    maxFuel = levels[currentLevel].fuel;
 
155
    currentFuel = (maxFuel == -1 ? 1 : maxFuel); // -1 means unlimited fuel
 
156
 
 
157
    // reset everything:
 
158
    items.explosion.hide();
 
159
    items.rocket.show();
 
160
    // place rocket randomly:
 
161
    var max = items.background.width - items.accelerometer.width - items.landing.width - items.rocket.width;
 
162
    var min = 20;
 
163
    items.rocket.x = Math.random() * (max- min) + min;
 
164
    items.rocket.y = startingOffsetPx;
 
165
    items.rocket.rotation = 0;
 
166
    items.rocket.accel = 0;
 
167
    items.rocket.leftAccel = 0;
 
168
    items.rocket.rightAccel = 0;
 
169
    items.rocket.body.linearVelocity = Qt.point(0,0)
 
170
    // for landing random placement shall not intersect with bar -- or osd
 
171
    // controls on mobile:
 
172
    min = items.onScreenControls ? items.leftRightControl.width : items.bar.fullButton * items.bar.barZoom;
 
173
    max = items.onScreenControls ? items.background.width - items.upDownControl.width - items.landing.width : max;
 
174
    items.landing.anchors.leftMargin = Math.random() * (max- min) + min;
 
175
    items.landing.overlayColor = "-g";
 
176
 
 
177
    // initialize world:
 
178
    items.world.pixelsPerMeter = pxYToAltitude(items.rocket.y) / startingAltitudeReal;
 
179
    items.world.gravity = Qt.point(0, items.gravity)
 
180
    items.world.running = false;
 
181
 
 
182
//    console.log("Starting level (surfaceOff=" + items.ground.surfaceOffset + ", ppm=" + items.world.pixelsPerMeter + ")");
 
183
 
 
184
    if (currentLevel === 0 && lastLevel !== 0) {
 
185
        items.ok.visible = false;
 
186
        items.intro.intro = [introTextSimple];
 
187
        items.intro.index = 0;
 
188
    } else if (currentLevel === levels.length / 2 && lastLevel !== 0) {
 
189
        items.ok.visible = false;
 
190
        items.intro.intro = [introTextRotate];
 
191
        items.intro.index = 0;
 
192
    } else {
 
193
        // go
 
194
        items.intro.index = -1;
 
195
        items.ok.visible = true;
 
196
    }
 
197
    lastLevel = currentLevel;
 
198
}
 
199
 
 
200
function pxAltitudeToY(alt)
 
201
{
 
202
    var y = items.background.height - items.ground.height + items.ground.surfaceOffset
 
203
            - items.rocket.height - 1 - alt;
 
204
    return y;
 
205
}
 
206
 
 
207
function pxYToAltitude(y)
 
208
{
 
209
    var altPx = items.background.height - items.ground.height + items.ground.surfaceOffset
 
210
            - y - items.rocket.height
 
211
            - 1;  // landing is 1 pixel above ground surface
 
212
    return altPx;
 
213
}
 
214
 
 
215
// calc real height of rocket in meters above surface
 
216
function getAltitudeReal()
 
217
{
 
218
    var altPx = pxYToAltitude(items.rocket.y);
 
219
    var altReal = altPx / items.world.pixelsPerMeter;
 
220
    return altReal;
 
221
}
 
222
 
 
223
function nextLevel() {
 
224
    if(numberOfLevel <= ++currentLevel ) {
 
225
        currentLevel = 0
 
226
    }
 
227
    initLevel();
 
228
}
 
229
 
 
230
function previousLevel() {
 
231
    if(--currentLevel < 0) {
 
232
        currentLevel = numberOfLevel - 1
 
233
    }
 
234
    initLevel();
 
235
}
 
236
 
 
237
function processKeyPress(event)
 
238
{
 
239
    var key = event.key;
 
240
    event.accepted = true;
 
241
    if (!items.world.running) {
 
242
        return;
 
243
    }
 
244
    var newAccel = 0;
 
245
    if (key === Qt.Key_Up || key === Qt.Key_Down) {
 
246
        if (key === Qt.Key_Up) {
 
247
            if (items.rocket.accel === 0)
 
248
                newAccel = dAccel;
 
249
            else
 
250
                newAccel = items.rocket.accel + dAccel;
 
251
        } else if (key === Qt.Key_Down)
 
252
            newAccel = items.rocket.accel - dAccel;
 
253
 
 
254
        if (newAccel < 0)
 
255
            newAccel = 0;
 
256
        if (newAccel > maxAccel)
 
257
            newAccel = maxAccel;
 
258
 
 
259
        if (newAccel !== items.rocket.accel && currentFuel > 0)
 
260
            items.rocket.accel = newAccel;
 
261
    } else if (key === Qt.Key_Right || key === Qt.Key_Left) {
 
262
        if (items.mode === "simple") {
 
263
            if (key === Qt.Key_Right && !event.isAutoRepeat && currentFuel > 0) {
 
264
                items.rocket.leftAccel = leftRightAccel;
 
265
                items.rocket.rightAccel = 0.0;
 
266
            } else if (key === Qt.Key_Left && !event.isAutoRepeat && currentFuel > 0) {
 
267
                items.rocket.rightAccel = leftRightAccel;
 
268
                items.rocket.leftAccel = 0.0;
 
269
            }
 
270
        } else { // "rotation"
 
271
            if (key === Qt.Key_Right)
 
272
                items.rocket.rotation += 10;
 
273
            else if (key === Qt.Key_Left)
 
274
                items.rocket.rotation -= 10;
 
275
            //console.log("XXX rotation=" + items.rocket.rotation + " bodyRot=" + items.rocket.body.rotation);
 
276
        }
 
277
    }
 
278
}
 
279
 
 
280
function processKeyRelease(event)
 
281
{
 
282
    if (!items.world.running) {
 
283
        event.accepted = false;
 
284
        return;
 
285
    }
 
286
    var key = event.key;
 
287
    event.accepted = true;
 
288
    //console.log("XXX release " + key + " = " + event.isAutoRepeat + " = " + Qt.Key_Right);
 
289
    if (key === Qt.Key_Right && !event.isAutoRepeat) {
 
290
        items.rocket.leftAccel = 0;
 
291
    } else if (key === Qt.Key_Left && !event.isAutoRepeat) {
 
292
        items.rocket.rightAccel = 0;
 
293
    } else
 
294
        event.accepted = false;
 
295
}
 
296
 
 
297
function finishLevel(success)
 
298
{
 
299
    items.rocket.accel = 0;
 
300
    items.rocket.leftAccel = 0;
 
301
    items.rocket.rightAccel = 0;
 
302
    items.rocket.body.linearVelocity = Qt.point(0,0)
 
303
    if (success)
 
304
        items.bonus.good("lion");
 
305
    else {
 
306
        items.explosion.show();
 
307
        items.rocket.hide();
 
308
        items.bonus.bad("lion");
 
309
    }
 
310
}
 
311
 
 
312
function degToRad(degrees) {
 
313
  return degrees * Math.PI / 180;
 
314
}