-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
458 lines (407 loc) · 14.3 KB
/
app.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
const THREE = require('three');
const Scene = require('./scene');
const Brick = require('./brick');
const FallingBrick = require('./fallingbrick');
const Queue = require('./queue');
// const socket = require('socket.io-client')(window.location.host);
const Physijs = require('physijs-webpack/browserify');
//==================
//--Define command--
//==================
const SPACE = 0;
const PLAY = 1;
const PAUSE = 2;
const GAMEOVER = 3;
const PLAYAGAIN = 4;
//==================
//---Inisialisasi---
//==================
var scene = new Scene();
let bricks = new Queue();
let brick = new Brick();
let fallingbricks = new Queue();
let fallingbrick = new FallingBrick();
let cutted_bricks = new Queue();
let scale = new THREE.Vector3();
let position = new THREE.Vector3();
let command = PLAY, startPos = 6.5, direction = 'z';
let hue = 0;
let scoreValue = 0;
var scoreDisplay = document.getElementById("score");
var gameoverDisplay = document.getElementById("game-over");
var playagainButton = document.getElementById("playagain");
var isPlay=0;
var myAudio = new Audio('../sound/ingame.mp3');
var geoo;
function init() {
// Mengatur parameter warna berdasarkan nilai hue-nya
hue = Math.floor(Math.random() * 360);
// Disable view gameover
gameoverDisplay.style.display = "none";
// Membuat ground tempat pijakan balok
// var ground_material = Physijs.createMaterial(
// new THREE.MeshLambertMaterial({color: 0xffffff}),
// 1.,
// 0,1
// );
// var ground = new Physijs.BoxMesh(
// new THREE.BoxGeometry(5, 1, 5),
// ground_material,
// 0
// );
// ground.position.set(0,-13.5,0);
// scene.add(ground);
// Membuat tumpukan awal hingga
// bagian bawah tertutupi
for(let i = -14;i < 0; i++) {
brick = new Brick({
position: new THREE.Vector3(0, i, 0),
direction: 'z',
color: "hsl(" + hue +", 100%, 50%)"
});
bricks.push(brick);
scene.add(brick.build);
}
// Tumpukan paling atas
brick = new Brick({
position: new THREE.Vector3(0, 0, startPos),
direction: 'z',
color: "hsl(" + hue +", 100%, 50%)"
});
bricks.push(brick);
scene.add(brick.build);
}
init();
//=======================
//-----Socket client-----
//=======================
// const roomName = 'nganu';
// socket.emit('join', roomName);
//-----------------------
// socket.on('keyboardEvent', function(data) {
// console.log(data);
// });
// socket.on('deviceOrientation', function(data) {
// console.log(data);
// });
// socket.on('sync', function(data) {
// // console.log(data);
// for(let i = 0;i < bricks.size(); i++) {
// bricks.items[i].position = data.position[i];
// bricks.items[i].scale = data.scale[i];
// }
// });
//=======================
//--Algoritma permainan--
//=======================
function animate() {
scene.simulate();
requestAnimationFrame(() => {animate()});
loop();
}
// distraksi hujan Box
function spawnBox() {
var xrandom = 2, zrandom = -2;
let i;
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function getXRandom(xrandom){
while (xrandom >= -2 && xrandom <= 2) {
xrandom = getRandomArbitrary(-10,10);
}
return xrandom;
}
function getZRandom(zrandom){
while (zrandom >= -2 && zrandom <= 2) {
zrandom = getRandomArbitrary(-10,10);
}
return zrandom;
}
i = Math.round(getRandomArbitrary(1,5));
// Hujan paling atas
for (let e = 0; e < i; e++) {
xrandom = getXRandom(xrandom);
zrandom = getZRandom(zrandom);
// console.log(i,zrandom,xrandom,"ok");
fallingbrick = new FallingBrick({
position: new THREE.Vector3(xrandom, 20, zrandom),
direction: 'z',
color: "hsl(" + hue +", 100%, 50%)"
});
fallingbricks.push(fallingbrick);
scene.add(fallingbrick.build);
}
}
//-----------------------
function loop() {
brick = bricks.back();
switch (command) {
// Balok melakukan update
case PLAY:
brick.move();
// hapus box distraksi jika melewati y <= 0
for(let i = 0;i < fallingbricks.size(); i++)
if(fallingbricks.items[i].params.position.y <= 0)
scene.remove(fallingbricks.items[i].name);
let message = {
score: scoreValue,
property: []
};
for(let i = 0;i < bricks.size(); i++) {
let object = {
position: bricks.items[i].position,
scale: bricks.items[i].scale,
hue: bricks.items[i].color.h
};
message.property.push(object);
}
// socket.emit('sync', message);
nambahlagu();
break;
case PAUSE:
break;
// Balok berhenti, memotong, dan stop
// sesuai kondisi
case SPACE:
if(cutted_bricks.size>3){
scene.remove(cutted_bricks.front().name)
cutted_bricks.pop()
}
prevBrick = bricks.get(bricks.size() - 2);
// distraksi box
spawnBox();
// Jika balok masih bisa memotong, maka loop lanjut
var cutted_brick = brick.cut(prevBrick);
if(cutted_brick[0]) {
// Balok baru berupa potongan akan dapat efek Physijs
// var fallingbrick = new FallingBrick(prevBrick.params);
// fallingbricks.push(fallingbrick);
// scene.add(fallingbrick.build);
if(brick.params.direction == 'x' && !cutted_brick[5]) {
if (cutted_brick[1].x<cutted_brick[3].x){
var a_fallingbrick = new FallingBrick({
position: new THREE.Vector3(cutted_brick[3].x + Number((brick.scale.x).toFixed(1)), -1, cutted_brick[3].z),
scale: new THREE.Vector3(cutted_brick[2].x, 1, Number((brick.scale.z).toFixed(1))),
direction: 'z',
color: "hsl(" + hue +", 100%, 50%)"
});
// fallingbricks.push(fallingbrick);
scene.add(a_fallingbrick.build);
cutted_bricks.push(a_fallingbrick)
}
else{
var a_fallingbrick = new FallingBrick({
position: new THREE.Vector3(cutted_brick[3].x - Number((brick.scale.x).toFixed(1)), -1, cutted_brick[3].z),
scale: new THREE.Vector3(cutted_brick[2].x, 1, Number((brick.scale.z).toFixed(1))),
direction: 'z',
color: "hsl(" + hue +", 100%, 50%)"
});
// fallingbricks.push(fallingbrick);
scene.add(a_fallingbrick.build);
cutted_bricks.push(a_fallingbrick)
}
}
else if(brick.params.direction == 'z' && !cutted_brick[5]) {
if (cutted_brick[1].z<cutted_brick[3].z){
var a_fallingbrick = new FallingBrick({
position: new THREE.Vector3(cutted_brick[3].x, -1, cutted_brick[3].z + Number((brick.scale.z).toFixed(1))),
scale: new THREE.Vector3(Number((brick.scale.x).toFixed(1)), 1, cutted_brick[2].z),
direction: 'z',
color: "hsl(" + hue +", 100%, 50%)"
});
// fallingbricks.push(fallingbrick);
scene.add(a_fallingbrick.build);
cutted_bricks.push(a_fallingbrick)
}
else{
var a_fallingbrick = new FallingBrick({
position: new THREE.Vector3(cutted_brick[3].x, -1, cutted_brick[3].z - Number((brick.scale.z).toFixed(1))),
scale: new THREE.Vector3(Number((brick.scale.x).toFixed(1)), 1, cutted_brick[2].z),
direction: 'z',
color: "hsl(" + hue +", 100%, 50%)"
});
// fallingbricks.push(fallingbrick);
scene.add(a_fallingbrick.build);
cutted_bricks.push(a_fallingbrick)
}
}
bricks.set(brick);
// Untuk setiap stepnya, balok yang lama turun 1 kotak
for(let i = 0;i < bricks.size(); i++)
bricks.items[i].down();
// Jika balok berjalan di-arah x,
// mengatur direction menjadi 'x'
if(direction == 'x') {
var topBrick = new Brick({
position: new THREE.Vector3(-startPos, 0, brick.position.z),
scale: new THREE.Vector3(brick.scale.x, brick.scale.y, brick.scale.z),
color: "hsl(" + hue +", 100%, 50%)",
direction: direction
});
// Update agar variasi
direction = 'z';
startPos = -startPos;
}
// Jika balok berjalan di-arah z,
// mengatur direction menjadi 'z'
else if(direction == 'z') {
var topBrick = new Brick({
position: new THREE.Vector3(brick.position.x, 0, startPos),
scale: new THREE.Vector3(brick.scale.x, brick.scale.y, brick.scale.z),
color: "hsl(" + hue +", 100%, 50%)",
direction: direction
});
// Update agar variasi
direction = 'x';
}
// Menambahkan balok baru
// Membuang balok lama
scene.add(topBrick.build);
scene.remove(bricks.front().name);
bricks.pop();
bricks.push(topBrick);
// Mengatur parameter warna agar dinamis,
// berdasarkan tingkat nilai Hue-nya.
hue = (hue + 5) % 360;
// Update score
scoreValue++;
if(cutted_brick[5]==true){
scoreValue++;
}
scoreDisplay.innerHTML = scoreValue;
// mengembalikan state menjadi play
command = PLAY;
}
// Jika balok tidak bisa memotong (game over)
else {
// Balok akan dapat efek Physijs
// var fallingbrick = new FallingBrick(prevBrick.params);
// fallingbricks.push(fallingbrick);
// scene.remove(prevBrick.name);
// scene.add(fallingbrick.build);
command = GAMEOVER;
var gameover = new Audio('../sound/gameover.mp3');
gameover.play();
}
break;
case GAMEOVER:
// Drop semua block
for(let i = 0;i < bricks.size(); i++)
scene.remove(bricks.items[i].name);
bricks.clear();
// // Drop semua falling block
for(let i = 0;i < fallingbricks.size(); i++)
scene.remove(fallingbricks.items[i].name);
fallingbricks.clear();
// Enable view gameover
gameoverDisplay.style.display = "block";
playagainButton.onclick = function() {
command = PLAYAGAIN;
}
break;
case PLAYAGAIN:
// re-inisialisasi semua block
init();
// reset score
scoreValue = 0;
scoreDisplay.innerHTML = scoreValue;
command = PLAY;
default:
break;
}
scene.render();
}
animate();
//===================
//---Event Handler---
//===================
/**
*
* @param {KeyboardEvent} event
*/
function onKeyDown(event) {
switch (event.code) {
case "Space":
if(command == PLAY)
command = SPACE;
var spasi = new Audio('../sound/spasi.mp3');
spasi.play();
break;
case "KeyP":
if(command == PLAY)
command = PAUSE;
else if(command == PAUSE)
command = PLAY;
case "Enter":
if(command == GAMEOVER)
command = PLAYAGAIN;
default:
break;
}
// socket.emit('keyboardEvent', event.code);
}
/**
*
* @param {TouchEvent} event
*/
function onTouchEvent(event) {
if(command == PLAY)
{
command = SPACE;
}
else if(command == GAMEOVER)
{
// command = PLAYAGAIN;
}
}
/**
*
* @param {DeviceOrientationEvent} event
*/
function handleOrientation(event) {
var orientation = {
alpha: Math.round(event.alpha),
beta: Math.round(event.beta),
gamma: Math.round(event.gamma)
}
// socket.emit('deviceOrientation', orientation);
}
if(window.DeviceOrientationEvent){
}else {
alert('DeviceOrientationEvent is not supported');
}
window.addEventListener('touchstart', onTouchEvent);
window.addEventListener('keydown', onKeyDown);
window.addEventListener('deviceorientation', handleOrientation);
function nambahlagu(){
if(isPlay==0)
{
myAudio.play();
}
else if (isPlay==1)
{
myAudio.stop();
}
}
//font
function tulisan(){
var loader = new THREE.FontLoader();
loader.load( '../fonts/perfect.typeface.json', function ( font ) {
var geometry = new THREE.TextGeometry( 'Perfect', {
font: font,
size: 80,
height: 5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 10,
bevelSize: 8,
bevelOffset: 0,
bevelSegments: 5
} );
} );
geoo= createMesh(geometry);
geoo.position.x=100;
geoo.position.y=100;
scene.add(geoo);
}