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

import QtQuick
import QtActionStore

GameObject {
    id: player

    property Controller controller
    property double playerSpeed: 3

    function update() {
        goUpdate() // does GameObject update functionality
    }

    ActionDispatch {
        actionStore: controller

        ActionHandler {
            actionTitle: "Left"
            onTriggered: {
                velocity.x = value * -playerSpeed
            }
        }

        ActionHandler {
            actionTitle: "Right"
            onTriggered: {
                velocity.x = value * playerSpeed
            }
        }

        ActionHandler {
            actionTitle: "Jump"
            onTriggered: {
                let force = -15
                velocity.y += force
            }
        }
    }

    function handleCollision(other: GameObject) {
        if (x + width > other.x && x < other.x) {
            // Left
            x = other.x - width
        } else if (x < other.x + other.width && x + width > other.x + other.width) {
            // Right
            x = other.x + other.width
        }

        if (y + height > other.y && y < other.y) {
            // Top
            y = other.y - height
            velocity.y = 0
        } else if (y < other.y + other.height && y + height > other.y + other.height) {
            // Bottom
            y = other.y + other.height
            velocity.y = 0
        }
    }
}