Gravity.qml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. }
  114. }
  115. // floor
  116. RectangleBoxBody {
  117. id: floor
  118. world: commonWorld
  119. height: 1
  120. anchors {
  121. left: parent.left
  122. right: parent.right
  123. top: parent.bottom
  124. }
  125. friction: 1
  126. density: 1
  127. }
  128. Repeater {
  129. model: d.columnCount
  130. delegate: columnComponent
  131. }
  132. // TODO: The boot (Monty Python foot) of death to be applied to the stacks
  133. RectangleBoxBody {
  134. id: rect
  135. enabled: false
  136. visible: false
  137. friction: 1.0
  138. density: 1000
  139. color: "red"
  140. width: 50; height: 50
  141. bullet: true
  142. SequentialAnimation {
  143. id: murderAnimation
  144. //loops: Animation.Infinite
  145. //running: true
  146. ScriptAction { script: { root.togglePause() } }
  147. ScriptAction { script: { rect.world = worldArray.pop() } }
  148. PropertyAction { target: rect; property: "x"; value: -rect.width }
  149. PropertyAction { target: rect; property: "y"; value: root.height }
  150. ParallelAnimation {
  151. NumberAnimation { target: rect; property: "x"; to: 2560; duration: 1000 }
  152. NumberAnimation { target: rect; property: "y"; to: 0; duration: 1000 }
  153. }
  154. }
  155. }
  156. Rectangle {
  157. visible: settings.viewItemCount
  158. z: 1
  159. color: "black"
  160. anchors { right: parent.right; top: parent.top }
  161. width: itemCountLabel.width
  162. height: itemCountLabel.height
  163. Text {
  164. id: itemCountLabel
  165. font.pixelSize: 100
  166. text: d.itemCount
  167. color: "white"
  168. }
  169. }
  170. Keys.onUpPressed: root.togglePause()
  171. Keys.onDownPressed: root.toggleChaos() //root.next()
  172. }