Update index.html
Browse files- index.html +59 -27
index.html
CHANGED
@@ -34,6 +34,7 @@
|
|
34 |
function addBall() {
|
35 |
var ball = new THREE.Mesh(ballGeometry, ballMaterial);
|
36 |
ball.position.set((Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25);
|
|
|
37 |
scene.add(ball);
|
38 |
balls.push(ball);
|
39 |
}
|
@@ -54,36 +55,60 @@
|
|
54 |
light.position.set(0, 30, 0);
|
55 |
scene.add(light);
|
56 |
|
57 |
-
var
|
|
|
|
|
|
|
58 |
|
59 |
function animate() {
|
60 |
requestAnimationFrame(animate);
|
61 |
|
62 |
-
|
63 |
-
balls[i].position.x += (Math.random() - 0.5) * speed;
|
64 |
-
balls[i].position.y += (Math.random() - 0.5) * speed;
|
65 |
-
balls[i].position.z += (Math.random() - 0.5) * speed;
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
|
75 |
-
balls[i].
|
76 |
}
|
77 |
|
78 |
if (balls[i].position.y - 0.5 < -15 && balls[i].position.x + 0.5 > paddle.position.x - 2 && balls[i].position.x - 0.5 < paddle.position.x + 2 && balls[i].position.z + 0.5 > paddle.position.z - 0.5 && balls[i].position.z - 0.5 < paddle.position.z + 0.5) {
|
79 |
-
balls[i].
|
80 |
}
|
81 |
|
82 |
for (var j = 0; j < bricks.length; j++) {
|
83 |
-
if (balls[i].position.
|
84 |
scene.remove(bricks[j]);
|
85 |
bricks.splice(j, 1);
|
86 |
-
balls[i].
|
87 |
addBrick();
|
88 |
}
|
89 |
}
|
@@ -92,6 +117,10 @@
|
|
92 |
renderer.render(scene, camera);
|
93 |
}
|
94 |
|
|
|
|
|
|
|
|
|
95 |
document.addEventListener('mousedown', function(event) {
|
96 |
if (event.button === 0) {
|
97 |
addBrick();
|
@@ -102,18 +131,21 @@
|
|
102 |
});
|
103 |
|
104 |
document.addEventListener('keydown', function(event) {
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
117 |
});
|
118 |
|
119 |
animate();
|
|
|
34 |
function addBall() {
|
35 |
var ball = new THREE.Mesh(ballGeometry, ballMaterial);
|
36 |
ball.position.set((Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25);
|
37 |
+
ball.velocity = new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
|
38 |
scene.add(ball);
|
39 |
balls.push(ball);
|
40 |
}
|
|
|
55 |
light.position.set(0, 30, 0);
|
56 |
scene.add(light);
|
57 |
|
58 |
+
var paddleGeometry = new THREE.BoxGeometry(4, 1, 1);
|
59 |
+
var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
|
60 |
+
var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
|
61 |
+
scene.add(paddle);
|
62 |
|
63 |
function animate() {
|
64 |
requestAnimationFrame(animate);
|
65 |
|
66 |
+
var paddleSpeed = 1.5;
|
|
|
|
|
|
|
67 |
|
68 |
+
if (keys["87"] || keys["38"]) { // w or up arrow
|
69 |
+
paddle.position.z -= paddleSpeed;
|
70 |
+
}
|
71 |
+
if (keys["83"] || keys["40"]) {
|
72 |
+
paddle.position.z += paddleSpeed;
|
73 |
+
}
|
74 |
+
if (keys["65"] || keys["37"]) { // a or left arrow
|
75 |
+
paddle.position.x -= paddleSpeed;
|
76 |
}
|
77 |
+
if (keys["68"] || keys["39"]) { // d or right arrow
|
78 |
+
paddle.position.x += paddleSpeed;
|
79 |
+
}
|
80 |
+
|
81 |
+
var mouseSpeed = 0.1;
|
82 |
+
|
83 |
+
if (mouseX !== null && mouseY !== null) {
|
84 |
+
paddle.position.x = (mouseX / window.innerWidth) * 50 - 25;
|
85 |
+
paddle.position.z = -(mouseY / window.innerHeight) * 50 + 25;
|
86 |
+
}
|
87 |
+
|
88 |
+
for (var i = 0; i < balls.length; i++) {
|
89 |
+
balls[i].position.add(balls[i].velocity);
|
90 |
+
|
91 |
+
if (balls[i].position.x + 0.5 > 25 || balls[i].position.x - 0.5 < -25) {
|
92 |
+
balls[i].velocity.x = -balls[i].velocity.x;
|
93 |
+
}
|
94 |
+
|
95 |
+
if (balls[i].position.y + 0.5 > 25 || balls[i].position.y - 0.5 < -25) {
|
96 |
+
balls[i].velocity.y = -balls[i].velocity.y;
|
97 |
+
}
|
98 |
+
|
99 |
if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
|
100 |
+
balls[i].velocity.z = -balls[i].velocity.z;
|
101 |
}
|
102 |
|
103 |
if (balls[i].position.y - 0.5 < -15 && balls[i].position.x + 0.5 > paddle.position.x - 2 && balls[i].position.x - 0.5 < paddle.position.x + 2 && balls[i].position.z + 0.5 > paddle.position.z - 0.5 && balls[i].position.z - 0.5 < paddle.position.z + 0.5) {
|
104 |
+
balls[i].velocity.y = Math.abs(balls[i].velocity.y);
|
105 |
}
|
106 |
|
107 |
for (var j = 0; j < bricks.length; j++) {
|
108 |
+
if (balls[i].position.distanceTo(bricks[j].position) <= 2) {
|
109 |
scene.remove(bricks[j]);
|
110 |
bricks.splice(j, 1);
|
111 |
+
balls[i].velocity.negate();
|
112 |
addBrick();
|
113 |
}
|
114 |
}
|
|
|
117 |
renderer.render(scene, camera);
|
118 |
}
|
119 |
|
120 |
+
var keys = {};
|
121 |
+
var mouseX = null;
|
122 |
+
var mouseY = null;
|
123 |
+
|
124 |
document.addEventListener('mousedown', function(event) {
|
125 |
if (event.button === 0) {
|
126 |
addBrick();
|
|
|
131 |
});
|
132 |
|
133 |
document.addEventListener('keydown', function(event) {
|
134 |
+
keys[event.keyCode] = true;
|
135 |
+
});
|
136 |
+
|
137 |
+
document.addEventListener('keyup', function(event) {
|
138 |
+
keys[event.keyCode] = false;
|
139 |
+
});
|
140 |
+
|
141 |
+
document.addEventListener('mousemove', function(event) {
|
142 |
+
mouseX = event.clientX;
|
143 |
+
mouseY = event.clientY;
|
144 |
+
});
|
145 |
+
|
146 |
+
document.addEventListener('mouseleave', function(event) {
|
147 |
+
mouseX = null;
|
148 |
+
mouseY = null;
|
149 |
});
|
150 |
|
151 |
animate();
|