Update index.html
Browse files- index.html +28 -37
index.html
CHANGED
@@ -96,7 +96,7 @@
|
|
96 |
const enemy = createEntity('enemy', {
|
97 |
class: 'enemy',
|
98 |
geometry: {primitive: 'sphere', radius: 0.5},
|
99 |
-
material: {color: '#FF0000', metalness: 0.7, roughness: 0.3},
|
100 |
position: new THREE.Vector3(
|
101 |
spawnPosition.x + (Math.random() - 0.5) * 2,
|
102 |
1.6, // Keep at player's eye level
|
@@ -111,64 +111,52 @@
|
|
111 |
updateDisplays();
|
112 |
}
|
113 |
|
114 |
-
function fireLaser(source, color,
|
115 |
const sourcePosition = source.object3D.position.clone();
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
const direction = new THREE.Vector3(0, 0, -1);
|
121 |
-
direction.applyQuaternion(camera.object3D.quaternion);
|
122 |
-
targetPosition = sourcePosition.clone().add(direction.multiplyScalar(100));
|
123 |
-
} else {
|
124 |
-
targetPosition = camera.object3D.position.clone();
|
125 |
-
}
|
126 |
-
|
127 |
-
const distance = sourcePosition.distanceTo(targetPosition);
|
128 |
-
const midPoint = new THREE.Vector3().addVectors(sourcePosition, targetPosition).multiplyScalar(0.5);
|
129 |
|
130 |
const laser = createEntity('laser', {
|
131 |
class: 'laser',
|
132 |
-
geometry: {primitive: 'cylinder', radius: 0.05, height:
|
133 |
material: {color: color, opacity: 0.7},
|
134 |
-
position:
|
135 |
});
|
136 |
|
137 |
-
laser.object3D.lookAt(
|
138 |
laser.object3D.rotateX(Math.PI / 2);
|
139 |
|
140 |
laser.setAttribute('animation', {
|
141 |
property: 'scale',
|
142 |
to: '1 0.01 1',
|
143 |
-
dur:
|
144 |
easing: 'linear'
|
145 |
});
|
146 |
|
147 |
setTimeout(() => {
|
148 |
scene.removeChild(laser);
|
149 |
-
},
|
150 |
|
151 |
// Check for collisions
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
});
|
160 |
-
} else if (isPointOnLine(sourcePosition, targetPosition, camera.object3D.position, 0.5)) {
|
161 |
-
health = Math.max(0, health - 10);
|
162 |
-
updateDisplays();
|
163 |
-
}
|
164 |
}
|
165 |
|
166 |
-
function
|
167 |
const lineVec = new THREE.Vector3().subVectors(lineEnd, lineStart);
|
168 |
const pointVec = new THREE.Vector3().subVectors(point, lineStart);
|
169 |
-
const
|
170 |
-
const
|
171 |
-
|
|
|
|
|
172 |
}
|
173 |
|
174 |
function handleHit(target, position) {
|
@@ -209,7 +197,9 @@
|
|
209 |
}
|
210 |
|
211 |
function shootFromCamera() {
|
212 |
-
|
|
|
|
|
213 |
}
|
214 |
|
215 |
function updateGame() {
|
@@ -231,7 +221,8 @@
|
|
231 |
health = Math.max(0, health - 50);
|
232 |
}
|
233 |
if (Math.random() < 0.01) {
|
234 |
-
|
|
|
235 |
}
|
236 |
}
|
237 |
});
|
|
|
96 |
const enemy = createEntity('enemy', {
|
97 |
class: 'enemy',
|
98 |
geometry: {primitive: 'sphere', radius: 0.5},
|
99 |
+
material: {color: '#FF0000', metalness: 0.7, roughness: 0.3, opacity: 0.5, transparent: true},
|
100 |
position: new THREE.Vector3(
|
101 |
spawnPosition.x + (Math.random() - 0.5) * 2,
|
102 |
1.6, // Keep at player's eye level
|
|
|
111 |
updateDisplays();
|
112 |
}
|
113 |
|
114 |
+
function fireLaser(source, color, direction) {
|
115 |
const sourcePosition = source.object3D.position.clone();
|
116 |
+
sourcePosition.y = 1.6; // Ensure laser starts at eye level
|
117 |
|
118 |
+
const laserLength = 100;
|
119 |
+
const endPosition = sourcePosition.clone().add(direction.multiplyScalar(laserLength));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
const laser = createEntity('laser', {
|
122 |
class: 'laser',
|
123 |
+
geometry: {primitive: 'cylinder', radius: 0.05, height: laserLength},
|
124 |
material: {color: color, opacity: 0.7},
|
125 |
+
position: sourcePosition.clone().add(direction.multiplyScalar(laserLength / 2))
|
126 |
});
|
127 |
|
128 |
+
laser.object3D.lookAt(endPosition);
|
129 |
laser.object3D.rotateX(Math.PI / 2);
|
130 |
|
131 |
laser.setAttribute('animation', {
|
132 |
property: 'scale',
|
133 |
to: '1 0.01 1',
|
134 |
+
dur: 1000,
|
135 |
easing: 'linear'
|
136 |
});
|
137 |
|
138 |
setTimeout(() => {
|
139 |
scene.removeChild(laser);
|
140 |
+
}, 1000);
|
141 |
|
142 |
// Check for collisions
|
143 |
+
const targets = document.querySelectorAll('.enemy, .spawner');
|
144 |
+
targets.forEach(target => {
|
145 |
+
const targetPos = target.object3D.position;
|
146 |
+
if (isPointNearLine(sourcePosition, endPosition, targetPos, 0.5)) {
|
147 |
+
handleHit(target, targetPos);
|
148 |
+
}
|
149 |
+
});
|
|
|
|
|
|
|
|
|
|
|
150 |
}
|
151 |
|
152 |
+
function isPointNearLine(lineStart, lineEnd, point, threshold) {
|
153 |
const lineVec = new THREE.Vector3().subVectors(lineEnd, lineStart);
|
154 |
const pointVec = new THREE.Vector3().subVectors(point, lineStart);
|
155 |
+
const lineLengthSq = lineVec.lengthSq();
|
156 |
+
const dotProduct = pointVec.dot(lineVec);
|
157 |
+
const t = Math.max(0, Math.min(1, dotProduct / lineLengthSq));
|
158 |
+
const projection = lineStart.clone().add(lineVec.multiplyScalar(t));
|
159 |
+
return point.distanceTo(projection) < threshold;
|
160 |
}
|
161 |
|
162 |
function handleHit(target, position) {
|
|
|
197 |
}
|
198 |
|
199 |
function shootFromCamera() {
|
200 |
+
const direction = new THREE.Vector3(0, 0, -1);
|
201 |
+
direction.applyQuaternion(camera.object3D.quaternion);
|
202 |
+
fireLaser(camera, '#00FF00', direction);
|
203 |
}
|
204 |
|
205 |
function updateGame() {
|
|
|
221 |
health = Math.max(0, health - 50);
|
222 |
}
|
223 |
if (Math.random() < 0.01) {
|
224 |
+
const enemyDirection = new THREE.Vector3().subVectors(cameraPosition, enemy.object3D.position).normalize();
|
225 |
+
fireLaser(enemy, '#FF0000', enemyDirection);
|
226 |
}
|
227 |
}
|
228 |
});
|