Squaby Demo
var currentSquabyDelay = 8000;
var currentPauseBetweenWaves = 5000;
var internalWaveIndex = 1;
var __isWaitingForNextWave = false;
var isCurrentlyCreating = false;
function initialize() {
currentWave = 1
internalWaveIndex = currentWave
percentageCreatedInWave = 1
squabiesBuiltInCurrentWave = 0
if(!waves) {
console.debug("SquabyCreatorLogic: no waves known yet!")
return
}
__isWaitingForNextWave = false;
squabyCreator.state = ""
if(!getCurrentWave())
return
currentSquabyDelay = getCurrentWave().squabyDelay;
currentPauseBetweenWaves = getCurrentWave().pauseBetweenWaves;
amountSquabiesInCurrentWave = getCurrentWave().amount;
setIntervalOfCreationTimer(currentSquabyDelay);
}
function setIntervalOfCreationTimer(interval) {
console.debug("SquabyCreatorLogic: set creationTimer interval to", interval);
creationTimer.interval = interval;
}
function updatePauseBetweenWaves() {
if(!getCurrentWave()) {
return
}
if(getCurrentWave().pauseBetweenWaves)
currentPauseBetweenWaves = getCurrentWave().pauseBetweenWaves;
else
currentPauseBetweenWaves -= pauseBetweenWavesDecrementPerWave;
if(currentPauseBetweenWaves<minimumPauseBetweenWaves)
currentPauseBetweenWaves = minimumPauseBetweenWaves;
}
function timerTriggered() {
if(isCurrentlyCreating) {
console.debug("OVERLOAD because too much is loading in the beginning - skip entity creation once!")
return;
}
isCurrentlyCreating = true;
console.debug("SquabyCreatorLogic: creationTimer triggered");
if(endlessGameRunning) {
currentWave = waves.length-1
internalWaveIndex = currentWave
__isWaitingForNextWave = true
}
if(__isWaitingForNextWave) {
currentWave++;
internalWaveIndex = currentWave
if(currentWave>waves.length && !endlessGameRunning) {
squabyCreator.stop()
isCurrentlyCreating = false;
return
}
if(!getCurrentWave()) {
isCurrentlyCreating = false
return
}
if(getCurrentWave().squabyDelay)
currentSquabyDelay = getCurrentWave().squabyDelay;
else
currentSquabyDelay -= squabyDelayDecrementPerWave;
if(currentSquabyDelay<minimumSquabyDelay)
currentSquabyDelay = minimumSquabyDelay;
updatePauseBetweenWaves()
if(getCurrentWave().amount)
amountSquabiesInCurrentWave = getCurrentWave().amount
squabiesBuiltInCurrentWave = 0;
__isWaitingForNextWave = false;
}
entityManager.createEntityFromEntityTypeAndVariationType( {entityType: "squaby", variationType: getSquabyType()} );
squabyCreator.currentActiveSquabies++
squabiesBuiltInCurrentWave++;
percentageCreatedInWave = squabiesBuiltInCurrentWave/amountSquabiesInCurrentWave;
if(squabiesBuiltInCurrentWave >= amountSquabiesInCurrentWave) {
__isWaitingForNextWave = true;
squabyCreator.state = "waitingForNextWave";
updatePauseBetweenWaves()
setIntervalOfCreationTimer(currentPauseBetweenWaves);
} else {
squabyCreator.state = "waitingForNextSquaby";
setIntervalOfCreationTimer(currentSquabyDelay);
}
isCurrentlyCreating = false;
}
function getCurrentWave() {
if(waves.length < internalWaveIndex-1 || waves.length === 0) {
console.debug("SquabyCreatorLogic: Out of bound when accessing waves!")
return 0
}
return waves[internalWaveIndex-1];
}
function getSquabyType() {
var types = []
if(!getCurrentWave())
return "squabyYellow"
var type = {}
type.type = "squabyYellow"
type.p = getCurrentWave().yellow
types.push(type)
type = {}
type.type = "squabyOrange"
type.p = getCurrentWave().orange
types.push(type)
type = {}
type.type = "squabyRed"
type.p = getCurrentWave().red
types.push(type)
type = {}
type.type = "squabyGreen"
type.p = getCurrentWave().green
types.push(type)
type = {}
type.type = "squabyBlue"
type.p = getCurrentWave().blue
types.push(type)
type = {}
type.type = "squabyGrey"
type.p = getCurrentWave().grey
types.push(type)
var nextCreationType = types[0].type;
var highestProbabilityForChosenType = types[0].p*Math.random();
for(var i=1;i<types.length;i++) {
var possiblyHighestProbability = types[i].p*Math.random();
if(possiblyHighestProbability > highestProbabilityForChosenType) {
highestProbabilityForChosenType = possiblyHighestProbability;
nextCreationType = types[i].type;
}
}