ChickenOutbreakScene.qml Example File

demos/ChickenOutbreak2/qml/ChickenOutbreakScene.qml
 import QtQuick 2.0
 import Felgo 3.0
 import "entities"

 SceneBase {
   id: scene

   property alias level: level
   property alias player: level.player
   property alias entityContainer: level
   property string messageBox: ""
   // make 1 grid (so 1 block and the player size) 48 logical px - a roost has this size, so 320/48= 6.6 can be displayed in one scene
   gridSize: 48

   Connections {
     // nativeUtils should only be connected, when this is the active scene
       target: window.activeScene === scene ? nativeUtils : null
       onMessageBoxFinished: {
         audioManager.play(audioManager.idMENUCLICK)
         console.debug("the user confirmed the Ok/Cancel dialog with:", accepted)
         if(accepted && messageBox === "") {
           level.stopGame();
           window.state = "main"
         } else {
           messageBox = ""
           level.resumeGame()
         }
       }
   }

   //scale: 0.15

   //Component.onCompleted: opacity = 1

   // allows collision detection between the entities, so if the player collides with roosts or the corn, and for removal of the out-of-scene entities

   // handles the repositioning of the background, if they are getting out of the scene
   ParallaxScrollingBackground {
     id: levelBackground

     property int initialOffsetx: -(levelBackground.image.width-scene.width)/2
     property int initialOffsety: -(levelBackground.image.height-scene.height)/2
     x: initialOffsetx
     y: initialOffsety

     sourceImage: "../assets/background-wood2.png"
     movementVelocity: level.movementAnimation.velocity
     running: level.movementAnimation.running

     // do not mirror it vertically, because the image is prepared to match on the top and the bottom
     mirrorSecondImage: false
   }

   Level {
     id: level

     onGameLost: {
       if(state !== "running")
         return

       player.prepareForDeath()
       level.pauseGame()
       // only allowed on mobiles
       if(!(system.desktopPlatform && system.publishBuild)) {
         saveitDialog.setActive()
       } else {
         player.killIt()
       }
     }

     PhysicsWorld {
       // this id gets used in Level, to test if a new roost or window can be built on the new position or if there is an entity already
       id: physicsWorld

       // for physics-based games, this should be set to 60!
       updatesPerSecondForPhysics: 60
       // make objects fall faster by increasing gravity
       gravity.y: level.gravity

       // this should be increased so it looks good, by default it is set to 1 to save performance
       velocityIterations: 4
       positionIterations: 4
       debugDrawVisible: false
     }
   }

   // this allows usage of the left and right keys on desktop systems or mobiles with physical keyboards
   Keys.forwardTo: player.controller

   MultiPointTouchArea {
     x: scene.gameWindowAnchorItem.x
     y: scene.gameWindowAnchorItem.y+50
     // use the full window as control item, press anywhere on the left half for steering left, on the right half for steering right
     width: scene.gameWindowAnchorItem.width
     height: scene.gameWindowAnchorItem.height-50
     enabled: level.state === "running" || level.state === "teleporting"
     property int startY
     onPressed: {
       startY = touchPoints[0].y
     }
     onReleased: {
       console.debug("TOUCH RELEASED")
       if(touchPoints[0].y > startY + 20) {
         console.debug("onSwipeDown")
         deactivateTeleport()
       } else if(touchPoints[0].y < startY - 20) {
         console.debug("onSwipeUp")
         activateTeleport()
       } else {
         if(level.state === "running") player.startOrStopParachute()
         else if(level.state === "teleporting") {
           var mousePosition = mapToItem(level, touchPoints[0].x, touchPoints[0].y)
           player.x = mousePosition.x - player.width/2
           player.y = mousePosition.y - player.height/2
           player.teleportParticle.start()
           audioManager.play(audioManager.idTELEPORT)
         }
       }
     }
   }

   function activateTeleport() {
     if(teleportCount > 0 && level.state !== "teleporting") {
       teleportCount--
       level.toggleTeleport()
       audioManager.play(audioManager.idSLOWMO)
       player.teleportGlowParticle.start()
     }
   }

   function deactivateTeleport() {
     if(level.state === "teleporting"){
       level.toggleTeleport()
       audioManager.play(audioManager.idSLOWMOOFF)
       player.teleportGlowParticle.stop()
     }
   }

   Image {
     id: scoreSign
     source: "../assets/sign.png"
     width: 150
     height: 100

     // place it on top of the window, not on top of the logical scene
     anchors.top: scene.gameWindowAnchorItem.top
     anchors.topMargin: -50
     anchors.left: scene.gameWindowAnchorItem.left
   }

   Text {
     id: scoreText
     x: scoreSign.x+25
     y: scoreSign.y+65
     text: "Score: " + player.totalScore
     font.family: fontHUD.name
     font.pixelSize: 22
     color: "#f5b68a"
     rotation: -2
   }

   Item {
     id: pauseSign
     width: pauseSignText.width
     height: pauseSignText.height
     // place it on top of the window, not on top of the logical scene
     anchors.top: scene.gameWindowAnchorItem.top
     anchors.topMargin: 10
     anchors.horizontalCenter: parent.horizontalCenter

     MenuText {
       id: pauseSignText
       color: "black"
       opacity: 0.5
       text: "II"
       font.pixelSize: 30
     }
     MouseArea {
       anchors.fill: parent
       onClicked: {
         parent.scale = 1.0
         pauseGame()
       }

       onPressed: {
         parent.scale = 0.85
       }
       onReleased: {
         parent.scale = 1.0
       }
       onCanceled: {
         parent.scale = 1.0
       }
     }
   }

   Image {
     source: "../assets/sign.png"
     width: 150
     height: 130

     // place it on top of the window, not on top of the logical scene
     anchors.top: scene.gameWindowAnchorItem.top
     anchors.topMargin: -65
     mirror: true
     anchors.right: scene.gameWindowAnchorItem.right
   }

   Image {
     id: teleportImage
     source: "../assets/teleport.png"
     anchors.top: scene.gameWindowAnchorItem.top
     anchors.topMargin: 12
     anchors.right: scene.gameWindowAnchorItem.right
     anchors.rightMargin: 100

     opacity: teleportCount > 0 ? 1 : 0.2
     width: 35
     height: 30
   }
   Text {
     id: teleportText
     anchors.top: teleportImage.bottom
     anchors.topMargin: -2
     anchors.horizontalCenter: teleportImage.horizontalCenter
     text: teleportCount
     visible: teleportCount > 0
     font.family: fontHUD.name
     font.pixelSize: 14
     color: "#f5b68a"
   }

   Image {
     id: parachuteImage
     source: "../assets/parachute.png"
     anchors.top: scene.gameWindowAnchorItem.top
     anchors.topMargin: 15
     anchors.right: scene.gameWindowAnchorItem.right
     anchors.rightMargin: 60

     opacity: parachuteCount > 0 ? 1 : 0.2
     width: 28
     height: 28
   }
   Text {
     id: parachuteText
     anchors.top: parachuteImage.bottom
     anchors.topMargin: -2
     anchors.horizontalCenter: parachuteImage.horizontalCenter
     text: parachuteCount
     visible: parachuteCount > 0
     font.family: fontHUD.name
     font.pixelSize: 14
     color: "#f5b68a"
   }

   Image {
     id: x2Image
     source: "../assets/x2.png"
     anchors.top: scene.gameWindowAnchorItem.top
     anchors.topMargin: 18
     anchors.right: scene.gameWindowAnchorItem.right
     anchors.rightMargin: 20
     opacity: player.doublePointsTime > 0 ? 1 : 0.2
     width: 25
     height: 25
   }

   Text {
     id: xText
     anchors.top: x2Image.bottom
     anchors.horizontalCenter: x2Image.horizontalCenter
     text: player.doublePointsTime
     visible: player.doublePointsTime > 0
     font.family: fontHUD.name
     font.pixelSize: 14
     color: "#f5b68a"
   }

   NewLevel{
     id: newLevelMessage
     anchors.centerIn: parent
   }

   ItemEditor {
     id: itemEditor
     visible: false
     opacity: 0.85
     z:20
     width: scene.width-45
   }

   Rectangle {
     id: itemEditorMenu
     color: "grey"
     width: 40
     height: 70
     y: 70
     anchors.right: scene.right
     opacity: 0.5
     visible: developerBuild
     enabled: visible
     z: 50
     MouseArea {
       anchors.fill: parent
       onClicked: {
         if(level.state === "running") {
           level.pauseGame()
         }
         else if(level.state === "paused") {
           level.resumeGame()
         }
         itemEditor.visible = itemEditor.visible ? false : true
       }
     }
     Text {
       text: "ItemEditor"
       rotation: -90
       font.pixelSize: 12
       anchors.verticalCenter: parent.verticalCenter
       anchors.right: parent.right
       anchors.rightMargin: -5
     }
   }
   Rectangle {
     anchors.top: itemEditorMenu.bottom
     anchors.topMargin: 10
     anchors.right: parent.right
     color: "grey"
     width: 40
     height: 40
     opacity: 0.5
     visible: developerBuild
     enabled: visible
     Text {
       font.pixelSize: 15
       text: level.difficulty
       anchors.centerIn: parent
     }
     MouseArea {
       anchors.fill: parent
       onClicked: {
         if(level.difficulty >=6)
           level.difficulty = 0
         else
           level.difficulty++
       }
     }
   }

   SaveItDialog {
     id: saveitDialog
     anchors.centerIn: parent

     onActivated: {
       scoreText.opacity = 0.5
       teleportText.opacity = 0.5
       parachuteText.opacity = 0.5
       xText.opacity = 0.5
     }

     onDeactivated: {
       scoreText.opacity = 1
       teleportText.opacity = 1
       parachuteText.opacity = 1
       xText.opacity = 1
     }

     onSaveIt: {
       visible = false
       player.x = scene.width/2
       player.y = scene.height/2-level.y
       level.saveIt()
       audioManager.play(audioManager.idSLOWMO)
     }

     onKillIt: {
       player.killIt()
     }
   }

   onBackButtonPressed: {
     if(level.state !== "teleporting" && !saveitDialog.visible) {
       audioManager.play(audioManager.idMENUCLICK)
       level.pauseGame();
       showPauseBackground.start()
     }
   }

   Timer {
     id: showPauseBackground
     interval: 250
     onTriggered: {
       nativeUtils.displayMessageBox(qsTr("Return to menu?"), "", 2);
     }
   }

   Timer {
     id: showPausedGame
     interval: 250
     onTriggered: {
       messageBox = "resume"
       nativeUtils.displayMessageBox(qsTr("Resume?"), "", 1);
     }
   }

   // gets called by ChickenOutbreakMain when this scene gets active
   // starts a game again - stopGame() was called before so it is save to call that here
   function enterScene() {
     level.startGame();
   }

   function pauseGame() {
     if(level.state !== "running")
       return

     level.pauseGame()
     showPausedGame.start()
   }
 }

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