-
Notifications
You must be signed in to change notification settings - Fork 11
/
GameBoard.qml
159 lines (136 loc) · 3.05 KB
/
GameBoard.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
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
158
159
import QtQuick 2.0
import Material 0.1
import QtMultimedia 5.0
import com.Kanari.GameLogic 1.0
import "GameBoardToggle.js" as Toggle
Item {
id: root
property int n: 5
property int m: 5
property double gridLength: Math.min(width / m, height / n)
property string levelName
property bool finished: false
onLevelNameChanged: {
if (levelName != "") {
logic.loadLevel(levelName)
}
}
signal pressed()
signal gameFinished()
signal gameNeedFill()
signal solveFinished(int time)
signal loadFailed(string message)
function restart() {
logic.restart()
finished = false
}
function solve() {
logic.solve()
}
function canSolve() {
return logic.canSolve()
}
function abortSolve() {
logic.abortSolve()
}
SoundEffect {
id: needFillSound
source: "qrc:/sound/Blow.wav"
}
SoundEffect {
id: lastConnectionSound
source: "qrc:/sound/Hero.wav"
}
SoundEffect {
id: connectedSound
source: "qrc:/sound/Submarine.wav"
}
SoundEffect {
id: disconnectedSound
source: "qrc:/sound/Pop.wav"
}
GameLogic {
id: logic
onLoadFinished: {
n = height
m = width
Toggle.createBoard(n, m, gridLength)
logic.displayCircles()
}
onLoadFailed: {
console.log("load failed")
root.loadFailed(message)
root.finished = true
}
onNoSolution: {
console.log("no solution")
}
onSolveFinished: {
root.solveFinished(time)
root.finished = true
}
onHideAll: Toggle.hideAll()
onRipple: Toggle.ripple(x, y)
onChangeGridColor: Toggle.changeGridColor(x, y, color)
onShowCircle: Toggle.showCircle(x, y, color)
onHideCircle: Toggle.hideCircle(x, y)
onShowLine: Toggle.showLine(x1, y1, vertical, color)
onHideLine: Toggle.hideLine(x1, y1, vertical)
onPlayConnectedSound: connectedSound.play()
onPlayDisconnectedSound: disconnectedSound.play()
onGameFinished: {
lastConnectionSound.play()
root.gameFinished()
root.finished = true
}
onGameNeedFill: {
needFillSound.play()
root.gameNeedFill()
}
}
Rectangle {
id: board
anchors.centerIn: parent
width: m * gridLength
height: n * gridLength
color: Theme.backgroundColor
MouseArea {
id: mouseArea
anchors.fill: parent
z: 10000
FollowMouseCircle {
id: followCircle
}
function pressEvent(mouse) {
if (!root.finished) {
var y = Math.floor(mouse.x / gridLength)
var x = Math.floor(mouse.y / gridLength)
var color = logic.colorAt(x, y)
// console.log(color)
followCircle.color = color
followCircle.state = color != "none" ? "shown" : "hidden"
logic.startPath(x, y);
}
}
onPressed: {
root.pressed()
pressEvent(mouse)
}
onPositionChanged: {
if (!root.finished) {
var y = Math.floor(mouse.x / gridLength)
var x = Math.floor(mouse.y / gridLength)
logic.movePath(x, y);
}
}
onReleased: {
if (!root.finished) {
var y = Math.floor(mouse.x / gridLength)
var x = Math.floor(mouse.y / gridLength)
logic.endPath(x, y);
followCircle.state = "hidden"
}
}
}
}
}