1
+ const THREE = require ( 'three' ) ;
2
+ const Physijs = require ( 'physijs-webpack/browserify' ) ;
3
+
4
+ class FallingBrick {
5
+ static speed = 0.07 ;
6
+
7
+ constructor ( param = { } ) {
8
+ //===============
9
+ //---Parameter---
10
+ //===============
11
+ var defaultParam = {
12
+ size : new THREE . Vector3 ( 1 , 1 , 1 ) ,
13
+ position : new THREE . Vector3 ( 0 , 0 , 0 ) ,
14
+ scale : new THREE . Vector3 ( 5 , 1 , 5 ) ,
15
+ castShadow : true ,
16
+ receiveShadow : true ,
17
+ color : "hsl(220, 100%, 50%)" ,
18
+ direction : 'x' ,
19
+ }
20
+
21
+ // Melakukan assign dari parameter
22
+ // ke default parameter agar nilai parameter
23
+ // di dalam class berubah
24
+ this . params = Object . assign ( defaultParam , param ) ;
25
+ this . speed = FallingBrick . speed ;
26
+
27
+ this . geometry = new THREE . BoxGeometry ( this . params . size . x , this . params . size . y , this . params . size . z ) ;
28
+ this . material = new THREE . MeshLambertMaterial ( { color : this . params . color } ) ;
29
+
30
+ // Physics mesh
31
+ this . physijs_material = Physijs . createMaterial (
32
+ this . material ,
33
+ .7 ,
34
+ .3
35
+ ) ;
36
+ this . physijs_box = new Physijs . BoxMesh (
37
+ this . geometry ,
38
+ this . physijs_material
39
+ ) ;
40
+ this . physijs_box . collisions = 0 ;
41
+ this . physijs_box . name = this . physijs_box . uuid ;
42
+ this . physijs_box . castShadow = this . params . castShadow ;
43
+ this . physijs_box . receiveShadow = this . params . receiveShadow ;
44
+ this . physijs_box . position . copy ( this . params . position ) ;
45
+ this . physijs_box . scale . copy ( this . params . scale ) ;
46
+ console . log ( this . physijs_box ) ;
47
+
48
+ // this.mesh = new THREE.Mesh( this.geometry, this.material );
49
+ // this.mesh.name = this.mesh.uuid;
50
+ // this.mesh.castShadow = this.params.castShadow;
51
+ // this.mesh.receiveShadow = this.params.receiveShadow;
52
+ // this.mesh.position.copy(this.params.position);
53
+ // this.mesh.scale.copy(this.params.scale);
54
+ }
55
+
56
+ static increaseSpeed ( ) {
57
+ FallingBrick . speed += 0.001 ;
58
+ this . speed = FallingBrick . speed ;
59
+ }
60
+
61
+ static resetSpeed ( ) {
62
+ FallingBrick . speed = 0.07 ;
63
+ }
64
+
65
+ // Karena scene me-render mesh,
66
+ // agar lebih mudah memanggilnya dibuat fungsi saja
67
+ get build ( ) {
68
+ // return this.mesh;
69
+ return this . physijs_box ;
70
+ }
71
+
72
+ get name ( ) {
73
+ // return this.mesh.name;
74
+ return this . physijs_box . name ;
75
+ }
76
+
77
+ get position ( ) {
78
+ // return this.mesh.position;
79
+ return this . physijs_box . position ;
80
+ }
81
+
82
+ /**
83
+ * @param {THREE.Vector3 } newPosition
84
+ */
85
+ set position ( newPosition ) {
86
+ this . position . copy ( newPosition ) ;
87
+ }
88
+
89
+ get scale ( ) {
90
+ // return this.mesh.scale;
91
+ return this . physijs_box . scale ;
92
+ }
93
+
94
+ /**
95
+ * @param {THREE.Vector3 } newScale
96
+ */
97
+ set scale ( newScale ) {
98
+ this . scale . copy ( newScale ) ;
99
+ }
100
+
101
+ down ( ) {
102
+ // this.mesh.position.y -= 1;
103
+ this . physijs_box . position . y -= 1 ;
104
+ }
105
+
106
+ /**
107
+ * @param {Brick } prevBrick Balok sebagai perbandingan saat ini dengan sebelumnya
108
+ * @returns {boolean } Kondisi apakah dia masih bisa memotong atau tidak
109
+ */
110
+ cut ( prevBrick )
111
+ {
112
+ var prevPosition = prevBrick . position ;
113
+ var curScale = this . scale ;
114
+ var curPosition = this . position ;
115
+
116
+ var diffX = curPosition . x - prevPosition . x ;
117
+ var diffZ = curPosition . z - prevPosition . z ;
118
+
119
+ console . log ( "x : " + diffX , curScale . x ) ;
120
+ console . log ( "Z : " + diffZ , curScale . z ) ;
121
+
122
+ // Jika ukuran saat ini lebih kecil
123
+ // dari potongannya, maka error / gameover (return false)
124
+ if ( curScale . x < Math . abs ( diffX )
125
+ || curScale . z < Math . abs ( diffZ ) ) {
126
+ FallingBrick . resetSpeed ( ) ;
127
+ return false ;
128
+ }
129
+
130
+ curScale . x -= Math . abs ( diffX ) ;
131
+ curPosition . x -= ( diffX / 2 ) ;
132
+ curScale . z -= Math . abs ( diffZ ) ;
133
+ curPosition . z -= ( diffZ / 2 ) ;
134
+
135
+ FallingBrick . increaseSpeed ( ) ;
136
+ this . scale . copy ( curScale ) ;
137
+ return true ;
138
+ }
139
+
140
+ /**
141
+ * Melakukan move object sesuai arah
142
+ * pada sumbu cartesian 'x' dan 'z'
143
+ */
144
+ move ( ) {
145
+ const batas = 6.5 ;
146
+
147
+ switch ( this . params . direction ) {
148
+ // Jika bergerak di-sumbu 'x'
149
+ case 'x' :
150
+ if ( this . position . x >= batas || this . position . x <= - batas )
151
+ this . speed = - this . speed ;
152
+
153
+ if ( this . position . x >= batas )
154
+ this . position . x = batas ;
155
+ else if ( this . position . x <= - batas )
156
+ this . position . x = - batas ;
157
+
158
+ // this.position.x += this.speed;
159
+ this . physijs_box . position . x += this . speed ;
160
+ break ;
161
+ // Jika bergerak di-sumbu 'z'
162
+ case 'z' :
163
+ if ( this . position . z >= batas || this . position . z <= - batas )
164
+ this . speed = - this . speed ;
165
+
166
+ if ( this . position . z >= batas )
167
+ this . position . z = batas ;
168
+ else if ( this . position . z <= - batas )
169
+ this . position . z = - batas ;
170
+
171
+ // this.mesh.position.z += this.speed;
172
+ this . physijs_box . position . z += this . speed ;
173
+ break ;
174
+ default :
175
+ break ;
176
+ }
177
+ }
178
+
179
+ }
180
+
181
+ module . exports = FallingBrick ;
0 commit comments