Update index.html
Browse files- index.html +31 -41
index.html
CHANGED
@@ -102,31 +102,38 @@
|
|
102 |
}
|
103 |
|
104 |
function fireLaser(source, color, target, isPlayer = false) {
|
105 |
-
const sourcePosition = source.object3D.position;
|
106 |
-
const targetPosition = target.object3D.position;
|
107 |
-
const direction = new THREE.Vector3().subVectors(targetPosition, sourcePosition).normalize();
|
108 |
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
const laser = createEntity('laser', {
|
113 |
class: 'laser',
|
114 |
-
geometry: {primitive: 'cylinder', radius: 0.05, height:
|
115 |
material: {color: color, opacity: 0.7},
|
116 |
position: midPoint
|
117 |
});
|
118 |
|
119 |
laser.object3D.lookAt(targetPosition);
|
120 |
-
|
121 |
-
// Rotate the laser to be horizontal for player, keep vertical for enemies
|
122 |
-
if (isPlayer) {
|
123 |
-
laser.object3D.rotateZ(Math.PI / 2);
|
124 |
-
}
|
125 |
|
126 |
laser.setAttribute('animation', {
|
127 |
property: 'scale',
|
128 |
to: '1 0.01 1',
|
129 |
-
dur: isPlayer ? 1000 : 2000,
|
130 |
easing: 'linear'
|
131 |
});
|
132 |
|
@@ -139,17 +146,24 @@
|
|
139 |
const targets = document.querySelectorAll('.enemy, .spawner');
|
140 |
targets.forEach(target => {
|
141 |
const targetPos = target.object3D.position;
|
142 |
-
|
143 |
-
if (distance < 2) {
|
144 |
handleHit(target, targetPos);
|
145 |
}
|
146 |
});
|
147 |
-
} else if (sourcePosition.
|
148 |
health = Math.max(0, health - 10);
|
149 |
updateDisplays();
|
150 |
}
|
151 |
}
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
function handleHit(target, position) {
|
154 |
if (target.classList.contains('enemy')) {
|
155 |
createExplosion(position);
|
@@ -188,31 +202,7 @@
|
|
188 |
}
|
189 |
|
190 |
function shootFromCamera() {
|
191 |
-
|
192 |
-
const direction = new THREE.Vector3(0, 0, -1);
|
193 |
-
direction.applyQuaternion(cameraEl.object3D.quaternion);
|
194 |
-
|
195 |
-
const raycaster = new THREE.Raycaster(cameraEl.object3D.position, direction);
|
196 |
-
const cursorEl = cursor;
|
197 |
-
const cursorPosition = new THREE.Vector3();
|
198 |
-
cursorEl.object3D.getWorldPosition(cursorPosition);
|
199 |
-
|
200 |
-
const intersects = raycaster.intersectObjects(scene.object3D.children, true);
|
201 |
-
let targetPosition;
|
202 |
-
|
203 |
-
if (intersects.length > 0) {
|
204 |
-
const intersect = intersects.find(i => i.object.el && (i.object.el.classList.contains('enemy') || i.object.el.classList.contains('spawner')));
|
205 |
-
if (intersect) {
|
206 |
-
targetPosition = intersect.point;
|
207 |
-
} else {
|
208 |
-
targetPosition = cursorPosition;
|
209 |
-
}
|
210 |
-
} else {
|
211 |
-
targetPosition = cursorPosition;
|
212 |
-
}
|
213 |
-
|
214 |
-
const dummyTarget = { object3D: { position: targetPosition } };
|
215 |
-
fireLaser(camera, '#00FF00', dummyTarget, true);
|
216 |
}
|
217 |
|
218 |
function updateGame() {
|
@@ -233,7 +223,7 @@
|
|
233 |
health = Math.max(0, health - 50);
|
234 |
}
|
235 |
if (Math.random() < 0.01) {
|
236 |
-
fireLaser(enemy, '#FF0000',
|
237 |
}
|
238 |
}
|
239 |
});
|
|
|
102 |
}
|
103 |
|
104 |
function fireLaser(source, color, target, isPlayer = false) {
|
105 |
+
const sourcePosition = source.object3D.position.clone();
|
106 |
+
const targetPosition = target.object3D.position.clone();
|
|
|
107 |
|
108 |
+
if (isPlayer) {
|
109 |
+
// For player, set source to be in front of the camera
|
110 |
+
sourcePosition.copy(camera.object3D.position).add(new THREE.Vector3(0, 0, -1));
|
111 |
+
// Set target to be far in the direction the camera is facing
|
112 |
+
const direction = new THREE.Vector3(0, 0, -1);
|
113 |
+
direction.applyQuaternion(camera.object3D.quaternion);
|
114 |
+
targetPosition.copy(sourcePosition).add(direction.multiplyScalar(100));
|
115 |
+
} else {
|
116 |
+
// For enemies, set target to be in front of the camera
|
117 |
+
targetPosition.copy(camera.object3D.position).add(new THREE.Vector3(0, 0, -1));
|
118 |
+
}
|
119 |
+
|
120 |
+
const distance = sourcePosition.distanceTo(targetPosition);
|
121 |
+
const midPoint = new THREE.Vector3().addVectors(sourcePosition, targetPosition).multiplyScalar(0.5);
|
122 |
|
123 |
const laser = createEntity('laser', {
|
124 |
class: 'laser',
|
125 |
+
geometry: {primitive: 'cylinder', radius: 0.05, height: distance},
|
126 |
material: {color: color, opacity: 0.7},
|
127 |
position: midPoint
|
128 |
});
|
129 |
|
130 |
laser.object3D.lookAt(targetPosition);
|
131 |
+
laser.object3D.rotateX(Math.PI / 2);
|
|
|
|
|
|
|
|
|
132 |
|
133 |
laser.setAttribute('animation', {
|
134 |
property: 'scale',
|
135 |
to: '1 0.01 1',
|
136 |
+
dur: isPlayer ? 1000 : 2000,
|
137 |
easing: 'linear'
|
138 |
});
|
139 |
|
|
|
146 |
const targets = document.querySelectorAll('.enemy, .spawner');
|
147 |
targets.forEach(target => {
|
148 |
const targetPos = target.object3D.position;
|
149 |
+
if (isPointOnLine(sourcePosition, targetPosition, targetPos, 0.5)) {
|
|
|
150 |
handleHit(target, targetPos);
|
151 |
}
|
152 |
});
|
153 |
+
} else if (isPointOnLine(sourcePosition, targetPosition, camera.object3D.position, 0.5)) {
|
154 |
health = Math.max(0, health - 10);
|
155 |
updateDisplays();
|
156 |
}
|
157 |
}
|
158 |
|
159 |
+
function isPointOnLine(lineStart, lineEnd, point, threshold) {
|
160 |
+
const lineVec = new THREE.Vector3().subVectors(lineEnd, lineStart);
|
161 |
+
const pointVec = new THREE.Vector3().subVectors(point, lineStart);
|
162 |
+
const projection = pointVec.projectOnVector(lineVec);
|
163 |
+
const distance = new THREE.Vector3().subVectors(pointVec, projection).length();
|
164 |
+
return distance < threshold && projection.length() <= lineVec.length();
|
165 |
+
}
|
166 |
+
|
167 |
function handleHit(target, position) {
|
168 |
if (target.classList.contains('enemy')) {
|
169 |
createExplosion(position);
|
|
|
202 |
}
|
203 |
|
204 |
function shootFromCamera() {
|
205 |
+
fireLaser(camera, '#00FF00', {object3D: {position: new THREE.Vector3()}}, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
}
|
207 |
|
208 |
function updateGame() {
|
|
|
223 |
health = Math.max(0, health - 50);
|
224 |
}
|
225 |
if (Math.random() < 0.01) {
|
226 |
+
fireLaser(enemy, '#FF0000', camera);
|
227 |
}
|
228 |
}
|
229 |
});
|