-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
136 lines (131 loc) · 3.05 KB
/
engine.js
File metadata and controls
136 lines (131 loc) · 3.05 KB
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
//http://www.redblobgames.com/grids/hexagons/
(function(){
win.World = {
"map": [],
"xy": function(x,y){
if(!World.map[x]){return false;}
if(this.map[x][y]){
return this.map[x][y];
}else{
return false;
}
},
"qr": function(q,r){
if(!World.map[r]){return false;}
y = q+Math.ceil(r/2);
if(this.map[r][y]){
return this.map[r][y];
}else{
return false;
}
}
};
win.Cell = function(x,y){
if(!this instanceof win.Cell){return new win.Cell(x,y);}
this.domNode = doc.mkNode("","cell");
this.domNode.mapNode = this;
var coord = [x,y];
this.xy = function(x,y){ //Even row system
if(x === undefined && y === undefined){
return [coord[0], coord[1]];
}else{
if(World.map[coord[0]]){
if(World.map[coord[0]][coord[1]]){
World.map[coord[0]][coord[1]] = undefined;
}
}
if(!World.map[x]){World.map[x]=[];}
World.map[x][y] = this;
coord = [x,y];
};
};
this.qr = function(q,r){ //Axial system
if(q === undefined && r === undefined){
return [coord[1] - Math.floor(coord[0]/2), coord[0]];
}else{
var y = q+Math.ceil(r/2);
World.map[coord[0], coord[1]] = undefined;
if(!World.map[r]){World.map[r]=[];}
World.map[r][y] = this;
coord = [r][y];
};
};
this.dir = function(a){
switch(Math.round((a%6+6)%6)){
case 0:
if(coord[1]%2){
return World.xy(coord[0],coord[1]-1);
}else{
return World.xy(coord[0]-1,coord[1]-1);
}
break;
case 1:
if(coord[1]%2){
return World.xy(coord[0]+1,coord[1]-1);
}else{
return World.xy(coord[0],coord[1]-1);
}
break;
case 2:
return World.xy(coord[0]+1,coord[1]);
break;
case 3:
if(coord[1]%2){
return World.xy(coord[0]+1,coord[1]+1);
}else{
return World.xy(coord[0],coord[1]+1);
}
break;
case 4:
if(coord[1]%2){
return World.xy(coord[0],coord[1]+1);
}else{
return World.xy(coord[0]-1,coord[1]+1);
}
break;
case 5:
return World.xy(coord[0]-1,coord[1]);
break;
default:
return false;
break;
};
};
this.elements = [];
this.on = this.domNode.addEventListener;
this.xy(x,y);
};
win.CellNode = function(name){
if(!this instanceof win.CellNode){return new win.CellNode(x,y);}
var self = this;
this.nodeName = name;
this.cells = [];
this.remove = function(x,y){
if(typeof(x) == "number" && typeof(y) == "number"){
i = self.cells.indexOf(World.xy(x,y));
if(i+1){
self.cells[i].domNode.rmAttr("data-cellnode-"+self.nodeName);
self.cells.splice(i,1);
}
}else if(x === undefined && y === undefined){
i=0;
while(i<self.cells.length){
self.cells[i].domNode.rmAttr("data-cellnode-"+self.nodeName);
i++;
}
self.cells = [];
}
};
this.xy = {};
this.qr = {};
this.xy.place = function(x,y){
n = World.xy(x,y);
if(!n){return false;}
i = self.cells.indexOf(n);
if( !(i+1) ){
self.cells[self.cells.length] = n;
n.domNode.addAttr("data-cellnode-"+self.nodeName);
}
}
};
})();