-
Notifications
You must be signed in to change notification settings - Fork 0
/
BHex-patch.gs
88 lines (73 loc) · 2.42 KB
/
BHex-patch.gs
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
BHex.Grid.prototype.mark = function (a) {
this.hexes.some(function(h) {
if (h.compareTo(a)){
h.blocked = true;
}
});
}
/*
/ Simple recursive function
function recursiveFunction() {
// Add base case
if (...) {
// Call the recursive function again
recursiveFunction()
} else {
// Return something instead of calling
// the recursive function again
}
}
// Call the recursive function
recursiveFunction()
*/
BHex.Grid.prototype.investigate = function (a) {
var grid = this;
// console.log('call me only once...::'+a.x+'::'+a.y+'::...');
var neighbors = [],
parentBorders = [
new BHex.Axial(a.x + 1, a.y), new BHex.Axial(a.x + 1, a.y - 1), new BHex.Axial(a.x, a.y - 1),
new BHex.Axial(a.x - 1, a.y), new BHex.Axial(a.x - 1, a.y + 1), new BHex.Axial(a.x, a.y + 1)
];
this.skipList.push(grid.getHexAt(a).getKey());
// console.log('>>> adding '+grid.getHexAt(a).getKey()+' to the skiplist...');
for (let c = 0; c < parentBorders.length; c++) {
let theHex = this.getHexAt(parentBorders[c]);
if( theHex && !theHex.blocked ) { // blocked=false
grid.mark(a);
// console.info('adding neighbors...: '+theHex.getKey());
neighbors.push(theHex);
} else { // blocked=true ... there is a neighbor
if(theHex && _.find(this.skipList, theHex.getKey())){
console.log('>>> adding '+theHex.getKey()+' to the skiplist...');
this.skipList.push(theHex.getKey());
this.skipList = _.uniq(this.skipList);
// console.log(this.skipList.join(' ::: '));
let childBorders = grid.investigate(theHex);
for (let d = 0; d < childBorders.length; d++) {
let childHex = grid.getHexAt(childBorders[d]);
if(childHex && !childHex.blocked){
neighbors.push(childHex);
} else {
continue;
}
};
}
}
};
neighbors = _.uniq(_.pull(neighbors, this.skipList));
// console.log(this.skipList.join(' ::: '));
return neighbors;
};
BHex.Grid.prototype.initMarkers = function (a) {
var grid = this;
this.skipList = [];
};
BHex.Grid.prototype.placeAtBorder = function (a) {
var grid = this;
var colli = grid.investigate(a);
if(colli.length>0){
let putTo = grid.getHexAt(_.sample(colli));
// console.info('>>> the new tile will be placed at: '+putTo.getKey());
return putTo;
}
};