SquabyCreatorLogic.js Example File

demos/Squaby/qml/gameScene/SquabyCreatorLogic.js
 //  this reduces the delay between 2 squaby creations by this value per wave
 //var squabyDelayDecrementPerWave = 500;
 //var pauseBetweenWavesDecrementPerWave = 500;

 // this guarantees that at high wave count the delay never gets lower than this number
 //var minimumSquabyDelay = 2000; // this setting has high impact on performance - if set too low, a heap of squabies gets created which might cause the application to run slowly on slow devices!
 //var minimumPauseBetweenWaves = 3000;

 var currentSquabyDelay = 8000; // this is the initial delay between 2 squaby creations
 var currentPauseBetweenWaves = 5000; // this is the initial waiting time between waves, which can be used to adjust the towers (repair, sell or upgrade them if in game its too hectic)

 // this is used for getting the last wave from the level - when no more waves are defined, they simply get repeated, and currentWave is increased so an increasing number can be shown in the hud!
 // so internalWaveIndex is equal to currentWave as long as waves.length, then it stays at wave.length
 var internalWaveIndex = 1;

 // this gets set to true after all squabies were created for this wave
 var __isWaitingForNextWave = false;
 //var squabiesBuiltInCurrentWave = 0;

 // this is true while timerTriggered is call - it is a guard for the initial (first time loading) of SquabyScene, where it might be that multiple entities are created
 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 = ""

   // warning no waves set
   if(!getCurrentWave())
     return

   currentSquabyDelay = getCurrentWave().squabyDelay;

   currentPauseBetweenWaves = getCurrentWave().pauseBetweenWaves;

   amountSquabiesInCurrentWave = getCurrentWave().amount;

   //console.debug("amountSquabiesInCurrentWave initialized to", amountSquabiesInCurrentWave)

   setIntervalOfCreationTimer(currentSquabyDelay);
 }

 function setIntervalOfCreationTimer(interval) {
   console.debug("SquabyCreatorLogic: set creationTimer interval to", interval);
   // ATTENTION: a change of interval automatically restarts the timer!
   creationTimer.interval = interval;
 }

 /*
 // look at the level file for description of each squaby type!

 // this is defined in Level now!
 // abbreviations for faster writing - earlier by types, now squabyTypes are the ids to real components!
 var sy = "squabyYellow";
 // change this to squabyOrange once it is available, but now for testing with only 1 squaby type leave it with that
 var so = "squabyOrange";
 var sr = "squabyRed";
 var sgreen = "squabyGreen";
 var sblue = "squabyBlue";
 var sgrey = "squabyGrey";

 // this is defined in level now, as this is a level-specific setting!

 var waves = [
 // for testing a single squaby type, use the line below with the intended types:
 //{amount: 50, types:[ {type: sgrey, p: 1}, {type: so, p: 0} ]},
 // optionally, the intended time between 2 squaby creations can be set, or also the desired delay from the last wave; if one of them is not set, the default decrement-values are used..
 {amount: 1, types:[ {type: sy, p: 1}, {type: so, p: 0} ]},
 // these 2 parameters are optional per level: {amount: 2, squabyDelay: 2, pauseBetweenWaves: 1.8, types:[ {type: sy, p: 1}, {type: so, p: 0.1} ]},
 {amount: 2, types:[ {type: sy, p: 1}, {type: so, p: 0.1} ]},
 {amount: 3, types:[ {type: sy, p: 1}, {type: so, p: 0.5} ]},
 {amount: 4, types:[ {type: sy, p: 1}, {type: so, p: 0.8} ]},
 {amount: 5, types:[ {type: sy, p: 1}, {type: so, p: 1.0} ]},
 {amount: 8, types:[ {type: sy, p: 1}, {type: so, p: 1.0}, {type: sr, p: 1.0} ]},
 {amount: 12, types:[ {type: sy, p: 1}, {type: so, p: 1.0}, {type: sr, p: 1.0}, {type: sgreen, p: 1.0}]},
 {amount: 20, types:[ {type: sy, p: 1}, {type: so, p: 1.0}, {type: sr, p: 1.0}, {type: sgreen, p: 1.0}, {type: sblue, p: 1.0}]},
 {amount: 30, types:[ {type: sy, p: 1}, {type: so, p: 1.0}, {type: sr, p: 1.0}, {type: sgreen, p: 1.0}, {type: sblue, p: 1.0}, {type: sgrey, p: 1.0}]},
 // the hardest squabies have higher probability here
 {amount: 30, types:[ {type: sy, p: 0.1}, {type: so, p: 0.2}, {type: sr, p: 1.0}, {type: sgreen, p: 1.0}, {type: sblue, p: 0.5}, {type: sgrey, p: 1.0}]},
 ]
 */

 function updatePauseBetweenWaves() {
   if(!getCurrentWave()) {
     return
   }

   // if the pauseBetweenWaves property is set, set it directly
   if(getCurrentWave().pauseBetweenWaves)
       currentPauseBetweenWaves = getCurrentWave().pauseBetweenWaves;
   // otherwise, decrease it by specified amount
   else
       currentPauseBetweenWaves -= pauseBetweenWavesDecrementPerWave;
   // guarantee the calculated value never gets below the minimum threshold
   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");

     // the squaby type per wave could be calculated randomly, based on the probability for each squaby type
     // check if randomnumber is deterministic (results in the same number generated every program start) - it IS when qsrand() is called at initialization, which is done in application.cpp!
     //var randomNumber = Math.floor(Math.random()*5 );
     //print("randomNumber: " + randomNumber);

     //print("__lastCreationTimer: " + __lastCreationTimer);
     //print("Core.time: " + Core.time);

     // use last wave for endless run
     if(endlessGameRunning) {
       currentWave = waves.length-1
       internalWaveIndex = currentWave
       __isWaitingForNextWave = true
     }

     // also use the lastCreationTimer for measuring the time elapsed since the last squaby in this wave was created (and since then it is in the paused state)
     if(__isWaitingForNextWave) {

         currentWave++;

         // also increase the internalWaveIndex
         internalWaveIndex = currentWave

         // e.g. when waves.length = 12, the maximum number of internalWaveIndex is also 12, because below the index is accessed with i-1
         // out-of-bounds check, is not necessary because also checked above
         if(currentWave>waves.length && !endlessGameRunning) {
           // stop next level logic will take over the role of a restart if endless game.
           squabyCreator.stop()
           isCurrentlyCreating = false;
           return
         }

         // warning no waves set
         if(!getCurrentWave()) {
           isCurrentlyCreating = false
           return
         }

         // if the squabyDelay property is set, set it directly
         if(getCurrentWave().squabyDelay)
             currentSquabyDelay = getCurrentWave().squabyDelay;
         // otherwise, decrease it by specified amount
         else
             currentSquabyDelay -= squabyDelayDecrementPerWave;
         // guarantee the calculated value never gets below the minimum threshold
         if(currentSquabyDelay<minimumSquabyDelay)
             currentSquabyDelay = minimumSquabyDelay;

         updatePauseBetweenWaves()

         if(getCurrentWave().amount)
           amountSquabiesInCurrentWave = getCurrentWave().amount

         squabiesBuiltInCurrentWave = 0;

         __isWaitingForNextWave = false;
     }

     // don't put this code into an else-block, because also if the wave was changed

     //var entity = EntityFactory.createEntityAndEngage(getSquabyType());
     // the default pose (-20, 40) is fine

     //entityManager.createEntityFromComponent(getSquabyType());
     entityManager.createEntityFromEntityTypeAndVariationType( {entityType: "squaby", variationType: getSquabyType()} );
     squabyCreator.currentActiveSquabies++

     squabiesBuiltInCurrentWave++;

     // this will never get 0, because after waiting for next wave a new squaby immediately gets created!
     percentageCreatedInWave = squabiesBuiltInCurrentWave/amountSquabiesInCurrentWave;

     if(squabiesBuiltInCurrentWave >= amountSquabiesInCurrentWave) {
         // end of this wave is reached, wait until next wave can be started
         __isWaitingForNextWave = true;

         squabyCreator.state = "waitingForNextWave";

         // update time which could differ after the first wave when settings have not been set
         updatePauseBetweenWaves()

         setIntervalOfCreationTimer(currentPauseBetweenWaves);

     } else {
         // while not waiting for the next wave, the state is waitingForNextSquaby
         squabyCreator.state = "waitingForNextSquaby";

         // for the next time the timer should have the normal delay between squabies in a wave
         setIntervalOfCreationTimer(currentSquabyDelay);
     }

     // does the timer start already while onTriggered() is called? the timer resolution is very unprecise!
     //creationTimer.start();

     isCurrentlyCreating = false;
 }

 function getCurrentWave() {
   if(waves.length < internalWaveIndex-1 || waves.length === 0) {
     console.debug("SquabyCreatorLogic: Out of bound when accessing waves!")
     return 0
   }
     // currentWave starts with 1, not with 0!
   // this is important to use the internalWaveIndex here, not currentWave
     return waves[internalWaveIndex-1];
 }

 function getSquabyType() {
     var types = [] //getCurrentWave().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;
     // Math.random returns a number  0 <= x < 1
     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;
         }
     }
     print("SquabyCreatorLogic: wavecreator pseudo-random function selected entity of type '" + nextCreationType + "' to be created next with probability value of " + highestProbabilityForChosenType);
     return nextCreationType;
 }

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support
Sign up for Free and start developing right away!
I want to make Games
I want to make Apps
Game Development
Cross-Platform, Any Resolution

Use one IDE to deploy to all supported platforms, including iOS and Android, from a single code base.

Support all screen resolutions, aspect ratios and sizes with auto-adapting UI elements, smart scene scaling and memory efficient asset selection.

Learn More

V-Play ranked #1 at research2guidance cross-platform tool benchmarking 2014 by 2188 developers comparing 40 leading tools.

Multiplayer

V-Play Multiplayer supports both real-time and turn-based gameplay, so you can use it to make many different types of cross-platform multiplayer games. It's perfect for making player-vs-player games like 'Words with Friends' or games for a large amount of players, such as 'Clash of Clans'.

Learn More Video

Highscores & Achievements

The V-Play Game Network is a cross-platform gaming service that allows players around the world to compare game highscores and achievements. Players can also challenge each other across multiple platforms and share their progress on Facebook.

Learn More

QML & JavaScript

Qt Meta Language is a highly intuitive reactive language, which is super easy to learn, yet it's extremely powerful and flexible. Mix it with JavaScript to create awesome stuff, with just a few lines of code.

import QtQuick 2.0
import VPlay 2.0

GameWindow {
  Scene {
    
    SimpleButton {
      anchors.centerIn: parent
      text: "Press Me"
      onClicked: {
        console.debug("Wow you pressed me!")
      }
    }
  }
}

Learn More

Monetize & Analyze

With V-Play, you can use many 3rd party services for ads, in-app purchases, analytics and more, with just a few lines of code. All of these plugins work cross-platform, which allows you to maintain a single code base.

Learn More

Level Editor

The LevelEditor can be used during development to create and modify levels for your game, which you can then bundle in your final publishing build. Additionally, you can also integrate the in-game level editor to your published game and let your gamers create new levels.

Learn More Video

Card Game like UNO, Hearthstone or Poker

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/OneCard/OneCard.pro

Match-3 like Candy Crush Saga

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/JuicySquash/JuicySquash.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/JuicySquashAdvanced/JuicySquashAdvanced.pro

Puzzle like 2048 or Threes!

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/2048/2048.pro

Casino like Big Win Slots

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/FlaskOfRum/FlaskOfRum.pro

Side Scroller like Jetpack Joyride or Flappy Bird

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/FlappyBird/FlappyBird.pro

Tower Defense like Castle Defense or Bloons TD

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/Squaby/Squaby.pro

Falldown & Jump like Doodle Jump or Mega Jump

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/ChickenOutbreak/ChickenOutbreak.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/ChickenOutbreak2/ChickenOutbreak2.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/DoodleJump/DoodleJump.pro

Platformer like Super Mario or Lep's World

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/Platformer/Platformer.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/PlatformerWithLevelEditor/PlatformerWithLevelEditor.pro

Action like Angry Birds, Fruit Ninja, Cut the Rope

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/StackTheBoxWithCommunityEditor/StackTheBoxWithCommunityEditor.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/BalloonPop/BalloonPop.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/CarChallenge/CarChallenge.pro

Arcade like Arkanoid or Space Invaders

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/ZombieBreak/ZombieBreak.pro

Community like Super Mario Maker or Minecraft

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/examples/Squaby/Squaby.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/StackTheBoxWithCommunityEditor/StackTheBoxWithCommunityEditor.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/PlatformerWithLevelEditor/PlatformerWithLevelEditor.pro

Any other Idea? let us know how we can help you

You are looking for another demo?

The V-Play SDK includes many open-source demos for different game genres. You can use their source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

If you do not find your game genre in this list and wonder if V-Play is the right choice, just contact us, we are happy to help!

Contact Us

App Development
Better Apps, Less Effort

Develop feature-rich, cross-platform mobile apps from a single code base.

V-Play apps look, feel and perform exactly like native iOS, Android and Desktop apps. With less code & much faster development time.

Learn More Highlights Video

V-Play ranked #1 at research2guidance cross-platform tool benchmarking 2014 by 2188 developers comparing 40 leading tools.

Save Time, Code & Money

Save up to 90% source code with V-Play, compared to frameworks like Xamarin, Titanium, React Native and others.

Learn More

QML & JavaScript

Qt Meta Language is a highly intuitive reactive language, which is super easy to learn, yet it's extremely powerful and flexible. Mix it with JavaScript to create awesome stuff, with just a few lines of code. QML also allows you to easily create smooth and complex animations.

import QtQuick 2.0
import VPlayApps 2.0

App {
  Page {
    
    AppButton {
      anchors.centerIn: parent
      text: "Press Me"
      onClicked: {
        console.debug("Wow you pressed me!")
      }
    }
  }
}

Learn More

Monetize & Analyze

With V-Play, you can use many 3rd party services for ads, in-app purchases, analytics and more, with just a few lines of code. All of these plugins work cross-platform, which allows you to maintain a single code base.

Learn More

Native Sensors & More

V-Play Apps harness the power of Qt, the leading cross-platform development framework used by over 1,000,000 developers.

This gives your access to native device features like sensors, camera, file system as well as multimedia, networking, localization and much more.

Learn More

Responsive Design

V-Play has built in responsive design capabilities, you can target phones and tablets with the same source code.

Learn More

Highly Extensible

You have existing JavaScript, C++ or native code? You want to add any 3rd party SDK that we do not offer already?

No worries, you can add any JS, C++, Java or Objective-C code to your project.

Learn More

Component Showcase App shows the most important V-Play features and components

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/showcase/Showcase.pro

Qt World Summit Conference App a full-featured conference management app made by V-Play

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/qtws2016/QtWS2016.pro

Twitter App how to build layouts like in the official Twitter app for iOS and Android

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/twitter/Twitter.pro

Maps App displays free bikes or boxes at bike stations for Vienna's bike sharing service Citybike Wien

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/maps/Maps.pro

Messaging App demonstrates how to create the UI of a Facebook Messenger like app

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/messaging/Messaging.pro

Weather App how to include powerful animations into your user interface

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/waether/Weather.pro

Any other Idea? let us know how we can help you

You are looking for another app demo?

The V-Play SDK includes many open-source demos for different app types. You can use their source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play..

If you do not find your app type in this list and wonder if V-Play is the right choice, just contact us, we are happy to help!

Contact Us