-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
307 lines (272 loc) · 8.62 KB
/
map.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var hex_size = 30;
var hex_3d_depth = 15;
var TILE_GRASS = 0;
var TILE_WATER = 1;
var TILE_OBSTACLE = 2;
var TILE_BASE0 = 3;
var TILE_BASE1 = 4;
var TILE_SPAWN0 = 5;
var TILE_SPAWN1 = 6;
var TILE_OBJECTIVE = 7;
/*
* Game co-ordinates are organised in the following manner to give a straight
* x and y axis.
* This aids in distance and path-finding calculations.
* http://keekerdc.com/2011/03/hexagon-grids-coordinate-systems-and-distance-calculations/
*/
function GameMap(width, height, map_data, objectives, buildings)
{
GameMap.baseConstructor.call(this, width, height);
var x;
var y;
var map_obj;
var map_group = create_group(mySvg);
/* Build up the data map */
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
map_obj = new Object();
if (typeof map_data == 'undefined')
{
map_obj.tile_type = 0;
}
else
{
map_obj.tile_type = map_data[y][x];
}
map_obj.occupied = false;
map_obj.obscured = true;
this.map[x][y] = map_obj;
}
}
/* Build up the graphical map and add data to external structures.
* Need to do this once we have a complete data map because tiles
* may have dependence on others
*/
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
map_obj = this.map[x][y];
var mx = hexmap_get_map_coord_x(x);
var my = hexmap_get_map_coord_y(x, y);
if (map_obj.tile_type != TILE_WATER)
{
var edge_array = this.get3dEdges(x, y);
map_obj.sprite =
create_hexagon
(
map_group,
gamemap_get_map_screenx(mx),
gamemap_get_map_screeny(mx, my),
hex_size,
gamemap_get_fill(map_obj.tile_type),
edge_array[0],
edge_array[1],
edge_array[2]
);
}
else
{
map_obj.sprite = undefined;
}
if (map_obj.tile_type == TILE_OBJECTIVE)
{
objectives.addObjective(hexmap_get_map_coord_x(x), hexmap_get_map_coord_y(x, y));
map_obj.sprite_over = create_image(mySvg, gamemap_get_map_screenx(mx), gamemap_get_map_screeny(mx, my), "stones.svg");
}
else if (map_obj.tile_type == TILE_SPAWN0 || map_obj.tile_type == TILE_SPAWN1)
{
map_obj.sprite_over = create_image(mySvg, gamemap_get_map_screenx(mx), gamemap_get_map_screeny(mx, my), "tent.svg");
}
else if (map_obj.tile_type == TILE_OBSTACLE)
{
map_obj.sprite_over = create_image(mySvg, gamemap_get_map_screenx(mx), gamemap_get_map_screeny(mx, my), "forest.svg", 1.5, 1.5);
}
else if (map_obj.tile_type == TILE_BASE0 || map_obj.tile_type == TILE_BASE1)
{
/* 0 or 1 depending on team */
var owner = map_obj.tile_type - TILE_BASE0;
buildings.addBuilding
(
new Building
(
hexmap_get_map_coord_x(x),
hexmap_get_map_coord_y(x, y),
2,
5,
owner,
this,
undefined /* sprite */
)
);
//map_obj.tile_type = TILE_GRASS;
}
}
}
}
KevLinDev.extend(GameMap, HexMap);
GameMap.prototype.get3dEdges =
function(x, y)
{
var r = new Array(false,false,false);
if (x % 2 == 0)
{
if (this.map[x-1][y].tile_type == TILE_WATER)
{
r[0] = true;
}
if (this.map[x][y+1].tile_type == TILE_WATER)
{
r[1] = true;
}
if (this.map[x+1][y].tile_type == TILE_WATER)
{
r[2] = true;
}
}
else
{
for (var i = -1; i < 2; i++)
{
if (this.map[x+i][y+1].tile_type == TILE_WATER)
{
r[i + 1] = true;
}
}
}
return r;
}
GameMap.prototype.clearEventHandlers =
function()
{
for (var y = 0; y < this.height; y++)
{
for (var x = 0; x < this.width; x++)
{
this.map[x][y].sprite.removeAttribute("onclick");
}
}
}
GameMap.prototype.redraw =
function()
{
for (var y = 0; y < this.height; y++)
{
for (var x = 0; x < this.width; x++)
{
var tile = this.map[x][y];
if (tile.sprite != undefined)
{
if (tile.obscured)
{
tile.sprite.setAttribute
(
"fill",
mod_svg_colour
(
gamemap_get_fill(tile.tile_type),
-64
)
);
}
else
{
tile.sprite.setAttribute("fill", gamemap_get_fill(tile.tile_type));
}
}
}
}
}
GameMap.prototype.dump =
function()
{
console.log("------------------------------------------------");
console.log("Map Dump");
console.log("width="+this.width+", height="+this.height);
for (var y = 0; y < this.height; y++)
{
line = "";
for (var x = 0; x < this.width; x++)
{
line += this.map[x][y].tile_type;
}
console.log(line)
}
console.log("------------------------------------------------");
}
GameMap.prototype.isPassable =
function(x, y)
{
var tile = this.getTile(x, y);
var v = tile.tile_type;
if (v === TILE_GRASS || v === TILE_SPAWN0 || v === TILE_SPAWN1 || v === TILE_OBJECTIVE)
{
return true;
}
return false;
}
GameMap.prototype.setOccupied =
function(x, y, v)
{
var tile = this.getTile(x, y);
tile.occupied = v;
}
GameMap.prototype.isOccupied =
function(x, y)
{
return this.getTile(x, y).occupied;
}
GameMap.prototype.obscureAll =
function()
{
var x;
var y;
for (x = 0; x < this.width; x++)
{
for (y = 0; y < this.height; y++)
{
this.map[x][y].obscured = true;
}
}
}
GameMap.prototype.setVisible =
function(x, y)
{
var tile = this.getTile(x, y);
tile.obscured = false;
}
function gamemap_get_fill(value)
{
switch(value)
{
case TILE_GRASS: return "#1c6c1c"; // grass - passable
case TILE_WATER: return "#CC00CC"; // water - impassable
case TILE_OBSTACLE: return "#165016"; // blocked - impassable
case TILE_BASE0: return "#800000"; // team 0 base
case TILE_BASE1: return "#000080"; // team 1 base
case TILE_SPAWN0: return "#800000"; // team 0 spawn
case TILE_SPAWN1: return "#000080"; // team 1 spawn
case TILE_OBJECTIVE: return "#2CA02C"; // neutral objective
default: return "#FFFF00";
}
}
function gamemap_get_hex_xoffset(r)
{
return Math.sin(Math.PI/6) * r;
}
function gamemap_get_hex_yoffset(r)
{
return Math.cos(Math.PI/6) * r;
}
function gamemap_get_map_screenx(x)
{
return hex_size + x * (hex_size + gamemap_get_hex_xoffset(hex_size));
}
function gamemap_get_map_screeny(x, y)
{
var my = hexmap_get_bufy(x, y);
var v = (gamemap_get_hex_yoffset(hex_size) + (my * gamemap_get_hex_yoffset(hex_size)) + (my + (x % 2)) * (gamemap_get_hex_yoffset(hex_size)));
return v;
}