Well.qml 4.9 KB

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