Update index.html
Browse files- index.html +61 -44
index.html
CHANGED
@@ -98,7 +98,6 @@
|
|
98 |
}));
|
99 |
enemy.creationTime = Date.now();
|
100 |
enemies.add(enemy);
|
101 |
-
setInterval(() => { if (enemy.parentNode) fireLaser(enemy, '#FF0000', player); }, 2000);
|
102 |
updateDisplays();
|
103 |
}
|
104 |
|
@@ -107,40 +106,39 @@
|
|
107 |
const targetPosition = target.object3D.position;
|
108 |
const laser = createEntity('laser', {
|
109 |
class: 'laser',
|
110 |
-
geometry: {primitive: '
|
111 |
-
material: {color: color, opacity: 0.7
|
112 |
position: sourcePosition
|
113 |
});
|
114 |
|
115 |
const direction = new THREE.Vector3().subVectors(targetPosition, sourcePosition).normalize();
|
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 |
-
updateLaserPosition();
|
144 |
}
|
145 |
|
146 |
function handleHit(target, position) {
|
@@ -180,6 +178,34 @@
|
|
180 |
updateDisplays();
|
181 |
}
|
182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
function updateGame() {
|
184 |
const now = Date.now();
|
185 |
const cameraPosition = camera.object3D.position;
|
@@ -197,6 +223,9 @@
|
|
197 |
enemies.delete(enemy);
|
198 |
health = Math.max(0, health - 50);
|
199 |
}
|
|
|
|
|
|
|
200 |
}
|
201 |
});
|
202 |
|
@@ -210,27 +239,15 @@
|
|
210 |
spawners.delete(spawner);
|
211 |
health = Math.max(0, health - 100);
|
212 |
}
|
|
|
|
|
|
|
213 |
});
|
214 |
|
215 |
while (spawners.size < 5) createSpawner();
|
216 |
updateDisplays();
|
217 |
}
|
218 |
|
219 |
-
function shootFromCamera() {
|
220 |
-
const direction = new THREE.Vector3(0, 0, -1);
|
221 |
-
direction.applyQuaternion(camera.object3D.quaternion);
|
222 |
-
const raycaster = new THREE.Raycaster(camera.object3D.position, direction);
|
223 |
-
const intersects = raycaster.intersectObjects(scene.object3D.children, true);
|
224 |
-
|
225 |
-
for (let intersect of intersects) {
|
226 |
-
const target = intersect.object.el;
|
227 |
-
if (target && (target.classList.contains('enemy') || target.classList.contains('spawner'))) {
|
228 |
-
fireLaser(camera, '#00FF00', target);
|
229 |
-
break;
|
230 |
-
}
|
231 |
-
}
|
232 |
-
}
|
233 |
-
|
234 |
document.addEventListener('keydown', (event) => { if (event.code === 'Space') shootFromCamera(); });
|
235 |
scene.addEventListener('click', shootFromCamera);
|
236 |
|
|
|
98 |
}));
|
99 |
enemy.creationTime = Date.now();
|
100 |
enemies.add(enemy);
|
|
|
101 |
updateDisplays();
|
102 |
}
|
103 |
|
|
|
106 |
const targetPosition = target.object3D.position;
|
107 |
const laser = createEntity('laser', {
|
108 |
class: 'laser',
|
109 |
+
geometry: {primitive: 'cylinder', radius: 0.05, height: 100},
|
110 |
+
material: {color: color, opacity: 0.7},
|
111 |
position: sourcePosition
|
112 |
});
|
113 |
|
114 |
const direction = new THREE.Vector3().subVectors(targetPosition, sourcePosition).normalize();
|
115 |
+
laser.object3D.lookAt(targetPosition);
|
116 |
|
117 |
+
laser.setAttribute('animation', {
|
118 |
+
property: 'scale',
|
119 |
+
to: '1 0.01 1',
|
120 |
+
dur: 1000,
|
121 |
+
easing: 'linear'
|
122 |
+
});
|
123 |
+
|
124 |
+
setTimeout(() => {
|
125 |
+
scene.removeChild(laser);
|
126 |
+
}, 1000);
|
127 |
+
|
128 |
+
// Check for collisions
|
129 |
+
if (source === player) {
|
130 |
+
const targets = document.querySelectorAll('.enemy, .spawner');
|
131 |
+
targets.forEach(target => {
|
132 |
+
const targetPos = target.object3D.position;
|
133 |
+
const distance = sourcePosition.distanceTo(targetPos);
|
134 |
+
if (distance < 2) {
|
135 |
+
handleHit(target, targetPos);
|
136 |
+
}
|
137 |
+
});
|
138 |
+
} else if (sourcePosition.distanceTo(player.object3D.position) < 2) {
|
139 |
+
health = Math.max(0, health - 10);
|
140 |
+
updateDisplays();
|
141 |
}
|
|
|
|
|
142 |
}
|
143 |
|
144 |
function handleHit(target, position) {
|
|
|
178 |
updateDisplays();
|
179 |
}
|
180 |
|
181 |
+
function shootFromCamera() {
|
182 |
+
const cameraEl = camera.components.camera.el;
|
183 |
+
const direction = new THREE.Vector3(0, 0, -1);
|
184 |
+
direction.applyQuaternion(cameraEl.object3D.quaternion);
|
185 |
+
|
186 |
+
const raycaster = new THREE.Raycaster(cameraEl.object3D.position, direction);
|
187 |
+
const cursorEl = cursor;
|
188 |
+
const cursorPosition = new THREE.Vector3();
|
189 |
+
cursorEl.object3D.getWorldPosition(cursorPosition);
|
190 |
+
|
191 |
+
const intersects = raycaster.intersectObjects(scene.object3D.children, true);
|
192 |
+
let targetPosition;
|
193 |
+
|
194 |
+
if (intersects.length > 0) {
|
195 |
+
const intersect = intersects.find(i => i.object.el && (i.object.el.classList.contains('enemy') || i.object.el.classList.contains('spawner')));
|
196 |
+
if (intersect) {
|
197 |
+
targetPosition = intersect.point;
|
198 |
+
} else {
|
199 |
+
targetPosition = cursorPosition;
|
200 |
+
}
|
201 |
+
} else {
|
202 |
+
targetPosition = cursorPosition;
|
203 |
+
}
|
204 |
+
|
205 |
+
const dummyTarget = { object3D: { position: targetPosition } };
|
206 |
+
fireLaser(camera, '#00FF00', dummyTarget);
|
207 |
+
}
|
208 |
+
|
209 |
function updateGame() {
|
210 |
const now = Date.now();
|
211 |
const cameraPosition = camera.object3D.position;
|
|
|
223 |
enemies.delete(enemy);
|
224 |
health = Math.max(0, health - 50);
|
225 |
}
|
226 |
+
if (Math.random() < 0.01) {
|
227 |
+
fireLaser(enemy, '#FF0000', player);
|
228 |
+
}
|
229 |
}
|
230 |
});
|
231 |
|
|
|
239 |
spawners.delete(spawner);
|
240 |
health = Math.max(0, health - 100);
|
241 |
}
|
242 |
+
if (Math.random() < 0.005 && enemies.size < 30) {
|
243 |
+
createEnemy(spawner.object3D.position);
|
244 |
+
}
|
245 |
});
|
246 |
|
247 |
while (spawners.size < 5) createSpawner();
|
248 |
updateDisplays();
|
249 |
}
|
250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
document.addEventListener('keydown', (event) => { if (event.code === 'Space') shootFromCamera(); });
|
252 |
scene.addEventListener('click', shootFromCamera);
|
253 |
|