Skip to content

Commit a932752

Browse files
committed
merapikan program
1 parent 82ab6fa commit a932752

File tree

3 files changed

+83
-59
lines changed

3 files changed

+83
-59
lines changed

js/app.js

+70-44
Original file line numberDiff line numberDiff line change
@@ -56,50 +56,76 @@ function animate() {
5656
}
5757

5858
function loop() {
59-
brick = bricks.back();
60-
brick.move();
61-
62-
if(command == SPACE) {
63-
prevBrick = bricks.get(bricks.size() - 2);
64-
brick.stop();
65-
if(!brick.cut(prevBrick))
66-
command = STOP;
67-
68-
bricks.set(brick);
69-
70-
// Untuk setiap stepnya, balok yang lama turun 1 kotak
71-
for(let i = 0;i < bricks.size(); i++)
72-
bricks.items[i].down();
73-
74-
if(direction == 'x') {
75-
var topBrick = new Brick({
76-
position: new THREE.Vector3(-startPos, 0, brick.position.z),
77-
scale: new THREE.Vector3(brick.scale.x, brick.scale.y, brick.scale.z),
78-
color: "hsl(" + hue +", 100%, 50%)",
79-
direction: direction
80-
});
81-
direction = 'z';
82-
startPos = -startPos;
83-
}
84-
else if(direction == 'z') {
85-
var topBrick = new Brick({
86-
position: new THREE.Vector3(brick.position.x, 0, startPos),
87-
scale: new THREE.Vector3(brick.scale.x, brick.scale.y, brick.scale.z),
88-
color: "hsl(" + hue +", 100%, 50%)",
89-
direction: direction
90-
});
91-
direction = 'x';
92-
}
93-
94-
// Menambahkan balok baru
95-
// Membuang balok lama
96-
scene.add(topBrick.build);
97-
scene.remove(bricks.front().build);
98-
bricks.pop();
99-
bricks.push(topBrick);
100-
101-
hue = (hue + 5) % 360;
102-
command = PLAY;
59+
switch (command) {
60+
// Balok melakukan update
61+
case PLAY:
62+
brick = bricks.back();
63+
brick.move();
64+
break;
65+
// Balok berhenti, memotong, dan stop
66+
// sesuai kondisi
67+
case SPACE:
68+
brick.stop();
69+
prevBrick = bricks.get(bricks.size() - 2);
70+
71+
// Jika balok masih bisa memotong, maka loop lanjut
72+
if(brick.cut(prevBrick)) {
73+
bricks.set(brick);
74+
75+
// Untuk setiap stepnya, balok yang lama turun 1 kotak
76+
for(let i = 0;i < bricks.size(); i++)
77+
bricks.items[i].down();
78+
79+
// Jika balok berjalan di-arah x,
80+
// mengatur direction menjadi 'x'
81+
if(direction == 'x') {
82+
var topBrick = new Brick({
83+
position: new THREE.Vector3(-startPos, 0, brick.position.z),
84+
scale: new THREE.Vector3(brick.scale.x, brick.scale.y, brick.scale.z),
85+
color: "hsl(" + hue +", 100%, 50%)",
86+
direction: direction
87+
});
88+
// Update agar variasi
89+
direction = 'z';
90+
startPos = -startPos;
91+
}
92+
// Jika balok berjalan di-arah x,
93+
// mengatur direction menjadi 'x'
94+
else if(direction == 'z') {
95+
var topBrick = new Brick({
96+
position: new THREE.Vector3(brick.position.x, 0, startPos),
97+
scale: new THREE.Vector3(brick.scale.x, brick.scale.y, brick.scale.z),
98+
color: "hsl(" + hue +", 100%, 50%)",
99+
direction: direction
100+
});
101+
// Update agar variasi
102+
direction = 'x';
103+
}
104+
105+
// Menambahkan balok baru
106+
// Membuang balok lama
107+
scene.add(topBrick.build);
108+
scene.remove(bricks.front().build);
109+
bricks.pop();
110+
bricks.push(topBrick);
111+
112+
// Mengatur parameter warna agar dinamis,
113+
// berdasarkan tingkat nilai Hue-nya.
114+
hue = (hue + 5) % 360;
115+
116+
// mengembalikan state menjadi play
117+
command = PLAY;
118+
}
119+
// Jika balok tidak bisa memotong (game over)
120+
else {
121+
command = STOP;
122+
}
123+
break;
124+
case STOP:
125+
126+
break;
127+
default:
128+
break;
103129
}
104130

105131
scene.render();

js/brick.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Brick {
6464
}
6565

6666
/**
67-
* @param {Brick} prevBrick
67+
* @param {Brick} prevBrick Balok sebagai perbandingan saat ini dengan sebelumnya
6868
* @returns {boolean} Kondisi apakah dia masih bisa memotong atau tidak
6969
*/
7070
cut(prevBrick)

js/scene.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Scene {
88
//===============
99
var defaultParam = {
1010
camera: {
11-
depth: 300,
11+
depth: 64,
1212
near: -50,
1313
far: 1000,
1414
position: new THREE.Vector3(2, 2, 2),
@@ -30,27 +30,25 @@ class Scene {
3030
this.renderer.setSize(window.innerWidth, window.innerHeight);
3131
this.renderer.setPixelRatio(window.devicePixelRatio);
3232
this.renderer.shadowMap.enabled = true;
33-
// document.body.appendChild( this.renderer.domElement );
3433

3534
//================
3635
//-----Camera-----
3736
//================
3837
const cameraConfig = this.params.camera;
39-
const aspect = Window.innerWidth / Window.innerHeight;
4038
const depth = cameraConfig.depth;
41-
const viewSize = 64;
39+
const frustumSize = 64;
4240
// this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
4341
// this.camera.position.y = 1;
4442
// this.camera.position.z = 5;
4543

46-
this.camera = new THREE.OrthographicCamera(depth * aspect / -2, depth * aspect / 2, depth / 2, depth / -2, cameraConfig.near, cameraConfig.far);
44+
this.camera = new THREE.OrthographicCamera(depth / -2, depth / 2, depth / 2, depth / -2, cameraConfig.near, cameraConfig.far);
4745
this.camera.position.copy(cameraConfig.position);
4846
this.camera.lookAt(new THREE.Vector3().fromArray(cameraConfig.lookAt));
4947

50-
this.camera.left = window.innerWidth / - viewSize;
51-
this.camera.right = window.innerWidth / viewSize;
52-
this.camera.top = window.innerHeight / viewSize;
53-
this.camera.bottom = window.innerHeight / - viewSize;
48+
this.camera.left = window.innerWidth / - frustumSize;
49+
this.camera.right = window.innerWidth / frustumSize;
50+
this.camera.top = window.innerHeight / frustumSize;
51+
this.camera.bottom = window.innerHeight / - frustumSize;
5452
this.camera.updateProjectionMatrix();
5553

5654
// var cameraHelper = new THREE.CameraHelper(this.camera);
@@ -90,13 +88,13 @@ class Scene {
9088
}
9189

9290
onWindowResize() {
93-
const viewSize = 64;
91+
const frustumSize = 64;
9492
this.renderer.setSize(window.innerWidth, window.innerHeight);
9593
this.renderer.setPixelRatio(window.devicePixelRatio);
96-
this.camera.left = window.innerWidth / - viewSize;
97-
this.camera.right = window.innerWidth / viewSize;
98-
this.camera.top = window.innerHeight / viewSize;
99-
this.camera.bottom = window.innerHeight / - viewSize;
94+
this.camera.left = window.innerWidth / - frustumSize;
95+
this.camera.right = window.innerWidth / frustumSize;
96+
this.camera.top = window.innerHeight / frustumSize;
97+
this.camera.bottom = window.innerHeight / - frustumSize;
10098
this.camera.updateProjectionMatrix();
10199
}
102100
}

0 commit comments

Comments
 (0)