awacke1 commited on
Commit
dc57967
·
1 Parent(s): b7c3012

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +119 -26
index.html CHANGED
@@ -15,10 +15,13 @@
15
  </head>
16
  <body>
17
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
 
18
  <script>
19
  let scene, camera, renderer;
 
20
  let blocks = [];
21
  let currentBlock = null;
 
22
 
23
  init();
24
  animate();
@@ -52,50 +55,140 @@
52
  plane.rotation.x = -Math.PI / 2;
53
  scene.add(plane);
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  // Add event listeners for input
56
- document.addEventListener('mousedown', onMouseDown, false);
57
- document.addEventListener('touchstart', onTouchStart, false);
58
  document.addEventListener('keydown', onKeyDown, false);
59
  }
60
 
61
  function animate() {
62
  requestAnimationFrame(animate);
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  renderer.render(scene, camera);
64
  }
65
 
66
  function createBlock() {
 
67
  let blockGeometry = new THREE.BoxGeometry(10, 10, 10);
68
- let blockMaterial = new THREE.MeshPhongMaterial({ map: new THREE.TextureLoader().load('block_texture.jpg') });
69
- let block = new THREE.Mesh(blockGeometry, blockMaterial);
70
- block.position.set(0, 5, 0);
71
- scene.add(block);
72
- blocks.push(block);
73
- currentBlock = block;
74
- }
75
 
76
- function placeBlock() {
77
- if (currentBlock) {
78
- currentBlock.position.y = 5;
79
- currentBlock = null;
80
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  }
 
82
 
83
- function onMouseDown(event) {
84
- event.preventDefault();
85
- createBlock();
 
 
86
  }
 
87
 
88
- function onTouchStart(event) {
89
- event.preventDefault();
90
- createBlock();
 
 
 
 
 
 
 
 
 
 
 
91
  }
 
92
 
93
- function onKeyDown(event) {
94
- if (event.keyCode === 32) {
95
- event.preventDefault();
96
- placeBlock();
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
  }
99
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  </body>
101
- </html>
 
15
  </head>
16
  <body>
17
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
18
+ <script src="https://cdn.jsdelivr.net/npm/cannon/build/cannon.min.js"></script>
19
  <script>
20
  let scene, camera, renderer;
21
+ let world, stack;
22
  let blocks = [];
23
  let currentBlock = null;
24
+ let score = 0;
25
 
26
  init();
27
  animate();
 
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];
84
+ let body = block.userData.physicsBody;
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 block object in the Three.js scene
95
  let blockGeometry = new THREE.BoxGeometry(10, 10, 10);
96
+ let blockMaterial = new THREE.MeshPhongMaterial({ map: new THREE.TextureLoader().load('block_texture.jpg') });
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 });
106
+ blockBody.addShape(blockShape);
107
+ blockBody.position.set(0, 15, 0);
108
+ world.addBody(blockBody);
109
+ block.userData.physicsBody = blockBody;
110
+ }
111
+
112
+ function placeBlock() {
113
+ if (currentBlock) {
114
+ let position = currentBlock.position.clone();
115
+ let body = currentBlock.userData.physicsBody;
116
+ body.position.copy(position);
117
+ body.velocity.set(0, 0, 0);
118
+ body.angularVelocity.set(0, 0, 0);
119
+ stack.push(currentBlock);
120
+ currentBlock = null;
121
  }
122
+ }
123
 
124
+ function steerBlock(direction) {
125
+ if (currentBlock) {
126
+ let position = currentBlock.position.clone();
127
+ position.x += direction * 10;
128
+ currentBlock.position.copy(position);
129
  }
130
+ }
131
 
132
+ function onKeyDown(event) {
133
+ if (event.keyCode === 65) { // A key
134
+ steerBlock(-1);
135
+ } else if (event.keyCode === 68) { // D key
136
+ steerBlock(1);
137
+ } else if (event.keyCode === 32) { // Spacebar
138
+ placeBlock();
139
+ let stability = checkStability();
140
+ if (stability) {
141
+ score += Math.pow(2, stack.length);
142
+ document.getElementById('score').innerHTML = 'Score: ' + score;
143
+ } else {
144
+ alert('Game Over!');
145
+ }
146
  }
147
+ }
148
 
149
+ function checkStability() {
150
+ let positions = [];
151
+ for (let i = 0; i < stack.length; i++) {
152
+ let block = stack[i];
153
+ let position = block.userData.physicsBody.position;
154
+ positions.push(position.clone());
155
+ }
156
+ for (let i = 0; i < stack.length - 1; i++) {
157
+ let block1 = stack[i];
158
+ let position1 = positions[i];
159
+ for (let j = i + 1; j < stack.length; j++) {
160
+ let block2 = stack[j];
161
+ let position2 = positions[j];
162
+ let distance = position1.distanceTo(position2);
163
+ if (distance < 10) {
164
+ return false;
165
+ }
166
  }
167
  }
168
+ return true;
169
+ }
170
+
171
+ function resetGame() {
172
+ // Remove all blocks from the scene and the physics world
173
+ while (blocks.length > 0) {
174
+ let block = blocks.pop();
175
+ scene.remove(block);
176
+ world.remove(block.userData.physicsBody);
177
+ }
178
+
179
+ // Reset the stack and the score
180
+ stack = [];
181
+ score = 0;
182
+ document.getElementById('score').innerHTML = 'Score: ' + score;
183
+
184
+ // Create a new block
185
+ createBlock();
186
+ }
187
+
188
+ resetGame();
189
+ </script>
190
+ <div id="score" style="position: absolute; top: 10px; left: 10px; font-size: 24px; font-weight: bold; color: white;"></div>
191
+ <button onclick="resetGame()" style="position: absolute; top: 10px; right: 10px; font-size: 24px;">Restart</button>
192
+
193
  </body>
194
+ </html>