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 pathSupport.js
93 lines (78 loc) · 2.51 KB
/
Support.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
/**
* Support class, an object to represent the supports of the track.
* @param data the supportDataObj from Constant.js
* @param piece the Piece that this is placed next to. See Piece.js
* @constructor
*/
function Support (data, piece){
// the height of the support
this.height = piece.y - GROUND_HEIGHT + data.heightOffset;
// the material all supports are made of
var material = MATERIAL_SUPPORT;
// the geometry of the cylinder, defined by the radius and the height
var geometry = new THREE.CylinderGeometry(
data.radius,
data.radius,
this.height,
32
);
this.mesh = new THREE.Mesh(geometry, material);
// set the position
this.setPosition(data, piece);
this.boundingBox = new THREE.BoxHelper(this.mesh);
scene.add(this.mesh);
scene.add(this.boundingBox);
this.boundingBox.visible = TRACK.boxes;
piece.supports.push(this);
}
/**
* Sets the position of the support as appropriate based on facing of the
* parent piece. Takes the data and the piece as an argument.
*
* @param data: the data object for the support
* @param piece: the parent piece
*/
Support.prototype.setPosition = function(data, piece){
// The y will always be the same regardless of scale.
this.mesh.position.y = (this.height / 2) + GROUND_HEIGHT;
switch(piece.facing){
case "forward":
this.mesh.position.x = piece.x + data.x;
this.mesh.position.z = piece.z - data.z;
break;
case "back":
this.mesh.position.x = piece.x - data.x;
this.mesh.position.z = piece.z + data.z;
break;
case "left":
this.mesh.position.x = piece.x - data.z;
this.mesh.position.z = piece.z - data.x;
break;
case "right":
this.mesh.position.x = piece.x + data.z;
this.mesh.position.z = piece.z + data.x;
break;
default: throw "error, facing not one of the possible directions"
}
};
/**
* This function deletes the current support. It's called by piece as the piece
* is deleted.
*/
Support.prototype.delete = function(){
scene.remove(this.mesh);
scene.remove(this.boundingBox);
};
/**
* Toggles the bounding box the support.
*/
Support.prototype.toggleBox = function(){
this.boundingBox.visible = !this.boundingBox.visible;
};
/**
* Toggles the bounding box the support.
*/
Support.prototype.toggleVisibility = function(){
this.mesh.visible = !this.mesh.visible;
this.toggleBox();
};