-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsme.js
127 lines (102 loc) · 3.93 KB
/
jsme.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
const protocol = window.location.protocol +'//',
host = protocol + window.location.hostname,
path = window.location.pathname.split('/').slice(0, -1).join('/'),
tile_dir = path +'/assets/tiles/';
let file_ext = '.png',
active_tile;
let getFiles = (xml) => {
let parse = new DOMParser(),
xmlRes = parse.parseFromString(xml.responseText, 'text/html'),
list = xmlRes.getElementsByTagName('a');
for (let i = 0; i < list.length; i++) {
let file = list[i].innerHTML.trim();
if (file.substr(-4) === '.png') {
document.getElementById('assets').innerHTML += '<div class="tile_asset '+ file +'"><img src="'+ tile_dir + file +'"></div>'
}
}
};
// Load assets
(() => {
let xhr = new XMLHttpRequest();
xhr.open('GET', tile_dir, false);
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == '200') {
getFiles(xhr);
console.log('Assets loaded...');
}
};
xhr.send(null);
})();
let removeClass = (el, ev, className) => {
for (let i = 0; i < el.length; i++) {
el[i].addEventListener(ev, function(e) {
e.preventDefault();
for (let i = 0; i < el.length; i++) {
el[i].classList.remove(className);
}
});
}
}
let addClass = (el, ev, className) => {
for (let i = 0; i < el.length; i++) {
el[i].addEventListener(ev, function(e) {
e.preventDefault();
let current = this;
for (let i = 0; i < el.length; i++) {
if (current !== el[i]) {
current.classList.add(className);
}
}
});
}
}
const container = document.getElementById('container'),
tiles_div = document.getElementById('tiles');
let row, col, tile_size = 64;
let map_num_rows = 20,
map_num_cols = 20;
// Generate Map
(() => {
for (let i = 0; i < map_num_rows; i++) {
tiles_div.innerHTML += '<div id="row-'+ i +'" class="row" style="width: '+ tile_size * map_num_cols +'px; height: '+ tile_size +'px;"></div>';
for (let j = 0; j < map_num_cols; j++) {
document.getElementById('row-'+ i).innerHTML += '<div id="tile-x'+ j +'y'+ i +'" class="tile" style="display: inline-block; width: '+ tile_size +'px; height: '+ tile_size +'px; z-index: '+ (i + 1) +'" title="X: '+ j +'\nY: '+ i +'\nZ: '+ (i + 1) +'"></div>';
}
}
})();
let tiles = document.getElementsByClassName('tile');
for (let i = 0; i < tiles.length; i++) {
tiles[i].addEventListener('click', function(e) {
let tile_img = tiles[i].getElementsByTagName('img');
if (active_tile != undefined && tiles[i].children.length <= 1) {
tiles[i].innerHTML = '<img src="'+ active_tile +'" width="'+ tile_size +'">';
}
});
}
let fillAll = () => {
if (!active_tile) {
alert('Select an asset to fill the tiles');
return;
}
console.log('Filling map with '+ active_tile);
for (let i = 0; i < map_num_rows; i++) {
for (let j = 0; j < map_num_cols; j++) {
let tile = document.getElementById('tile-x'+ j +'y'+ i);
tile.innerHTML = '<img src="'+ active_tile +'" width="'+ tile_size +'">';
}
}
}
document.getElementById('fillAll').addEventListener('click', fillAll);
let clickAsset = function() {
removeClass(document.getElementsByClassName('tile_asset'), 'click', 'active');
addClass(document.getElementsByClassName('tile_asset'), 'click', 'active');
active_tile = this.children[0].src;
}
document.addEventListener('DOMContentLoaded', () => {
console.log('DOM loaded...');
let tile_assets = document.getElementsByClassName('tile_asset');
for (let i = 0; i < tile_assets.length; i++) {
tile_assets[i].setAttribute('id', 'tile_asset-'+ i);
tile_assets[i].addEventListener('click', clickAsset, false);
}
});