levelLogic.js Example File

demos/ChickenOutbreak2/qml/scripts/levelLogic.js
 var newRoostCenterPos = Qt.point(0,0)
 var newWheelCenterPos = Qt.point(0,0)
 var randomValue
 var coinCenterPos = Qt.point(0,0)
 var coinPositionModifier = 0.7*scene.gridSize
 var gridSizeHalf = scene.gridSize/2
 var newWindowTopleftPos = Qt.point(0,0)
 var newWindowCenterPos = Qt.point(0,0)
 var roostUrl = Qt.resolvedUrl("../entities/Roost.qml")
 var wheelUrl = Qt.resolvedUrl("../entities/Wheel.qml")
 var coinUrl = Qt.resolvedUrl("../entities/Coin.qml")
 var doublePointsUrl = Qt.resolvedUrl("../entities/DoublePoints.qml")
 var parachuteUrl = Qt.resolvedUrl("../entities/Parachute.qml")
 var trippleCoinUrl = Qt.resolvedUrl("../entities/TrippleCoin.qml")
 var badCoinUrl = Qt.resolvedUrl("../entities/BadCoin.qml")
 var coinTypeUrl
 var windowUrl = Qt.resolvedUrl("../entities/HenhouseWindow.qml")
 var newElementProperties = {}

 function createRandomRowForRowNumber(rowNumber) {
   for(var i=0; i<roostColumns; i++) {
         randomValue = Math.random()
         if(randomValue < wheelCreationProbability ) {
             newWheelCenterPos.x = i*gridSize + 43
             newWheelCenterPos.y = rowNumber*gridSize + 43

             if(newWheelCenterPos.y-lastWheelY < minimumWheelHeightDifference) {
                 console.debug("difference between last Window and current to create one too small!")
                 continue;
             }

             if(physicsWorld.bodyAt(newWheelCenterPos)) {
                 console.debug("no Wheel can be created because there is something already");
                 continue;
             }
             newElementProperties.x = newWheelCenterPos.x
             newElementProperties.y = newWheelCenterPos.y
             entityManager.createEntityFromUrlWithProperties(wheelUrl,newElementProperties)

             lastWheelY = newWheelCenterPos.y;
         }
         randomValue = Math.random()
         if(randomValue < platformCreationProbability ) {
             newRoostCenterPos.x = i*gridSize + gridSizeHalf
             newRoostCenterPos.y = rowNumber*gridSize + gridSizeHalf

             if(physicsWorld.bodyAt(newRoostCenterPos)) {
                 console.debug("no Roost can be created because there is something already");
                 continue;
             }
             //console.debug("creating a new Roost at position", i*gridSize + gridSize/2, ",", rowNumber*gridSize + gridSize/2);

             newElementProperties.x = newRoostCenterPos.x
             newElementProperties.y = newRoostCenterPos.y
             newElementProperties.rotation = Math.random() < platformRotationProbability ? (Math.floor(Math.random() * 70) -34) : 0
             entityManager.createEntityFromUrlWithProperties(roostUrl,newElementProperties)

             // create a coin in 30% of all created blocks
             randomValue = Math.random()
             if(randomValue < coinCreationPropability) {
                 // look at 1 grid position above
                 coinCenterPos.x = newRoostCenterPos.x
                 coinCenterPos.y = newRoostCenterPos.y-scene.gridSize

                 // test if one grid above is an empty field (so if no block is built there) - if so, a coin can be created
                 if(physicsWorld.bodyAt(coinCenterPos)) {
                     console.debug("there is a block above the to create block, don't create a coin here!")
                     continue;
                 }

                 randomValue = Math.random()
                 if(randomValue < doublePointsCreationPropability) {
                   newElementProperties.x = coinCenterPos.x
                   newElementProperties.y = coinCenterPos.y+0.6*scene.gridSize // move slightly up, so it looks better
                   coinTypeUrl = doublePointsUrl
                 }
                 else if(Math.random() < parachuteCreationPropability) {
                   newElementProperties.x = coinCenterPos.x
                   newElementProperties.y = coinCenterPos.y+0.6*scene.gridSize // move slightly up, so it looks better
                   coinTypeUrl = parachuteUrl
                 }
                 else if(Math.random() < trippleCoinCreationPropability) {
                   newElementProperties.x = coinCenterPos.x-6
                   newElementProperties.y = coinCenterPos.y+0.5*scene.gridSize
                   coinTypeUrl = trippleCoinUrl
                 } else if(Math.random() < badCoinCreationPropability) {
                   newElementProperties.x = coinCenterPos.x
                   newElementProperties.y = coinCenterPos.y+0.6*scene.gridSize // move slightly up, so it looks better
                   coinTypeUrl = badCoinUrl
                 } else {
                   newElementProperties.x = coinCenterPos.x
                   newElementProperties.y = coinCenterPos.y+0.65*scene.gridSize // move slightly up, so it looks better
                   coinTypeUrl = coinUrl
                 }
                 entityManager.createEntityFromUrlWithProperties(coinTypeUrl,newElementProperties)
             }
             // reset rotation to zero windows and wheels should not be rotated
             newElementProperties.rotation = 0
         } else if(i < roostColumns-1 && randomValue < windowCreationProbability ) {
             // the i<columns-1 check is required, because the window has a size of 2 grids, and its anchor is top left
             newWindowTopleftPos.x = i*gridSize
             newWindowTopleftPos.y = rowNumber*gridSize
             newWindowCenterPos.x = i*gridSize+32
             newWindowCenterPos.y = rowNumber*gridSize+30

             //console.debug("newWindowTopleftPos.y-lastWindowY:", newWindowTopleftPos.y-lastWindowY)
             // this avoids creating too many windows, so not possible to have more than 2 on a scene with this code!
             if(newWindowTopleftPos.y-lastWindowY < minimumWindowHeightDifference) {
                 console.debug("difference between last Window and current to create one too small!")
                 continue;
             }

             // this might happen if a window was already created at this position
             if(physicsWorld.bodyAt(newWindowTopleftPos) || physicsWorld.bodyAt(newWindowCenterPos)) {
                 console.debug("body at position x:", newWindowTopleftPos.x, ", y:", newWindowTopleftPos.y, physicsWorld.bodyAt(newWindowTopleftPos))
                 console.debug("there is a window at the position where to create a window, so no creation is done");
                 continue;
             }

             newElementProperties.x = newWindowCenterPos.x
             newElementProperties.y = newWindowCenterPos.y
             entityManager.createEntityFromUrlWithProperties(windowUrl,newElementProperties)

             lastWindowY = newWindowTopleftPos.y;
             // windows uses 2 grids, fast forward
             i++
         }

     }
 }

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