This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstant.js
637 lines (553 loc) · 16.6 KB
/
constant.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
"use strict";
// json template for track pieces
// the height and width of the track. NOTE: UNSCALED
var PIECE_HEIGHT = 16.7998;
var PIECE_WIDTH = 40.7995;
// helper function to clone objects since JS passes objects by reference
function cloneVector(object) {
return {
x: object.x,
y: object.y,
z: object.z
};
}
/**
* TrackConst is an object which makes all of the constants. It was chosen over
* a regular JSON object due to it's ability to reference itself
* @constructor just calls itself, shouldn't be called anywhere else
*/
function TrackConst() {
// call all functions for the tracks
this.FLAT = this.flat();
this.FLAT_TO_UP = this.flatToUp();
this.UP = this.up();
this.UP_TO_FLAT = this.upToFlat();
this.FLAT_TO_DOWN = this.flatToDown();
this.DOWN = this.down();
this.DOWN_TO_FLAT = this.downToFlat();
this.TURN_LEFT_SMALL = this.turnLeftSmall();
this.TURN_RIGHT_SMALL = this.turnRightSmall();
this.TURN_LEFT_BIG = this.turnLeftBig();
this.TURN_RIGHT_BIG = this.turnRightBig();
// END OF TRACK DECLARATIONS
// scale all tracks as appropriate
this.scale();
}
/*
* This is simple function to scale them all, I tried using a for loop, but it
* was all sorts of buggy.
*/
TrackConst.prototype.scale = function () {
this.FLAT.scale();
this.FLAT_TO_UP.scale();
this.UP.scale();
this.UP_TO_FLAT.scale();
this.FLAT_TO_DOWN.scale();
this.DOWN.scale();
this.DOWN_TO_FLAT.scale();
this.TURN_LEFT_SMALL.scale();
this.TURN_RIGHT_SMALL.scale();
this.TURN_LEFT_BIG.scale();
this.TURN_RIGHT_BIG.scale();
};
/*
* These are the functions for the constants, every constant has a function
* which generates an object of type TrackType (see below)
*/
// FLAT type ===================================================================
TrackConst.prototype.flat = function () {
var flat = new TrackType();
flat.name = "name";
flat.filename = "modelJS/straight.json";
flat.size = {
x: 60,
y: 16.7998,
z: 40.7995
};
flat.advanceAxis = {
x: 1,
y: 0,
z: 0
};
flat.centerOffset = { //offset to the center of the tube of the track piece. used for the animation line.
x: -PIECE_WIDTH * SCALE / 2,
y: PIECE_HEIGHT * SCALE,
z: 0
};
flat.extraPoints = []; //only used for turn peices. extra points to make curve smooth.
flat.name = "Flat";
flat.directionChange = "none";
var support = new SupportDataObj();
support.x = flat.size.x / 2;
support.z = flat.size.z / 2;
support.heightOffset = 4;
flat.supportData.push(support);
return flat;
};
// FLAT_TO_UP ==================================================================
TrackConst.prototype.flatToUp = function () {
var flatToUp = new TrackType();
flatToUp.name = "flat to up";
flatToUp.filename = "modelJS/slopeFlatToUp.json";
flatToUp.size = {
x: 64.3378,
y: 38.7466,
z: 40.8
};
flatToUp.advanceAxis = {
x: 1,
y: 1,
z: 0
};
flatToUp.centerOffset = {
x: -PIECE_WIDTH * SCALE / 2,
y: PIECE_HEIGHT * SCALE,
z: 0
};
flatToUp.extraPoints = [];
flatToUp.directionChange = "none";
var support = new SupportDataObj();
support.x = flatToUp.size.x / 2;
support.z = flatToUp.size.z / 2;
support.heightOffset = 6;
flatToUp.supportData.push(support);
return flatToUp;
};
// UP ==========================================================================
TrackConst.prototype.up = function () {
var up = new TrackType();
up.filename = "modelJS/up.json";
up.name = "Up";
up.size = {
x: 54.3057,
y: 54.3057,
z: 40.7995
};
up.startOffset = {
x: 11.88,
y: 11.88,
z: 0.0
};
// this is zero because the start offset compensates
up.endOffset = {
x: 0.0,
y: 0.0,
z: 0.0
};
up.centerOffset = {
x: -PIECE_WIDTH * SCALE / 2,
y: 0,
z: 0
};
up.extraPoints = [];
up.advanceAxis = cloneVector(this.FLAT_TO_UP.advanceAxis);
up.directionChange = "none";
var support = new SupportDataObj();
support.x = up.size.x / 2;
support.z = up.size.z / 2;
support.heightOffset = 20;
up.supportData.push(support);
return up;
};
// UP TO FLAT ==================================================================
TrackConst.prototype.upToFlat = function () {
var upToFlat = new TrackType();
upToFlat.name = "Up To Flat";
upToFlat.filename = "modelJS/slopeUpToFlat.json";
upToFlat.size = {
x: 52.4583,
y: 33.8259,
z: 40.8
};
upToFlat.startOffset = cloneVector(this.UP.startOffset);
// the end offset defaults work except y
upToFlat.endOffset.y = upToFlat.size.y - this.FLAT.size.y;
upToFlat.advanceAxis = {
x: 1,
y: 0,
z: 0
};
upToFlat.centerOffset = {
x: -PIECE_WIDTH * SCALE / 2,
y: 0,
z: 0
};
upToFlat.extraPoints = [];
upToFlat.directionChange = "none";
var support = new SupportDataObj();
support.x = upToFlat.size.x / 2;
support.z = upToFlat.size.z / 2;
support.heightOffset = 20;
upToFlat.supportData.push(support);
return upToFlat;
};
// FLAT TO DOWN ================================================================
TrackConst.prototype.flatToDown = function () {
var flatToDown = new TrackType();
flatToDown.name = "flat to down";
flatToDown.filename = "modelJS/slopeFlatToDown.json";
flatToDown.size = {
x: 52.4583,
y: 33.8259,
z: 40.8
};
flatToDown.centerOffset = {
x: -PIECE_WIDTH * SCALE / 2,
y: 20 * SCALE,
z: 0
};
flatToDown.extraPoints = [];
flatToDown.startOffset.y = (this.FLAT.size.y + 0.2);
flatToDown.advanceAxis.x = 1;
flatToDown.directionChange = "none";
var support = new SupportDataObj();
support.x = flatToDown.size.x / 2;
support.z = flatToDown.size.z / 2;
support.heightOffset = 20;
flatToDown.supportData.push(support);
return flatToDown;
};
// DOWN ========================================================================
TrackConst.prototype.down = function () {
var down = new TrackType();
down.name = "down";
down.filename = "modelJS/down.json";
down.size = {
x: 54.3057,
y: 54.3057,
z: 40.7995
};
down.startOffset.x = 11.88;
down.startOffset.y = down.size.y - 11.88;
down.endOffset.y = down.size.y;
down.advanceAxis = {
x: 1,
y: -1,
z: 0
};
down.centerOffset = {
x: -PIECE_WIDTH * SCALE / 2,
y: 50 * SCALE,
z: 0
};
down.extraPoints = [];
down.directionChange = "none";
var support = new SupportDataObj();
support.x = down.size.x / 2;
support.z = down.size.z / 2;
support.heightOffset = 20;
down.supportData.push(support);
return down;
};
// DOWN TO FLAT ================================================================
TrackConst.prototype.downToFlat = function () {
var downToFlat = new TrackType();
downToFlat.name = "down to flat";
downToFlat.filename = "modelJS/slopeDownToFlat.json";
downToFlat.size = cloneVector(this.FLAT_TO_UP.size);
downToFlat.startOffset = {
x: 12,
y: 26.6,
z: 0.0
};
downToFlat.centerOffset = {
x: -PIECE_WIDTH * SCALE / 2,
y: 35 * SCALE,
z: 0
};
downToFlat.extraPoints = [];
downToFlat.advanceAxis.x = 1;
downToFlat.directionChange = "none";
var support = new SupportDataObj();
support.x = downToFlat.size.x / 2;
support.z = downToFlat.size.z / 2;
support.heightOffset = 10;
downToFlat.supportData.push(support);
return downToFlat;
};
// SMALL LEFT TURN =============================================================
TrackConst.prototype.turnLeftSmall = function () {
var turnLeftSmall = new TrackType();
turnLeftSmall.name = "turn left small";
turnLeftSmall.filename = "modelJS/turnLeftSmall.json";
turnLeftSmall.size = {
x: 113.99999745190144,
y: 16.799999624490738,
z: 112.3539974886924
};
turnLeftSmall.advanceAxis = {
x: 1.0,
y: 0.0,
z: -1.0
};
turnLeftSmall.centerOffset = {
x: -PIECE_WIDTH * SCALE / 2,
y: PIECE_HEIGHT * SCALE,
z: 0
};
turnLeftSmall.extraPoints = [
{
x: 0.4,
z: -0.08
},
{
x: 0.8,
z: -0.4
}
];
turnLeftSmall.directionChange = "left";
var support1 = new SupportDataObj();
support1.x = 6;
support1.z = PIECE_WIDTH / 2;
support1.heightOffset = 2;
turnLeftSmall.supportData.push(support1);
var support2 = new SupportDataObj();
support2.x = turnLeftSmall.size.x - (PIECE_WIDTH / 2);
support2.z = turnLeftSmall.size.z - 6;
support2.heightOffset = 2;
turnLeftSmall.supportData.push(support2);
return turnLeftSmall;
};
// SMALL RIGHT TURN ============================================================
TrackConst.prototype.turnRightSmall = function () {
var turnRightSmall = new TrackType();
turnRightSmall.name = "turn right small";
turnRightSmall.filename = "modelJS/turnRightSmall.json";
turnRightSmall.size = cloneVector(this.TURN_LEFT_SMALL.size);
turnRightSmall.startOffset.z = (turnRightSmall.size.z - this.FLAT.size.z) * -1;
turnRightSmall.endOffset.x = this.FLAT.size.z * (-1);
turnRightSmall.advanceAxis = {
x: 1.0,
y: 0.0,
z: 0.0
};
turnRightSmall.centerOffset = {
x: -(turnRightSmall.size.x - PIECE_WIDTH/2) * SCALE,
y: PIECE_HEIGHT * SCALE,
z: 0
};
turnRightSmall.extraPoints = [
{
x: 0.4,
z: 0.08
},
{
x: 0.8,
z: 0.4
}
];
turnRightSmall.directionChange = "right";
var support1 = new SupportDataObj();
support1.x = 6;
support1.z = turnRightSmall.size.z - (PIECE_WIDTH / 2);
support1.heightOffset = 2;
turnRightSmall.supportData.push(support1);
var support2 = new SupportDataObj();
support2.x = turnRightSmall.size.x - (PIECE_WIDTH / 2);
support2.z = 6;
support2.heightOffset = 2;
turnRightSmall.supportData.push(support2);
return turnRightSmall;
};
TrackConst.prototype.turnLeftBig = function () {
var turnLeftBig = new TrackType();
turnLeftBig.name = "turn left big";
turnLeftBig.filename = "modelJS/turnLeftBig.json";
turnLeftBig.size = {
x: 185.99999584257603,
y: PIECE_HEIGHT,
z: 191.25699572507293
};
turnLeftBig.advanceAxis = {
x: 1.0,
y: 0.0,
z: -1.0
};
turnLeftBig.centerOffset = {
x: -PIECE_WIDTH/2 * SCALE,
y: PIECE_HEIGHT * SCALE,
z: 0
};
turnLeftBig.extraPoints = [
{
x: 0.75,
z: -0.15
},
{
x: 1.5,
z: -0.9
}
];
turnLeftBig.directionChange = "left";
var support1 = new SupportDataObj();
support1.x = 6;
support1.z = PIECE_WIDTH / 2;
support1.heightOffset = 2;
turnLeftBig.supportData.push(support1);
var support2 = new SupportDataObj();
support2.x = turnLeftBig.size.x - (PIECE_WIDTH / 2);
support2.z = turnLeftBig.size.z - 6;
support2.heightOffset = 2;
turnLeftBig.supportData.push(support2);
var support3 = new SupportDataObj();
// magic numbers... I'm so sorry. Doing something better would be math
support3.x = turnLeftBig.size.x / 2 + (PIECE_WIDTH / 2) + 6;
support3.z = turnLeftBig.size.z / 4 + (PIECE_WIDTH / 2);
support3.heightOffset = 2;
turnLeftBig.supportData.push(support3);
return turnLeftBig;
};
TrackConst.prototype.turnRightBig = function () {
var turnRightBig = new TrackType();
turnRightBig.name = "turn right big";
turnRightBig.filename = "modelJS/turnRightBig.json";
turnRightBig.size = {
x: 185.99999584257603,
y: PIECE_HEIGHT,
z: 191.25699572507293
};
turnRightBig.startOffset.z = (turnRightBig.size.z - this.FLAT.size.z) * -1;
turnRightBig.endOffset.x = this.FLAT.size.z * (-1);
turnRightBig.advanceAxis = {
x: 1.0,
y: 0.0,
z: 0.0
};
turnRightBig.centerOffset = {
x: -(turnRightBig.size.x - PIECE_WIDTH/2) * SCALE,
y: PIECE_HEIGHT * SCALE,
z: 0
};
turnRightBig.extraPoints = [
{
x: 0.75,
z: 0.15
},
{
x: 1.5,
z: 0.9
}
];
turnRightBig.directionChange = "right";
var support1 = new SupportDataObj();
support1.x = 6;
support1.z = turnRightBig.size.z - (PIECE_WIDTH / 2);
support1.heightOffset = 2;
turnRightBig.supportData.push(support1);
var support2 = new SupportDataObj();
support2.x = turnRightBig.size.x - (PIECE_WIDTH / 2);
support2.z = 6;
support2.heightOffset = 2;
turnRightBig.supportData.push(support2);
var support3 = new SupportDataObj();
support3.x = turnRightBig.size.x * 3 / 4 - (PIECE_WIDTH / 2);
support3.z = turnRightBig.size.z* 3 / 4 - (PIECE_WIDTH / 2);
support3.heightOffset = 2;
turnRightBig.supportData.push(support3);
return turnRightBig;
};
/**
* TrackType is a small class that has many of the fields of the class Piece,
* however it is only intended to generate an object to serve as children of
* TrackConst
*
* @constructor takes no arguments because of how specific every constant is,
* they're redefined in the functions of TrackConst
*/
function TrackType() {
this.name = ""; // name of the piece
this.filename = ""; // the path to the filename of the modelJSON
// the size of the piece (unscaled)
this.size = {
x: 0.0,
y: 0.0,
z: 0.0
};
// the offset that the piece must be moved back by in order to be in the
// right place. Is SUBTRACTED from Track.currentX, Y, and Z
this.startOffset = {
x: 0.0,
y: 0.0,
z: 0.0
};
// Similar to end offset, positioning to
this.endOffset = {
x: 0.0,
y: 0.0,
z: 0.0
};
/*
* Which way the track should advance for every axis relative to the piece
* should only be 1, -1, or 0 and never anything else
*/
this.advanceAxis = {
x: 0.0,
y: 0.0,
z: 0.0
};
// string which holds the direction the piece changes the track in english
this.directionChange = "";
// an array of objects of type SupportDataObj see said class below for more
this.supportData = [];
}
// Function to scale all fields, called on generation by TrackConst.scale()
TrackType.prototype.scale = function () {
function scaleVector(vector) {
var ret = {};
ret.x = vector.x * SCALE;
ret.y = vector.y * SCALE;
ret.z = vector.z * SCALE;
return ret;
}
this.size = scaleVector(this.size);
this.startOffset = scaleVector(this.startOffset);
this.endOffset = scaleVector(this.endOffset);
for (var i = 0; i < this.supportData.length; i++)
this.supportData[i].scale();
};
/**
* This class is meant to be a child of the TrackType class. It holds the data
* necessary to generate supports for the pieces in constants. Every piece will
* use different data, and some will make more supports than others. Hence why
* it's in an array
*
* @constructor does not do anything, but initialize to default values. The
* TrackConst class will make all values correct
*/
function SupportDataObj() {
this.intersect = 0;
// how high the piece should go past the bounding box to connect with the
// track
this.heightOffset = 0.0;
// the x, y, and z values of the support relative to it's parent
this.x = 0.0;
this.y = 0.0;
this.z = 0.0;
// the radius of the support (it's just a cylinder)
this.radius = 3;
}
// another scaling function, scales all fields
SupportDataObj.prototype.scale = function () {
this.intersect *= SCALE;
this.heightOffset *= SCALE;
this.x *= SCALE;
this.y *= SCALE;
this.z *= SCALE;
this.radius *= SCALE;
};
/**
* helper function that copies all fields of the object into a new one and
* returns it. Implemented because of JavaScript's tendency to pass by reference
*/
SupportDataObj.prototype.copy = function () {
var ret = {};
ret.intersect = this.intersect;
ret.heightOffset = this.heightOffset;
ret.x = this.x;
ret.y = this.y;
ret.z = this.z;
ret.radius = this.radius;
return ret;
};
// Declaring the global variable here. Executes all code in this file.
const TRACK_TYPES = new TrackConst();