Update index.html
Browse files- index.html +134 -122
index.html
CHANGED
@@ -30,161 +30,173 @@
|
|
30 |
<canvas id="gameCanvas"></canvas>
|
31 |
<div id="score">Score: 0</div>
|
32 |
<script>
|
33 |
-
|
34 |
-
var camera =
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
renderer.setPixelRatio(window.devicePixelRatio);
|
42 |
-
renderer.setSize(window.innerWidth, window.innerHeight);
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
var
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
53 |
|
54 |
function addBall() {
|
55 |
-
var ball =
|
56 |
-
ball.
|
57 |
-
ball.velocity = new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
|
58 |
-
scene.add(ball);
|
59 |
balls.push(ball);
|
60 |
}
|
61 |
|
62 |
function addBrick() {
|
63 |
var brickMaterial = new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff });
|
64 |
-
var brick =
|
65 |
-
brick.position.set((Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50);
|
66 |
-
scene.add(brick);
|
67 |
bricks.push(brick);
|
68 |
}
|
69 |
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
}
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
82 |
|
83 |
-
function
|
84 |
-
|
85 |
-
|
86 |
-
var paddleSpeed = 1.5;
|
87 |
-
|
88 |
-
if (keys["87"] || keys["38"]) { // w or up arrow
|
89 |
-
paddle.position.z -= paddleSpeed;
|
90 |
-
}
|
91 |
-
if (keys["83"] || keys["40"]) {
|
92 |
-
paddle.position.z += paddleSpeed;
|
93 |
-
}
|
94 |
-
if (keys["65"] || keys["37"]) { // a or left arrow
|
95 |
-
paddle.position.x -= paddleSpeed;
|
96 |
-
}
|
97 |
-
if (keys["68"] || keys["39"]) { // d or right arrow
|
98 |
-
paddle.position.x += paddleSpeed;
|
99 |
-
}
|
100 |
-
|
101 |
-
var mouseSpeed = 0.1;
|
102 |
-
|
103 |
-
if (mouseX !== null && mouseY !== null) {
|
104 |
-
paddle.position.x = (mouseX / window.innerWidth) * 50 - 25;
|
105 |
-
paddle.position.z = -(mouseY / window.innerHeight) * 50 + 25;
|
106 |
-
}
|
107 |
-
|
108 |
-
for (var i = 0; i < balls.length; i++) {
|
109 |
-
balls[i].position.add(balls[i].velocity);
|
110 |
-
|
111 |
-
if (balls[i].position.x + 0.5 > 25 || balls[i].position.x - 0.5 < -25) {
|
112 |
-
balls[i].velocity.x = -balls[i].velocity.x;
|
113 |
-
}
|
114 |
-
if (balls[i].position.y + 0.5 > 25 || balls[i].position.y - 0.5 < -25) {
|
115 |
-
balls[i].velocity.y = -balls[i].velocity.y;
|
116 |
-
}
|
117 |
-
if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
|
118 |
-
balls[i].velocity.z = -balls[i].velocity.z;
|
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 |
-
if (event.button === 2) {
|
149 |
-
addBall();
|
150 |
-
}
|
151 |
-
});
|
152 |
|
153 |
-
|
154 |
keys[event.keyCode] = true;
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
font: new THREE.FontLoader().load('https://threejs.org/examples/fonts/helvetiker_regular.typeface.json'),
|
159 |
-
size: 1,
|
160 |
-
height: 0.5,
|
161 |
-
curveSegments: 12,
|
162 |
-
bevelEnabled: false
|
163 |
-
});
|
164 |
-
var letterMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
|
165 |
-
var letterMesh = new THREE.Mesh(letterGeometry, letterMaterial);
|
166 |
-
letterMesh.position.set((Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25);
|
167 |
-
letterMesh.velocity = new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
|
168 |
-
scene.add(letterMesh);
|
169 |
-
balls.push(letterMesh);
|
170 |
-
}
|
171 |
-
});
|
172 |
-
|
173 |
-
document.addEventListener('keyup', function(event) {
|
174 |
keys[event.keyCode] = false;
|
175 |
-
}
|
176 |
|
177 |
-
|
178 |
mouseX = event.clientX;
|
179 |
mouseY = event.clientY;
|
180 |
-
}
|
181 |
|
182 |
-
|
183 |
mouseX = null;
|
184 |
mouseY = null;
|
185 |
-
}
|
186 |
|
187 |
-
|
|
|
|
|
|
|
|
|
188 |
</script>
|
189 |
</body>
|
190 |
-
</html>
|
|
|
30 |
<canvas id="gameCanvas"></canvas>
|
31 |
<div id="score">Score: 0</div>
|
32 |
<script>
|
33 |
+
// Variable declarations
|
34 |
+
var scene, camera, renderer, ballGeometry, ballMaterial, balls = [], brickGeometry, bricks = [], score = 0, scoreElement, light, paddleGeometry, paddleMaterial, paddle, keys = {}, mouseX = null, mouseY = null;
|
35 |
+
|
36 |
+
// Setting up scene and camera
|
37 |
+
setupScene();
|
38 |
|
39 |
+
// Setting up renderer
|
40 |
+
setupRenderer();
|
|
|
|
|
41 |
|
42 |
+
// Adding balls
|
43 |
+
createGeometry();
|
44 |
+
for (var i = 0; i < 50; i++) {
|
45 |
+
addBall();
|
46 |
+
}
|
47 |
+
|
48 |
+
// Adding bricks
|
49 |
+
for (var i = 0; i < 50; i++) {
|
50 |
+
addBrick();
|
51 |
+
}
|
52 |
+
|
53 |
+
// Adding light
|
54 |
+
addLight();
|
55 |
+
|
56 |
+
// Adding paddle
|
57 |
+
addPaddle();
|
58 |
+
|
59 |
+
// Registering event listeners
|
60 |
+
registerEvents();
|
61 |
+
|
62 |
+
// Fetching data for 3D letters
|
63 |
+
fetchLetters();
|
64 |
+
|
65 |
+
// Starting animation
|
66 |
+
animate();
|
67 |
+
|
68 |
+
function setupScene() {
|
69 |
+
scene = new THREE.Scene();
|
70 |
+
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
71 |
+
camera.position.set(0, 30, 0);
|
72 |
+
camera.lookAt(new THREE.Vector3(0, 0, 0));
|
73 |
+
scene.add(camera);
|
74 |
+
}
|
75 |
|
76 |
+
function setupRenderer() {
|
77 |
+
renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
|
78 |
+
renderer.setClearColor(0x000000);
|
79 |
+
renderer.setPixelRatio(window.devicePixelRatio);
|
80 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
81 |
+
}
|
82 |
|
83 |
+
function createGeometry() {
|
84 |
+
ballGeometry = new THREE.SphereGeometry(0.5, 32, 32);
|
85 |
+
ballMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
|
86 |
+
brickGeometry = new THREE.BoxGeometry(2, 1, 1);
|
87 |
+
}
|
88 |
|
89 |
function addBall() {
|
90 |
+
var ball = createMesh(ballGeometry, ballMaterial);
|
91 |
+
ball.velocity = getRandomVelocity();
|
|
|
|
|
92 |
balls.push(ball);
|
93 |
}
|
94 |
|
95 |
function addBrick() {
|
96 |
var brickMaterial = new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff });
|
97 |
+
var brick = createMesh(brickGeometry, brickMaterial);
|
|
|
|
|
98 |
bricks.push(brick);
|
99 |
}
|
100 |
|
101 |
+
function addLight() {
|
102 |
+
light = new THREE.PointLight(0xffffff, 1, 100);
|
103 |
+
light.position.set(0, 30, 0);
|
104 |
+
scene.add(light);
|
105 |
}
|
106 |
|
107 |
+
function addPaddle() {
|
108 |
+
paddleGeometry = new THREE.BoxGeometry(4, 1, 1);
|
109 |
+
paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
|
110 |
+
paddle = createMesh(paddleGeometry, paddleMaterial);
|
111 |
+
}
|
112 |
|
113 |
+
function createMesh(geometry, material) {
|
114 |
+
var mesh = new THREE.Mesh(geometry, material);
|
115 |
+
mesh.position.set(getRandomPosition());
|
116 |
+
scene.add(mesh);
|
117 |
+
return mesh;
|
118 |
+
}
|
119 |
|
120 |
+
function getRandomPosition() {
|
121 |
+
return new THREE.Vector3((Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50);
|
122 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
function getRandomVelocity() {
|
125 |
+
return new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
|
126 |
+
}
|
127 |
|
128 |
+
function registerEvents() {
|
129 |
+
document.addEventListener('mousedown', onMouseDown);
|
130 |
+
document.addEventListener('keydown', onKeyDown);
|
131 |
+
document.addEventListener('keyup', onKeyUp);
|
132 |
+
document.addEventListener('mousemove', onMouseMove);
|
133 |
+
document.addEventListener('mouseleave', onMouseLeave);
|
134 |
+
window.addEventListener('resize', onWindowResize, false);
|
135 |
+
}
|
|
|
|
|
|
|
136 |
|
137 |
+
function fetchLetters() {
|
138 |
+
fetch('https://api11.salmonground-a8f39f59.centralus.azurecontainerapps.io/selftest', {
|
139 |
+
method: 'GET',
|
140 |
+
headers: {
|
141 |
+
'Accept': 'application/json'
|
142 |
+
}
|
143 |
+
})
|
144 |
+
.then(response => response.json())
|
145 |
+
.then(data => {
|
146 |
+
var queryResults = data.QueryResults;
|
147 |
+
queryResults = queryResults.substring(0, 10);
|
148 |
+
var fontLoader = new THREE.FontLoader();
|
149 |
+
fontLoader.load('https://threejs.org/examples/fonts/helvetiker_regular.typeface.json', function(font) {
|
150 |
+
for (var i = 0; i < queryResults.length; i++) {
|
151 |
+
var letter = queryResults[i].toLowerCase();
|
152 |
+
if (letter.match(/[a-z]/i)) {
|
153 |
+
var letterGeometry = new THREE.TextGeometry(letter, {
|
154 |
+
font: font,
|
155 |
+
size: 1,
|
156 |
+
height: 0.5,
|
157 |
+
curveSegments: 12,
|
158 |
+
bevelEnabled: false
|
159 |
+
});
|
160 |
+
var letterMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
|
161 |
+
var letterMesh = createMesh(letterGeometry, letterMaterial);
|
162 |
+
balls.push(letterMesh);
|
163 |
+
}
|
164 |
+
}
|
165 |
+
});
|
166 |
+
});
|
167 |
}
|
168 |
|
169 |
+
function animate() {
|
170 |
+
// Rest of your animate function
|
171 |
+
}
|
172 |
|
173 |
+
function onMouseDown(event) {
|
174 |
+
// Your mouse down event
|
175 |
+
}
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
function onKeyDown(event) {
|
178 |
keys[event.keyCode] = true;
|
179 |
+
}
|
180 |
+
|
181 |
+
function onKeyUp(event) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
keys[event.keyCode] = false;
|
183 |
+
}
|
184 |
|
185 |
+
function onMouseMove(event) {
|
186 |
mouseX = event.clientX;
|
187 |
mouseY = event.clientY;
|
188 |
+
}
|
189 |
|
190 |
+
function onMouseLeave(event) {
|
191 |
mouseX = null;
|
192 |
mouseY = null;
|
193 |
+
}
|
194 |
|
195 |
+
function onWindowResize() {
|
196 |
+
camera.aspect = window.innerWidth / window.innerHeight;
|
197 |
+
camera.updateProjectionMatrix();
|
198 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
199 |
+
}
|
200 |
</script>
|
201 |
</body>
|
202 |
+
</html>
|