Gravity.qml 6.2 KB

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