-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
472 lines (409 loc) · 11.5 KB
/
sketch.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
// module aliases
Matter.use('matter-collision-events');
var Engine = Matter.Engine,
// Render = Matter.Render, Not needed rendering is done by p5.js
World = Matter.World,
Bodies = Matter.Bodies,
Composites = Matter.Composites;
Svg = Matter.Svg;
Constraint = Matter.Constraint,
Events = Matter.Events;
Runner = Matter.Runner;
var engine;
var world;
var runEngine = false;
var succesTimer = null;
var level = null;
var elements = [];
var helper = new Helper();
var numberOfBeams = 0;
var constructionHistory = [];
var gridIsOn = false;
const gridSize = 50;
const FPS = 50;
const CANVAS_WIDTH = 1200;
const CANVAS_HEIGHT = 650;
function setup() {
var p5Canvas = createCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
p5Canvas.parent("canvasContainer");
frameRate(FPS);
gridIsOn = false;
if(engine){
World.clear(engine.world);
Engine.clear(engine);
}
engine = Engine.create();
//engine.constraintIterations = 5;
world = engine.world;
loadLevel('Level00');
}
function testConstruction() {
runEngine = true;
level.bodyHitByPart = false;
level.doCatastrophe();
this.succesTimer = setTimeout(successOrFailModal, level.timeToSuccess);
}
function successOrFailModal(){
if(level.bodyHitByPart){
var failure_modal = document.getElementById("failure");
failure_modal.style.display = "flex";
runEngine = false;
}
else{
var success_modal = document.getElementById("success");
success_modal.style.display = "flex";
runEngine = false;
}
}
function nextLevel(){
var success_modal = document.getElementById("success");
success_modal.style.display = "none";
var failure_modal = document.getElementById("failure");
failure_modal.style.display = "none";
if(level.nextLevel != null){
var select_box = document.getElementById("selectLevel");
var select_options = document.getElementsByTagName("option");
console.log(select_options);
for(option of select_options)
{
console.log(option.value + ":" + level.nextLevel);
if(option.value == level.nextLevel)
{
//option.dispatchEvent(new Event('click'));
toggle_level(option, 0);
console.log("found level");
break;
}
}
/*select_box.value = level.nextLevel;
select_box.dispatchEvent(new Event('change'));*/
}
else{
//loadLevel("Finish")
//insert Finish screen
}
}
function repeatLevel(){
var success_modal = document.getElementById("success");
success_modal.style.display = "none";
var failure_modal = document.getElementById("failure");
failure_modal.style.display = "none";
//loadLevel();
construct();
}
function undo() {
if (constructionHistory.length == 0) {
return;
}
numberOfBeams--;
document.getElementById('beamsToGo').innerHTML = level.maxBeams - numberOfBeams;
constructionHistory.pop();
construct();
}
function construct() {
var success_modal = document.getElementById("success");
success_modal.style.display = "none";
var failure_modal = document.getElementById("failure");
failure_modal.style.display = "none";
clearTimeout(this.succesTimer);
runEngine = false;
elements.forEach(item => item.remove());
elements = [];
level.clearOthers();
level.clearHumans();
level.setupHumans();
level.resetStaticJoints();
redrawFromHistory();
}
function loadLevel(levelString = null) {
var e = document.getElementById('selectLevel');
var levelClassString = levelString === null ? e.options[e.selectedIndex].value : levelString;
clearAll();
switch (levelClassString) {
case 'Level00':
this.level = new Level00(this.width, this.height, this.bodyHit);
break;
case 'Level01':
this.level = new Level01(this.width, this.height, this.bodyHit);
break;
case 'Level02':
this.level = new Level02(this.width, this.height, this.bodyHit);
break;
case 'Level03':
this.level = new Level03(this.width, this.height, this.bodyHit);
break;
case 'Level04':
this.level = new Level04(this.width, this.height, this.bodyHit);
break;
case 'Level05':
this.level = new Level05(this.width, this.height, this.bodyHit);
break;
case 'Level06':
this.level = new Level06(this.width, this.height, this.bodyHit);
break;
case 'Level07':
this.level = new Level07(this.width, this.height, this.bodyHit);
break;
case 'Level08':
this.level = new Level08(this.width, this.height, this.bodyHit);
break;
case 'Level09':
level = new Level09(this.width, this.height, this.bodyHit);
break;
case 'Level10':
level = new Level10(this.width, this.height, this.bodyHit);
break;
default:
this.level = new Level01(this.width, this.height, this.bodyHit);
break;
}
this.level.setup();
this.level.setupHumans();
}
function checkMouseOnBody() {
level.anchors.forEach(item => {
item.mouseOnBody = item.pointIsIn(mouseX, mouseY);
});
elements.forEach(item => {
item.mouseOnBody = item.pointIsIn(mouseX, mouseY);
});
}
function bodyHit() {
level.bodyHitByPart = true;
if(this.succesTimer){
clearTimeout(this.succesTimer);
}
}
var mouseDraggedX = -1;
var mouseDraggedY = -1;
function mouseDragged() {
if (mousePressedX >= 0 && mousePressedY >= 0) {
drawing = true;
drawingLegal = false;
mouseDraggedX = mouseX;
mouseDraggedY = mouseY;
var sorted = helper.sortTwoPoints(mousePressedX, mousePressedY, mouseX, mouseY);
var calc = helper.doBasicCalculations(sorted.firstPoint, sorted.secondPoint);
drawingLegal = calc.c > 60 && calc.c <= 250;
if (level.pointIsInGround(mouseDraggedX, mouseDraggedY)) {
drawingLegal = false;
}
}
checkMouseOnBody();
}
function mouseMoved() {
checkMouseOnBody();
}
function mouseClicked() {
elements.forEach(item => {
if (item.pointIsIn(mouseX, mouseY)) {
item.mouseClicked();
}
});
}
var mousePressedX = -1;
var mousePressedY = -1;
var drawing = false;
var drawingLegal = false;
var bodyA = null;
function mousePressed() {
var jointPerPoint = getJointPerPoint(mouseX, mouseY);
if (jointPerPoint !== null) {
mousePressedX = jointPerPoint.x;
mousePressedY = jointPerPoint.y;
bodyA = jointPerPoint.body;
} else {
mousePressedX = -1;
mousePressedY = -1;
bodyA = null;
}
}
function getJointPerPoint(x, y) {
var anchors = level.anchors.filter(anchor => anchor.pointIsIn(x, y));
var joints = elements.filter(anchor => {
return anchor instanceof Joint && anchor.pointIsIn(x, y)
});
var items = anchors.concat(joints);
if (items.length >= 1) {
return {
x: items[0].body.position.x,
y: items[0].body.position.y,
body: items[0].body
};
}
return null;
}
function getGridCoords(mouseX, mouseY) {
if (gridIsOn)
return {
x: (Math.round(mouseX / gridSize) * gridSize),
y: (Math.round(mouseY / gridSize) * gridSize)
};
else
return {
x: mouseX,
y: mouseY
}
}
function mouseReleased() {
if (mousePressedX < 0 || mousePressedY < 0
|| drawing == false || drawingLegal == false || runEngine == true) {
drawing = false;
return;
}
mouse = getGridCoords(mouseX, mouseY);
var bodyC = null;
var jointPerPoint = getJointPerPoint(mouse.x, mouse.y);
if (jointPerPoint !== null) {
mouse.x = jointPerPoint.x;
mouse.y = jointPerPoint.y;
bodyC = jointPerPoint.body;
}
drawing = false;
var sorted = helper.sortTwoPoints(mousePressedX, mousePressedY, mouse.x, mouse.y);
var calc = helper.doBasicCalculations(sorted.firstPoint, sorted.secondPoint);
var sb = new SteelBeam(calc.x, calc.y, calc.c, 15, calc.angle);
numberOfBeams++;
document.getElementById('beamsToGo').innerHTML = level.maxBeams - numberOfBeams;
elements.push(sb);
if (bodyC === null) {
var j = new Joint(mouse.x, mouse.y, 10);
bodyC = j.body;
elements.push(j);
}
createJointConstraints(bodyA, sb.body, bodyC, calc, sorted.orient);
addToHistory(bodyA.position.x, bodyA.position.y, calc, sorted.orient, mouse.x, mouse.y);
}
function createJointConstraints(bodyA, bodyB, bodyC, calc, orient) {
var c1 = Constraint.create({
bodyA: bodyA,
bodyB: bodyB,
stiffness: 0.92,
length: 0.0,
pointA: {
x: 0,
y: 0
},
pointB: {
x: orient == 'down' ? -calc.a / 2 : calc.a / 2,
y: orient == 'down' ? -calc.b / 2 : calc.b / 2
}
});
var c2 = Constraint.create({
bodyA: bodyB,
bodyB: bodyC,
stiffness: 0.92,
length: 0,
pointA: {
x: orient == 'down' ? calc.a / 2 : -calc.a / 2,
y: orient == 'down' ? calc.b / 2 : -calc.b / 2
},
pointB: {
x: 0,
y: 0
}
});
World.add(world, c1);
World.add(world, c2);
}
function redrawFromHistory() {
constructionHistory.forEach(historyElement => {
var calc = historyElement.calc;
var sb = new SteelBeam(calc.x, calc.y, calc.c, 15, calc.angle);
elements.push(sb);
var jointA = getJointPerPoint(historyElement.bodyAx, historyElement.bodyAy);
var bodyC = null;
var jointPerPoint = getJointPerPoint(historyElement.jointX, historyElement.jointY);
if (jointPerPoint !== null) {
bodyC = jointPerPoint.body;
} else {
var j = new Joint(historyElement.jointX, historyElement.jointY, 10);
bodyC = j.body;
elements.push(j);
}
createJointConstraints(jointA.body, sb.body, bodyC, calc, historyElement.orient);
});
}
function addToHistory(bodyAx, bodyAy, calc, orient, jointX, jointY) {
constructionHistory.push({
bodyAx: bodyAx,
bodyAy: bodyAy,
calc: calc,
orient: orient,
jointX: jointX,
jointY: jointY
});
}
function keyPressed() {
}
function draw() {
if (runEngine) {
Engine.update(engine);
testBreakage();
}
level.show();
elements.forEach(item => item.show());
gridIsOn = keyIsDown(SHIFT);
if (gridIsOn === undefined) {
gridIsOn = false;
}
if (gridIsOn) {
push();
var lines_x = level.width / gridSize;
var lines_y = level.height / gridSize;
for (var lineNr = 0; lineNr < lines_x; lineNr++) {
strokeWeight(1);
stroke('black');
line(lineNr * gridSize, 0, lineNr * gridSize, level.height);
}
for (var lineNr = 0; lineNr < lines_y; lineNr++) {
strokeWeight(1);
stroke('black');
line(0, lineNr * gridSize, level.width, lineNr * gridSize);
}
pop();
}
if (drawing == true && runEngine == false) {
if (numberOfBeams >= level.maxBeams) {
drawing = false;
drawingLegal = false;
showMaxBeamsModal();
/*alert("You have reached the maximum allowed number of beams for this level!"
+ " Please test your construction or undo and try again.");*/
return;
}
push();
strokeWeight(3);
drawingLegal ? stroke('green') : stroke('red');
grid_mouseDragged = getGridCoords(mouseDraggedX, mouseDraggedY);
line(mousePressedX, mousePressedY, grid_mouseDragged.x, grid_mouseDragged.y);
pop();
}
}
function showMaxBeamsModal()
{
var maxbeams_modal = document.getElementById("max_beams");
maxbeams_modal.style.display = "flex";
}
function closeModal(modal)
{
var modal = document.getElementById(modal);
modal.style.display = "none";
}
function clearAll() {
runEngine = false;
elements.forEach(item => item.remove());
elements = [];
constructionHistory = [];
numberOfBeams = 0;
if (level !== null) {
level.clear();
}
}
function testBreakage() {
var breakageAngularSpeed = 0.11;
var brokeConstraints = world.constraints.filter(c => {
return c.bodyA.angularSpeed > breakageAngularSpeed || c.bodyB.angularSpeed > breakageAngularSpeed
});
brokeConstraints.forEach(c => World.remove(world, c));
}