forked from kholland4/platformeng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleveledit.js
330 lines (280 loc) · 9.36 KB
/
leveledit.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
var canvas;
var ctx;
var dSize = {"w": 0, "h": 0, "scale": 0.75};
var map;
var player;
var voffset = {"x": 0, "y": 0};
var dragging = false;
var dragTarget = null;
var lastSel = null;
var dragOffset = {"x": 0, "y": 0};
var mouse = {"x": 0, "y": 0};
var smouse = {"x": 0, "y": 0};
var dragSnap = false;
function screenToWorld(x, y) {
x = (x / dSize.scale) + voffset.x;
y = (y / dSize.scale) + voffset.y;
return {"x": x, "y": y};
}
function dragStart(e) {
if(e != undefined) { smouse.x = e.clientX; smouse.y = e.clientY; }
mouse = screenToWorld(smouse.x, smouse.y);
dragTarget = map.collide(new Box(mouse.x, mouse.y, 1, 1), false);
if(dragTarget === false) {
if(player.collide(new Box(mouse.x, mouse.y, 1, 1))) {
dragTarget = player;
} else {
dragTarget = null;
}
}
lastSel = dragTarget;
if(dragTarget != null) {
var p = dragTarget.getCenter();
dragOffset.x = p.x - mouse.x;
dragOffset.y = p.y - mouse.y;
dragging = true;
}
showPropsUI();
}
function dragStop(e) {
dragging = false;
dragTarget = null;
}
function dragUpdate(e) {
if(e != undefined) { smouse.x = e.clientX; smouse.y = e.clientY; }
mouse = screenToWorld(smouse.x, smouse.y);
if(dragging && dragTarget != null) {
if(dragSnap) {
if(dragTarget instanceof Box) {
var xd = ((dragTarget.w / 2) % 50);
var yd = ((dragTarget.h / 2) % 50);
dragTarget.setCenter({"x": (Math.round((mouse.x + dragOffset.x - xd) / 50) * 50) + xd, "y": (Math.round((mouse.y + dragOffset.y - yd) / 50) * 50) + yd});
} else {
dragTarget.setCenter({"x": Math.round((mouse.x + dragOffset.x) / 50) * 50, "y": Math.round((mouse.y + dragOffset.y) / 50) * 50});
}
} else {
dragTarget.setCenter({"x": mouse.x + dragOffset.x, "y": mouse.y + dragOffset.y});
}
}
if(document.activeElement == document.body) {
updatePropsUI();
}
}
function addObject(obj) {
mouse = screenToWorld(smouse.x, smouse.y);
obj.setCenter(mouse);
map.autoAddObject(obj);
dragStart();
}
var propsUITarget;
function showPropsUI(obj=lastSel) {
if(obj == null) { document.getElementById("sidepanel").style.display = "none"; return; }
propsUITarget = obj;
var objName = obj.constructor.name;
document.getElementById("sp-header").innerText = objName;
var container = document.getElementById("sp-main");
while(container.firstChild) { container.removeChild(container.firstChild); }
var col1 = document.createElement("div");
col1.className = "sp-col sp-col1";
container.appendChild(col1);
var col2 = document.createElement("div");
col2.className = "sp-col sp-col2";
container.appendChild(col2);
for(var key in obj) {
if(typeof obj[key] == "undefined" || typeof obj[key] == "function" || (typeof obj[key] == "object" && obj[key].constructor.name != "Object")) { continue; }
if(typeof obj[key] == "object") { continue; } //TODO
var label = document.createElement("div");
label.className = "sp-col-label";
label.innerText = key;
col1.appendChild(label);
var input;
if(typeof obj[key] != "boolean") {
input = document.createElement("input");
input.className = "sp-col-textbox";
if(typeof obj[key] == "number") { input.type = "number"; } else { input.type = "text"; }
input.value = obj[key];
} else {
input = document.createElement("select");
input.className = "sp-col-dropdown";
var opt = document.createElement("option"); opt.value = "true"; opt.innerText = "true"; input.appendChild(opt);
var opt = document.createElement("option"); opt.value = "false"; opt.innerText = "false"; input.appendChild(opt);
if(obj[key]) { input.value = "true"; } else { input.value = "false"; }
}
input.dataset.key = key;
input.dataset.type = typeof obj[key];
input.onchange = function() {
if(this.dataset.type == "string") {
propsUITarget[this.dataset.key] = String(this.value);
} else if(this.dataset.type == "number") {
propsUITarget[this.dataset.key] = parseInt(this.value);
} else if(this.dataset.type == "boolean") {
propsUITarget[this.dataset.key] = this.value.toLowerCase() == "true";
} else {
propsUITarget[this.dataset.key] = this.value;
}
};
col2.appendChild(input);
}
var btn = document.createElement("button");
btn.innerText = "Duplicate";
btn.title = "hotkey: v";
btn.onclick = function() {
if(lastSel != null) {
addObject(lastSel.constructor.deserialize(lastSel.serialize()));
}
};
container.appendChild(btn);
var btn = document.createElement("button");
btn.innerText = "Delete";
btn.title = "hotkey: x";
btn.onclick = function() {
if(lastSel != null) {
map.autoDelObject(lastSel);
lastSel = null;
showPropsUI();
}
};
container.appendChild(btn);
document.getElementById("sidepanel").style.display = "block";
}
function updatePropsUI(obj=lastSel) {
if(obj == null) { return; }
if(obj != propsUITarget) { showPropsUI(obj); return; }
//var objName = obj.constructor.name;
//document.getElementById("sp-header").innerText = objName;
var container = document.getElementById("sp-main");
var i = 0;
for(var key in obj) {
if(typeof obj[key] == "undefined" || typeof obj[key] == "function" || (typeof obj[key] == "object" && obj[key].constructor.name != "Object")) { continue; }
if(typeof obj[key] == "object") { continue; } //TODO
container.children[1].children[i].value = obj[key];
i++;
}
}
function lExportGame() {
var ospectate = player.spectate;
var ospeedMul = player.speedMul;
player.spectate = false;
player.speedMul = 2;
exportGame();
player.spectate = ospectate;
player.speedMul = ospeedMul;
}
function lImportGame() {
importGame(function() {
player.spectate = true;
player.speedMul = 4;
});
}
function addCtl(name) {
if(name == "box") {
addObject(new Box(0, 0, 300, 50));
} else if(name == "coin") {
addObject(new Coin(0, 0));
} else if(name == "annotation") {
addObject(new Annotation(0, 0, 100, 100, "image", imgs.unknown));
}
}
function init() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
canvas.addEventListener("mousedown", dragStart);
canvas.addEventListener("mouseup", dragStop);
canvas.addEventListener("mousemove", dragUpdate);
document.addEventListener("keydown", function(e) {
if(document.activeElement != document.body) { return; }
if(e.keyCode == 13) { //enter
if(player.spectate) {
player.spectate = false;
player.speedMul = 2;
} else {
player.spectate = true;
player.speedMul = 4;
}
player.speed.x = 0;
player.speed.y = 0;
} else if(e.key == "b") {
addCtl("box");
} else if(e.key == "c") {
addCtl("coin");
} else if(e.key == "n") {
addCtl("annotation");
} else if(e.key == "x") {
if(lastSel != null) {
map.autoDelObject(lastSel);
lastSel = null;
showPropsUI();
}
} else if(e.key == "v") {
if(lastSel != null) {
addObject(lastSel.constructor.deserialize(lastSel.serialize()));
}
} else if(e.keyCode == 17) { //ctrl
dragSnap = true;
}
});
document.addEventListener("keyup", function(e) {
if(document.activeElement != document.body) { return; }
if(e.keyCode == 17) { //ctrl
dragSnap = false;
}
});
resize();
window.addEventListener("resize", resize);
initImgLoader();
//disable WASD - TODO - just disable controls when menu focused
//delete keyMapping[87]; delete keyMapping[83]; delete keyMapping[65]; delete keyMapping[68];
initControls();
map = new Map();
map.background = imgs.bktile;
for(var i = 0; i < 3; i++) {
map.addBox(new Box(20 + (i * 500), 300 - (i * 150), 300, 50));
}
player = new Player(map, {speedMul: 4, spectate: true});
animate();
}
document.addEventListener("DOMContentLoaded", init);
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
dSize.w = window.innerWidth;
dSize.h = window.innerHeight;
}
var prevTime = performance.now();
var tCounter = 0;
var timelineOffset = 0;
var timeFlow = 1;
function animate() {
window.requestAnimationFrame(animate);
var time = performance.now();
var timeDelta = time - prevTime;
prevTime = time;
if(timeDelta > 500) {
timelineOffset += timeDelta - 450;
timeDelta = 50;
}
var timeScale = timeDelta / 1000;
var ttime = time - timelineOffset;
ctx.fillStyle = "white";
ctx.fillRect(0, 0, dSize.w, dSize.h);
voffset.x = player.pos.x - Math.round(dSize.w / 2 / dSize.scale);
//voffset.y = player.pos.y - Math.round(dSize.h / 2);
//var target = -Math.round(dSize.h / 2 / dSize.scale);
var amt = 3;
if(player.pos.y - (dSize.h / amt / dSize.scale) < voffset.y) {
voffset.y = player.pos.y - (dSize.h / amt / dSize.scale);
}
if(player.pos.y + (dSize.h / amt / dSize.scale) > voffset.y + (dSize.h / dSize.scale)) {
voffset.y = player.pos.y + (dSize.h * -(1 - (1/amt)) / dSize.scale);
}
if(timeFlow > 0) {
timeScale *= timeFlow;
map.animate(ttime, timeScale);
map.draw(ctx, voffset, dSize.scale);
player.animate(timeScale, getControls(), true);
player.draw(ctx, voffset, dSize.scale);
//player.coinCount += map.gatherCoins(player);
//map.interact(player);
}
if(getControls().any) { dragUpdate(); }
}