1
import Qt 4.7
2
import Tiled 1.0
3
import Box2D 2.0 as Box2D
4
5
Rectangle {
6
    id: screen
7
    width: 800
8
    height: 480
9
10
    Flickable {
11
        id: viewport
12
        anchors.fill: screen
13
        contentWidth: map.width
14
        contentHeight: map.height
15
        boundsBehavior: Flickable.StopAtBounds
16
        interactive: false
17
        contentX: Math.min(contentWidth - width, Math.max(0, playerdata.x - width / 2))
18
        contentY: Math.min(contentHeight - height, Math.max(0, playerdata.y - height / 2))
19
20
        TileMap {
21
            id: map
22
            source: "data/desert.tmx"
23
24
            Box2D.World {
25
                frameTime: 1000 / 60
26
27
                Box2D.Body {
28
                    x: 200
29
                    width: 100
30
                    height: 50
31
                    rotation: 90 + 44
32
33
                    Rectangle {
34
                        anchors.fill: parent
35
                        color: "red"
36
                        border.color: "black"
37
                        border.width: 2
38
                        smooth: true
39
                    }
40
                }
41
                Box2D.Body {
42
                    x: 60
43
                    width: 50
44
                    height: 100
45
                    rotation: 46
46
47
                    Rectangle {
48
                        anchors.fill: parent
49
                        color: "red"
50
                        border.color: "black"
51
                        border.width: 2
52
                        smooth: true
53
                    }
54
                }
55
                Box2D.Body {
56
                    x: 100
57
                    y: -150
58
                    width: 100
59
                    height: 100
60
                    rotation: 44
61
62
63
                    Rectangle {
64
                        anchors.fill: parent
65
                        color: "red"
66
                        border.color: "black"
67
                        border.width: 2
68
                        smooth: true
69
                    }
70
                }
71
                Box2D.Body {
72
                    x: 200
73
                    y: -1400
74
                    width: 50
75
                    height: 25
76
                    rotation: 90 + 35.134
77
78
                    Rectangle {
79
                        anchors.fill: parent
80
                        color: "orange"
81
                        border.color: "black"
82
                        border.width: 2
83
                        smooth: true
84
                    }
85
                }
86
                Box2D.Body {
87
                    x: 200
88
                    y: -799.9
89
                    width: 100
90
                    height: 50.25
91
                    rotation: 90 + 44
92
93
                    Rectangle {
94
                        anchors.fill: parent
95
                        color: "purple"
96
                        border.color: "black"
97
                        border.width: 2
98
                        smooth: true
99
                    }
100
                }
101
                Box2D.Body {
102
                    x: 60
103
                    y: -400
104
                    width: 50
105
                    height: 100
106
                    rotation: 46
107
108
                    Rectangle {
109
                        anchors.fill: parent
110
                        color: "red"
111
                        border.color: "black"
112
                        border.width: 2
113
                        smooth: true
114
                    }
115
                }
116
                Box2D.Body {
117
                    x: 100
118
                    y: -550
119
                    width: 100
120
                    height: 100
121
                    rotation: 44
122
123
124
                    Rectangle {
125
                        anchors.fill: parent
126
                        color: "red"
127
                        border.color: "black"
128
                        border.width: 2
129
                        smooth: true
130
                    }
131
                }
132
                Box2D.Body {
133
                    x: -50
134
                    y: 400
135
                    width: 900
136
                    height: 100
137
                    bodyType: Box2D.Body.Static
138
139
                    Rectangle {
140
                        anchors.fill: parent
141
                        color: "green"
142
                        border.color: "black"
143
                        border.width: 2
144
                        smooth: true
145
                    }
146
                }
147
            }
148
        }
149
150
        Image {
151
            id: playerSprite
152
            source: "../data/player.png"
153
            x: playerdata.x
154
            y: playerdata.y
155
        }
156
    }
157
158
    Text {
159
        id: nameLabel
160
        x: 10
161
        y: 10
162
        text: map.source
163
    }
164
    Item {
165
        id: playerdata
166
        Behavior on x {
167
            NumberAnimation { duration: 100; easing.type: "Linear" }
168
        }
169
        Behavior on y {
170
            NumberAnimation { duration: 100; easing.type: "Linear" }
171
        }
172
        property int speed: 15
173
        property int veloX: 0
174
        property int veloY: 0
175
        // Walk timer
176
        Timer {
177
            id: playerwalktimer
178
            interval: 100
179
            repeat: true
180
            running: true
181
            onTriggered: {
182
                playerdata.x += playerdata.veloX;
183
                playerdata.y += playerdata.veloY;
184
            }
185
        }
186
    }
187
188
    focus: true
189
    Keys.onPressed: {
190
        // Toggle maps
191
        if (event.key == Qt.Key_Space) {
192
            if (map.source == "data/desert.tmx") {
193
                map.source = "data/sewers.tmx";
194
                nameLabel.color = "white";
195
            } else {
196
                map.source = "data/desert.tmx";
197
                nameLabel.color = "black";
198
            }
199
        }
200
201
        // Start Movement
202
        if (!event.isAutoRepeat) {
203
            if (event.key == Qt.Key_Left) {
204
                playerdata.veloX -= playerdata.speed
205
            }
206
            if (event.key == Qt.Key_Right) {
207
                playerdata.veloX += playerdata.speed
208
            }
209
            if (event.key == Qt.Key_Up) {
210
                playerdata.veloY -= playerdata.speed
211
            }
212
            if (event.key == Qt.Key_Down) {
213
                playerdata.veloY += playerdata.speed
214
            }
215
        }
216
    }
217
    Keys.onReleased: {
218
        // Stop Movement
219
        if (!event.isAutoRepeat) {
220
            if (event.key == Qt.Key_Left) {
221
                playerdata.veloX += playerdata.speed
222
            }
223
            if (event.key == Qt.Key_Right) {
224
                playerdata.veloX -= playerdata.speed
225
            }
226
            if (event.key == Qt.Key_Up) {
227
                playerdata.veloY += playerdata.speed
228
            }
229
            if (event.key == Qt.Key_Down) {
230
                playerdata.veloY -= playerdata.speed
231
            }
232
        }
233
    }
234
}