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

Create v2-index.html

Browse files
Files changed (1) hide show
  1. v2-index.html +171 -0
v2-index.html ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Tower Building Game</title>
5
+ <meta charset="utf-8">
6
+ <style>
7
+ body {
8
+ margin: 0;
9
+ overflow: hidden;
10
+ }
11
+ canvas {
12
+ display: block;
13
+ }
14
+ </style>
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
+ init();
26
+ animate();
27
+ function init() {
28
+ // Create a Three.js scene
29
+ scene = new THREE.Scene();
30
+
31
+ // Create a camera
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];
72
+ let body = block.userData.physicsBody;
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 block object in the Three.js scene
81
+ let blockGeometry = new THREE.BoxGeometry(10, 10, 10);
82
+ let blockMaterial = new THREE.MeshPhongMaterial({ map: new THREE.TextureLoader().load('block_texture.jpg') });
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 });
91
+ blockBody.addShape(blockShape);
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();
99
+ let body = currentBlock.userData.physicsBody;
100
+ body.position.copy(position);
101
+ body.velocity.set(0, 0, 0);
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();
110
+ position.x += direction * 10;
111
+ currentBlock.position.copy(position);
112
+ }
113
+ }
114
+ function onKeyDown(event) {
115
+ if (event.keyCode === 65) { // A key
116
+ steerBlock(-1);
117
+ } else if (event.keyCode === 68) { // D key
118
+ steerBlock(1);
119
+ } else if (event.keyCode === 32) { // Spacebar
120
+ placeBlock();
121
+ let stability = checkStability();
122
+ if (stability) {
123
+ score += Math.pow(2, stack.length);
124
+ document.getElementById('score').innerHTML = 'Score: ' + score;
125
+ } else {
126
+ alert('Game Over!');
127
+ }
128
+ }
129
+ }
130
+ function checkStability() {
131
+ let positions = [];
132
+ for (let i = 0; i < stack.length; i++) {
133
+ let block = stack[i];
134
+ let position = block.userData.physicsBody.position;
135
+ positions.push(position.clone());
136
+ }
137
+ for (let i = 0; i < stack.length - 1; i++) {
138
+ let block1 = stack[i];
139
+ let position1 = positions[i];
140
+ for (let j = i + 1; j < stack.length; j++) {
141
+ let block2 = stack[j];
142
+ let position2 = positions[j];
143
+ let distance = position1.distanceTo(position2);
144
+ if (distance < 10) {
145
+ return false;
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) {
154
+ let block = blocks.pop();
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>