Stack With Friends Demo
import Felgo 3.0
import QtQuick 2.0
import QtSensors 5.3
import "../common"
import "../entities"
SceneBase {
id: gameScene
property alias itemEditor: itemEditor
property int createdBoxes: 1
property variant selectedEntity: null
property string cameFromScene
property int score
signal gameLost
state: "levelEditing"
onBackButtonPressed: {
gameScene.stopGame()
if(cameFromScene === "menu") {
mainItem.state = "menu"
} else {
mainItem.state = "selectLevel"
}
}
onStateChanged: {
if(state === "levelEditing") {
resetGame()
}
}
function goToPlayMode() {
state = "playing"
if(levelEditor.currentLevelId)
gameNetwork.currentActiveLeaderboard = levelEditor.currentLevelId.toString()
}
function goToLevelEditingMode() {
state = "levelEditing"
}
function entitySelected(entity) {
if(selectedEntity && selectedEntity !== entity) {
selectedEntity.entityState = ""
}
selectedEntity = entity
editEntityOverlay.obstacle = entity
}
function resetGame() {
entityManager.removeEntitiesByFilter(["box"]);
gameScene.createdBoxes = 0;
boxTimer.restart()
spawnBoxItem.resetBar()
score = 0
}
function stopGame() {
resetGame()
state = "stopped"
}
function gameOver() {
audioManager.play(audioManager.idGAMEOVER)
gameLost()
if(gameScene.state === "playing" && levelEditor.currentLevelId) {
console.debug("current levelId for highscore:", levelEditor.currentLevelId)
if(levelEditor.currentLevelStorageLocation === levelEditor.authorGeneratedLevelsLocation) {
gameNetwork.reportScore(score, levelEditor.currentLevelData.levelMetaData.publishedLevelId)
} else if(levelEditor.currentLevelStorageLocation === levelEditor.userGeneratedLevelsLocation) {
gameNetwork.reportScore(score, levelEditor.currentLevelId)
} else if(levelEditor.currentLevelStorageLocation === levelEditor.downloadedLevelsLocation) {
gameNetwork.reportScore(score, levelEditor.currentLevelId)
}
}
resetGame()
}
MultiResolutionImage {
source: "../../assets/img/menubg.png"
anchors.centerIn: parent
}
MultiResolutionImage {
source: system.desktopPlatform ? "../../assets/img/bg-deco-desktop.png" : "../../assets/img/bg-deco.png"
anchors.centerIn: parent
}
Item {
width: 210
anchors.right: parent.right
anchors.rightMargin: 20
y: 40
ResponsiveText {
text: levelEditor.currentLevelNameString
anchors.right: parent.right
color: "#185010"
font.pixelSize: 25
font.family: fontHUD.name
}
}
Text {
x: 58
y: 20
width: parent.width
text: "Score: " + score
color: "#185010"
font.pixelSize: 19
visible: gameScene.state !== "levelEditing"
font.family: fontHUD.name
}
GameHUD {
id: gameHUD
z:1
}
ItemEditor {
id: itemEditor
opacity: 0.9
z:1
visible: false
anchors.right: parent.gameWindowAnchorItem.right
anchors.bottom: parent.gameWindowAnchorItem.bottom
height: parent.height*2/3
}
Accelerometer {
id: accelerometer
active: !system.desktopPlatform && (gameScene.state === "playing" || gameScene.state === "testing")
onReadingChanged: {
world.gravity.x = reading.y*2
}
}
PhysicsWorld {
id: world
gravity.y: 9.81
z: 10
debugDrawVisible: false
updatesPerSecondForPhysics: 60
velocityIterations: 5
positionIterations: 5
}
Component {
id: mouseJoint
MouseJoint {
maxForce: mainItem.maxForce * world.pixelsPerMeter
dampingRatio: mainItem.dampingRatio
frequencyHz: mainItem.frequencyHz
}
}
MouseArea {
anchors.fill: parent
property Body selectedBody: null
property MouseJoint mouseJointWhileDragging: null
onPressed: {
gameScene.entitySelected(null)
selectedBody = world.bodyAt(Qt.point(mouseX, mouseY));
console.debug("selected body at position", mouseX, mouseY, ":", selectedBody);
if(selectedBody) {
mouseJointWhileDragging = mouseJoint.createObject(world)
mouseJointWhileDragging.target = Qt.point(mouseX, mouseY)
mouseJointWhileDragging.bodyB = selectedBody
}
}
onPositionChanged: {
if (mouseJointWhileDragging)
mouseJointWhileDragging.target = Qt.point(mouseX, mouseY)
}
onReleased: {
if(selectedBody) {
selectedBody = null
if (mouseJointWhileDragging)
mouseJointWhileDragging.destroy()
}
}
}
Ground {
height: 16
anchors {
bottom: gameScene.bottom
left: gameScene.left
right: gameScene.right
}
}
Wall {
id: leftWall
width: 48
height: gameScene.height
anchors {
left: gameScene.left
}
}
Wall {
width: 16
height: gameScene.height
anchors {
right: gameScene.right
}
}
Ceiling {
height: 16
width: gameScene.width
anchors {
top: gameScene.top
left: gameScene.left
leftMargin: 32
}
onCollidedWithBox: {
gameOver()
}
}
Item {
id: spawnBoxItem
width: 32
height: gameScene.height - 16
y: 16
Image {
id: spawnBoxBar
source: "../../assets/img/spawnbar.png"
anchors.bottom: parent.bottom
anchors.bottomMargin: 16
width: parent.width
height: parent.height
}
Timer {
id: spawnBoxBarTimer
interval: 25
running: gameScene.state === "playing" || gameScene.state === "testing"
repeat: true
onTriggered: {
spawnBoxBar.height -= spawnBoxItem.height / (spawnInterval / spawnBoxBarTimer.interval)
}
}
Timer {
id: spawnBarLockTimer
interval: 350
}
function resetBar() {
spawnBoxBar.height = spawnBoxItem.height
}
MouseArea {
anchors.fill: parent
enabled: gameScene.state === "playing" || gameScene.state === "testing"
onClicked: {
if(spawnBarLockTimer.running) return
var bonus = Math.floor(5*spawnBoxBar.height/spawnBoxItem.height)
gameScene.spawnBox(bonus)
flurry.logEvent("Game.InstantBox")
}
}
}
Timer {
id: boxTimer
interval: spawnInterval
running: gameScene.state === "playing" || gameScene.state === "testing"
repeat: true
onTriggered: {
gameScene.spawnBox(0)
}
}
function spawnBox(bonusScore) {
spawnBarLockTimer.start()
spawnBoxItem.resetBar()
spawnBoxBarTimer.restart()
boxTimer.restart()
var newEntityProperties = {
x: utils.generateRandomValueBetween(68, gameScene.width-36),
y: 48,
rotation: Math.random()*360
}
entityManager.createEntityFromUrlWithProperties(
Qt.resolvedUrl("../entities/Box.qml"),
newEntityProperties);
audioManager.play(audioManager.idSPAWN)
gameScene.createdBoxes++
score += 5 + bonusScore
}
Connections {
target: mainItem
onSpawnIntervalChanged: {
if(gameScene.state == "testing") {
spawnBoxItem.resetBar()
spawnBoxBarTimer.restart()
boxTimer.restart()
}
}
}
EditEntityOverlay {
id: editEntityOverlay
z: 100