Gravity.qml 5.5 KB

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