Physics.qml 6.6 KB

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