This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map - Copy.js
579 lines (536 loc) · 19.5 KB
/
map - Copy.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*************************************************************/
// File: map.js
// Purpose: Generate the map editor HTML, and load the map
// Author: Nick Dugger
// Notes: This file is long and messy, because it does a lot.
/*************************************************************/
function mapEditor(loadTiles) {
function infoPane() {
// Display active info in the infoPane div
document.querySelector("#activeProject").innerHTML = project.title;
document.querySelector("#activeMap").innerHTML = map.name;
}
infoPane();
// Set active class to active map, in list
var last = document.querySelector("li[data-map=\"" + maps.length + "\"");
last.classList.add("active");
var mapWidth = map.width * map.grid;
var mapHeight = map.height * map.grid;
// Set variables to elements
var editorDiv = document.querySelector("#mapEditor");
var tilesDiv = document.querySelector("#tiles");
// "Generate" HTML elements
console.log("Generating editor HTML...");
var tilesetHTML = "<img id=\"tilesetUse\" src=\"project/" + map.tileset + "\" />";
var choiceMark = "<div id=\"choiceMark\"></div>";
var mapBG = "<div id=\"mapBG\"></div>";
var ml1 = "<canvas id=\"layer1\" width=\"" + mapWidth + "\" height=\"" + mapHeight + "\"></canvas>";
var ml2 = "<canvas id=\"layer2\" width=\"" + mapWidth + "\" height=\"" + mapHeight + "\"></canvas>";
var ml3 = "<canvas id=\"layer3\" width=\"" + mapWidth + "\" height=\"" + mapHeight + "\"></canvas>";
var ml4 = "<canvas id=\"layer4\" width=\"" + mapWidth + "\" height=\"" + mapHeight + "\"></canvas>";
var mEvents = "<canvas id=\"layerE\" width=\"" + mapWidth + "\" height=\"" + mapHeight + "\"></canvas>";
var gridContainer = "<div id=\"gridContainer\"></div>";
var mapBlanketDiv = "<div id=\"mapBlanket\"></div>";
var paintMark = "<div id=\"paintMark\"></div>";
// Put the elements into their parent elements
editorDiv.innerHTML += mapBG + ml1 + ml2 + ml3 + ml4 + mEvents + gridContainer + mapBlanketDiv + paintMark;
tilesDiv.innerHTML += choiceMark + tilesetHTML;
// Set context origin's to their respective canvas elements:
var layer1 = document.querySelector("#layer1");
var ctx1 = layer1.getContext("2d");
var layer2 = document.querySelector("#layer2");
var ctx2 = layer2.getContext("2d");
var layer3 = document.querySelector("#layer3");
var ctx3 = layer3.getContext("2d");
var layer4 = document.querySelector("#layer4");
var ctx4 = layer4.getContext("2d");
var layerE = document.querySelector("#layerE");
var ctxE = layerE.getContext("2d");
// Set variables to elements
var mapBlanket = document.querySelector("#mapBlanket");
var tilesetImage = document.getElementById("tilesetUse");
// Set W/H in CSS for non-canvas elements
document.querySelector("#mapGapX").setAttribute("style", "width:" + mapWidth + "px;");
document.querySelector("#mapGapY").setAttribute("style", "height:" + mapHeight + "px;");
document.querySelector("#mapBG").setAttribute("style", "width:" + mapWidth + "px; height:" + mapHeight + "px;");
document.querySelector("#mapBlanket").setAttribute("style", "width:" + mapWidth + "px; height:" + mapHeight + "px;");
console.log("> HTML Generated.");
// Mousedown listener for choosing the layer to paint on
document.querySelector("#layer1Radio").addEventListener("click", function() {
mapLayer2 = mapLayer3 = mapLayer4 = mapEvents = false;
mapLayer1 = true;
chooseLayer();
}, false);
document.querySelector("#layer2Radio").addEventListener("click", function() {
mapLayer1 = mapLayer3 = mapLayer4 = mapEvents = false;
mapLayer2 = true;
chooseLayer();
}, false);
document.querySelector("#layer3Radio").addEventListener("click", function() {
mapLayer1 = mapLayer2 = mapLayer4 = mapEvents = false;
mapLayer3 = true;
chooseLayer();
}, false);
document.querySelector("#layer4Radio").addEventListener("click", function() {
mapLayer1 = mapLayer2 = mapLayer3 = mapEvents = false;
mapLayer4 = true;
chooseLayer();
}, false);
document.querySelector("#eventRadio").addEventListener("click", function() {
mapLayer1 = mapLayer2 = mapLayer3 = mapLayer4 = false;
mapEvents = true;
chooseLayer();
}, false);
// Create empty arrays to store tile data
var layer1Tiles = [];
var layer2Tiles = [];
var layer3Tiles = [];
var layer4Tiles = [];
var events = [];
// Create custom "tile" objects
function tileObj1(x, y, img) {
this.x = x;
this.y = y;
this.img = img;
};
function tileObj2(x, y, img) {
this.x = x;
this.y = y;
this.img = img;
};
function tileObj3(x, y, img) {
this.x = x;
this.y = y;
this.img = img;
};
function tileObj4(x, y, img) {
this.x = x;
this.y = y;
this.img = img;
};
// Create custom "event" object
function eventObj(x, y, data) {
this.x = x;
this.y = y;
this.data = data;
};
// Variables to help choose selected map layer
var mapLayer1 = true, mapLayer2, mapLayer3, mapLayer4, mapEvents;
var activeLayer = layer1;
var layerTiles;
var paintOn = 1;
layer1.classList.add("activeLayer");
// Detecting which layer is chosen, and adding an activeLayer class
function chooseLayer() {
if (mapLayer1 === true) {
activeLayer = layer1;
ctx = layer1.getContext("2d");
layerTiles = layer1Tiles;
paintOn = 1;
layer1.classList.add("activeLayer");
layer2.classList.remove("activeLayer");
layer3.classList.remove("activeLayer");
layer4.classList.remove("activeLayer");
} else if (mapLayer2 === true) {
activeLayer = layer2;
ctx = layer2.getContext("2d");
layerTiles = layer2Tiles;
paintOn = 2;
layer1.classList.remove("activeLayer");
layer2.classList.add("activeLayer");
layer3.classList.remove("activeLayer");
layer4.classList.remove("activeLayer");
} else if (mapLayer3 === true) {
activeLayer = layer3;
ctx = layer3.getContext("2d");
layerTiles = layer3Tiles;
paintOn = 3;
layer1.classList.remove("activeLayer");
layer2.classList.remove("activeLayer");
layer3.classList.add("activeLayer");
layer4.classList.remove("activeLayer");
} else if (mapLayer4 === true) {
activeLayer = layer4;
ctx = layer4.getContext("2d");
layerTiles = layer4Tiles;
paintOn = 4;
layer1.classList.remove("activeLayer");
layer2.classList.remove("activeLayer");
layer3.classList.remove("activeLayer");
layer4.classList.add("activeLayer");
} else if (mapEvents === true) {
activeLayer = layerE;
ctx = layerE.getContext("2d");
layerTiles = events;
paintOn = 5;
layer1.classList.add("activeLayer");
layer2.classList.add("activeLayer");
layer3.classList.add("activeLayer");
layer4.classList.add("activeLayer");
}
};
chooseLayer();
// Create SVG-based grid, overlaying the map canvas
function showGrid() {
for (var i = map.grid; i < mapWidth; i += map.grid) {
var lineY = "<line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"" + mapHeight + "\" />";
var gridSvgY = "<svg width=\"2\" height=\"" + mapHeight + "\" style=\"margin-left:" + (i - 1) + "\">" + lineY + "</svg>";
document.getElementById("gridContainer").innerHTML += gridSvgY;
}
for (var i = map.grid; i < mapHeight; i += map.grid) {
var lineX = "<line y1=\"0\" x1=\"0\" y2=\"0\" x2=\"" + mapWidth + "\" />";
var gridSvgX = "<svg height=\"2\" width=\"" + mapWidth + "\" style=\"margin-top:" + (i - 1) + "\">" + lineX + "</svg>";
document.getElementById("gridContainer").innerHTML += gridSvgX;
}
};
showGrid();
var gridStatus = true;
document.querySelector("#gridCheck").addEventListener("mousedown", gridToggle, false);
function gridToggle() {
if (gridStatus === true) {
gridStatus = false;
document.querySelector("#gridContainer").classList.add("hidden");
} else if (gridStatus === false) {
gridStatus = true;
document.querySelector("#gridContainer").classList.remove("hidden");
}
}
// Create tile-grid, map.grid sized rectangles that will adopt tileset images
function gridTiles() {
for (var x = 0; x < mapWidth; x += map.grid) {
for (var y = 0; y < mapHeight; y += map.grid) {
// create new tile, and push it to the tiles array
//var img = {
// autotile: false
//};
var newTile1 = new tileObj1(x, y, null);
layer1Tiles.push(newTile1);
var newTile2 = new tileObj2(x, y, null);
layer2Tiles.push(newTile2);
var newTile3 = new tileObj3(x, y, null);
layer3Tiles.push(newTile3);
var newTile4 = new tileObj4(x, y, null);
layer4Tiles.push(newTile4);
var newEvent = new eventObj(x, y, null);
events.push(newEvent);
}
}
};
gridTiles();
// Variables to help with tile choosing
var isAutotile = false;
var choiceX = 0;
var choiceY = 0;
var choiceW = map.grid;
var choiceH = map.grid;
// Display the choiceMark div over the chosen tile (this is pretty messy):
document.getElementById("choiceMark").setAttribute(
"style",
"width:" + map.grid +
"px; height:" + map.grid +
"px; margin-top:" + (- map.grid) +
"px; top:" + map.grid + "px; left:0"
);
// Clck event to grab tile(s) from the tileset
tilesetImage.addEventListener("mousedown", tileChoose, false);
function tileChoose(e) {
isAutotile = false;
var clickX = e.pageX - tilesetImage.offsetLeft + tilesetImage.parentNode.scrollLeft;
var clickY = e.pageY - tilesetImage.offsetTop + tilesetImage.parentNode.scrollTop - 40 - 32;
// use click coordinates, in conjunction with a modulo, to find the tile coordinates
var chooseX = clickX - (clickX % map.grid);
var chooseY = clickY - (clickY % map.grid);
// Display the choiceMark div over the chosen tile (this is pretty messy):
document.getElementById("choiceMark").setAttribute(
"style",
"width:" + map.grid +
"px; height:" + map.grid +
"px; margin-top:" + (- map.grid) +
"px; top:" + ((chooseY) + map.grid) +
"px; left:" + (chooseX) + "px; display:block;"
);
// Set the values for our global Choice variables:
choiceX = chooseX;
choiceY = chooseY;
choiceW = map.grid;
choiceH = map.grid;
};
// Show the paint marker over the map
mapBlanket.addEventListener("mousemove", function(e) {
var sideWidth = document.querySelector("#mapSide").offsetWidth;
var headHeight = document.querySelector("#system").offsetHeight + 32;
var moveX = e.pageX - mapBlanket.offsetLeft - sideWidth + mapBlanket.parentNode.scrollLeft;
var moveY = e.pageY - mapBlanket.offsetTop - headHeight + mapBlanket.parentNode.scrollTop;
// use move coordinates, in conjunction with a modulo, to find the tile coordinates
var tileX = moveX - (moveX % map.grid);
var tileY = moveY - (moveY % map.grid);
// Display the choiceMark div over the chosen tile (this is pretty messy):
document.getElementById("paintMark").setAttribute(
"style",
"width:" + map.grid +
"px; height:" + map.grid +
"px; top:" + (tileY) +
"px; left:" + (tileX) + "px; display:block;"
);
}, false);
mapBlanket.addEventListener("mouseout", function() {
document.getElementById("paintMark").setAttribute("style", "display:none;");
}, false);
// Painting the chosen tile onto the selected layer's canvas
mapBlanket.addEventListener("mousedown", tilePaint, false);
function tilePaint(e) {
var sideWidth = document.getElementById("mapSide").offsetWidth;
var headHeight = document.getElementById("system").offsetHeight + 32;
var clickX = e.pageX - mapBlanket.offsetLeft - sideWidth + mapBlanket.parentNode.scrollLeft;
var clickY = e.pageY - mapBlanket.offsetTop - headHeight + mapBlanket.parentNode.scrollTop;
var tileX = clickX - (clickX % map.grid);
var tileY = clickY - (clickY % map.grid);
if (paintOn == 5) {
eventThis(tileX, tileY);
} else if (paintOn < 5) {
paintThis(tileX, tileY);
}
mapBlanket.addEventListener("mousemove", clickDrag, false);
function clickDrag(e) {
var sideWidth = document.getElementById("mapSide").offsetWidth;
var headHeight = document.getElementById("system").offsetHeight + 32;
var clickX = e.pageX - mapBlanket.offsetLeft - sideWidth + mapBlanket.parentNode.scrollLeft;
var clickY = e.pageY - mapBlanket.offsetTop - headHeight + mapBlanket.parentNode.scrollTop;
var tileX = clickX - (clickX % map.grid);
var tileY = clickY - (clickY % map.grid);
if (paintOn < 5) {
paintThis(tileX, tileY);
}
mapBlanket.addEventListener("mouseup", function(e) {
mapBlanket.removeEventListener("mousemove", clickDrag, false);
}, false);
mapBlanket.addEventListener("mouseout", function(e) {
mapBlanket.removeEventListener("mousemove", clickDrag, false);
}, false);
}
mapBlanket.addEventListener("mouseup", function(e) {
mapBlanket.removeEventListener("mousemove", clickDrag, false);
}, false);
};
// Function to paint tile on active layer
function paintThis(tileX, tileY) {
// get all tiles in the tiles array for the current layer
var tilesLength = layerTiles.length;
for (var i = tilesLength - 1; i >= 0; i--) {
if (paintOn == 1) {
var tile = layer1Tiles[i];
} else if (paintOn == 2) {
var tile = layer2Tiles[i];
} else if (paintOn == 3) {
var tile = layer3Tiles[i];
} else if (paintOn == 4) {
var tile = layer4Tiles[i];
}
if (isAutotile === false) {
// match click coordinates with a tile's coordinates
if (tile.x == tileX && tile.y == tileY) {
// set tile.img to the coordinates of the tile wanted from the tileset canvas
tile.img = {
x: choiceX,
y: choiceY,
w: choiceW,
h: choiceH
};
// clear occupying rectangle, then fill with chosen tile(s):
ctx.clearRect(tileX, tileY, choiceW, choiceH);
ctx.drawImage(tilesetImage, choiceX, choiceY, choiceW, choiceH, tileX, tileY, choiceW, choiceH);
}
} else {
//autotileThis(tile, tileX, tileY);
}
}
};
// Function to place an event down
function eventThis(tileX, tileY) {
document.querySelector("body").classList.add("eventing");
document.querySelector("#layer1Radio").classList.add("hidden");
document.querySelector("#layer2Radio").classList.add("hidden");
document.querySelector("#layer3Radio").classList.add("hidden");
document.querySelector("#layer4Radio").classList.add("hidden");
// get all tiles in the tiles array for the current layer
var tilesLength = layerTiles.length;
for (var i = tilesLength - 1; i >= 0; i--) {
var event = events[i];
// match click coordinates with a tile's coordinates
if (event.x == tileX && event.y == tileY) {
/*document.querySelector("#saveEvent").addEventListener("mousedown", function() {
saveE(event);
}, false);
function saveE(event) {
event.data = {
name: document.querySelector("#eventName").value,
img: null,
type: document.querySelector("#eventType").value,
fov: document.querySelector("#eFOV").value,
orientaion: document.querySelector("#eventOrientation").value,
trigger: 0,
functions: eventEditor.getSession().getValue()
};
}*/
eventing(events, event, tileX, tileY);
// clear occupying rectangle, then fill with chosen tile(s):
ctx.clearRect(tileX, tileY, choiceW, choiceH);
ctx.fillStyle = "rgba(255, 255, 255, 0.4)";
ctx.fillRect((event.x + 5), (event.y + 5), (map.grid - 10), (map.grid - 10));
ctx.strokeStyle = "rgba(255, 255, 255, 0.9)";
ctx.lineWidth = 2;
ctx.strokeRect((event.x + 4), (event.y + 4), (map.grid - 8), (map.grid - 8));
};
};
};
// Save the map
document.querySelector("#saveMap").addEventListener("mousedown", function(e) {
// Set layers as new variables so trimming only effects new arrrays
var l1t, l2t, l3t, l4t, le;
// Strip layers of their empty tile objects.
l1t = layer1Tiles.slice();
for (var i = l1t.length - 1; i >= 0; i--) {
if (l1t[i].img === null) {
l1t.splice(i, 1);
}
}
l2t = layer2Tiles.slice();
for (var i = l2t.length - 1; i >= 0; i--) {
if (l2t[i].img === null) {
l2t.splice(i, 1);
}
}
l3t = layer3Tiles.slice();
for (var i = l3t.length - 1; i >= 0; i--) {
if (l3t[i].img === null) {
l3t.splice(i, 1);
}
}
l4t = layer4Tiles.slice();
for (var i = l4t.length - 1; i >= 0; i--) {
if (l4t[i].img === null) {
l4t.splice(i, 1);
}
}
le = events.slice();
for (var i = le.length - 1; i >= 0; i--) {
if (le[i].data === null) {
le.splice(i, 1);
}
}
map.layer1 = l1t;
map.layer2 = l2t;
map.layer3 = l3t;
map.layer4 = l4t;
var mapJSON = JSON.stringify(map, undefined, 4);
$.ajax({
url: "/savemap",
type: "POST",
dataType: "json",
data: mapJSON,
contentType: "application/json",
cache: false,
success: function(data) {
console.log(data);
}
});
}, false);
// Loading the map
// If tiles already exist, paint them onto the canvas
if (loadTiles === true) {
var tilesExist = map.layer1.length + map.layer2.length + map.layer3.length + map.layer4.length;
if (tilesExist > 0) {
console.log("Painting tiles...");
existingTiles(map);
};
};
// Load other map
/*document.querySelector("#maps").addEventListener("click", function(e) {
var chosenMap = e.target.getAttribute("data-map");
if (chosenMap !== null) {
$.ajax({
url: "project/maps/" + chosenMap + "/map.json",
dataType: "json",
data: {},
async: false,
success: function(data) {
map = data;
existingTiles(map);
document.querySelector("#maps ul li.active").classList.remove("active");
e.target.classList.add("active");
}
})
}
});*/
function existingTiles(map) {
infoPane();
tilesetImage.onload = function() {
for (var n = layer1Tiles.length - 1; n >= 0; n--) {
var newTile = layer1Tiles[n];
for (var o = map.layer1.length - 1; o >= 0; o--) {
var oldTile = map.layer1[o];
//console.log(oldTile, newTile);
if (newTile.x == oldTile.x && newTile.y == oldTile.y) {
newTile = oldTile;
layer1Tiles.splice(n, 1);
layer1Tiles.push(newTile);
function paintExisting1() {
ctx1.clearRect(0, 0, mapWidth, mapHeight);
ctx1.drawImage(tilesetImage, newTile.img.x, newTile.img.y, map.grid, map.grid, newTile.x, newTile.y, map.grid, map.grid);
}
paintExisting1();
};
};
};
for (var n = layer2Tiles.length - 1; n >= 0; n--) {
var newTile = layer2Tiles[n];
for (var o = map.layer2.length - 1; o >= 0; o--) {
var oldTile = map.layer2[o];
//console.log(oldTile, newTile);
if (newTile.x == oldTile.x && newTile.y == oldTile.y) {
newTile = oldTile;
layer2Tiles.splice(n, 1);
layer2Tiles.push(newTile);
function paintExisting2() {
ctx2.drawImage(tilesetImage, newTile.img.x, newTile.img.y, map.grid, map.grid, newTile.x, newTile.y, map.grid, map.grid);
}
paintExisting2();
};
};
};
for (var n = layer3Tiles.length - 1; n >= 0; n--) {
var newTile = layer3Tiles[n];
for (var o = map.layer3.length - 1; o >= 0; o--) {
var oldTile = map.layer3[o];
//console.log(oldTile, newTile);
if (newTile.x == oldTile.x && newTile.y == oldTile.y) {
newTile = oldTile;
layer3Tiles.splice(n, 1);
layer3Tiles.push(newTile);
function paintExisting3() {
ctx3.drawImage(tilesetImage, newTile.img.x, newTile.img.y, map.grid, map.grid, newTile.x, newTile.y, map.grid, map.grid);
}
paintExisting3();
};
};
};
for (var n = layer4Tiles.length - 1; n >= 0; n--) {
var newTile = layer4Tiles[n];
for (var o = map.layer4.length - 1; o >= 0; o--) {
var oldTile = map.layer4[o];
//console.log(oldTile, newTile);
if (newTile.x == oldTile.x && newTile.y == oldTile.y) {
newTile = oldTile;
layer4Tiles.splice(n, 1);
layer4Tiles.push(newTile);
function paintExisting4() {
ctx4.drawImage(tilesetImage, newTile.img.x, newTile.img.y, map.grid, map.grid, newTile.x, newTile.y, map.grid, map.grid);
}
paintExisting4();
};
};
};
};
console.log("> Tiles painted.");
};
};