Physics.qml 5.3 KB

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