-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsteroid.qml
121 lines (94 loc) · 2.75 KB
/
Asteroid.qml
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
import QtQuick 2.0
import Bacon2D 1.0
Entity {
id: asteroid
objectName: "asteroid"
property variant center: Qt.point(x + width / 2, y + height / 2)
property double maxImpulse: 1000
property double maxAngularVelocity: 0.1
property int splitLevel: 1
property variant childAsteroid
signal asteroidCreated();
signal asteroidDestroyed();
width: asteroidImage.width
height: asteroidImage.height
entityType: Bacon2D.DynamicType
Fixture {
anchors.fill: parent
material: asteroidMaterial
shape: Circle {
anchors.fill: parent
}
}
Material {
id: asteroidMaterial
friction: 0.3
density: 5
restitution: 0.5
}
Image {
id: asteroidImage
source: "images/asteroid-s"+ asteroid.splitLevel + "-" + Math.ceil((Math.random() * 5) + 1) + ".png"
}
Sprite {
id: explosionAnimation
visible: false
animation: "explosion"
anchors.centerIn: parent
animations: SpriteAnimation {
running: false
name: "explosion"
source: "images/explosion.png"
frames: 4
duration: 400
onFinished: explosionAnimation.visible = false
}
}
Component.onCompleted: {
asteroidCreated();
applyRandomImpulse();
setRandomAngularVelocity();
}
function randomDirection() {
return ((Math.random() >= 0.5) ? -1.0 : 1.0);
}
function randomImpulse() {
return Math.random() * asteroid.maxImpulse * randomDirection();
}
function randomAngularVelocity() {
return asteroid.maxAngularVelocity * randomDirection();
}
function applyRandomImpulse() {
var impulse = Qt.point(randomImpulse(), randomImpulse());
applyLinearImpulse(impulse, center);
}
function setRandomAngularVelocity() {
setAngularVelocity(randomAngularVelocity());
}
function createChild(component)
{
var asteroidObject = component.createObject(gameScene);
asteroidObject.x = asteroid.x + Math.random() * asteroid.width;
asteroidObject.y = asteroid.y + Math.random() * asteroid.height;
}
function createChildren(component) {
createChild(component);
createChild(component);
}
function damage() {
explosionAnimation.visible = true;
explosionAnimation.initializeAnimation();
explodeTimer.start();
}
Timer {
id: explodeTimer
interval: 400
running: false
onTriggered: {
if (asteroid.childAsteroid != undefined)
createChildren(asteroid.childAsteroid)
asteroidDestroyed();
asteroid.destroy();
}
}
}