StackTheBox Demo
import Felgo 4.0
import QtQuick 2.0
import "entities"
GameWindow {
id: gameWindow
activeScene: scene
EntityManager {
id: entityManager
entityContainer: scene
}
Rectangle {
anchors.fill: parent
color: "black"
}
Scene {
id: scene
width: 480
height: 320
property int createdBoxes: 1
property real safetyDistance: -1
Text {
text: "Boxes: " + scene.createdBoxes
color: "white"
z: 1
}
PhysicsWorld {
id: physicsWorld
gravity.y: 9.81
debugDrawVisible: false
updatesPerSecondForPhysics: 60
velocityIterations: 5
positionIterations: 5
}
Component {
id: mouseJoint
Item {
id: jointItem
property alias bodyB: joint.bodyB
property alias target: joint.target
MouseJoint {
id: joint
maxForce: 30000 * physicsWorld.pixelsPerMeter
dampingRatio: 1
frequencyHz: 2
}
Connections {
target: joint.bodyB !== null ? joint.bodyB.target : null
function onEntityDestroyed() { joint.bodyB = null; jointItem.destroy() }
}
}
}
MouseArea {
anchors.fill: parent
property Body selectedBody: null
property Item mouseJointWhileDragging: null
onPressed: {
selectedBody = physicsWorld.bodyAt(Qt.point(mouseX, mouseY));
console.debug("selected body at position", mouseX, mouseY, ":", selectedBody);
if(selectedBody) {
var properties = {
target: Qt.point(mouseX, mouseY),
bodyB: selectedBody
}
mouseJointWhileDragging = mouseJoint.createObject(physicsWorld, properties)
}
}
onPositionChanged: {
if (mouseJointWhileDragging)
mouseJointWhileDragging.target = Qt.point(mouseX, mouseY)
}
onReleased: {
if(selectedBody) {
selectedBody = null
if (mouseJointWhileDragging)
mouseJointWhileDragging.destroy()
}
}
}
Box {
id: box1
entityId: "box1"
x: scene.width/2
y: 50
Component.onCompleted: {
if(scene.safetyDistance === -1) {
scene.safetyDistance = box1.width*Math.SQRT2/2 + leftWall.width + 5
console.debug("init safetyZoneHorizontal with", scene.safetyDistance)
}
}
}
Wall {
height: 20
anchors {
bottom: scene.bottom
left: scene.left
right: scene.right
}
}
Wall {
id: leftWall
width: 20
height: scene.height
anchors {
left: scene.left
}
}
Wall {
width: 20
height: scene.height
anchors {
right: scene.right
}
}
Wall {
id: topWall
height: 20
width: scene.width
anchors {
top: scene.top
}
color: "red"
onCollidedWithBox: {
entityManager.removeEntitiesByFilter(["box"]);
scene.createdBoxes = 0;
}
}
Column {
anchors.right: parent.right
spacing: 5
SimpleButton {
text: "Toggle Audio"
onClicked: settings.soundEnabled = !settings.soundEnabled
anchors.right: parent.right
}
SimpleButton {
visible: false
text: "Toggle Particles"
onClicked: settings.particlesEnabled = !settings.particlesEnabled
}
}
Timer {
id: timer
interval: generateRandomInterval()
running: true
repeat: true
onTriggered: {
var newEntityProperties = {
x: utils.generateRandomValueBetween(scene.safetyDistance, scene.width-scene.safetyDistance),
y: scene.safetyDistance,
rotation: Math.random()*360
}
entityManager.createEntityFromUrlWithProperties(
Qt.resolvedUrl("entities/Box.qml"),
newEntityProperties);
scene.createdBoxes++
timer.interval = generateRandomInterval()
timer.restart()
}
function generateRandomInterval() {
return utils.generateRandomValueBetween(1000, 3000);
}
}