Cascade.qml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 columnArray: []
  11. property var pictureDelegate: Component {
  12. CascadeDelegate {}
  13. }
  14. anchors.fill: parent
  15. QtObject {
  16. id: d
  17. property int columnCount: 6
  18. property real pace: cascadeSettings.pace/60.0
  19. property bool paused: false
  20. }
  21. Repeater {
  22. model: d.columnCount
  23. delegate: columnComponent
  24. }
  25. Settings {
  26. id: cascadeSettings
  27. category: "Cascade"
  28. property int feedRate: 1000
  29. // 0 is abutting
  30. property int verticalOffset: 500
  31. property real pace: 3
  32. property real density: 1.0
  33. property real friction: 1.0
  34. // Very computationally heavy: 40% vs 20% for 0.1 vs 0
  35. property real restitution: 0
  36. }
  37. Component {
  38. id: columnComponent
  39. Item {
  40. id: column
  41. property bool shifty: false
  42. property int stackHeight: 0
  43. property int xOffset: width * index
  44. property var pictureArray: []
  45. function addExistingImage(image) {
  46. // make sure there is no spacial conflict in limbo, or shit goes tits up
  47. image.x = image.y = index*-1000
  48. image.linearVelocity.x = image.linearVelocity.y = 0.0
  49. image.beyondThePale.connect(removeImage)
  50. image.x = xOffset
  51. stackHeight += image.height
  52. image.y = -image.height - pictureArray.length*100
  53. image.world = isolatedWorld
  54. pictureArray.push(image)
  55. }
  56. function addImage() {
  57. var image = pictureDelegate.createObject(column, { x: -1000, y: -1000 })
  58. addExistingImage(image)
  59. globalUtil.itemCount++
  60. }
  61. function removeImage(image) {
  62. image.beyondThePale.disconnect(removeImage)
  63. stackHeight -= image.height
  64. //console.log('Image slipped through the cracks')
  65. if (index === d.columnCount-1) {
  66. console.log('Image deleted')
  67. image.destroy()
  68. globalUtil.itemCount--
  69. } else {
  70. columnArray[index+1].addExistingImage(image)
  71. }
  72. }
  73. function shift() {
  74. if (pictureArray.length > 0) {
  75. var image = pictureArray.shift()
  76. image.world = image.world.limbo
  77. }
  78. }
  79. onStackHeightChanged: {
  80. if (stackHeight > (1.3 + 1/d.columnCount)*root.height) {
  81. shifty = true
  82. }
  83. }
  84. width: parent.width/globalSettings.columnCount
  85. anchors { top: parent.top; bottom: parent.bottom }
  86. World {
  87. id: isolatedWorld
  88. timeStep: d.pace
  89. running: true
  90. property var limbo: World {
  91. timeStep: isolatedWorld.timeStep
  92. running: isolatedWorld.running
  93. }
  94. }
  95. RectangleBoxBody {
  96. id: floor
  97. world: isolatedWorld
  98. height: 0
  99. width: parent.width
  100. x: xOffset
  101. anchors {
  102. top: parent.bottom
  103. }
  104. friction: 1
  105. }
  106. Timer {
  107. id: pumpTimer
  108. interval: 1000
  109. repeat: true
  110. running: (index === 0) && !shifty
  111. onTriggered: addImage()
  112. }
  113. Timer {
  114. id: deathTimer
  115. running: true
  116. repeat: true
  117. interval: 5000
  118. onTriggered: {
  119. if (shifty) {
  120. shift()
  121. shifty = false
  122. }
  123. }
  124. }
  125. Connections {
  126. target: root
  127. onTogglePause: d.paused = !d.paused
  128. onNext: deathTimer.triggered()
  129. }
  130. Component.onCompleted: {
  131. columnArray.push(this)
  132. }
  133. }
  134. }
  135. Keys.onUpPressed: root.togglePause()
  136. Keys.onDownPressed: root.toggleChaos() //root.next()
  137. Component.onCompleted: {
  138. globalVars.loadFullImage = true
  139. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  140. }
  141. }