Gravity.qml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import QtQuick 2.5
  2. import Box2D 2.0
  3. Item {
  4. id: root
  5. signal togglePause
  6. signal toggleChaos
  7. signal next
  8. property bool globalWorld: settings.fitByHeight
  9. property var worldArray: []
  10. property var pictureDelegate: Qt.createComponent(settings.fitByHeight ? "VerticalArtDelegate.qml" : "HorizontalArtDelegate.qml")
  11. anchors.fill: parent
  12. QtObject {
  13. id: d
  14. property double pace: settings.pace/60.0
  15. property int itemCount: 0
  16. }
  17. World {
  18. // Global world at odds with relative positions!
  19. id: commonWorld
  20. timeStep: d.pace
  21. }
  22. Component {
  23. id: columnComponent
  24. Item {
  25. id: column
  26. x: xOffset - effectiveXOffset
  27. width: parent.width/settings.columnCount
  28. anchors { top: parent.top; bottom: parent.bottom }
  29. property var pictureArray: []
  30. property var physicsWorld: settings.globalWorld ? commonWorld : columnWorld
  31. property bool fixedRotation: true
  32. property int xOffset: width * index
  33. property int effectiveXOffset: settings.globalWorld ? xOffset : 0
  34. World {
  35. id: columnWorld
  36. timeStep: d.pace
  37. Component.onCompleted: worldArray.push(columnWorld)
  38. }
  39. RectangleBoxBody {
  40. world: physicsWorld
  41. height: 1
  42. anchors {
  43. left: parent.left
  44. right: parent.right
  45. top: parent.bottom
  46. }
  47. friction: 1
  48. density: 1
  49. }
  50. Timer {
  51. id: feedTimer
  52. running: true
  53. repeat: true
  54. interval: 1000*(settings.interval > 60 ? 60*(settings.interval-60) : settings.interval)*(Math.random()+1)
  55. onTriggered: {
  56. d.itemCount++
  57. pictureArray.push(pictureDelegate.createObject(column, { y: -2000 }))
  58. if (pictureArray.length > settings.columnCount) {
  59. pictureArray.shift().detonate()
  60. d.itemCount--
  61. }
  62. }
  63. }
  64. Connections {
  65. target: root
  66. onTogglePause: feedTimer.running = !feedTimer.running
  67. onNext: feedTimer.triggered()
  68. onToggleChaos: fixedRotation = !fixedRotation
  69. }
  70. Timer {
  71. id: initialPopulation
  72. property int runCount: 0
  73. interval: 500
  74. running: runCount < settings.columnCount
  75. repeat: true
  76. onTriggered: {
  77. runCount = runCount + 1;
  78. feedTimer.triggered()
  79. }
  80. }
  81. }
  82. }
  83. // floor
  84. RectangleBoxBody {
  85. world: commonWorld
  86. height: 1
  87. anchors {
  88. left: parent.left
  89. right: parent.right
  90. top: parent.bottom
  91. }
  92. friction: 1
  93. density: 1
  94. }
  95. Repeater {
  96. model: settings.columnCount
  97. delegate: columnComponent
  98. }
  99. // TODO: The boot (Monty Python foot) of death to be applied to the stacks
  100. RectangleBoxBody {
  101. id: rect
  102. enabled: false
  103. visible: false
  104. friction: 1.0
  105. density: 1000
  106. color: "red"
  107. width: 50; height: 50
  108. bullet: true
  109. SequentialAnimation {
  110. id: murderAnimation
  111. //loops: Animation.Infinite
  112. //running: true
  113. ScriptAction { script: { root.togglePause() } }
  114. ScriptAction { script: { rect.world = worldArray.pop() } }
  115. PropertyAction { target: rect; property: "x"; value: -rect.width }
  116. PropertyAction { target: rect; property: "y"; value: root.height }
  117. ParallelAnimation {
  118. NumberAnimation { target: rect; property: "x"; to: 2560; duration: 1000 }
  119. NumberAnimation { target: rect; property: "y"; to: 0; duration: 1000 }
  120. }
  121. }
  122. }
  123. Rectangle {
  124. z: 1
  125. color: "black"
  126. anchors { right: parent.right; top: parent.top }
  127. width: itemCountLabel.width
  128. height: itemCountLabel.height
  129. Text {
  130. id: itemCountLabel
  131. font.pixelSize: 100
  132. text: d.itemCount
  133. color: "white"
  134. }
  135. }
  136. Keys.onUpPressed: root.togglePause()
  137. Keys.onDownPressed: root.toggleChaos() //root.next()
  138. }