-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.qml
More file actions
455 lines (359 loc) · 12.6 KB
/
main.qml
File metadata and controls
455 lines (359 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import QtQuick 2.0
import Bacon2D 1.0
Game {
id: root
property int currentLevel: 0
property int bigFontSize: root.width / 15.0
width: 800
height: 600
currentScene: levelCompletedScene
FontLoader { id: dPuntillasFont; source: "fonts/d-puntillas-D-to-tiptoe.ttf" }
Scene {
id: levelCompletedScene
anchors.fill: parent
Rectangle {
color: "black"
anchors.fill: parent
}
Column {
anchors.centerIn: parent
Text {
id: levelCompletedText
anchors.horizontalCenter: parent.horizontalCenter
color: "white"
font.family: dPuntillasFont.name
font.pointSize: root.bigFontSize
visible: root.currentLevel > 0
text: "Level " + root.currentLevel + " completed!"
}
Text {
id: startNextText
anchors.horizontalCenter: parent.horizontalCenter
color: "white"
font.family: dPuntillasFont.name
font.pointSize: root.bigFontSize
text: root.currentLevel > 0 ? "Next Level" : "Start"
MouseArea {
anchors.fill: parent
onClicked: {
currentLevel++;
gameScene._reset();
root.currentScene = gameScene;
}
}
}
}
}
Scene {
id: gameOverScene
anchors.fill: parent
Rectangle {
color: "black"
anchors.fill: parent
}
Column {
Text {
id: gameOverText
color: "white"
font.family: dPuntillasFont.name
font.pointSize: root.bigFontSize
anchors.horizontalCenter: parent.horizontalCenter
text: "Game over!"
}
Text {
color: "white"
font.family: dPuntillasFont.name
font.pointSize: root.bigFontSize
anchors.horizontalCenter: parent.horizontalCenter
text: "Game over!"
}
}
}
Scene {
id: gameScene
anchors.fill: parent
focus: true
debug: false
gravity: Qt.point(0, 0)
property int numberOfAsteroids: 3 + Math.floor(root.currentLevel * 0.5)
property int totalAsteroids: 0
property bool isUpPressed: upArea.pressed
property bool isDownPressed: downArea.pressed
property bool isLeftPressed: false
property bool isRightPressed: false
Keys.onUpPressed: isUpPressed = true
Keys.onDownPressed: isDownPressed = true
Keys.onLeftPressed: isLeftPressed = true
Keys.onRightPressed: isRightPressed = true
Keys.onSpacePressed: ship.fire();
Keys.onReleased: {
switch (event.key) {
case Qt.Key_Up:
isUpPressed = false
break
case Qt.Key_Down:
isDownPressed = false
break
case Qt.Key_Left:
isLeftPressed = false
break
case Qt.Key_Right:
isRightPressed = false
break
}
}
onContactPostSolve: {
var entityA = contact.fixtureA.entity
var entityB = contact.fixtureB.entity
if (entityA.objectName === "asteroid" || entityB.objectName === "asteroid") {
var asteroidObject
if (entityA.objectName === "bullet" || entityB.objectName === "bullet") {
var bulletObject;
if (entityA.objectName === "bullet") {
asteroidObject = entityB;
bulletObject = entityA;
} else {
asteroidObject = entityA;
bulletObject = entityB;
}
bulletObject.destroy();
asteroidObject.damage();
}
}
console.log(entityA.objectName, entityB.objectName)
}
onTotalAsteroidsChanged: {
console.log("Total asteroids:", gameScene.totalAsteroids)
if (gameScene.totalAsteroids == 0)
game.currentScene = levelCompletedScene
}
Rectangle {
id: background
color: "black"
anchors.fill: parent
}
Image {
id: backgroundStars
anchors.fill: parent
source: "images/background_stars.png"
fillMode: Image.Tile
}
MouseArea {
anchors.fill: background
onClicked: ship.fire();
}
ScriptBehavior {
id: keepInsideViewBehavior
script: {
keepInsideView(entity);
}
function keepInsideView(entity) {
// vertical swap
if ((entity.y + entity.height) < 0)
entity.y = gameScene.height;
if (entity.y > gameScene.height)
entity.y = -entity.height;
// horizontal swap
if (entity.x > gameScene.width)
entity.x = -entity.width
if ((entity.x + entity.width) < 0)
entity.x = gameScene.width
}
}
ScriptBehavior {
id: shipBehavior
script: {
keepInsideViewBehavior.keepInsideView(entity)
if (gameScene.isUpPressed || gameScene.isDownPressed) {
var heading = Qt.point(gameScene.isUpPressed ? 10 : -10, 0);
var angle = entity.rotation * Math.PI / 180.0;
var rotatedHeading = gameScene.rotatePoint(heading, angle);
entity.applyLinearImpulse(rotatedHeading, entity.center);
}
if (gameScene.isLeftPressed || gameScene.isRightPressed) {
var rotationSpeed = gameScene.isRightPressed ? -1 : 1;
entity.setAngularVelocity(rotationSpeed);
} else if (!gameScene.isLeftPressed && !gameScene.isRightPressed)
entity.setAngularVelocity(0);
}
}
Component {
id: bulletComponent
Entity {
id: bullet
property point center: Qt.point(x + width / 2, y + height / 2)
objectName: "bullet"
width: 5
height: 5
entityType: Bacon2D.DynamicType
behavior: keepInsideViewBehavior
Fixture {
anchors.fill: parent
material: Material {
friction: 0.3
density: 3
restitution: 0.5
}
shape: Circle {
anchors.fill: parent
fill: ColorFill {
brushColor: "yellow"
}
}
}
Timer {
running: true
interval: 1000
triggeredOnStart: false
repeat: false
onTriggered: bullet.destroy()
}
}
}
Entity {
id: ship
objectName: "ship"
width: shipSprite.width
height: shipSprite.height
x: gameScene.width / 2.0 - ship.width / 2.0
y: gameScene.height / 2.0 - ship.height / 2.0
property point center: Qt.point(x + width / 2, y + height / 2)
entityType: Bacon2D.DynamicType
rotation: knob.rotation
behavior: shipBehavior
Fixture {
anchors.fill: parent
material: shipMaterial
shape: Box {
anchors.fill: parent
}
}
Material {
id: shipMaterial
friction: 0.3
density: 3
restitution: 0.5
}
Sprite {
id: shipSprite
animation: "thrusting"
animations: [
SpriteAnimation {
name: "thrusting"
source: "images/ship_sprite.png"
frames: 4
duration: gameScene.isUpPressed ? 200 : 500
loops: Animation.Infinite
}
]
}
function fire() {
var originPoint = Qt.point(ship.width / 2.0 + 5, 0);
var angle = ship.rotation * Math.PI / 180.0;
var rotatedOrigin = gameScene.rotatePoint(originPoint, angle);
console.log(rotatedOrigin.x, rotatedOrigin.y)
var bulletObject = bulletComponent.createObject(gameScene);
bulletObject.x = ship.center.x + rotatedOrigin.x
bulletObject.y = ship.center.y + rotatedOrigin.y
bulletObject.applyLinearImpulse(rotatedOrigin, bulletObject.center)
}
}
Component {
id: asteroidSpriteComponentL1
Asteroid {
id: asteroid
maxImpulse: 1000
maxAngularVelocity: 0.1
splitLevel: 1
childAsteroid: asteroidSpriteComponentL2
behavior: keepInsideViewBehavior
onAsteroidCreated: gameScene.totalAsteroids += 1
onAsteroidDestroyed: gameScene.totalAsteroids -= 1
}
}
Component {
id: asteroidSpriteComponentL2
Asteroid {
id: asteroid
maxImpulse: 400
maxAngularVelocity: 0.1
splitLevel: 2
childAsteroid: asteroidSpriteComponentL3
behavior: keepInsideViewBehavior
onAsteroidCreated: gameScene.totalAsteroids += 1
onAsteroidDestroyed: gameScene.totalAsteroids -= 1
}
}
Component {
id: asteroidSpriteComponentL3
Asteroid {
id: asteroid
maxImpulse: 100
maxAngularVelocity: 0.1
splitLevel: 3
behavior: keepInsideViewBehavior
onAsteroidCreated: gameScene.totalAsteroids += 1
onAsteroidDestroyed: gameScene.totalAsteroids -= 1
}
}
Knob {
id: knob
z: 1
anchors.bottom: parent.bottom
width: parent.width >= 800 ? 200 : parent.width/2
height: width
}
Column {
id: thrustArea
z: 1
anchors {
bottom: parent.bottom
right: parent.right
bottomMargin: 10
rightMargin: 10
}
height: (width * 2) + spacing
width: parent.width >= 800 ? 100 : parent.width/3
spacing: 10
Rectangle {
anchors {
left: parent.left
right: parent.right
}
height: parent.width
color: "white"
opacity: 0.1
MouseArea {
id: upArea
anchors.fill: parent
}
}
Rectangle {
anchors {
left: parent.left
right: parent.right
}
height: parent.width
color: "white"
opacity: 0.1
MouseArea {
id: downArea
anchors.fill: parent
}
}
}
function rotatePoint(point, angle) {
var rotatedPoint = Qt.point((point.x * Math.cos(angle)) - (point.y * Math.sin(angle)),
(point.x * Math.sin(angle)) + (point.y * Math.cos(angle)));
return rotatedPoint;
}
function _reset() {
var asteroidObject;
for (var i = 0; i < gameScene.numberOfAsteroids; i++) {
asteroidObject = asteroidSpriteComponentL1.createObject(gameScene);
asteroidObject.x = Math.random() * gameScene.width;
asteroidObject.y = Math.random() * gameScene.height;
}
}
}
}