Gravity.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import QtQuick 2.5
  2. import Box2D 2.0
  3. Item {
  4. id: root
  5. signal togglePause
  6. signal toggleChaos
  7. signal next
  8. property var pictureDelegate: Qt.createComponent("HorizontalArtDelegate.qml")
  9. anchors.fill: parent
  10. QtObject {
  11. id: d
  12. property double pace: settings.pace/60.0
  13. property int itemCount: 0
  14. property int itemTravel: settings.itemTravel
  15. property int primedColumns: 0
  16. property int columnCount: settings.columnCount
  17. property bool running: primedColumns >= columnCount
  18. function reset() {
  19. itemCount = 0
  20. primedColumns = 0
  21. }
  22. onColumnCountChanged: reset()
  23. }
  24. World {
  25. id: bullshitWorld
  26. timeStep: d.pace
  27. running: d.running
  28. }
  29. World {
  30. id: commonWorld
  31. timeStep: d.pace
  32. running: d.running
  33. }
  34. Component {
  35. id: columnComponent
  36. Item {
  37. id: column
  38. property int columnHeight: 0
  39. property bool full: false
  40. onColumnHeightChanged: {
  41. if (!column.full && (columnHeight > root.height)) {
  42. d.primedColumns += 1
  43. column.full = true
  44. }
  45. }
  46. function addImage() {
  47. if (columnHeight < (1.1+1/d.columnCount)*root.height) {
  48. var item = pictureDelegate.createObject(column)
  49. columnHeight += (item.height + d.itemTravel)
  50. item.y = floor.y - columnHeight
  51. d.itemCount++
  52. pictureArray.push(item)
  53. }
  54. }
  55. x: width * index
  56. width: parent.width/d.columnCount
  57. anchors { top: parent.top; bottom: parent.bottom }
  58. property var pictureArray: []
  59. property var physicsWorld: settings.globalWorld ? commonWorld : columnWorld
  60. property bool fixedRotation: true
  61. World {
  62. id: columnWorld
  63. timeStep: d.pace
  64. // There is less item fall through when this runs perpetually
  65. running: d.running
  66. }
  67. RectangleBoxBody {
  68. world: physicsWorld
  69. height: 1
  70. anchors {
  71. left: parent.left
  72. right: parent.right
  73. top: parent.bottom
  74. }
  75. friction: 1
  76. density: 1
  77. }
  78. Timer {
  79. id: pumpTimer
  80. interval: Math.random()*500 + 500
  81. repeat: true
  82. running: true
  83. onTriggered: {
  84. column.addImage()
  85. }
  86. }
  87. Timer {
  88. id: deathTimer
  89. running: d.running
  90. repeat: true
  91. interval: 1000*(settings.interval > 60 ? 60*(settings.interval-60) : settings.interval)*(Math.random()+1)
  92. onTriggered: {
  93. if (pictureArray.length > 0) {
  94. var image = pictureArray.shift()
  95. image.world = bullshitWorld
  96. d.itemCount--
  97. columnHeight -= (image.height + d.itemTravel)
  98. }
  99. }
  100. }
  101. Connections {
  102. target: root
  103. onTogglePause: deathTimer.running = !deathTimer.running
  104. onNext: deathTimer.triggered()
  105. onToggleChaos: fixedRotation = !fixedRotation
  106. }
  107. Timer {
  108. id: settleTimer
  109. running: false
  110. interval: 200
  111. onTriggered: deathTimer.triggered()
  112. }
  113. DebugDraw {
  114. id: debugDraw
  115. z: 1
  116. world: physicsWorld
  117. anchors.fill: parent
  118. opacity: 0.75
  119. visible: true
  120. }
  121. }
  122. }
  123. // floor
  124. RectangleBoxBody {
  125. id: floor
  126. world: commonWorld
  127. height: 1
  128. anchors {
  129. left: parent.left
  130. right: parent.right
  131. top: parent.bottom
  132. }
  133. friction: 1
  134. density: 1
  135. }
  136. Repeater {
  137. model: d.columnCount
  138. delegate: columnComponent
  139. }
  140. // TODO: The boot (Monty Python foot) of death to be applied to the stacks
  141. RectangleBoxBody {
  142. id: rect
  143. enabled: false
  144. visible: false
  145. friction: 1.0
  146. density: 1000
  147. color: "red"
  148. width: 50; height: 50
  149. bullet: true
  150. SequentialAnimation {
  151. id: murderAnimation
  152. //loops: Animation.Infinite
  153. //running: true
  154. ScriptAction { script: { root.togglePause() } }
  155. ScriptAction { script: { rect.world = worldArray.pop() } }
  156. PropertyAction { target: rect; property: "x"; value: -rect.width }
  157. PropertyAction { target: rect; property: "y"; value: root.height }
  158. ParallelAnimation {
  159. NumberAnimation { target: rect; property: "x"; to: 2560; duration: 1000 }
  160. NumberAnimation { target: rect; property: "y"; to: 0; duration: 1000 }
  161. }
  162. }
  163. }
  164. Rectangle {
  165. visible: settings.viewItemCount
  166. z: 1
  167. color: "black"
  168. anchors { right: parent.right; top: parent.top }
  169. width: itemCountLabel.width
  170. height: itemCountLabel.height
  171. Text {
  172. id: itemCountLabel
  173. font.pixelSize: 100
  174. text: d.itemCount
  175. color: "white"
  176. }
  177. }
  178. Keys.onUpPressed: root.togglePause()
  179. Keys.onDownPressed: root.toggleChaos() //root.next()
  180. }