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
|
import QtQuick 2.0
import QtQuick.Layouts 1.1
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1
import Machines 1.0
import QtMultimedia 5.0
import QtQuick.Window 2.2
import QtSystemInfo 5.0
import "ui"
import "components"
Window {
height: units.gu(40)
width: units.gu(70)
minimumWidth: height + units.gu(10)
minimumHeight: units.gu(35)
title: "Machines vs. Machines"
Component.onCompleted: {
if (!args.values.d) {
visibility = Window.FullScreen
}
}
MainView {
id: app
anchors.fill: parent
useDeprecatedToolbar: false
applicationName: "com.ubuntu.developer.mzanetti.machines-vs-machines"
property bool performanceOverlayEnabled: false
property bool managingTowers: false
/******************************************************
* Global definitions *
******************************************************/
// Colors
backgroundColor: "#ededed"
property color foregroundColor: "#333333"
property color textColor: foregroundColor
property color playButtonColor: "#2eacd2"
property color buttonTextColor: "#ffffff"
property color confirmationButtonColor: "#35af44"
property color rejectButtonColor: "#db3131"
property color cancelButtonColor: "#8f8f8f"
property string modalWindowBackgroundColor: "#CC000000"
// Sizes
property int appHeight: Math.min(app.height, app.width)
property int buttonSize: appHeight / 10
property int margins: appHeight / 30
property int titleSize: appHeight / 25
property int fontSize: appHeight / 26
property int hugeFontSize: appHeight / 10
Component.onCompleted: {
app.updateAudio();
}
Engine {
id: engine
dataDir: Qt.resolvedUrl("../data/lpbuild")
}
Audio {
id: menuMusic
loops: Audio.Infinite
volume: 1.0 * SettingsBackend.volume / 100
}
Audio {
id: gamePlayMusic
loops: Audio.Infinite
source: engine.levelPack ? Qt.resolvedUrl(engine.dataDir + "/" + engine.levelPack.id + "/sounds/" + engine.levelPack.get(engine.level).soundFile + ".ogg") : ""
volume: 1.0 * SettingsBackend.volume / 100
}
ScreenSaver {
screenSaverEnabled: engine.playStatus !== Engine.PlayStatusRunning
}
Connections {
target: engine
onPlayStatusChanged: {
app.updateAudio();
}
}
Connections {
target: Qt.application
onActiveChanged: {
app.updateAudio();
if (!active && engine.playStatus == Engine.PlayStatusRunning) {
engine.pauseGame();
}
}
}
function updateAudio() {
if (!SettingsBackend.audioEnabled) {
menuMusic.stop();
gamePlayMusic.stop();
return;
}
if (!Qt.application.active) {
menuMusic.pause();
gamePlayMusic.pause();
return;
}
switch (engine.playStatus) {
case Engine.PlayStatusNotStarted:
// menuMusic.stop();
if (mainPage.item) {
menuMusic.source = Qt.resolvedUrl(engine.dataDir + "/" + mainPage.item.currentLevelpackId + "/sounds/" + engine.levelPacks.levelPack(mainPage.item.currentLevelpackId).titleSound + ".ogg");
} else {
menuMusic.source = Qt.resolvedUrl(engine.dataDir + "/" + engine.levelPack.id + "/sounds/" + engine.levelPacks.levelPack(engine.levelPack.id).titleSound + ".ogg");
}
menuMusic.play();
gamePlayMusic.stop();
break;
case Engine.PlayStatusRunning:
case Engine.PlayStatusPaused:
menuMusic.stop();
gamePlayMusic.play();
break;
default:
}
}
Connections {
target: SettingsBackend
onAudioEnabledChanged: {
app.updateAudio()
}
}
automaticOrientation: true
PageLoader {
id: mainPage
page: "MainPage.qml"
shown: engine.levelPack == null
Binding {
target: mainPage.item
property: "showDeveloperOptions"
value: args.values.d
}
Connections {
target: mainPage.item
onOpenSettings: settings.shown = true
onLoadLevelPack: {
engine.loadLevelPack(levelPack);
}
onOpenInfoPage: {
infoPage.shown = true
}
onCurrentLevelpackIdChanged: app.updateAudio()
}
}
Arguments {
id: args
defaultArgument.help: "Expects URL of the media to play."
defaultArgument.valueNames: ["URL"]
Argument {
name: "d"
help: "wheter or not to enable developer options"
required: false
}
}
PageLoader {
id: settings
page: "Settings.qml"
Binding {
target: settings.item
property: "showDeveloperOptions"
value: args.values.d
}
}
PageLoader {
id: levelSelector
page: "LevelSelector.qml"
shown: engine.levelPack && engine.playStatus != Engine.PlayStatusRunning && engine.playStatus != Engine.PlayStatusPaused
Binding {
target: levelSelector.item
property: "engine"
value: engine
}
Connections {
target: levelSelector.item
onCloseButtonClicked: {
engine.unloadLevelPack()
}
onLevelSelected: {
if (index <= engine.highestUnlockedLevel) {
levelSplash.level = index
levelSplash.shown = true;
}
}
onShowScoreInfo: {
scoreInfo.shown = true;
}
}
}
PageLoader {
id: levelSplash
page: "LevelSplash.qml"
property int level: -1
Binding {
target:levelSplash.item
property: "level"
value: levelSplash.level
}
Connections {
target: levelSplash.item
onStartLevel: {
if(!SettingsBackend.tutorialShown) {
tutorial.shown = true;
} else {
engine.startNewGame(levelSplash.level)
}
}
}
}
PageLoader {
id: tutorial
page: "Tutorial.qml"
Connections {
target: tutorial.item
onCloseButtonClicked: {
engine.startNewGame(levelSplash.level)
SettingsBackend.tutorialShown = true;
}
}
}
PageLoader {
id: gameView
page: "GameView.qml"
shown: engine.playStatus !== Engine.PlayStatusNotStarted
visible: shown
Binding {
target: gameView.item
property: "engine"
value: engine
}
Binding {
target: gameView.item
property: "fieldOverlay"
value: SettingsBackend.fieldOverlayEnabled
}
}
PageLoader {
id: levelResultsSplash
page: "LevelResultsSplash.qml"
shown: engine.playStatus == Engine.PlayStatusLost || engine.playStatus == Engine.PlayStatusWon
Binding {
target: levelResultsSplash.item
property: "engine"
value: engine
}
Connections {
target: levelResultsSplash.item
onRetry:{
engine.startNewGame(engine.level)
}
}
}
PageLoader {
id: levelPausedSplash
page: "LevelPausedSplash.qml"
shown: engine.playStatus == Engine.PlayStatusPaused
cached: engine.playStatus == Engine.PlayStatusPaused || engine.playStatus == Engine.PlayStatusRunning
Binding {
target: levelPausedSplash.item
property: "level"
value: engine.level
}
}
PageLoader {
id: towerInfoDialog
page: "UnlockTowerOverlay.qml"
cached: engine.playStatus == Engine.PlayStatusRunning || engine.playStatus == Engine.PlayStatusPaused
property var lineup: null
property int selectedLevel: 0
Binding {
target: towerInfoDialog.item
property: "lineup"
value: towerInfoDialog.lineup
}
onShownChanged: {
if (shown) {
engine.pauseGame();
towerInfoDialog.item.selectedLevel = towerInfoDialog.selectedLevel;
} else {
engine.resumeGame();
}
}
}
PageLoader {
id: scoreInfo
page: "ScoreInfo.qml"
}
PageLoader {
id: infoPage
page: "InfoPage.qml"
}
Rectangle {
id: rotateRect
anchors.fill: parent
color: app.backgroundColor
property bool landscape: width > height
opacity: landscape ? 0 : 1
Behavior on opacity {
UbuntuNumberAnimation {}
}
onLandscapeChanged: {
if (!landscape && engine.playStatus == Engine.PlayStatusRunning) {
engine.pauseGame();
}
}
GameLabel {
anchors.centerIn: parent
text: "Please rotate your device"
font.pixelSize: app.buttonSize
rotation: 90
}
}
}
}
|