MyShip.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import Qt 4.7
  5. import "Game.js" as GameScript
  6. Item {
  7. id: myShip
  8. objectName: "myShip"
  9. property variant myShipSize
  10. property int originalY
  11. // Fires missile if exists
  12. function fire() {
  13. if (myShip.opacity==1) {
  14. GameScript.fireMissile(myShip.x+myShip.width/2,myShip.y,myShip.height*-1)
  15. if (myShip.y+myShip.height < gameArea.height+5) {
  16. goDownAnim.restart()
  17. }
  18. }
  19. }
  20. function createGraphicsForLevel() {
  21. myShip.myShipSize = LevelPlugin.graphSize(LevelPlugin.pathToMyShipPic())
  22. myShip.height = myShipSize.height
  23. myShip.width = myShipSize.width
  24. image.source = "file:/"+LevelPlugin.pathToMyShipPic()
  25. myShip.y = gameArea.height - myShip.height - 10
  26. originalY = myShip.y
  27. myShip.x = (gameArea.width - myShip.width)/ 2
  28. }
  29. Image {
  30. id: image
  31. smooth: true
  32. }
  33. Keys.onSpacePressed: { fire() }
  34. Keys.onSelectPressed: { fire() }
  35. Keys.onRightPressed: {
  36. if (myShip.x < (gameArea.width - myShip.width - 20)) {
  37. toRightAnim.restart()
  38. }
  39. }
  40. Keys.onLeftPressed: {
  41. if (myShip.x > 20) {
  42. toLeftAnim.restart()
  43. }
  44. }
  45. // To right animation
  46. PropertyAnimation { id: toRightAnim; target: myShip; easing.type: Easing.OutQuint;
  47. properties: "x"; from: myShip.x; to: myShip.x + 20; duration: 500 }
  48. // To left animation
  49. PropertyAnimation { id: toLeftAnim; target: myShip; easing.type: Easing.OutQuint;
  50. properties: "x"; from: myShip.x; to: myShip.x - 20; duration: 500 }
  51. // Go down on fire animation
  52. SequentialAnimation {
  53. id: goDownAnim
  54. NumberAnimation { target: myShip; property:"y"; from: myShip.y; to: myShip.y+5;
  55. easing.type: Easing.Linear; duration: 200 }
  56. NumberAnimation { target: myShip; property:"y"; from: myShip.y; to: originalY;
  57. easing.type: Easing.Linear; duration: 200 }
  58. }
  59. /*
  60. MouseArea {
  61. anchors.fill: parent
  62. drag.target: myShip
  63. drag.axis: Drag.XAxis
  64. drag.minimumX: 0
  65. drag.maximumX: gameArea.width - myShip.width
  66. onReleased: {
  67. fire()
  68. }
  69. }
  70. */
  71. /*
  72. property bool isAutoRepeat: false
  73. Keys.onPressed: {
  74. if (event.key == Qt.Key_Right) {
  75. if (event.isAutoRepeat) {
  76. isAutoRepeat = true
  77. toRightAnim.restart()
  78. } else {
  79. isAutoRepeat = false
  80. toRotateRightAndCenterAnim.restart()
  81. }
  82. event.accepted = true
  83. }
  84. else if (event.key == Qt.Key_Left) {
  85. if (event.isAutoRepeat) {
  86. isAutoRepeat = true
  87. toLeftAnim.restart()
  88. } else {
  89. isAutoRepeat = false
  90. toRotateLeftAndCenterAnim.restart()
  91. }
  92. event.accepted = true
  93. }
  94. }
  95. Keys.onReleased: {
  96. if (isAutoRepeat) {
  97. toRotateRightAndCenterAnim.stop()
  98. toRotateLeftAndCenterAnim.stop()
  99. } else {
  100. toRotateCenter.restart()
  101. }
  102. event.accepted = true
  103. }
  104. // Rotation on movement animation
  105. transform: Rotation { id:rotationId; origin.x: width/2; origin.y: height/2; axis { x: 0; y: 1; z: 0 } angle: 0 }
  106. SequentialAnimation {
  107. id: toRotateRightAndCenterAnim
  108. PropertyAnimation { target: rotationId; easing.type: Easing.Linear;
  109. properties: "angle"; from: 0; to: 30; duration: 100 }
  110. PropertyAnimation { target: myShip; easing.type: Easing.OutQuint;
  111. properties: "x"; from: myShip.x; to: myShip.x + 20; duration: 500 }
  112. PropertyAnimation { target: rotationId; easing.type: Easing.Linear;
  113. properties: "angle"; to: 0; duration: 100 }
  114. }
  115. SequentialAnimation {
  116. id: toRotateLeftAndCenterAnim
  117. PropertyAnimation { target: rotationId; easing.type: Easing.Linear;
  118. properties: "angle"; from: 0; to: -30; duration: 100 }
  119. PropertyAnimation { target: myShip; easing.type: Easing.OutQuint;
  120. properties: "x"; from: myShip.x; to: myShip.x - 20; duration: 500 }
  121. PropertyAnimation { target: rotationId; easing.type: Easing.Linear;
  122. properties: "angle"; to: 0; duration: 100 }
  123. }
  124. PropertyAnimation { id: toRotateRight; target: rotationId; easing.type: Easing.Linear;
  125. properties: "angle"; from: 0; to: 30; duration: 100 }
  126. PropertyAnimation { id: toRotateCenter; target: rotationId; easing.type: Easing.Linear;
  127. properties: "angle"; to: 0; duration: 100 }
  128. PropertyAnimation { id: toRotateLeft; target: rotationId; easing.type: Easing.Linear;
  129. properties: "angle"; from: 0; to: -30; duration: 100 }
  130. */
  131. }