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 "../levels"
 import ".."

 EntityBase {
   id: player
   variationType: "playerRed"
   entityType: "player"

   property bool onLake: false
   property int life: GameInfo.maxEnergy
   property bool activateShield: false             // for activating powerUpShield
   property int activeShieldCounter: 0             // count from 0 to 80 every 100 millisecond for the duration of active powerUps
   property bool activateAccelerator: false        // for activating powerUpAccelerator
   property int activeAcceleratorCounter: 0        // count from 0 to 80 every 100 millisecond for the duration of active powerUps
   property bool activatePowershot: false          // for activating powerUpPowershot
   property int activePowershotCounter: 0          // count from 0 to 80 every 100 millisecond for the duration of active powerUps
   property int stdTimeDistanceBetweenBullets: 500 // standard time distance between two bullets
   property int minTimeDistanceBullet: stdTimeDistanceBetweenBullets // interval between two bullets
   property bool activateHitShield: false          // activate shield for short after a hit
   property int activeHitShieldCounter: 0          // count from 0 to 5 every 100 millisecond after being hit by a bullet
   property alias tank: tank
   property string bodySource
   property string headSource
   property alias winnerSound: winnerSound

   // sound plays when one player wins the match
   SoundEffect {
     volume: 0.3
     id: winnerSound
     // an ogg file is not playable on windows, because the extension is not supported!
     source: "../../assets/snd/Winner.wav"
   }

   // each player controls a tank
   Tank {
     property bool currentlyBeaming: false   // whether the player is currently teleporting with the igloos or not
     id: tank
     rotation: 0
     tankBody.source: variationType === "tankBlue" ? "../../assets/img/BlueBody.gif" : "../../assets/img/RedBody.gif"
     tankHead.source: variationType === "tankBlue" ? "../../assets/img/BlueHead.gif" : "../../assets/img/RedHead.gif"
   }

   Timer {
     id: timerPlayer
     interval: 100; running: true; repeat: true;

     // increase the powerUp timers every 100ms and deactive the effects after a certain time
     // show the current powerUp effects
     onTriggered: {
       if (activateShield) {
         tank.shield.opacity = 1
         activeShieldCounter ++;
       }
       if (activeShieldCounter === 80) {
         activateShield = false;
         activeShieldCounter = 0;
       }
       if (activateShield == false) {
         tank.shield.opacity = 0
       }
       if (activateAccelerator) {
         activeAcceleratorCounter ++;
         minTimeDistanceBullet = 20;
         tank.fire.opacity = 1
       }
       if (activeAcceleratorCounter === 80) {
         activateAccelerator = false;
         activeAcceleratorCounter = 0;
         minTimeDistanceBullet = stdTimeDistanceBetweenBullets;
       }
       if (activateAccelerator == false) {
         tank.fire.opacity = 0
       }
       if (activatePowershot) {
         activePowershotCounter ++
       }
       if (activePowershotCounter === 80) {
         activatePowershot = false; activePowershotCounter = 0;
       }
       if (activateHitShield) {
         activeHitShieldCounter ++;
         tank.opacity = 0.2;
       }
       if (activeHitShieldCounter === 10) {
         activateHitShield = false;
         activeHitShieldCounter = 0;
         tank.opacity = 1;
       }
     }
   }

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

   // plays when tank gets hit
   SoundEffect {
     volume: 0.3
     id: screamSound2
     // an ogg file is not playable on windows, because the extension is not supported!
     source: "../../assets/snd/Injury2.wav"
   }

   // unspecific damage counts as a normal bullet
   function onDamage() {
     onDamageWithBulletType(0)
   }

   // play one of two random scream sounds
   function scream() {
     var random = Math.floor(Math.random() * 2) + 1 //Sounds 1 - 2
     if (random == 1){
       screamSound1.play()
     }else{
       screamSound2.play()
     }
   }

   // decrease health depending on the bullet type and other powerUps
   function onDamageWithBulletType(bulletType) {
     // if the player is immune after being hit or currently beaming, he takes no damage
     if (activateHitShield || tank.currentlyBeaming) {
       return
     }

     // calculate the damage
     var damage = 0;
     switch (bulletType) {
     case 0:
       // normal bullet
       damage = GameInfo.normalDamage;
       break;

     case 1:
       // powerBullet
       damage = GameInfo.powerDamage;
       break;

     default:
       damage = GameInfo.normalDamage;
       break;
     }

     // decrease the damage if the player has the shield powerUp
     if (activateShield) {
       damage = damage / 100.0 * (100 - GameInfo.shieldDamageReduction)
     }

     // substract the damage from the player's health if the game is still running
     if (!GameInfo.gameOver){
       life = life - damage;
     }

     // make the player invincible after being hit
     activateHitShield = true

     // use the scream function to play the correct audio
     scream()

     // check if life went below 0
     if (life <= 0) {
       endGame()
     }
   }

   function endGame(){
     // player blue lost
     if (variationType == "playerBlue"){
       GameInfo.winnerRed = true
       GameInfo.redVictory += 1
       GameInfo.winner = "Red"
     }else if (variationType == "playerRed"){
       GameInfo.winnerRed = false
       GameInfo.blueVictory += 1
       GameInfo.winner = "Blue"
     }

     // show game over screen
     winnerSound.play()

     // end the game
     // pause the game, stop all movement and remove created entities
     GameInfo.gamePaused = true
     GameInfo.gameOver = true

     entityManager.getEntityById("tankRed").circleCollider.linearDamping = 2000
     entityManager.getEntityById("tankBlue").circleCollider.linearDamping = 2000
     entityManager.getEntityById("tankRed").tankBody.playing = false
     entityManager.getEntityById("tankBlue").tankBody.playing = false

     var toRemoveEntityTypes = ["powAccelerator", "powLifeUp", "powPowershot", "powShield", "singleBullet", "singleBulletOpponent"];
     entityManager.removeEntitiesByFilter(toRemoveEntityTypes);
   }

   // reset all player variables, restor health and reset position & rotation
   function reset(){
     player.onLake = false;
     player.life = GameInfo.maxEnergy;
     tank.x = tank.originX;
     tank.y = tank.originY;

     if(variationType == "playerRed"){
       tank.tankBody.rotation = 0;
       tank.tankCannon.rotation = 90;
       tank.rotation = 0;
     }else if (variationType == "playerBlue"){
       tank.tankBody.rotation = 180
       tank.tankCannon.rotation = 270
       tank.rotation = 0;
     }

     // stop powerUp timer and remove powerUp effects
     activateShield = false                // for activating powerUpShield
     activeShieldCounter = 0               // count from 0 to 80 every 100 millisecond for the duration of active powerUps
     activateAccelerator = false           // for activating powerUpAccelerator
     activeAcceleratorCounter = 0          // count from 0 to 80 every 100 millisecond for the duration of active powerUps
     activatePowershot = false             // for activating powerUpPowershot
     activePowershotCounter = 0            // count from 0 to 80 every 100 millisecond for the duration of active powerUps
     stdTimeDistanceBetweenBullets =  500  // standard time distance between two bullets
     minTimeDistanceBullet = stdTimeDistanceBetweenBullets // interval between two bullets
     activateHitShield = false             // activate shield for short after a hit
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded