Squaby Demo
import QtQuick 2.0
import Felgo 4.0
import "../levels"
import "../balancing"
import "hud"
import "../otherScenes"
import "../common"
SquabySceneBase {
id: scene
property alias level: level
property alias hud: hud
property alias entityContainer: level
property alias itemEditor: itemEditor
property alias tutorials: tutorials
property int hudHeight: 64
property bool cameFromLevelEditing: false
property bool endlessGameAllowed: false
signal lastWaveSend
signal changeToNextLevel
onLastWaveSend: {
endlessGameAllowed = true
showWaveDefeatedScene()
}
onChangeToNextLevel: {
endlessGameAllowed = false
showWaveDefeatedScene()
}
function loadNextLevel() {
for(var ii = 0; ii < levelEditor.applicationJSONLevels.length; ++ii) {
if(levelEditor.applicationJSONLevels[ii].levelId == level.nextLevelId) {
twoPhaseLevelLoader.startLoadingLevel(false,levelEditor.applicationJSONLevels[ii])
break
}
}
}
onBackButtonPressed: {
if(scene.state !== "levelEditing") {
showPauseScene();
} else {
if(saveDialog.opacity == 1) {
saveDialog.opacity = 0
} else if(publishDialog.opacity == 1) {
publishDialog.opacity = 0
} else {
hud.levelEditingHud.menuBackButtonClicked()
}
}
}
SquabyCreator {
id: squabyCreator
enabled: true
}
MouseArea {
anchors.fill: parent
onClicked: {
if(scene.state === "levelEditing") {
hud.state = "levelEditing";
return;
}
console.debug("SquabyScene: resetting the hud to default state because no tower was selected, so set upgrade menu to invisible");
scene.state = ""
hud.state = "buildTowers"
}
}
BackgroundImage {
id:levelBackground
source: Qt.resolvedUrl("../../assets/img/floor-merged.png")
anchors.centerIn: parent
}
Image {
id: raster
visible: scene.state === "levelEditing"
opacity: 0.5
source: Qt.resolvedUrl("../../assets/img/raster.png")
}
SquabyLevelContainer {
id: level
width: scene.width
height: scene.height-hudHeight
Component.onCompleted: console.debug("SquabyLevelContainer.onCompleted()")
}
PhysicsWorld {
debugDraw.opacity: 0.2
id: physicsWorld
debugDrawVisible: false
height: parent.height-hudHeight
}
HUD {
id: hud
width: scene.width
height: scene.hudHeight
anchors.bottom: scene.gameWindowAnchorItem.bottom
onMenuButtonClicked: {
console.debug("SquabyScene: menu button clicked")
showPauseScene();
}
}
Tutorials {
id: tutorials
currentLevel: (scene.state !== "levelEditing") ? (level.levelLoader.loadedLevel ? level.levelLoader.loadedLevel.levelData.levelMetaData.levelName ? level.levelLoader.loadedLevel.levelData.levelMetaData.levelName : "" : "") : ""
}
ItemEditor {
id: itemEditor
anchors.fill: scene.gameWindowAnchorItem
signal backButtonClicked()
currentEditableType: "GameSettings"
opacity: 0
contentDelegate: SquabyContentDelegate{ }
numberDelegate: SquabyNumberDelegate{ }
boolDelegate: SquabyBoolDelegate{ }
customTypes: { "waveArrayDelegate" : Qt.resolvedUrl("../levels/WaveArrayDelegate.qml"),
"simpleArrayDelegate" : Qt.resolvedUrl("../levels/SimpleArrayDelegate.qml")}
Component.onCompleted: {
levelEditor.itemEditorItem = itemEditor
}
visible: opacity > 0
function slideIn() {
itemEditor.opacity = 1
}
function slideOut() {
itemEditor.opacity = 0
}
}
DialogField {
id: saveDialog
width: scene.gameWindowAnchorItem.width
height: scene.gameWindowAnchorItem.height
descriptionText: qsTr("You leave the level, do you want to ")
options1Text: qsTr("Save Changes and Exit")
options2Text: qsTr("Discard Changes and Exit")
opacity: 0
z: 100
onOption1Pressed: {
if(!scene.level.pathEntity.waypoints.length && !scene.level.waves.length) {
flurry.logEvent("LevelEditor.Save","NoPath.NoWaves")
hud.levelEditingHud.messageBoxID = "save"
NativeUtils.displayMessageBox(qsTr("Save Level"), qsTr("No path and waves are set! Save and leave the level anyway?"),2)
} else if(!scene.level.pathEntity.waypoints.length) {
flurry.logEvent("LevelEditor.Save","NoPath")
hud.levelEditingHud.messageBoxID = "save"
NativeUtils.displayMessageBox(qsTr("Save Level"), qsTr("No path is set! Save and leave the level anyway?"),2)
} else if(!scene.level.waves.length) {
flurry.logEvent("LevelEditor.Save","NoWaves")
hud.levelEditingHud.messageBoxID = "save"
NativeUtils.displayMessageBox(qsTr("Save Level"), qsTr("No waves are set! Save and leave the level anyway?"),2)
} else {
hud.levelEditingHud.publishLevel(false)
}
}
onOption2Pressed: {
scene.state = ""
window.state = "levels";
}
property variant levelData
}
DialogField {
id: publishDialog
width: scene.gameWindowAnchorItem.width
height: scene.gameWindowAnchorItem.height
descriptionText: levelEditor.currentLevelData.levelMetaData && levelEditor.currentLevelData.levelMetaData.publishedLevelId ?
qsTr("You are about to update a level. It'll keep ratings and downloads! Remove rating and download statistics by unpublishing the level.") :
qsTr("You are about to publish a level. It can be edited afterwards but it'll keep ratings and downloads! Remove rating and download statistics by unpublishing the level.")
options1Text: qsTr("Publish Level")
options2Text: qsTr("Change Level Name")
opacity: 0
z: 100
onOption1Pressed: {
hud.levelEditingHud.publishLevel(true)
}
onOption2Pressed: {
NativeUtils.displayTextInput("Enter levelName", "Enter the level name. Choose a name that expresses what makes your level special.", "", levelEditor.currentLevelNameString)
opacity = 1
}
}
function showPauseScene() {
if(!scene.cameFromLevelEditing && scene.state === "levelEditing" || resetGame || startClock.waitsForStart)
return
console.debug("HUD: menuButton clicked, current scene state:", scene.state);
if(scene.state === "levelEditing") {
console.debug("ERROR: This state should not be possible!")
} else {
scene.state = "ingameMenuReleaseVersion"
pauseScene()
window.state = "pause"
}
console.debug("HUD: new scene state:", scene.state);
}
function showWaveDefeatedScene() {
if(!scene.cameFromLevelEditing && scene.state === "levelEditing" && window.state !== "gameover" || resetGame || player.lives<=0)
return
scene.state = "ingameMenuReleaseVersion"
pauseScene()
window.state = "waveDefeated"
}
property int marginLabelElemements: 5
Row {
id: scoreRow
anchors.left: scene.gameWindowAnchorItem.left
anchors.leftMargin: marginLabelElemements
anchors.top: scene.gameWindowAnchorItem.top
visible: scene.state !== "levelEditing"
spacing: marginLabelElemements
SingleSquabySprite {
id: scoreImage
source: Qt.resolvedUrl("../../assets/img/menu_labels/labelScore.png")
}
Text {
anchors.verticalCenter: scoreImage.verticalCenter
anchors.verticalCenterOffset: -2
text: player.score
font.family: hudFont.name
font.pixelSize: 18
color: "white"
}
}
Item {
id: levelEditorChangeButton
width: sprite.width
height: sprite.height
anchors.top: gameWindowAnchorItem.top
anchors.horizontalCenter: gameWindowAnchorItem.horizontalCenter
scale: 0.7
visible: (scene.cameFromLevelEditing || scene.state === "levelEditing") && !itemEditor.visible
opacity: scene.cameFromLevelEditing ? 0.4 : 0.8
SingleSquabySprite {
id: sprite
source: Qt.resolvedUrl("../../assets/img/button.png")
}
SingleSquabySprite {
source: Qt.resolvedUrl("../../assets/img/" + (scene.cameFromLevelEditing ? "button-pause.png" : "button-play.png"))
anchors.centerIn: parent
}
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: {
parent.scale = 0.7
if(cameFromLevelEditing) {
scene.leaveGameToLevelEditingMode()
} else {
scene.startGameFromLevelEditingMode()
}
}
onPressed: {
parent.scale = 0.58
}
onReleased: {
parent.scale = 0.7
}
onCanceled: {
parent.scale = 0.7
}
}
}
Row {
id: livesRow
anchors.right: scene.gameWindowAnchorItem.right
anchors.rightMargin: marginLabelElemements
anchors.top: scene.gameWindowAnchorItem.top
visible: scene.state !== "levelEditing"
spacing: marginLabelElemements
SingleSquabySprite {
id: livesImage
source: Qt.resolvedUrl("../../assets/img/menu_labels/labelLives.png")
}
Text {
id: livesText
anchors.verticalCenter: livesImage.verticalCenter
anchors.verticalCenterOffset: -2
text: player.lives
font.family: hudFont.name
font.pixelSize: 18
color: "white"
}
}
Loader {
id: pathCreationOverlay
source: Qt.resolvedUrl("../levels/PathCreationOverlay.qml")
visible: false
width: parent.width
height: parent.height-hudHeight
}
states: [
State {
name: ""
},
State {
name: "ingameMenuReleaseVersion"
},
State {
name: "levelEditing"
PropertyChanges { target: hud; state: "levelEditing"}
StateChangeScript {
script: {
startClock.stop()
squabyCreator.stop()
removeAllSquabiesAndTowers()
}
}
}
]
function startGameFromLevelEditingMode() {
console.debug("Start Game from Level Editing Mode!")
scene.cameFromLevelEditing = true;
player.initializeProperties()
startClock.restart()
scene.state = ""
}
function leaveGameToLevelEditingMode() {
removeAllSquabiesAndTowers();
entityManager.removeAllPooledEntities()
hud.state = "buildTowers"
scene.cameFromLevelEditing = false;
scene.state = "levelEditing"
squabyCreator.reset()
}
function enterScene() {
console.debug("SquabyScene: onEntered")
console.debug("wasInPauseBefore:", wasInPauseBefore)
system.resumeGameForObject(level);
console.debug("SquabyScene: storageLocation of current loaded level:", levelEditor.currentLevelData.levelMetaData.storageLocation)
console.debug("SquabyScene: currentLevelData:", JSON.stringify(levelEditor.currentLevelData))
if(editAuthorLevel || createdNewLevel) {
if(createdNewLevel) {
createdNewLevel = false
editAuthorLevel = true
levelEditor.duplicateCurrentLevel({ levelMetaData: { levelName: levelEditor.currentLevelNameString } })
}
if(cameFromLevelEditing) {
scene.state = ""
if(resetGame) {
player.initializeProperties()
startClock.restart()
resetGame = false
} else {
squabyCreator.start()
}
} else {
console.debug("SquabyScene: the level is an authorGenerated one - switch scene to state levelEditing")
scene.state = "levelEditing"
}
} else {
cameFromLevelEditing = false
scene.state = ""
console.debug("SquabyScene: no authorGeneratedLevel, so either an applicationLevel or a userGeneratedLevel was loaded")
if(wasInPauseBefore) {
if(!tutorials.running) {
squabyCreator.start()
}
tutorials.resume()
} else {
player.initializeProperties()
tutorials.reset()
startClock.restart()
}
}
if(!levelStore.noAdsGood.purchased)
chartboostView.doNotShowAdvert()
wasInPauseBefore = false
resetGame = false
if(scene.state !== "levelEditing") {
var sum = 0;
for (var i in scene.level.waves) {
sum += scene.level.waves[i].amount
}
}
console.debug("SquabyScene: end of enterScene()")
}
function exitScene() {
console.debug("SquabyScene: onExited")
system.resumeGameForObject(level);
startClock.stop()
squabyCreator.stop()
tutorials.reset()
endlessGameAllowed = false
removeAllSquabiesAndTowers()
wasInPauseBefore = false
hud.state = "buildTowers"
resetGame = false
}
function nextLevel() {
exitScene()
loadNextLevel()
}
function continueGame() {
if(!scene.cameFromLevelEditing && scene.state === "levelEditing")
return
squabyCreator.continueEndless()
}
function restartGame() {
console.debug("Restart level with scene.state:", scene.state )
if(!scene.cameFromLevelEditing && scene.state === "levelEditing")
return
exitScene()
resetGame = true
}
function removeAllSquabiesAndTowers() {
var toRemoveEntityTypes = ["squaby", "nailgun", "flamethrower", "taser", "tesla", "turbine"];
entityManager.removeEntitiesByFilter(toRemoveEntityTypes)
}
property bool shouldRestart: false
property bool wasInPauseBefore: false
property bool resetGame: false
function pauseScene() {
wasInPauseBefore = true
startClock.pause()
tutorials.pause()
console.debug("SquabyScene: calling System.pauseGameForObject() with scene.state: ",scene.state)
system.pauseGameForObject(level);
}
Rectangle {
id: loadMessage
z:1001
x: scene.gameWindowAnchorItem.x
y: scene.gameWindowAnchorItem.y
width: scene.gameWindowAnchorItem.width
height: scene.gameWindowAnchorItem.height
color: "black"
opacity: 0.5
visible: itemEditor.state === "loading"
Text {
anchors.centerIn: parent
text: "loading..."
color: "white"
}
}
StartClock {
id: startClock