-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld-designer.js
323 lines (258 loc) · 8.16 KB
/
world-designer.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
let mousePos = { x: 0, y: 0 };
let designerWorld = { width: innerWidth, height: innerHeight, scale: 1, viewLeft: 0, viewTop: 0, lines: [], spawnPoint: {}};
let lineRemovalDistance = 20; // Pixels
let selectedTool = "add-line";
let mapCanvas, mapCtx;
window.addEventListener("load", function ()
{
mapCanvas = document.getElementById("map");
mapCtx = mapCanvas.getContext("2d");
setCanvasSize();
designerWorld.spawnPoint = {x: designerWorld.width / 2, y: designerWorld.height / 2};
let selectableTools = $("#remove-line, #add-line, #change-spawn");
selectableTools.click(function ()
{
selectedTool = $(this).attr("id");
selectableTools.removeClass("selected");
$(this).addClass("selected");
});
$("#change-world-size").click(function ()
{
let worldWidth = prompt("Please enter world width");
let worldHeight = prompt("Please enter world height");
designerWorld.height = parseInt(worldHeight);
designerWorld.width = parseInt(worldWidth);
});
$("#export-world").click(function ()
{
download(JSON.stringify(designerWorld), 'world.gannmap', 'text/plain');
});
$("#import-world").click(function ()
{
fileInput.click();
});
$("#reset-world").click(function ()
{
resetWorld();
});
$(mapCanvas).mousemove(canvasMouseMove);
$(mapCanvas).mousedown(canvasMouseDown);
$(mapCanvas).mouseup(canvasMouseUp);
$(mapCanvas).on('touchmove',canvasMouseMove);
$(mapCanvas).on('touchstart',canvasMouseDown);
$(mapCanvas).on('touchend',canvasMouseUp);
var inputElement = document.createElement('div');
inputElement.innerHTML = '<input type="file">';
var fileInput = inputElement.firstChild;
fileInput.addEventListener('change', function ()
{
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function ()
{
let jsonData = JSON.parse(reader.result);
designerWorld = jsonData;
updateMapStatusMode(true);
};
reader.readAsText(file);
});
render();
});
window.addEventListener("resize", setCanvasSize);
let drawingLine = null;
let mouseDown = false;
let mouseDragging = false;
let mouseDownPos = null;
let initialViewLeft = 0, initialViewTop = 0;
function canvasMouseDown(e)
{
mouseDown = true;
mouseDownPos = { x: e.pageX, y: e.pageY };
initialViewLeft = designerWorld.viewLeft;
initialViewTop = designerWorld.viewTop;
}
function canvasMouseUp(e)
{
mouseDown = false;
if (!mouseDragging)
{
canvasClick(e);
}
mouseDragging = false;
}
function canvasMouseMove(e)
{
mousePos.x = e.pageX;
mousePos.y = e.pageY;
if (mouseDown && Math.max(Math.abs(mouseDownPos.x - mousePos.x), Math.abs(mouseDownPos.y - mousePos.y)) > 2)
mouseDragging = true;
if (mouseDragging)
{
designerWorld.viewLeft = initialViewLeft + (mouseDownPos.x - mousePos.x);
designerWorld.viewTop = initialViewTop + (mouseDownPos.y - mousePos.y);
}
}
function canvasClick(e)
{
let clickPoint = { x: e.pageX, y: e.pageY };
if (selectedTool == "add-line")
{
if (drawingLine && drawingLine.from)
{
updateMapStatusMode(true);
let lines = designerWorld.lines;
lines.push({ from: { x: drawingLine.from.x, y: drawingLine.from.y }, to: { x: mousePos.x + designerWorld.viewLeft, y: mousePos.y + designerWorld.viewTop } })
drawingLine = null;
}
else
{
drawingLine = { from: { x: e.pageX + designerWorld.viewLeft, y: e.pageY + designerWorld.viewTop } };
}
}
else if (selectedTool == "remove-line")
{
let lines = designerWorld.lines;
for (let i = 0; i < lines.length; i++)
{
let line = lines[i];
let mouseToLineDist = pDistance(clickPoint.x + designerWorld.viewLeft, clickPoint.y + designerWorld.viewTop, line.from.x, line.from.y, line.to.x, line.to.y);
if (mouseToLineDist < lineRemovalDistance)
{
lines.splice(i, 1);
break;
}
}
}
else if (selectedTool == "change-spawn")
{
let newSpawnX = clickPoint.x + designerWorld.viewLeft,
newSpawnY = clickPoint.y + designerWorld.viewTop;
if(newSpawnX <= 0 || newSpawnX >= designerWorld.width || newSpawnY <= 0 || newSpawnY >= designerWorld.height)
alert("Spawn point should be set inside the world");
else
designerWorld.spawnPoint = {
x: newSpawnX,
y: newSpawnY
};
}
}
function updateMapStatusMode(isCustom)
{
/*
"Auto Generate": 0,
"User Custom": 1
*/
options.mapPreset = isCustom ? 1 : 0;
}
function resetWorld()
{
updateMapStatusMode(false);
designerWorld.lines = [];
designerWorld.spawnPoint = {x: designerWorld.width / 2, y: designerWorld.height / 2};
}
function setCanvasSize()
{
mapCanvas.height = innerHeight;
mapCanvas.width = innerWidth;
}
function render()
{
requestAnimationFrame(render);
if(viewMode != "design")
return;
mapCtx.clearRect(0, 0, innerWidth, innerHeight);
let lines = designerWorld.lines;
lines.forEach(function (line)
{
let lineFrom = line.from;
let lineTo = line.to;
mapCtx.beginPath();
mapCtx.moveTo(lineFrom.x - designerWorld.viewLeft, lineFrom.y - designerWorld.viewTop);
mapCtx.lineTo(lineTo.x - designerWorld.viewLeft, lineTo.y - designerWorld.viewTop);
mapCtx.closePath();
mapCtx.stroke();
});
if (selectedTool == "remove-line")
{
lines.forEach(function (line)
{
let lineFrom = line.from;
let lineTo = line.to;
mapCtx.beginPath();
mapCtx.moveTo(lineFrom.x - designerWorld.viewLeft, lineFrom.y - designerWorld.viewTop);
mapCtx.lineTo(lineTo.x - designerWorld.viewLeft, lineTo.y - designerWorld.viewTop);
mapCtx.closePath();
mapCtx.lineWidth = lineRemovalDistance;
mapCtx.strokeStyle = "rgba(0, 0, 50, 0.6)";
mapCtx.stroke();
});
// Reset ctx props
mapCtx.lineWidth = 1;
mapCtx.strokeStyle = "black";
}
let spawnX = designerWorld.spawnPoint.x, spawnY = designerWorld.spawnPoint.y;
mapCtx.beginPath();
mapCtx.arc(spawnX - designerWorld.viewLeft, spawnY - designerWorld.viewTop, 20, 0, Math.PI * 2);
mapCtx.closePath();
mapCtx.fillStyle = "rgba(100, 100, 255, 0.6)";
mapCtx.fill();
mapCtx.beginPath();
mapCtx.arc(spawnX - designerWorld.viewLeft, spawnY - designerWorld.viewTop, 10, 0, Math.PI * 2);
mapCtx.closePath();
mapCtx.fillStyle = "rgba(100, 100, 255, 1)";
mapCtx.fill();
if (drawingLine)
{
mapCtx.beginPath();
mapCtx.moveTo(drawingLine.from.x - designerWorld.viewLeft, drawingLine.from.y - designerWorld.viewTop);
mapCtx.lineTo(mousePos.x, mousePos.y);
mapCtx.closePath();
mapCtx.stroke();
}
// Draw world boundries
mapCtx.beginPath();
mapCtx.rect(-designerWorld.viewLeft, -designerWorld.viewTop, designerWorld.width, designerWorld.height);
mapCtx.closePath();
mapCtx.stroke();
}
// https://stackoverflow.com/a/6853926
function pDistance(x, y, x1, y1, x2, y2)
{
var A = x - x1;
var B = y - y1;
var C = x2 - x1;
var D = y2 - y1;
var dot = A * C + B * D;
var len_sq = C * C + D * D;
var param = -1;
if (len_sq != 0) //in case of 0 length line
param = dot / len_sq;
var xx, yy;
if (param < 0)
{
xx = x1;
yy = y1;
}
else if (param > 1)
{
xx = x2;
yy = y2;
}
else
{
xx = x1 + param * C;
yy = y1 + param * D;
}
var dx = x - xx;
var dy = y - yy;
return Math.sqrt(dx * dx + dy * dy);
}
// https://stackoverflow.com/a/34156339
function download(content, fileName, contentType)
{
var a = document.createElement("a");
var file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}