Physics.qml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import QtQuick 2.5
  2. import Box2D 2.0
  3. import Qt.labs.settings 1.0
  4. import ".."
  5. View {
  6. id: root
  7. signal togglePause
  8. signal toggleChaos
  9. signal next
  10. property var pictureDelegate: Component {
  11. ArtDelegate {}
  12. }
  13. Settings {
  14. id: physicsSettings
  15. category: "Physics"
  16. property int itemTravel: 0
  17. property real pace: 1
  18. property bool globalWorld: false
  19. // Very computationally heavy: 40% vs 20% for 0.1 vs 0
  20. property real restitution: 0
  21. }
  22. QtObject {
  23. id: d
  24. property real pace: physicsSettings.pace/60.0
  25. property bool paused: false
  26. }
  27. World {
  28. id: world
  29. timeStep: d.pace
  30. running: physicsSettings.globalWorld && globalUtil.primed
  31. property var limbo: World {
  32. timeStep: world.timeStep
  33. running: world.running
  34. }
  35. }
  36. Component {
  37. id: columnComponent
  38. Item {
  39. id: column
  40. property int stackHeight: 0
  41. property bool full: false
  42. property int xOffset: width * index
  43. property var pictureArray: []
  44. property bool fixedRotation: true
  45. function considerImage() {
  46. if (stackHeight < (1.3 + 1/globalSettings.columnCount)*root.height) {
  47. addImage()
  48. }
  49. }
  50. function addImage() {
  51. var image = pictureDelegate.createObject(column, { x: -1000, y: -1000 })
  52. if (globalSettings.effect !== "" && Effects.validate(globalSettings.effect)) {
  53. image.effect = effectDelegate.createObject(column, { target: image, effect: globalSettings.effect })
  54. }
  55. image.beyondThePale.connect(removeImage)
  56. image.world = physicsSettings.globalWorld ? world : isolatedWorld
  57. image.x = xOffset
  58. stackHeight += (image.height + physicsSettings.itemTravel)
  59. image.y = floor.y - stackHeight
  60. pictureArray.push(image)
  61. globalUtil.itemCount++
  62. }
  63. function removeImage(image) {
  64. if (image.effect) {
  65. image.effect.destroy()
  66. }
  67. stackHeight -= (image.height + physicsSettings.itemTravel)
  68. image.destroy()
  69. globalUtil.itemCount--
  70. }
  71. function shift() {
  72. if (pictureArray.length > 0) {
  73. var image = pictureArray.shift()
  74. image.world = image.world.limbo
  75. addImage()
  76. }
  77. }
  78. Component.onCompleted: {
  79. columnArray.push(this)
  80. }
  81. onStackHeightChanged: {
  82. if (!column.full && (stackHeight > root.height)) {
  83. globalUtil.registerColumnPrimed()
  84. column.full = true
  85. }
  86. }
  87. width: parent.width/globalSettings.columnCount
  88. anchors { top: parent.top; bottom: parent.bottom }
  89. World {
  90. id: isolatedWorld
  91. timeStep: d.pace
  92. running: !physicsSettings.globalWorld && globalUtil.primed
  93. property var limbo: World {
  94. timeStep: isolatedWorld.timeStep
  95. running: isolatedWorld.running
  96. }
  97. }
  98. RectangleBoxBody {
  99. id: floor
  100. world: isolatedWorld
  101. height: 0
  102. width: parent.width
  103. x: xOffset
  104. anchors {
  105. top: parent.bottom
  106. }
  107. friction: 1
  108. }
  109. Timer {
  110. id: pumpTimer
  111. interval: Math.random()*500 + 500
  112. repeat: true
  113. running: true
  114. onTriggered: considerImage()
  115. }
  116. Timer {
  117. id: deathTimer
  118. running: !globalSettings.commonFeed && globalUtil.primed && d.paused
  119. repeat: true
  120. interval: globalUtil.adjustedInterval
  121. onTriggered: shift()
  122. }
  123. Connections {
  124. target: root
  125. onTogglePause: d.paused = !d.paused
  126. onNext: deathTimer.triggered()
  127. onToggleChaos: fixedRotation = !fixedRotation
  128. }
  129. Timer {
  130. id: settleTimer
  131. running: false
  132. interval: 200
  133. onTriggered: deathTimer.triggered()
  134. }
  135. }
  136. }
  137. // floor
  138. RectangleBoxBody {
  139. id: globalFloor
  140. world: world
  141. height: 0
  142. anchors {
  143. left: parent.left
  144. right: parent.right
  145. top: parent.bottom
  146. }
  147. friction: 1
  148. }
  149. DebugDraw {
  150. id: debugDraw
  151. enabled: false
  152. z: 1
  153. world: world
  154. anchors.fill: parent
  155. opacity: 0.75
  156. visible: enabled
  157. }
  158. Keys.onUpPressed: root.togglePause()
  159. Keys.onDownPressed: root.toggleChaos() //root.next()
  160. Component.onCompleted: {
  161. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  162. effectDelegate.status !== Component.Ready && console.log('Component failed with:' + effectDelegate.errorString())
  163. }
  164. }