Update index.html
Browse files- index.html +67 -49
index.html
CHANGED
|
@@ -88,7 +88,8 @@
|
|
| 88 |
transform: translate(-50%, -50%);
|
| 89 |
cursor: grab;
|
| 90 |
box-shadow: 0 0 15px rgba(0, 150, 255, 0.6);
|
| 91 |
-
transition:
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
#joystickHandle:active {
|
|
@@ -278,10 +279,9 @@
|
|
| 278 |
|
| 279 |
let scene, camera, renderer, playerCube, garage;
|
| 280 |
let playerPos = { x: 0, y: 1, z: 0 };
|
| 281 |
-
let
|
| 282 |
-
let
|
| 283 |
-
let moveSpeed = 0.
|
| 284 |
-
let turnSpeed = 0.04;
|
| 285 |
let cameraRotation = { x: 0, y: 0 };
|
| 286 |
let spawnPoint = { x: -3.5719999999999996, y: 1, z: 0.20199999999999968 };
|
| 287 |
let joystickActive = false;
|
|
@@ -373,11 +373,13 @@
|
|
| 373 |
joystick.addEventListener('touchstart', (e) => {
|
| 374 |
joystickActive = true;
|
| 375 |
e.preventDefault();
|
|
|
|
| 376 |
});
|
| 377 |
|
| 378 |
joystick.addEventListener('touchmove', (e) => {
|
| 379 |
if (!joystickActive) return;
|
| 380 |
e.preventDefault();
|
|
|
|
| 381 |
const touch = e.touches[0];
|
| 382 |
const rect = joystick.getBoundingClientRect();
|
| 383 |
const centerX = rect.left + rect.width / 2;
|
|
@@ -388,30 +390,30 @@
|
|
| 388 |
const maxDistance = 60;
|
| 389 |
|
| 390 |
if (distance > 5) {
|
| 391 |
-
const angle = Math.atan2(dy, dx);
|
| 392 |
const limitedDistance = Math.min(distance, maxDistance);
|
| 393 |
-
const
|
| 394 |
-
const
|
| 395 |
|
| 396 |
-
joystickHandle.style.
|
| 397 |
-
joystickHandle.style.top = `calc(50% + ${handleY}px)`;
|
| 398 |
|
| 399 |
-
//
|
| 400 |
-
|
| 401 |
-
//
|
| 402 |
-
|
| 403 |
} else {
|
| 404 |
-
|
| 405 |
-
|
|
|
|
| 406 |
}
|
| 407 |
});
|
| 408 |
|
| 409 |
-
joystick.addEventListener('touchend', () => {
|
| 410 |
joystickActive = false;
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
|
|
|
| 415 |
});
|
| 416 |
|
| 417 |
document.getElementById('upBtn').addEventListener('touchstart', (e) => {
|
|
@@ -428,32 +430,45 @@
|
|
| 428 |
let lastLookPos = { x: 0, y: 0 };
|
| 429 |
|
| 430 |
document.addEventListener('touchstart', (e) => {
|
|
|
|
|
|
|
| 431 |
const touch = e.touches[0];
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
e.preventDefault();
|
| 451 |
-
}
|
| 452 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
});
|
| 454 |
|
| 455 |
document.addEventListener('touchmove', (e) => {
|
| 456 |
-
if (!isLooking) return;
|
| 457 |
e.preventDefault();
|
| 458 |
const touch = e.touches[0];
|
| 459 |
const deltaX = touch.clientX - lastLookPos.x;
|
|
@@ -549,17 +564,20 @@
|
|
| 549 |
function animate() {
|
| 550 |
requestAnimationFrame(animate);
|
| 551 |
|
| 552 |
-
//
|
| 553 |
if (joystickActive) {
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
const
|
| 557 |
-
|
|
|
|
|
|
|
|
|
|
| 558 |
|
| 559 |
const newPos = {
|
| 560 |
-
x: playerPos.x + forwardX,
|
| 561 |
y: playerPos.y,
|
| 562 |
-
z: playerPos.z + forwardZ
|
| 563 |
};
|
| 564 |
|
| 565 |
playerPos = checkCollisions(newPos);
|
|
|
|
| 88 |
transform: translate(-50%, -50%);
|
| 89 |
cursor: grab;
|
| 90 |
box-shadow: 0 0 15px rgba(0, 150, 255, 0.6);
|
| 91 |
+
transition: none;
|
| 92 |
+
will-change: transform;
|
| 93 |
}
|
| 94 |
|
| 95 |
#joystickHandle:active {
|
|
|
|
| 279 |
|
| 280 |
let scene, camera, renderer, playerCube, garage;
|
| 281 |
let playerPos = { x: 0, y: 1, z: 0 };
|
| 282 |
+
let moveSideways = 0;
|
| 283 |
+
let moveForwardBack = 0;
|
| 284 |
+
let moveSpeed = 0.08;
|
|
|
|
| 285 |
let cameraRotation = { x: 0, y: 0 };
|
| 286 |
let spawnPoint = { x: -3.5719999999999996, y: 1, z: 0.20199999999999968 };
|
| 287 |
let joystickActive = false;
|
|
|
|
| 373 |
joystick.addEventListener('touchstart', (e) => {
|
| 374 |
joystickActive = true;
|
| 375 |
e.preventDefault();
|
| 376 |
+
e.stopPropagation();
|
| 377 |
});
|
| 378 |
|
| 379 |
joystick.addEventListener('touchmove', (e) => {
|
| 380 |
if (!joystickActive) return;
|
| 381 |
e.preventDefault();
|
| 382 |
+
e.stopPropagation();
|
| 383 |
const touch = e.touches[0];
|
| 384 |
const rect = joystick.getBoundingClientRect();
|
| 385 |
const centerX = rect.left + rect.width / 2;
|
|
|
|
| 390 |
const maxDistance = 60;
|
| 391 |
|
| 392 |
if (distance > 5) {
|
|
|
|
| 393 |
const limitedDistance = Math.min(distance, maxDistance);
|
| 394 |
+
const normalizedDx = (dx / distance) * limitedDistance;
|
| 395 |
+
const normalizedDy = (dy / distance) * limitedDistance;
|
| 396 |
|
| 397 |
+
joystickHandle.style.transform = `translate(calc(-50% + ${normalizedDx}px), calc(-50% + ${normalizedDy}px))`;
|
|
|
|
| 398 |
|
| 399 |
+
// Forward/backward: negative dy = forward, positive dy = backward
|
| 400 |
+
moveForwardBack = -(dy / maxDistance);
|
| 401 |
+
// Left/right: negative dx = left, positive dx = right
|
| 402 |
+
moveSideways = dx / maxDistance;
|
| 403 |
} else {
|
| 404 |
+
joystickHandle.style.transform = 'translate(-50%, -50%)';
|
| 405 |
+
moveForwardBack = 0;
|
| 406 |
+
moveSideways = 0;
|
| 407 |
}
|
| 408 |
});
|
| 409 |
|
| 410 |
+
joystick.addEventListener('touchend', (e) => {
|
| 411 |
joystickActive = false;
|
| 412 |
+
e.preventDefault();
|
| 413 |
+
e.stopPropagation();
|
| 414 |
+
joystickHandle.style.transform = 'translate(-50%, -50%)';
|
| 415 |
+
moveForwardBack = 0;
|
| 416 |
+
moveSideways = 0;
|
| 417 |
});
|
| 418 |
|
| 419 |
document.getElementById('upBtn').addEventListener('touchstart', (e) => {
|
|
|
|
| 430 |
let lastLookPos = { x: 0, y: 0 };
|
| 431 |
|
| 432 |
document.addEventListener('touchstart', (e) => {
|
| 433 |
+
if (joystickActive) return;
|
| 434 |
+
|
| 435 |
const touch = e.touches[0];
|
| 436 |
+
|
| 437 |
+
// Check if touching joystick area
|
| 438 |
+
const joystickRect = joystick.getBoundingClientRect();
|
| 439 |
+
if (touch.clientX >= joystickRect.left - 20 &&
|
| 440 |
+
touch.clientX <= joystickRect.right + 20 &&
|
| 441 |
+
touch.clientY >= joystickRect.top - 20 &&
|
| 442 |
+
touch.clientY <= joystickRect.bottom + 20) {
|
| 443 |
+
return;
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
// Check if touching buttons area
|
| 447 |
+
const buttonsArea = document.querySelector('.button-group');
|
| 448 |
+
const buttonsRect = buttonsArea.getBoundingClientRect();
|
| 449 |
+
if (touch.clientX >= buttonsRect.left - 20 &&
|
| 450 |
+
touch.clientX <= buttonsRect.right + 20 &&
|
| 451 |
+
touch.clientY >= buttonsRect.top - 20 &&
|
| 452 |
+
touch.clientY <= buttonsRect.bottom + 20) {
|
| 453 |
+
return;
|
|
|
|
|
|
|
| 454 |
}
|
| 455 |
+
|
| 456 |
+
// Check if touching coordinates area
|
| 457 |
+
const coordsRect = document.getElementById('coordinates').getBoundingClientRect();
|
| 458 |
+
if (touch.clientX >= coordsRect.left - 10 &&
|
| 459 |
+
touch.clientX <= coordsRect.right + 10 &&
|
| 460 |
+
touch.clientY >= coordsRect.top - 10 &&
|
| 461 |
+
touch.clientY <= coordsRect.bottom + 10) {
|
| 462 |
+
return;
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
isLooking = true;
|
| 466 |
+
lastLookPos = { x: touch.clientX, y: touch.clientY };
|
| 467 |
+
e.preventDefault();
|
| 468 |
});
|
| 469 |
|
| 470 |
document.addEventListener('touchmove', (e) => {
|
| 471 |
+
if (!isLooking || joystickActive) return;
|
| 472 |
e.preventDefault();
|
| 473 |
const touch = e.touches[0];
|
| 474 |
const deltaX = touch.clientX - lastLookPos.x;
|
|
|
|
| 564 |
function animate() {
|
| 565 |
requestAnimationFrame(animate);
|
| 566 |
|
| 567 |
+
// Calculate movement based on camera direction
|
| 568 |
if (joystickActive) {
|
| 569 |
+
// Forward/backward relative to camera
|
| 570 |
+
const forwardX = -Math.sin(cameraRotation.y) * moveForwardBack * moveSpeed;
|
| 571 |
+
const forwardZ = -Math.cos(cameraRotation.y) * moveForwardBack * moveSpeed;
|
| 572 |
+
|
| 573 |
+
// Left/right strafe relative to camera
|
| 574 |
+
const strafeX = Math.cos(cameraRotation.y) * moveSideways * moveSpeed;
|
| 575 |
+
const strafeZ = -Math.sin(cameraRotation.y) * moveSideways * moveSpeed;
|
| 576 |
|
| 577 |
const newPos = {
|
| 578 |
+
x: playerPos.x + forwardX + strafeX,
|
| 579 |
y: playerPos.y,
|
| 580 |
+
z: playerPos.z + forwardZ + strafeZ
|
| 581 |
};
|
| 582 |
|
| 583 |
playerPos = checkCollisions(newPos);
|