Gravity.qml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.leftViewport.connect(function() { d.itemCount--; })
  35. item.y = -colHeight - item.height
  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
  67. repeat: true
  68. running: true
  69. onTriggered: {
  70. column.addImage()
  71. interval = Math.random()*500 + 500
  72. }
  73. }
  74. Timer {
  75. id: deathTimer
  76. running: true
  77. repeat: true
  78. interval: 1000*(settings.interval > 60 ? 60*(settings.interval-60) : settings.interval)*(Math.random()+1)
  79. onTriggered: {
  80. if (pictureArray.length > 0) {
  81. pictureArray.shift().world = bullshitWorld
  82. }
  83. }
  84. }
  85. Connections {
  86. target: root
  87. onTogglePause: deathTimer.running = !deathTimer.running
  88. onNext: deathTimer.triggered()
  89. onToggleChaos: fixedRotation = !fixedRotation
  90. }
  91. Timer {
  92. id: settleTimer
  93. running: false
  94. interval: 200
  95. onTriggered: deathTimer.triggered()
  96. }
  97. Component.onCompleted: settleTimer.start()
  98. }
  99. }
  100. // floor
  101. RectangleBoxBody {
  102. id: floor
  103. world: commonWorld
  104. height: 1
  105. anchors {
  106. left: parent.left
  107. right: parent.right
  108. top: parent.bottom
  109. }
  110. friction: 1
  111. density: 1
  112. }
  113. Repeater {
  114. model: settings.columnCount
  115. delegate: columnComponent
  116. }
  117. // TODO: The boot (Monty Python foot) of death to be applied to the stacks
  118. RectangleBoxBody {
  119. id: rect
  120. enabled: false
  121. visible: false
  122. friction: 1.0
  123. density: 1000
  124. color: "red"
  125. width: 50; height: 50
  126. bullet: true
  127. SequentialAnimation {
  128. id: murderAnimation
  129. //loops: Animation.Infinite
  130. //running: true
  131. ScriptAction { script: { root.togglePause() } }
  132. ScriptAction { script: { rect.world = worldArray.pop() } }
  133. PropertyAction { target: rect; property: "x"; value: -rect.width }
  134. PropertyAction { target: rect; property: "y"; value: root.height }
  135. ParallelAnimation {
  136. NumberAnimation { target: rect; property: "x"; to: 2560; duration: 1000 }
  137. NumberAnimation { target: rect; property: "y"; to: 0; duration: 1000 }
  138. }
  139. }
  140. }
  141. Rectangle {
  142. z: 1
  143. color: "black"
  144. anchors { right: parent.right; top: parent.top }
  145. width: itemCountLabel.width
  146. height: itemCountLabel.height
  147. Text {
  148. id: itemCountLabel
  149. font.pixelSize: 100
  150. text: d.itemCount
  151. color: "white"
  152. }
  153. }
  154. Keys.onUpPressed: root.togglePause()
  155. Keys.onDownPressed: root.toggleChaos() //root.next()
  156. }