-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameBlock.qml
71 lines (68 loc) · 1.58 KB
/
GameBlock.qml
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
import QtQuick 2.0
Block {
property var game
property alias speed: blockTimer.interval
property string nextState
signal gameOver
signal newState(string state, string nextState)
id: root
Component.onCompleted: {
nextState = randomState()
reset()
}
Timer {
id: blockTimer
repeat: true
running: false
onTriggered: root.down()
}
function left() {
if (game.canBlockMove(root, 0, -1))
col -= 1
}
function right() {
if (game.canBlockMove(root, 0, 1))
col += 1
}
function down() {
if (game.canBlockMove(root, 1, 0)) {
row += 1
}
else {
stop()
game.freezeBlock(root)
reset()
}
}
function drop() {
blockTimer.interval = 10
}
function rotate() {
rotateCCW()
if (!game.canBlockMove(root, 0, 0))
rotateCW()
}
function stop() {
blockTimer.running = false
}
function restart() {
blockTimer.restart()
}
function reset() {
col = gameField.cols / 2
row = 1
cellOffsetListIndex = 0
state = nextState
if (game.canBlockMove(root, 0, 0)) {
nextState = randomState()
newState(state, nextState)
blockTimer.interval = 1000 * Math.pow(4.0/5.0, gameField.level)
restart()
}
else
gameOver()
}
function randomState() {
return states[Math.floor(Math.random() * states.length)].name
}
}