Gravity.qml 5.3 KB

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