Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Durdles - 2-Player Action Game

 import QtQuick 2.0
 import Felgo 3.0
 import "../common" as Common
 import ".."
 import "../entities"
 import "../entities/powerUps"
 import "../scenes"

 // the fire button in the easy mode allows to shoot bullets in the direction the tank is currently facing
 // the image visualizes the fire button
 Rectangle {
   id: fireButton
   radius: GameInfo.radius
   opacity: GameInfo.opacity
   color: "transparent"
   width: 90
   height: 90

   // target player and tank is set from outside
   property var player
   property var tank
   property alias snowballSound1: snowballSound1
   property alias snowballSound2: snowballSound2
   property alias snowballSound3: snowballSound3
   property alias icicleSound1: icicleSound1
   property alias icicleSound2: icicleSound2
   property alias icicleSound3: icicleSound3

   // this image visualizes the fire button and is used to calculate the player's movement
   Image {
     id: fireImage
     source: GameInfo.easyMode ? "../../assets/img/FireEasy.png" : "../../assets/img/FireHard.png"
     anchors.centerIn: parent
     width: parent.width
     height: parent.height
     anchors.fill: parent
   }

   // the MultiPointTouchArea reacts to input and creates bullets
   // it is only enabled on easy mode and fires in the body's direction
   MultiPointTouchArea {
     z: 2
     id: easyTouchControls
     enabled: !GameInfo.gamePaused && GameInfo.easyMode ? true : false
     anchors.fill: parent

     touchPoints: [
       TouchPoint {id: fireTouchPoint}
     ]

     // saves the last time a bullet was fired
     property real lastTime: 0

     onPressed: {
       // calculate the time between the current tap and the last time a bullet was fired
       var currentTime = new Date().getTime()
       var timeDiff = currentTime - lastTime

       // if enough time has passed, create a new bullet
       if (timeDiff > player.minTimeDistanceBullet) {
         lastTime = currentTime

         // play either the icicle or snowball sound depending on the current bullet type
         if (player.activatePowershot){
           icicle()
         }else{
           snowball()
         }

         // animate the head when a bullet is fired
         tank.tankHead.playing=true

         // pick a bullet speed depending on the current powerup
         var speed = (player.activateAccelerator) ? 240 : 120

         // pick the calculation of the tankBody and add 90 because of the rotated image
         var rotation = tank.tankBody.rotation + 90

         // calculate a bullet movement vector with the rotation and the speed
         var xDirection = Math.cos(rotation * Math.PI / 180.0) * speed
         var yDirection = Math.sin(rotation * Math.PI / 180.0) * speed

         // calculate the bullet spawn point: start at the center of the tank translate it outside of the body towards the final direction
         var startX = (16 * Math.cos((rotation) * Math.PI / 180)) + tank.x + tank.width / 2
         var startY = (16 * Math.sin((rotation) * Math.PI / 180)) + tank.y + tank.height / 2

         // create and remove bullet entities at runtime
         entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("../entities/Bullet.qml"), {
                                                           "start" : Qt.point(startX, startY),
                                                           "velocity" : Qt.point(xDirection, yDirection),
                                                           "rotation" : tank.tankBody.rotation + 180,
                                                           "bulletType" : player.activatePowershot ? 1 : 0});
       }
     }
   }

   // the MultiPointTouchArea reacts to input and creates bullets
   // it is only enabled on hard mode and allows free cannon movement
   MultiPointTouchArea {
     z: 2
     id: hardTouchControls
     enabled: !GameInfo.gamePaused && !GameInfo.easyMode ? true : false
     anchors.fill: parent

     // becomes true when a touch-cycle starts
     property bool pressBool: false

     // stores the last time a bullet was shot
     property var lastTime: 0
     property variant playerTwoAxisController: tank.getComponent("TwoAxisController")

     touchPoints: [
       TouchPoint {id: point1}
     ]

     // update the direction of the cannon with every touch
     onTouchUpdated: upDateCannon()

     // signal that the button is pressed
     // update the direction of the cannon
     onPressed: {
       pressBool = true
       upDateCannon()
     }

     // shoot a bullet in the direction of the tankHead
     onReleased: {
       // calculate the time difference between the current time and the last fired bullet
       var currentTime = new Date().getTime()
       var timeDiff = currentTime - lastTime

       // if enough time has passed, create a new bullet
       if (pressBool && timeDiff > player.minTimeDistanceBullet) {
         lastTime = currentTime

         // play either the icicle or snowball sound depending on the current bullet type
         if (player.activatePowershot){
           icicle()
         }else{
           snowball()
         }

         // animate the head when a bullet is fired
         tank.tankHead.playing = true

         // pick a bullet speed depending on the current powerup
         var speed = (player.activateAccelerator) ? 240 : 120

         // calculate a bullet movement vector with the rotation and the speed
         var xDirection = Math.cos(tank.tankCannon.rotation * Math.PI / 180.0) * speed
         var yDirection = Math.sin(tank.tankCannon.rotation * Math.PI / 180.0) * speed

         // calculate the bullet spawn point: start at the center of the tank translate it outside of the body towards the final direction
         var startX= (16 * Math.cos((tank.tankCannon.rotation) * Math.PI / 180)) + tank.x + tank.width / 2
         var startY= (16 * Math.sin((tank.tankCannon.rotation) * Math.PI / 180)) + tank.y + tank.height / 2

         // create and remove bullet entities at runtime
         entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("../entities/Bullet.qml"), {
                                                           "start" : Qt.point(startX, startY),
                                                           "velocity" : Qt.point(xDirection, yDirection),
                                                           "rotation" : tank.tankCannon.rotation + 90,
                                                           "bulletType" : player.activatePowershot ? 1 : 0});
       }
       // signal that the button is no longer pressed
       pressBool= false
     }
   }

   // plays when a tank shoots a normal bullet
   SoundEffect {
     volume: 0.3
     id: snowballSound1
     // an ogg file is not playable on windows, because the extension is not supported!
     source: "../../assets/snd/Snow1.wav"
   }

   // plays when a tank shoots a normal bullet
   SoundEffect {
     volume: 0.3
     id: snowballSound2
     source: "../../assets/snd/Snow2.wav"
   }

   // plays when a tank shoots a normal bullet
   SoundEffect {
     volume: 0.3
     id: snowballSound3
     source: "../../assets/snd/Snow3.wav"
   }

   // plays when a tank shoots a strong bullet
   SoundEffect {
     volume: 0.3
     id: icicleSound1
     source: "../../assets/snd/Icicle1.wav"
   }

   // plays when a tank shoots a strong bullet
   SoundEffect {
     volume: 0.3
     id: icicleSound2
     source: "../../assets/snd/Icicle2.wav"
   }

   // plays when a tank shoots a strong bullet
   SoundEffect {
     volume: 0.3
     id: icicleSound3
     source: "../../assets/snd/Icicle3.wav"
   }

   // plays one of three random snowball sounds
   function snowball() {
     var random = Math.floor(Math.random() * 3) + 1
     if (random==1) snowballSound1.play()
     if (random==2) snowballSound2.play()
     if (random==3) snowballSound3.play()
   }

   // plays one of three random icicle sounds
   function icicle() {
     var random = Math.floor(Math.random() * 3) + 1
     if (random==1) icicleSound1.play()
     if (random==2) icicleSound2.play()
     if (random==3) icicleSound3.play()
   }

   // update the cannon rotation by calculating the angle between the distance and the current touch point
   function upDateCannon(){
     var x = point1.x
     var y = point1.y
     x = x - (fireButton.width / 2)
     y = (y - (fireButton.height / 2)) * (-1)
     var angle = calcAngle(x, y)
     tank.tankCannon.rotation = angle
   }

   // calculate the angle between a zero point and a second one
   function calcAngle(touchX, touchY) {
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded