-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.html
326 lines (290 loc) · 9.91 KB
/
edit.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>A Game made by Taha Chaudhry!</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- import the webpage's stylesheet -->
<style>
* {
box-sizing: border-box;
}
body, html {
margin: 0;
}
.app {
width: 100vw;
height: 100vh;
max-width: 100vw;
max-height: 100vh;
position: absolute;
top: 0;
left: 0;
display: grid;
grid-template-columns: 1fr auto;
overflow: hidden;
}
.map-area {
overflow: auto;
background: #ccc;
/* display: grid;
align-items:center;
justify-content: center; */
}
.map {
margin: 50px;
background: #fff;
border: 1px solid #666;
}
.tools {
border-left: 1px solid #888;
overflow: auto;
}
.tools > * {
display: block;
}
.tools > * + * {
margin-top: 1em;
}
label {
padding: 0 1em;
display: block;
}
#alpha {
/* display: none; */
}
</style>
<!-- import the webpage's javascript file -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.1/pixi.min.js"></script>
<!-- <script src="/editor.js" defer></script> -->
</head>
<body>
<div class="app">
<div class="map-area">
<canvas class="map"></canvas>
</div>
<div class="tools">
<canvas class="tiles"></canvas>
<form onsubmit="return false;" class="layer">
<label><input type="radio" name="layer" value="sky"> sky</label>
<label><input type="radio" name="layer" value="background"> background</label>
<label><input type="radio" name="layer" value="stage"> stage</label>
<label><input type="radio" name="layer" value="collision"> collision</label>
</form>
<button class="copy">
copy map data
</button>
<pre></pre>
</div>
</div>
<script>
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
function loadImage(path) {
return new Promise((resolve, reject) => {
let image = new Image();
image.onload = () => resolve(image);
image.onerror = reject;
image.src = path
});
}
function getTop(el) {
if (el.offsetParent) {
return getTop(el.offsetParent) + el.offsetTop;
}
return el.offsetTop;
}
function getLeft(el) {
if (el.offsetParent) {
return getLeft(el.offsetParent) + el.offsetLeft;
}
return el.offsetLeft;
}
Promise.all([
fetch('/map.json').then(r => r.json()),
loadImage('https://cdn.glitch.com/bf08baaa-913a-4fd3-af23-ba148998403d%2Fnature-paltformer-tileset-16x16.png?v=1562185449857')
]).then(init).catch(e => console.error(e));
const TILESIZE = 16;
const SCALE = 2;
let dbg = document.querySelector('pre');
let mapArea = document.querySelector('.map-area');
let layerSelect = document.querySelector('.layer').elements.layer;
let copyButton = document.querySelector('.copy');
let currentTile = 0;
function init([map, tileSet]) {
let tilesWidth = (tileSet.width / TILESIZE | 0);
let tileCount = tilesWidth * (tileSet.height / TILESIZE | 0);
let drawing = false;
let drawValue = null;
function draw(x, y) {
map[layerSelect.value][y * map.width + x] = drawValue;
}
function flood(x, y) {
let { width, height } = map;
let pos = (y * width + x);
let layer = map[layerSelect.value];
let sampledValue = layer[pos];
let floodValue = currentTile;
if (layerSelect.value === 'collision') {
floodValue = 1 - sampledValue;
}
let seen = new Set();
let stack = [pos];
while (stack.length) {
let p = stack.pop();
seen.add(p);
layer[p] = floodValue;
let x = p % width;
let y = p / width | 0;
if (!seen.has(p + 1) &&
x < width - 1 &&
layer[p + 1] === sampledValue) {
stack.push(p + 1);
}
if (!seen.has(p - 1) &&
x > 0 &&
layer[p - 1] === sampledValue) {
stack.push(p - 1);
}
if (!seen.has(p + width) &&
y < height - 1 &&
layer[p + width] === sampledValue) {
stack.push(p + width);
}
if (!seen.has(p - width) &&
y > 0 &&
layer[p - width] === sampledValue) {
stack.push(p - width);
}
}
}
function drawMap() {
mapCtx.clearRect(0, 0, mapCanvas.width, mapCanvas.height);
for (let i = 0; i < map.stage.length; i++) {
let x = i % map.width;
let y = i / map.width | 0;
if (map.sky[i] !== 12)
drawTile(x * TILESIZE * SCALE, y * TILESIZE * SCALE, map.sky[i]);
if (map.background[i] !== 12)
drawTile(x * TILESIZE * SCALE, y * TILESIZE * SCALE, map.background[i]);
if (map.stage[i] !== 12)
drawTile(x * TILESIZE * SCALE, y * TILESIZE * SCALE, map.stage[i]);
if (map.collision[i]) {
mapCtx.fillStyle = 'rgba(255,0,0,0.25)';
mapCtx.fillRect(x * TILESIZE * SCALE, y * TILESIZE * SCALE, TILESIZE * SCALE, TILESIZE * SCALE);
}
}
}
function drawTile(x, y, tile) {
mapCtx.drawImage(
tileSet,
(tile % tilesWidth) * TILESIZE,
(tile / tilesWidth | 0) * TILESIZE,
TILESIZE, TILESIZE,
x, y, TILESIZE * SCALE, TILESIZE * SCALE
);
}
function drawTiles() {
tileCtx.clearRect(0, 0, tileCanvas.width, tileCanvas.height);
tileCtx.drawImage(tileSet, 0, 0, tileCanvas.width, tileCanvas.height);
tileCtx.strokeStyle = 'red';
tileCtx.lineWidth = 2;
let x = (currentTile % tilesWidth) * TILESIZE * SCALE;
let y = (currentTile / tilesWidth | 0) * TILESIZE * SCALE;
tileCtx.strokeRect(x + .5, y + .5, TILESIZE * SCALE - 1, TILESIZE * SCALE - 1);
tileCtx.strokeStyle = '#000';
}
function getMapCoords(e) {
return [
(e.pageX - getLeft(mapCanvas) + mapArea.scrollLeft) / TILESIZE / SCALE | 0,
(e.pageY - getTop(mapCanvas) + mapArea.scrollTop) / TILESIZE / SCALE | 0,
];
}
function getTileCoords(e) {
return [
(e.pageX - getLeft(tileCanvas)) / TILESIZE / SCALE | 0,
(e.pageY - getTop(tileCanvas)) / TILESIZE / SCALE | 0,
];
}
let mapCanvas = document.querySelector('.map');
mapCanvas.width = map.width * TILESIZE * SCALE;
mapCanvas.height = map.height * TILESIZE * SCALE;
let mapCtx = mapCanvas.getContext('2d');
mapCtx.imageSmoothingEnabled = false;
drawMap();
mapCanvas.addEventListener('mousemove', function (e) {
let [x, y] = getMapCoords(e);
dbg.innerText = [x, y];
drawMap();
mapCtx.strokeRect(x * TILESIZE * SCALE, y * TILESIZE * SCALE, TILESIZE * SCALE, TILESIZE * SCALE);
if (drawing) {
draw(x, y);
drawMap();
}
});
mapCanvas.addEventListener('mousedown', function (e) {
if (shift) {
return;
}
drawing = true;
let [x, y] = getMapCoords(e);
let pos = y * map.width + x;
if (layerSelect.value === 'collision') {
drawValue = 1 - map.collision[pos];
} else {
drawValue = currentTile;
}
draw(x, y);
drawMap();
});
mapCanvas.addEventListener('mouseup', function (e) {
drawing = false;
});
mapCanvas.addEventListener('click', function (e) {
if (shift) {
let [x, y] = getMapCoords(e);
flood(x, y);
drawMap();
}
});
let shift = false;
window.addEventListener('keydown', function (e) {
if (e.key === 'Shift') {
shift = true;
}
});
window.addEventListener('keyup', function (e) {
if (e.key === 'Shift') {
shift = false;
}
});
let tileCanvas = document.querySelector('.tiles');
tileCanvas.width = tileSet.width * SCALE;
tileCanvas.height = tileSet.height * SCALE;
let tileCtx = tileCanvas.getContext('2d');
tileCtx.imageSmoothingEnabled = false;
drawTiles();
copyButton.addEventListener('click', e => {
copyToClipboard(JSON.stringify(map));
});
tileCanvas.addEventListener('mousemove', function (e) {
let [x, y] = getTileCoords(e);
drawTiles();
tileCtx.strokeRect(x * TILESIZE * SCALE, y * TILESIZE * SCALE, TILESIZE * SCALE, TILESIZE * SCALE);
});
tileCanvas.addEventListener('click', function (e) {
let [x, y] = getTileCoords(e);
currentTile = y * tilesWidth + x;
drawTiles();
});
}
</script>
</body>
</html>