File size: 7,365 Bytes
0610c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61e6b81
0610c43
 
61e6b81
0610c43
 
 
 
 
 
 
 
61e6b81
0610c43
 
 
 
61e6b81
0610c43
 
 
61e6b81
0610c43
 
 
61e6b81
0610c43
 
 
 
 
 
61e6b81
0610c43
 
 
 
 
61e6b81
0610c43
 
 
 
 
 
61e6b81
0610c43
 
 
61e6b81
0610c43
 
61e6b81
0610c43
 
61e6b81
0610c43
 
 
 
 
 
 
61e6b81
0610c43
 
 
61e6b81
0610c43
61e6b81
 
 
0610c43
 
 
 
 
61e6b81
0610c43
 
 
 
 
 
 
61e6b81
 
 
 
 
 
 
 
0610c43
61e6b81
0610c43
 
 
 
 
 
 
 
 
61e6b81
 
 
 
0610c43
 
61e6b81
0610c43
 
 
 
 
 
 
61e6b81
0610c43
 
 
 
 
 
 
 
 
61e6b81
 
 
 
 
0610c43
 
 
 
 
61e6b81
0610c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61e6b81
0610c43
 
61e6b81
0610c43
 
 
 
 
 
 
61e6b81
0610c43
 
 
61e6b81
 
0610c43
 
 
61e6b81
0610c43
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html>
<head>
    <title>Tower Building Game</title>
    <meta charset="utf-8">
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/cannon/build/cannon.min.js"></script>
    <script>
        let scene, camera, renderer;
        let world, stack;
        let blocks = [];
        let currentBlock = null;
        let score = 0;

        init();
        animate();

        function init() {
            // Create a Three.js scene
            scene = new THREE.Scene();
            
            // Create a camera
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            camera.position.set(0, 50, 100);
            camera.lookAt(0, 0, 0);

            // Create a renderer
            renderer = new THREE.WebGLRenderer({ antialias: true });
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.body.appendChild(renderer.domElement);

            // Add lights to the scene
            let ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
            scene.add(ambientLight);

            let directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
            directionalLight.position.set(0, 100, 100);
            scene.add(directionalLight);

            // Add ground plane to the scene
            let planeGeometry = new THREE.PlaneGeometry(100, 100);
            let planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff });
            let plane = new THREE.Mesh(planeGeometry, planeMaterial);
            plane.rotation.x = -Math.PI / 2;
            scene.add(plane);

            // Create a physics world
            world = new CANNON.World();
            world.gravity.set(0, -9.82, 0);
            world.broadphase = new CANNON.NaiveBroadphase();
            world.solver.iterations = 10;

            // Create a ground plane in the physics world
            let groundShape = new CANNON.Plane();
            let groundBody = new CANNON.Body({ mass: 0 });
            groundBody.addShape(groundShape);
            groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
            world.addBody(groundBody);

            // Add event listeners for input
            document.addEventListener('keydown', onKeyDown, false);
        }

        function animate() {
            requestAnimationFrame(animate);

            // Step the physics world
            world.step(1 / 60);

            // Update the position of the blocks in the Three.js scene
            for (let i = 0; i < blocks.length; i++) {
                let block = blocks[i];
                let body = block.userData.physicsBody;
                block.position.copy(body.position);
                block.quaternion.copy(body.quaternion);
            }

            // Render the scene
            renderer.render(scene, camera);
        }

        function createBlock() {
            // Create a
        let colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
        let blockMaterial = new THREE.MeshPhongMaterial({ color: colors[Math.floor(Math.random() * colors.length)], envMap: cubeCamera.renderTarget.texture });
        let block = new THREE.Mesh(blockGeometry, blockMaterial);
        block.position.set(0, 5, 0);
        scene.add(block);
        blocks.push(block);
        currentBlock = block;

        // Create a physics body for the block
        let blockShape = new CANNON.Box(new CANNON.Vec3(5, 5, 5));
        let blockBody = new CANNON.Body({ mass: 1 });
        blockBody.addShape(blockShape);
        blockBody.position.set(0, 15, 0);
        world.addBody(blockBody);
        block.userData.physicsBody = blockBody;

        // Create a cube camera for the reflection map
        let cubeCamera = new THREE.CubeCamera(1, 1000, 128);
        cubeCamera.position.copy(block.position);
        scene.add(cubeCamera);

        // Render the reflection map
        cubeCamera.update(renderer, scene);
    }

    function placeBlock() {
        if (currentBlock) {
            let position = currentBlock.position.clone();
            let body = currentBlock.userData.physicsBody;
            body.position.copy(position);
            body.velocity.set(0, 0, 0);
            body.angularVelocity.set(0, 0, 0);
            stack.push(currentBlock);
            currentBlock = null;
            setTimeout(() => {
                createBlock();
                currentBlock.userData.physicsBody.velocity.set(0, -1, 0);
            }, 100);
        }
    }

    function steerBlock(direction) {
        if (currentBlock) {
            let position = currentBlock.position.clone();
            position.x += direction * 10;
            currentBlock.position.copy(position);
        }
    }

    function onKeyDown(event) {
        if (event.keyCode === 65) { // A key
            steerBlock(-1);
        } else if (event.keyCode === 68) { // D key
            steerBlock(1);
        } else if (event.keyCode === 32) { // Spacebar
            placeBlock();
            let stability = checkStability();
            if (stability) {
                let position = stack[stack.length - 1].userData.physicsBody.position;
                let distance = position.distanceTo(new THREE.Vector3(0, 15, 0));
                let accuracy = Math.max(0, 1 - distance / 10) * 100;
                score += Math.pow(2, stack.length) * accuracy;
                document.getElementById('score').innerHTML = 'Score: ' + Math.floor(score);
            } else {
                alert('Game Over!');
            }
        }
    }

    function checkStability() {
        let positions = [];
        for (let i = 0; i < stack.length; i++) {
            let block = stack[i];
            let position = block.userData.physicsBody.position;
            positions.push(position.clone());
        }
        for (let i = 0; i < stack.length - 1; i++) {
            let block1 = stack[i];
            let position1 = positions[i];
            for (let j = i + 1; j < stack.length; j++) {
                let block2 = stack[j];
                let position2 = positions[j];
                let distance = position1.distanceTo(position2);
                if (distance < 10) {
                    return false;
                }
            }
        }
       
        return true;
    }

    function resetGame() {
        // Remove all blocks from the scene and the physics world
        while (blocks.length > 0) {
            let block = blocks.pop();
            scene.remove(block);
            world.remove(block.userData.physicsBody);
        }

        // Reset the stack and the score
        stack = [];
        score = 0;
        document.getElementById('score').innerHTML = 'Score: ' + Math.floor(score);

        // Create a new block
        createBlock();
    }

    resetGame();
</script>
<div id="score" style="position: absolute; top: 10px; left: 10px; font-size: 24px; font-weight: bold; color: white;"></div>
<button onclick="resetGame()" style="position: absolute; top: 10px; right: 10px; font-size: 24px;">Restart</button>
</body>
</html>