Squaby Demo
import QtQuick 2.0
import "SquabyCreatorLogic.js" as Logic
Item {
id: squabyCreator
property alias running: creationTimer.running
property bool enabled: true
property variant waves: level.waves
property int currentWave: 1
property real percentageCreatedInWave: 1
property int squabyDelayDecrementPerWave: level.loadedLevel ? level.loadedLevel.squabyDelayDecrementPerWave : 100
property int pauseBetweenWavesDecrementPerWave: level.loadedLevel ? level.loadedLevel.pauseBetweenWavesDecrementPerWave : 300
property int minimumSquabyDelay: level.loadedLevel ? level.loadedLevel.minimumSquabyDelay : 500
property int minimumPauseBetweenWaves: level.loadedLevel ? level.loadedLevel.minimumPauseBetweenWaves : 500
property int initialDelayWhenGameStarts: 2000
property int squabiesBuiltInCurrentWave: 0
property int amountSquabiesInCurrentWave: 1
property int currentActiveSquabies: 0
property bool endlessGameRunning: false
onCurrentActiveSquabiesChanged: {
triggerLevelChange()
}
function triggerLevelChange() {
if(currentActiveSquabies <= 0 && currentWave>waves.length && !endlessGameRunning) {
if(level.endlessGame) {
scene.lastWaveSend()
} else {
scene.changeToNextLevel()
}
}
}
function squabyDied(squabyType) {
tutorials.nextAction(squabyType,"died")
currentActiveSquabies--
}
property date __lastStartedTime
property int timeSpentBeforePaused: 0
Binding {
target: player
property: "squabiesBuiltInCurrentWave"
value: squabiesBuiltInCurrentWave
}
function continueEndless() {
endlessGameRunning = true
start()
}
function restart() {
if(!enabled)
return
console.debug("SquabyCreator.restart()")
currentActiveSquabies = 0
endlessGameRunning = false
creationTimer.stop()
Logic.initialize()
timeSpentBeforePaused = 0
creationTimer.restart()
}
function reset() {
currentActiveSquabies = 0
endlessGameRunning = false
creationTimer.stop()
Logic.initialize()
timeSpentBeforePaused = 0
}
function start() {
if(!enabled)
return
console.debug("SquabyCreator.start()")
if(running) {
console.debug("ERROR: SquabyCreator was already running, but start() was called again - this is a programmer error and should not happen")
return;
}
if(timeSpentBeforePaused != 0) {
var newInterval = creationTimer.interval - timeSpentBeforePaused
if(newInterval <= 0) {
timeSpentBeforePaused = 0
} else {
creationTimer.interval = newInterval
}
}
running = true
}
function pause() {
var now = new Date()
var dt = now - __lastStartedTime
console.debug("SquabyCreator.pause: timeSpentBeforePaused:", dt, ", current interval:", creationTimer.interval)
timeSpentBeforePaused = dt
running = false
}
function stop() {
timeSpentBeforePaused = 0
running = false
}
Component.onCompleted: {
Logic.initialize();
}
onEnabledChanged: {
console.debug("SquabyCreator: changed enabled to:", enabled);
if(enabled && !running) {
running = true;
}
if(!enabled && running)
running = false;
}
onRunningChanged: {
console.debug("SquabyCreator: onRunningChanged to:", running);
if(running && !enabled) {
console.debug("SquabyCreator: running was true, but enabled was false, thus change running to false as well");
running = false;
}
console.debug("SquabyCreator: after modifying running, changed running to:", running);
}
onWavesChanged: {
console.debug("SquabyCreator: waves changed! initialize now!")
Logic.initialize()
}
onCurrentWaveChanged: {
console.debug("SquabyCreator: currentWave changed to", currentWave)
player.wave = currentWave;
triggerLevelChange()
}
Timer {
id: creationTimer
interval: initialDelayWhenGameStarts
repeat: true
triggeredOnStart: true
onTriggered: {
if(timeSpentBeforePaused != 0) {
console.debug("SquabyCreator.onTriggered: do not create yet and wait for the next interval, time spent:", timeSpentBeforePaused, ", interval:", interval)
timeSpentBeforePaused = 0
return;
}
Logic.timerTriggered();
__lastStartedTime = new Date()
}
onRunningChanged: {
if(running) {
__lastStartedTime = new Date()
}
}
onIntervalChanged: {
if(running) {
__lastStartedTime = new Date()
}
}
}
function createNextSquabyImmediately() {
if(tutorials.running == true && !tutorials.nextAction("squabyCreator","immediately"))
return
if(squabiesBuiltInCurrentWave >= amountSquabiesInCurrentWave && currentWave >= waves.length)
return
timeSpentBeforePaused = 0
var returnVal = new Date() - __lastStartedTime
if(squabiesBuiltInCurrentWave >= amountSquabiesInCurrentWave) {
returnVal = Logic.currentPauseBetweenWaves-returnVal
} else {
returnVal = Logic.currentSquabyDelay-returnVal
}
creationTimer.restart();
return returnVal
}
function createNextSingleSquabyImmediately() {
Logic.timerTriggered();
}
states: [
State {
name: "waitingForNextSquaby"
},
State {
name: "waitingForNextWave"
}