aboutsummaryrefslogtreecommitdiffstats
path: root/examples/universalinput/quickaction/main.qml
blob: f1a7d0fbee65cbd07bfd7fc818310c0d3af66cf5 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick.Controls
import QtActionStore

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 800
    height: 600
    title: "Platforms"

    Rectangle {
        id: gameArea
        width: parent.width
        height: parent.height
        color: "black"

        property int paddleWidth: 10
        property int paddleHeight: 80
        property double gravity: 0.5

        GameObjectList {
            id: gameObjectList
            gameObjects: [p1, p2, p3, p4, p5]
        }

        Player {
            id: player
            width: 20
            height: 20
            color: "red"
            controller: Controller {}
        }

        Platform {
            id: p1
            x: 0
            y: 575
            width: 800
            height: 50
        }

        Platform {
            id: p2
            x: 30
            y: 500
            width: 300
            height: 50
        }

        Platform {
            id: p3
            x: 100
            y: 400
            width: 300
            height: 50
        }

        Platform {
            id: p4
            x: 0
            y: 300
            width: 300
            height: 50
        }

        Platform {
            id: p5
            x: 300
            y: 200
            width: 300
            height: 50
        }

        function updateGame(delta : real) : void {
            // Gravity
            player.velocity.y += gravity

            // Updates position based on velocity
            player.update()

            // Handle collisions
            gameObjectList.gameObjects.forEach(gameObject => {
                if (player.colliding(gameObject))
                    player.handleCollision(gameObject)
            })

            // Player bounds check
            if (player.x < 0)
                player.x = 0
            else if (player.x > width - player.width)
                player.x = width - player.width

            if (player.y < 0)
                player.y = 0
            else if (player.y > height - player.height)
                player.y = height - player.height
        }

        FrameAnimation {
            running: true
            onTriggered: gameArea.updateGame(frameTime)
        }
    }

    ActionStoreEventListener {
        id: actionEventListener
        actionStores: [player.controller]
        focus: true
    }
}