Game.qml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) 2010 Nokia Corporation.
  3. */
  4. import Qt 4.7
  5. import "Game.js" as GameScript
  6. Item {
  7. id: gameArea
  8. width: 360; height: 640 // NOTE: have to exists some default sizes
  9. anchors.fill: parent
  10. objectName: "gameArea"
  11. // Who win, you or computer
  12. property bool gameOverProperty: false
  13. // for info message to user
  14. property variant messageBox
  15. // Level activated signal for Qt side to load activated level
  16. signal levelActivated(int index)
  17. // Pause whole game, if level is loaded
  18. function pauseGame() {
  19. if (GameScript.levelPlugin) {
  20. GameScript.pauseGame(true)
  21. myShip.focus = false
  22. btnPause.opacity = 0
  23. menu.showPauseMenu()
  24. }
  25. }
  26. // Qt side call this for QML take new level into use
  27. function levelReadyForCreation() {
  28. // Hide menu
  29. menu.hideMenu()
  30. // Show message
  31. message.showMessage("Loading Level...",2000)
  32. // Wait before create level
  33. levelCreationTimer.restart()
  34. }
  35. // Fires ememy missile. Function is called from Qt side
  36. function fireEnemyMissile(aXpox, aFromYpos, aToYpos) {
  37. GameScript.fireEnemyMissile(aXpox, aFromYpos, aToYpos)
  38. }
  39. // Game Over handling. Function is called from Qt side
  40. function gameOver(youWin) {
  41. // Stop GameEngine timers
  42. GameEngine.enableEngineTimer(false)
  43. gameOverProperty = youWin
  44. GameScript.hideMissiles()
  45. gameOverTimer.restart()
  46. //GameEngine.vibra()
  47. }
  48. // End game and show menu
  49. function endGame(showMessage) {
  50. backgroundPic.opacity = 1
  51. // Stop GameEngine timers
  52. GameEngine.enableEngineTimer(false)
  53. // Do game over
  54. GameScript.gameOver()
  55. GameScript.hideMissiles()
  56. // Clear GameEngine QML objects
  57. GameEngine.clearQmlObjects()
  58. myShip.opacity = 0
  59. myShip.focus = false
  60. btnPause.opacity = 0
  61. idMainLogo.opacity = 1
  62. menu.showLevelMenu()
  63. if (showMessage) {
  64. message.showMessage("Game End",2000)
  65. }
  66. }
  67. // Timer for level creationing
  68. Timer {
  69. id: levelCreationTimer
  70. interval: 1000; running: false; repeat: false
  71. onTriggered: {
  72. btnPause.opacity = 1
  73. myShip.opacity = 1
  74. myShip.focus = true
  75. // Create new level
  76. var ret = GameScript.createLevel()
  77. if (ret==0) {
  78. backgroundPic.opacity = 0
  79. // Find QML object for GameEngine
  80. GameEngine.findQmlObjects()
  81. // Enable GameEngine timer
  82. GameEngine.enableEngineTimer(true)
  83. } else {
  84. // Level cannot be created!
  85. endGame(false)
  86. }
  87. }
  88. }
  89. // Timer for game over
  90. Timer {
  91. id: gameOverTimer
  92. interval: 1500; running: false; repeat: false
  93. onTriggered: {
  94. backgroundPic.opacity = 1
  95. idMainLogo.opacity = 1
  96. // Do game over
  97. GameScript.gameOver()
  98. // Clear GameEngine QML objects
  99. GameEngine.clearQmlObjects()
  100. menu.showLevelMenu()
  101. if (gameOverProperty) {
  102. GameEngine.playInternalSound(4)
  103. message.showMessage("Game Over, You win!",2000)
  104. } else {
  105. GameEngine.playInternalSounds(3,3)
  106. message.showMessage("Game Over, You lose",2000)
  107. }
  108. }
  109. }
  110. // Menu signal to slot connections
  111. Connections {
  112. target: menu
  113. // Level selected
  114. onLevelSelected: {
  115. // Stop bigship animation
  116. bigShipAnim.stop()
  117. bigShip.opacity = 0
  118. // Hide logo
  119. idMainLogo.opacity = 0
  120. // Signal level activated
  121. gameArea.levelActivated(levelIndex)
  122. }
  123. // Level resumed selected
  124. onResumeSelected: {
  125. menu.hideMenu()
  126. GameScript.pauseGame(false)
  127. myShip.focus = true
  128. }
  129. }
  130. // This component creation completed
  131. Component.onCompleted: {
  132. // Show main Level selection menu
  133. menu.showLevelMenu()
  134. // Start big ship animation
  135. bigShipAnim.restart()
  136. // Set variable
  137. messageBox = message
  138. // Play game start sound
  139. GameEngine.gameStartSound()
  140. // Fade splash screen
  141. fadeAnim.restart()
  142. // Rotate splash logo
  143. rotAnim.restart()
  144. }
  145. // Background image for the game
  146. Image {
  147. id: backgroundPic
  148. z:1
  149. source: "qrc:/gfx/background2.png"
  150. fillMode: Image.PreserveAspectCrop
  151. smooth: true
  152. anchors.fill: parent
  153. }
  154. Image {
  155. id: bigShip
  156. source: "qrc:/gfx/bigship.png"
  157. z:1.5
  158. smooth: true
  159. x: width * -1
  160. y: parent.height * 0.55
  161. }
  162. SequentialAnimation {
  163. id: bigShipAnim;
  164. NumberAnimation {target:bigShip; property:"x"; to:gameArea.width; easing.type: Easing.Linear; duration: 80000 }
  165. PropertyAction {target:bigShip; properties: "opacity"; value: 0}
  166. }
  167. // Enemies grid
  168. Item {
  169. // This is general level QML plaseholder
  170. // Into this is level QML created in createLevel()
  171. id:levelId
  172. anchors.fill: parent
  173. z:2
  174. }
  175. Image {
  176. id: idMainLogo
  177. z:19
  178. source: "qrc:/gfx/quickhit_logo.png"
  179. smooth: true
  180. anchors.horizontalCenter: gameArea.horizontalCenter
  181. y:gameArea.height / 10 * 1
  182. }
  183. // Game menu
  184. Menu {
  185. z:20
  186. id:menu
  187. width: gameArea.width / 5 * 4
  188. height: gameArea.height / 10 * 4
  189. anchors.verticalCenter: gameArea.verticalCenter
  190. anchors.horizontalCenter: gameArea.horizontalCenter
  191. }
  192. // My ship
  193. MyShip {
  194. z:10
  195. id: myShip
  196. opacity: 0
  197. }
  198. // Mouse area of your ship
  199. MouseArea {
  200. //anchors.fill: parent
  201. width: parent.width
  202. height: myShip.height
  203. x:0
  204. y:gameArea.height - myShip.height
  205. drag.target: myShip
  206. drag.axis: Drag.XAxis
  207. drag.minimumX: 0
  208. drag.maximumX: gameArea.width - myShip.width
  209. property int startPos: 0
  210. property int dragCount: 0
  211. onPressed: {
  212. startPos = mouseX
  213. dragCount = 0
  214. }
  215. onPositionChanged: {
  216. //dragCount = Math.abs(mouseX-startPos)
  217. }
  218. onReleased: {
  219. //if (dragCount<20) {
  220. myShip.fire()
  221. //}
  222. }
  223. }
  224. // Pause button
  225. Button {
  226. id: btnSound
  227. z:12
  228. //animationEnabled: false
  229. anchors.top: parent.top
  230. anchors.topMargin: 10
  231. buttonPath: "qrc:/gfx/soundOn.png"
  232. buttonId: 4
  233. width: gameArea.width / 10
  234. height: gameArea.width / 10
  235. x: gameArea.width - width - 15
  236. opacity: 1
  237. Connections {
  238. target: btnSound
  239. onBtnClicked: {
  240. if (btnSound.buttonId==4) {
  241. // Sound off
  242. btnSound.buttonPath = "qrc:/gfx/soundOff.png"
  243. btnSound.buttonId = 5
  244. GameEngine.enableSounds(false)
  245. } else {
  246. // Sound on
  247. btnSound.buttonPath = "qrc:/gfx/soundOn.png"
  248. btnSound.buttonId = 4
  249. GameEngine.enableSounds(true)
  250. }
  251. }
  252. }
  253. }
  254. // Pause button
  255. Button {
  256. id: btnPause
  257. z:13
  258. anchors.top: parent.top
  259. anchors.topMargin: 10
  260. buttonPath: "qrc:/gfx/pause.png"
  261. buttonId: 3
  262. width: gameArea.width / 10
  263. height: gameArea.width / 10
  264. x: gameArea.width - width - btnPause.width - 15*2
  265. opacity: 0
  266. Connections {
  267. target: btnPause
  268. onBtnClicked: {
  269. GameScript.pauseGame(true)
  270. myShip.focus = false
  271. btnPause.opacity = 0
  272. menu.showPauseMenu()
  273. }
  274. }
  275. }
  276. // Hidden missiles ready for to be launched
  277. Missile {
  278. z:3
  279. id: missile_1
  280. x:0
  281. y:10
  282. }
  283. Missile {
  284. z:4
  285. id: missile_2
  286. x: 20
  287. y:10
  288. }
  289. Missile {
  290. z:5
  291. id: missile_3
  292. x: 40
  293. y:10
  294. }
  295. Missile {
  296. z:6
  297. id: missile_4
  298. x: 60
  299. y:10
  300. }
  301. Missile {
  302. z:7
  303. id: missile_5
  304. x: 80
  305. y:10
  306. }
  307. Missile {
  308. z:8
  309. objectName: "enemy_missile"
  310. id: enemy_missile_1
  311. enemyMissile: true
  312. }
  313. Missile {
  314. z:9
  315. objectName: "enemy_missile"
  316. id: enemy_missile_2
  317. enemyMissile: true
  318. }
  319. // Messages to the user
  320. Message {
  321. id: message
  322. z:21
  323. }
  324. // Splach screen
  325. Rectangle {
  326. id: blackFace
  327. x:-2
  328. y:0
  329. width: parent.width + 2
  330. height: parent.height
  331. z:100
  332. color: "black"
  333. opacity: 1
  334. MouseArea {
  335. anchors.fill: parent
  336. onPressed: {
  337. mouse.accepted = true
  338. fadeAnim.stop()
  339. blackFace.opacity = 0
  340. }
  341. }
  342. Image {
  343. id: idLogo
  344. source: "qrc:/gfx/quickhit_logo.png"
  345. smooth: true
  346. anchors.horizontalCenter: parent.horizontalCenter
  347. anchors.verticalCenter: parent.verticalCenter
  348. opacity: 0
  349. }
  350. NumberAnimation { id: rotAnim; target: idLogo; property: "rotation"; to: 20; duration: 9000 }
  351. }
  352. // Splach screen animation
  353. SequentialAnimation {
  354. id: fadeAnim
  355. PauseAnimation {duration: 1000 }
  356. NumberAnimation {target: idLogo; property: "opacity"; from: 0; to: 1; duration: 2000 }
  357. PauseAnimation {duration: 2000 }
  358. NumberAnimation {target: idLogo; property: "opacity"; from: 1; to: 0; duration: 2000 }
  359. NumberAnimation {target: blackFace; property: "opacity"; from: 1; to: 0; duration: 2000 }
  360. }
  361. }