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

 // the igloo teleports the tank to a random other igloo
 EntityBase {
   id: iglu
   entityId: "iglu"
   entityType: "iglu"
   width: 75
   height: 90
   z: 5

   // make the image and the colliders accessible from the outside
   property alias igluBody: igluBody
   property alias porter: porter
   property alias rectColliderLeft: rectColliderLeft
   property alias rectColliderRight: rectColliderRight
   property alias rectColliderBottom: rectColliderBottom
   property alias teleportSound: teleportSound
   property alias glowTimer: glowTimer

   // plays whenever the tank teleports
   GameSoundEffect {
     volume: 0.3
     id: teleportSound
     // an ogg file is not playable on windows, because the extension is not supported!
     source: Qt.resolvedUrl("../../assets/snd/Teleport.wav")
   }

   // the visual representation of the igloo
   Image {
     z: 10
     id: igluBody
     width: parent.width
     height: parent.height
     anchors.centerIn: parent
   }

   // the left solid igloo wall
   PolygonCollider {
     bodyType: Body.Static
     id: rectColliderLeft
     anchors.centerIn: parent

     vertices: [
       Qt.point(igluBody.x + igluBody.width/5, igluBody.y),
       Qt.point(igluBody.x, igluBody.y + igluBody.height/2),
       Qt.point(igluBody.x + igluBody.width/7, igluBody.y + igluBody.height/7*6),
       Qt.point(igluBody.x + igluBody.width/2, igluBody.y+igluBody.height)
     ]

     transform: Rotation {
       origin.x: iglu.width / 2
       origin.y: iglu.width / 2
       angle: 135
     }
   }

   // the right solid igloo wall
   PolygonCollider {
     bodyType: Body.Static
     id: rectColliderRight

     vertices: [
       Qt.point(igluBody.x + igluBody.width/5*4, igluBody.y),
       Qt.point(igluBody.x + igluBody.width, igluBody.y + igluBody.height/2),
       Qt.point(igluBody.x + igluBody.width/7*6, igluBody.y + igluBody.height/7*6),
       Qt.point(igluBody.x+igluBody.width/2, igluBody.y+igluBody.height)
     ]

     anchors.centerIn: parent
   }

   // the bottom solid igloo wall
   PolygonCollider {
     bodyType: Body.Static
     id: rectColliderBottom

     vertices: [
       Qt.point(igluBody.x + igluBody.width/5, igluBody.y + igluBody.height/5*2),
       Qt.point(igluBody.x + igluBody.width/5*4, igluBody.y + igluBody.height/5*2),
       Qt.point(igluBody.x+igluBody.width/2, igluBody.y+igluBody.height)
     ]

     anchors.centerIn: parent
   }

   // two seperate timers in case the two tanks enter the same igloo at the same time
   // this timer is used to let the blue player disappear for a certain amount of time when he enters an iglu
   Timer {
     id: teleportTimerPlayerBlue
     interval: 750
     running: false
     repeat: false

     property var destIglu // destinationIglu
     property var teleportedPlayer
     property var destinationX
     property var destinationY

     // player reappears at a different igloo after a certain amount of time
     // signal that he is no longer teleporting
     onTriggered:{
       teleportedPlayer.x = destinationX
       teleportedPlayer.y = destinationY
       //teleportedPlayer.rotation = destIglu.rotation
       teleportedPlayer.opacity = 1.0
       teleportedPlayer.currentlyBeaming = false
     }
   }

   // this timer is used to let the red player disappear for a certain amount of time when he enters an iglu
   Timer {
     id: teleportTimerPlayerRed
     interval: 750
     running: false
     repeat: false

     property var destIglu // destinationIglu
     property var teleportedPlayer
     property var destinationX
     property var destinationY

     // player reappears at a different igloo after a certain amount of time
     // signal that he is no longer teleporting
     onTriggered: {
       teleportedPlayer.x = destinationX
       teleportedPlayer.y = destinationY
       teleportedPlayer.opacity = 1.0
       teleportedPlayer.currentlyBeaming = false
     }
   }

   // this timer makes the teleport destination igloo blink a few times
   Timer {
     id: glowTimer
     interval: 250
     running: false
     repeat: true
     property int glowCounter: 0

     // switch between the light igloo and the normal igloo image
     onTriggered:{
       if (glowCounter % 2 == 0){
         igluBody.source= "../../assets/img/IgluLight.png"
       }else{
         igluBody.source= "../../assets/img/Iglu.png"
       }
       glowCounter++
       if (glowCounter>=4) {
         glowCounter=0
         igluBody.source= "../../assets/img/Iglu.png"
         glowTimer.running=false
       }
     }
   }

   BoxCollider {
     id: porter
     width: igluBody.width/2
     height: igluBody.height/2
     anchors.centerIn: parent
     collisionTestingOnlyMode: true

     // handle a collision with a tank
     fixture.onBeginContact: (other, contactNormal) => {
       var collidedEntity = other.getBody().target;

       // if tankRed or tankBlue entered the igloo
       if (collidedEntity.isOfType("tank")) {
         teleportSound.play()

         // place the tank outside of the field while teleporting
         collidedEntity.x = scene.x - 200
         //collidedEntity.x = fixture.parent.parent.parent.x + 100
         collidedEntity.y = scene.y - 200

         // load all iglus of this level
         var iglus = entityManager.getEntityArrayByType("iglu")

         // randomly pick one of the igloos as teleport destination
         var random = Math.floor(Math.random() * (iglus.length))
         var destinationIglu = iglus[random]

         // repeat the step if the destination igloo is the entered igloo
         while(iglu.entityId == destinationIglu.entityId) {
           random = Math.floor(Math.random() * (iglus.length))
           destinationIglu = iglus[random]
         }

         // calculate the future tank destination
         var destinationX = (40 * Math.cos((destinationIglu.rotation - 10) * Math.PI / 180)) + destinationIglu.x + tankRed.width / 2
         var destinationY = (40 * Math.sin((destinationIglu.rotation - 10) * Math.PI / 180)) + destinationIglu.y + tankRed.width / 2

         // make the tank invisible and start with the igloo light effect
         collidedEntity.opacity = 0.0
         destinationIglu.glowTimer.running=true

         // calculate the tank's future destination and start their teleport timer
         if (collidedEntity.isOfType("tankRed")) {
           // playerRed
           collidedEntity.currentlyBeaming = true
           teleportTimerPlayerRed.destinationX = destinationX
           teleportTimerPlayerRed.destinationY = destinationY
           teleportTimerPlayerRed.destIglu = destinationIglu
           teleportTimerPlayerRed.teleportedPlayer = collidedEntity
           teleportTimerPlayerRed.start()
         } else {
           // playerBlue
           collidedEntity.currentlyBeaming = true
           teleportTimerPlayerBlue.destinationX = destinationX
           teleportTimerPlayerBlue.destinationY = destinationY
           teleportTimerPlayerBlue.destIglu = destinationIglu
           teleportTimerPlayerBlue.teleportedPlayer = collidedEntity
           teleportTimerPlayerBlue.start()
         }
       }
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded