forked from foolswood/Hexagongame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_game.es
65 lines (65 loc) · 2.07 KB
/
std_game.es
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
function gameStandard(iface, m, doneCallback) {
var hexes = loadMaze(m.maze, iface)
var pos, col, nextCol
//End marker
iface.endMarker.position = m.end
iface.endMarker.visible = true
//Player marker and initial conditions
if (m.startColour === undefined)
m.startColour = "w"
col = m.startColour
pos = m.start
nextCol = hexes[pos].colour
iface.playerMarker.colour = col
iface.playerMarker.position = pos
iface.playerMarker.visible = true
//Route
var route = [[pos, col]];
iface.playerMarker.callback = function() {
//resets maze
if (route.length === 1) { //maze at start
doneCallback(null)
} else { //reset
pos = route[0][0]
nextCol = hexes[m.start].colour
col = m.startColour
route = [route[0]]
iface.playerMarker.position = pos
iface.playerMarker.colour = col
}
}
var hexFunc = function(hex) {
var entryPoints = hex.dividers.map(function(x) {return x[0].toString()})
var hexCol = hex.colour
var hexPos = hex.position
var moveFunc = function() {
var i = entryPoints.indexOf(pos.toString())
if (i != -1) {
var dcol = hex.dividers[i][1].colour
if (col === "w" || dcol === "w" || col === dcol) {
col = nextCol
pos = hexPos
nextCol = hexCol
iface.playerMarker.position = pos
iface.playerMarker.colour = col
route.push([pos, hexCol])
return
}
}
throw "Invalid Move"
}
if (hexPos.toString() === m.end.toString()) {
return function() {
moveFunc()
iface.addRoute(route)
iface.winModal(function() {doneCallback(col)})
}
} else {
return moveFunc
}
}
for (var h in hexes) {
var hex = hexes[h]
hex.callback = hexFunc(hex)
}
}