summaryrefslogtreecommitdiffstats
path: root/quick3d/chapter09/completegame/Gamelogic.qml
blob: 5b3feccc78e709f7766f20544b13d546208e4a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.0

Item {
    property bool upPressed: false
    property bool downPressed: false
    property bool leftPressed: false
    property bool rightPressed: false

    property variant targetComponent: Qt.createComponent("Target.qml");
    property variant bossEnemyComponent: Qt.createComponent("Enemy.qml")

    property real maneuverability: 0.3
    //The game timer is our event loop. It processes the key events and updates the position of the hamburger
    Timer {
        id: gameTimer
        running: root.state=="Game"||root.state=="BossFight"
        interval: 50;
        repeat: true;
        onTriggered: {
        if (player.energy<player.maxEnergy)
          player.energy++;
            //Velocity is updated
            player.vx+=player.ax*0.05;
            player.vy+=player.ay*0.05;
            //Acceleration is updated
            player.ax=(player.ax+maneuverability*leftPressed - maneuverability*rightPressed)/1.1;
            player.ay=(player.ay+maneuverability*downPressed - maneuverability*upPressed)/1.1;
            //Position is updated
            player.position.x += player.vx*0.05;
            player.position.y += player.vy*0.05;
            //If the player exceeds a boundary, the movement is stopped
            if (player.position.x>x_bound) {
                player.position.x = x_bound
                player.vx = 0;
                if (player.ax>0)
                    player.ax = 0;
            }
            else if (player.position.x<-x_bound) {
                player.position.x = -x_bound
                player.vx = 0;
                if (player.ax<0)
                    player.ax = 0;
            }
            else if (player.position.y<-y_bound) {
                player.position.y = -y_bound
                player.vy = 0;
                if (player.ay<0)
                    player.ay = 0;
            }
            else if (player.position.y>y_bound) {
                player.position.y = y_bound
                player.vy = 0;
                if (player.ay>0)
                    player.ay = 0;
            }
        }
    }
    focus: true
    //Handling of basic key events
    Keys.onPressed: {
        if (event.key == Qt.Key_A)
            leftPressed = true
        if (event.key == Qt.Key_D)
            rightPressed = true
    if (event.key == Qt.Key_W && root.state=="Game")
        upPressed = true
    if (event.key == Qt.Key_S && root.state=="Game")
        downPressed = true
    if (event.key == Qt.Key_Space && root.state=="BossFight")
        fireLaser();
    }
    Keys.onReleased: {
        if (event.key == Qt.Key_A)
            leftPressed = false
        if (event.key == Qt.Key_D)
            rightPressed = false
        if (event.key == Qt.Key_W)
            upPressed = false
        if (event.key == Qt.Key_S)
            downPressed = false
    }

    //Timer creates targets in a certain interval
    Timer {
      id: targetTimer
      interval: 4000
      repeat: true
      running: root.state=="Game"
      onTriggered: {
          var component;
          //After a certain amount of targets were created the boss enemy appears
          if (targetCount>0) {
          targetTimer.stop()
          enemy = bossEnemyComponent.createObject(level)
          }
          //Targets are constantly created and fly towards the player
          else {
          targetCount++
          var object = targetComponent.createObject(level, {"position.x": (Math.random()-0.5) *8, "position.y":  (Math.random()-0.5) *6,
                              "scale": 3-0.2*targetCount, "collisionTarget": player})
          object.collisionDetected.connect(targetCollision)
          }
      }
    }

    function targetCollision(sender) {
    score++
    }

    function fireLaser() {
    if (player.energy>=40) {
        player.energy -=40
        var component = Qt.createComponent("Bullet.qml")
        var laserObject = component.createObject(level, {"position": player.position,"collisionTarget": enemy})
        laserObject.collisionDetected.connect(enemy.hit)
    }
    }

}