Gravity.qml 5.2 KB

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