Update v2-index.html
Browse files- v2-index.html +45 -7
v2-index.html
CHANGED
@@ -22,8 +22,10 @@
|
|
22 |
let blocks = [];
|
23 |
let currentBlock = null;
|
24 |
let score = 0;
|
|
|
25 |
init();
|
26 |
animate();
|
|
|
27 |
function init() {
|
28 |
// Create a Three.js scene
|
29 |
scene = new THREE.Scene();
|
@@ -32,40 +34,50 @@
|
|
32 |
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
33 |
camera.position.set(0, 50, 100);
|
34 |
camera.lookAt(0, 0, 0);
|
|
|
35 |
// Create a renderer
|
36 |
renderer = new THREE.WebGLRenderer({ antialias: true });
|
37 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
38 |
document.body.appendChild(renderer.domElement);
|
|
|
39 |
// Add lights to the scene
|
40 |
let ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
|
41 |
scene.add(ambientLight);
|
|
|
42 |
let directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
|
43 |
directionalLight.position.set(0, 100, 100);
|
44 |
scene.add(directionalLight);
|
|
|
45 |
// Add ground plane to the scene
|
46 |
let planeGeometry = new THREE.PlaneGeometry(100, 100);
|
47 |
let planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff });
|
48 |
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
|
49 |
plane.rotation.x = -Math.PI / 2;
|
50 |
scene.add(plane);
|
|
|
51 |
// Create a physics world
|
52 |
world = new CANNON.World();
|
53 |
world.gravity.set(0, -9.82, 0);
|
54 |
world.broadphase = new CANNON.NaiveBroadphase();
|
55 |
world.solver.iterations = 10;
|
|
|
56 |
// Create a ground plane in the physics world
|
57 |
let groundShape = new CANNON.Plane();
|
58 |
let groundBody = new CANNON.Body({ mass: 0 });
|
59 |
groundBody.addShape(groundShape);
|
60 |
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
|
61 |
world.addBody(groundBody);
|
|
|
62 |
// Add event listeners for input
|
63 |
document.addEventListener('keydown', onKeyDown, false);
|
64 |
}
|
|
|
65 |
function animate() {
|
66 |
requestAnimationFrame(animate);
|
|
|
67 |
// Step the physics world
|
68 |
world.step(1 / 60);
|
|
|
69 |
// Update the position of the blocks in the Three.js scene
|
70 |
for (let i = 0; i < blocks.length; i++) {
|
71 |
let block = blocks[i];
|
@@ -73,18 +85,21 @@
|
|
73 |
block.position.copy(body.position);
|
74 |
block.quaternion.copy(body.quaternion);
|
75 |
}
|
|
|
76 |
// Render the scene
|
77 |
renderer.render(scene, camera);
|
78 |
}
|
|
|
79 |
function createBlock() {
|
80 |
-
// Create a
|
81 |
-
|
82 |
-
|
83 |
let block = new THREE.Mesh(blockGeometry, blockMaterial);
|
84 |
block.position.set(0, 5, 0);
|
85 |
scene.add(block);
|
86 |
blocks.push(block);
|
87 |
currentBlock = block;
|
|
|
88 |
// Create a physics body for the block
|
89 |
let blockShape = new CANNON.Box(new CANNON.Vec3(5, 5, 5));
|
90 |
let blockBody = new CANNON.Body({ mass: 1 });
|
@@ -92,7 +107,16 @@
|
|
92 |
blockBody.position.set(0, 15, 0);
|
93 |
world.addBody(blockBody);
|
94 |
block.userData.physicsBody = blockBody;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
}
|
|
|
96 |
function placeBlock() {
|
97 |
if (currentBlock) {
|
98 |
let position = currentBlock.position.clone();
|
@@ -102,8 +126,13 @@
|
|
102 |
body.angularVelocity.set(0, 0, 0);
|
103 |
stack.push(currentBlock);
|
104 |
currentBlock = null;
|
|
|
|
|
|
|
|
|
105 |
}
|
106 |
}
|
|
|
107 |
function steerBlock(direction) {
|
108 |
if (currentBlock) {
|
109 |
let position = currentBlock.position.clone();
|
@@ -111,6 +140,7 @@
|
|
111 |
currentBlock.position.copy(position);
|
112 |
}
|
113 |
}
|
|
|
114 |
function onKeyDown(event) {
|
115 |
if (event.keyCode === 65) { // A key
|
116 |
steerBlock(-1);
|
@@ -120,13 +150,17 @@
|
|
120 |
placeBlock();
|
121 |
let stability = checkStability();
|
122 |
if (stability) {
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
125 |
} else {
|
126 |
alert('Game Over!');
|
127 |
}
|
128 |
}
|
129 |
}
|
|
|
130 |
function checkStability() {
|
131 |
let positions = [];
|
132 |
for (let i = 0; i < stack.length; i++) {
|
@@ -146,8 +180,10 @@
|
|
146 |
}
|
147 |
}
|
148 |
}
|
|
|
149 |
return true;
|
150 |
}
|
|
|
151 |
function resetGame() {
|
152 |
// Remove all blocks from the scene and the physics world
|
153 |
while (blocks.length > 0) {
|
@@ -155,17 +191,19 @@
|
|
155 |
scene.remove(block);
|
156 |
world.remove(block.userData.physicsBody);
|
157 |
}
|
|
|
158 |
// Reset the stack and the score
|
159 |
stack = [];
|
160 |
score = 0;
|
161 |
-
document.getElementById('score').innerHTML = 'Score: ' + score;
|
|
|
162 |
// Create a new block
|
163 |
createBlock();
|
164 |
}
|
|
|
165 |
resetGame();
|
166 |
</script>
|
167 |
<div id="score" style="position: absolute; top: 10px; left: 10px; font-size: 24px; font-weight: bold; color: white;"></div>
|
168 |
<button onclick="resetGame()" style="position: absolute; top: 10px; right: 10px; font-size: 24px;">Restart</button>
|
169 |
-
|
170 |
</body>
|
171 |
</html>
|
|
|
22 |
let blocks = [];
|
23 |
let currentBlock = null;
|
24 |
let score = 0;
|
25 |
+
|
26 |
init();
|
27 |
animate();
|
28 |
+
|
29 |
function init() {
|
30 |
// Create a Three.js scene
|
31 |
scene = new THREE.Scene();
|
|
|
34 |
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
35 |
camera.position.set(0, 50, 100);
|
36 |
camera.lookAt(0, 0, 0);
|
37 |
+
|
38 |
// Create a renderer
|
39 |
renderer = new THREE.WebGLRenderer({ antialias: true });
|
40 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
41 |
document.body.appendChild(renderer.domElement);
|
42 |
+
|
43 |
// Add lights to the scene
|
44 |
let ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
|
45 |
scene.add(ambientLight);
|
46 |
+
|
47 |
let directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
|
48 |
directionalLight.position.set(0, 100, 100);
|
49 |
scene.add(directionalLight);
|
50 |
+
|
51 |
// Add ground plane to the scene
|
52 |
let planeGeometry = new THREE.PlaneGeometry(100, 100);
|
53 |
let planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff });
|
54 |
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
|
55 |
plane.rotation.x = -Math.PI / 2;
|
56 |
scene.add(plane);
|
57 |
+
|
58 |
// Create a physics world
|
59 |
world = new CANNON.World();
|
60 |
world.gravity.set(0, -9.82, 0);
|
61 |
world.broadphase = new CANNON.NaiveBroadphase();
|
62 |
world.solver.iterations = 10;
|
63 |
+
|
64 |
// Create a ground plane in the physics world
|
65 |
let groundShape = new CANNON.Plane();
|
66 |
let groundBody = new CANNON.Body({ mass: 0 });
|
67 |
groundBody.addShape(groundShape);
|
68 |
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
|
69 |
world.addBody(groundBody);
|
70 |
+
|
71 |
// Add event listeners for input
|
72 |
document.addEventListener('keydown', onKeyDown, false);
|
73 |
}
|
74 |
+
|
75 |
function animate() {
|
76 |
requestAnimationFrame(animate);
|
77 |
+
|
78 |
// Step the physics world
|
79 |
world.step(1 / 60);
|
80 |
+
|
81 |
// Update the position of the blocks in the Three.js scene
|
82 |
for (let i = 0; i < blocks.length; i++) {
|
83 |
let block = blocks[i];
|
|
|
85 |
block.position.copy(body.position);
|
86 |
block.quaternion.copy(body.quaternion);
|
87 |
}
|
88 |
+
|
89 |
// Render the scene
|
90 |
renderer.render(scene, camera);
|
91 |
}
|
92 |
+
|
93 |
function createBlock() {
|
94 |
+
// Create a
|
95 |
+
let colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
|
96 |
+
let blockMaterial = new THREE.MeshPhongMaterial({ color: colors[Math.floor(Math.random() * colors.length)], envMap: cubeCamera.renderTarget.texture });
|
97 |
let block = new THREE.Mesh(blockGeometry, blockMaterial);
|
98 |
block.position.set(0, 5, 0);
|
99 |
scene.add(block);
|
100 |
blocks.push(block);
|
101 |
currentBlock = block;
|
102 |
+
|
103 |
// Create a physics body for the block
|
104 |
let blockShape = new CANNON.Box(new CANNON.Vec3(5, 5, 5));
|
105 |
let blockBody = new CANNON.Body({ mass: 1 });
|
|
|
107 |
blockBody.position.set(0, 15, 0);
|
108 |
world.addBody(blockBody);
|
109 |
block.userData.physicsBody = blockBody;
|
110 |
+
|
111 |
+
// Create a cube camera for the reflection map
|
112 |
+
let cubeCamera = new THREE.CubeCamera(1, 1000, 128);
|
113 |
+
cubeCamera.position.copy(block.position);
|
114 |
+
scene.add(cubeCamera);
|
115 |
+
|
116 |
+
// Render the reflection map
|
117 |
+
cubeCamera.update(renderer, scene);
|
118 |
}
|
119 |
+
|
120 |
function placeBlock() {
|
121 |
if (currentBlock) {
|
122 |
let position = currentBlock.position.clone();
|
|
|
126 |
body.angularVelocity.set(0, 0, 0);
|
127 |
stack.push(currentBlock);
|
128 |
currentBlock = null;
|
129 |
+
setTimeout(() => {
|
130 |
+
createBlock();
|
131 |
+
currentBlock.userData.physicsBody.velocity.set(0, -1, 0);
|
132 |
+
}, 100);
|
133 |
}
|
134 |
}
|
135 |
+
|
136 |
function steerBlock(direction) {
|
137 |
if (currentBlock) {
|
138 |
let position = currentBlock.position.clone();
|
|
|
140 |
currentBlock.position.copy(position);
|
141 |
}
|
142 |
}
|
143 |
+
|
144 |
function onKeyDown(event) {
|
145 |
if (event.keyCode === 65) { // A key
|
146 |
steerBlock(-1);
|
|
|
150 |
placeBlock();
|
151 |
let stability = checkStability();
|
152 |
if (stability) {
|
153 |
+
let position = stack[stack.length - 1].userData.physicsBody.position;
|
154 |
+
let distance = position.distanceTo(new THREE.Vector3(0, 15, 0));
|
155 |
+
let accuracy = Math.max(0, 1 - distance / 10) * 100;
|
156 |
+
score += Math.pow(2, stack.length) * accuracy;
|
157 |
+
document.getElementById('score').innerHTML = 'Score: ' + Math.floor(score);
|
158 |
} else {
|
159 |
alert('Game Over!');
|
160 |
}
|
161 |
}
|
162 |
}
|
163 |
+
|
164 |
function checkStability() {
|
165 |
let positions = [];
|
166 |
for (let i = 0; i < stack.length; i++) {
|
|
|
180 |
}
|
181 |
}
|
182 |
}
|
183 |
+
|
184 |
return true;
|
185 |
}
|
186 |
+
|
187 |
function resetGame() {
|
188 |
// Remove all blocks from the scene and the physics world
|
189 |
while (blocks.length > 0) {
|
|
|
191 |
scene.remove(block);
|
192 |
world.remove(block.userData.physicsBody);
|
193 |
}
|
194 |
+
|
195 |
// Reset the stack and the score
|
196 |
stack = [];
|
197 |
score = 0;
|
198 |
+
document.getElementById('score').innerHTML = 'Score: ' + Math.floor(score);
|
199 |
+
|
200 |
// Create a new block
|
201 |
createBlock();
|
202 |
}
|
203 |
+
|
204 |
resetGame();
|
205 |
</script>
|
206 |
<div id="score" style="position: absolute; top: 10px; left: 10px; font-size: 24px; font-weight: bold; color: white;"></div>
|
207 |
<button onclick="resetGame()" style="position: absolute; top: 10px; right: 10px; font-size: 24px;">Restart</button>
|
|
|
208 |
</body>
|
209 |
</html>
|