cutechicken commited on
Commit
ebc3901
β€’
1 Parent(s): 1bd8588

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +26 -18
game.js CHANGED
@@ -105,10 +105,13 @@ class TankPlayer {
105
  update(mouseX, mouseY, scene) {
106
  if (!this.body || !this.turretGroup) return;
107
 
108
- // 포탑 νšŒμ „ - 마우슀 이동에 따라 자유둭게 360도 νšŒμ „
109
- this.turretGroup.rotation.y += mouseX * 0.03;
 
 
 
110
 
111
- // ν”Œλ ˆμ΄μ–΄ μ΄μ•Œ 이동 처리
112
  for (let i = this.bullets.length - 1; i >= 0; i--) {
113
  const bullet = this.bullets[i];
114
  bullet.position.add(bullet.velocity);
@@ -481,9 +484,14 @@ async addDesertDecorations() {
481
  document.addEventListener('mousemove', (event) => {
482
  if (this.isLoading || this.isGameOver || !document.pointerLockElement) return;
483
 
484
- // μ»€μ„œ 이동에 따라 κ°’ μ—…λ°μ΄νŠΈ (νšŒμ „ 각도λ₯Ό μ œν•œ)
485
- this.mouse.x = Math.max(Math.min(this.mouse.x + event.movementX * 0.002, 1), -1);
486
- this.mouse.y = Math.max(Math.min(this.mouse.y + event.movementY * 0.002, 1), -1);
 
 
 
 
 
487
  });
488
 
489
 
@@ -532,26 +540,26 @@ async addDesertDecorations() {
532
  this.tank.move(direction);
533
  }
534
 
535
- // 카메라 μœ„μΉ˜ μ—…λ°μ΄νŠΈ - 포탑 νšŒμ „λ§Œ 따라가도둝 μˆ˜μ •
536
  const tankPos = this.tank.getPosition();
537
- const turretRotation = this.tank.turretGroup.rotation.y;
538
  const cameraDistance = 30;
539
  const cameraHeight = 15;
540
 
541
- // 카메라 μœ„μΉ˜λ₯Ό 포탑 νšŒμ „μ— 맞좰 μ„€μ •
542
  this.camera.position.set(
543
- tankPos.x - Math.sin(turretRotation) * cameraDistance,
544
  tankPos.y + cameraHeight,
545
- tankPos.z - Math.cos(turretRotation) * cameraDistance
546
- );
547
-
548
- // 카메라가 항상 포탑을 바라보도둝 μ„€μ •
549
- const lookAtPoint = new THREE.Vector3(
550
- tankPos.x + Math.sin(turretRotation) * 10,
551
- tankPos.y + 5,
552
- tankPos.z + Math.cos(turretRotation) * 10
553
  );
554
 
 
 
 
 
 
 
 
555
  this.camera.lookAt(lookAtPoint);
556
  }
557
 
 
105
  update(mouseX, mouseY, scene) {
106
  if (!this.body || !this.turretGroup) return;
107
 
108
+ // 포탑 νšŒμ „ - 마우슀 μœ„μΉ˜μ— 따라 νšŒμ „
109
+ const screenCenter = new THREE.Vector2(0, 0);
110
+ const mousePosition = new THREE.Vector2(mouseX, mouseY);
111
+ const angle = Math.atan2(mousePosition.x - screenCenter.x, mousePosition.y - screenCenter.y);
112
+ this.turretGroup.rotation.y = angle;
113
 
114
+ // ν”Œλ ˆμ΄μ–΄ μ΄μ•Œ μ—…λ°μ΄νŠΈ
115
  for (let i = this.bullets.length - 1; i >= 0; i--) {
116
  const bullet = this.bullets[i];
117
  bullet.position.add(bullet.velocity);
 
484
  document.addEventListener('mousemove', (event) => {
485
  if (this.isLoading || this.isGameOver || !document.pointerLockElement) return;
486
 
487
+ // 마우슀 μ›€μ§μž„μ„ ν™”λ©΄ 쀑앙 κΈ°μ€€μœΌλ‘œ μ •κ·œν™”
488
+ const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
489
+ const movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
490
+
491
+ this.mouse.x += movementX * 0.002;
492
+ this.mouse.y += movementY * 0.002;
493
+
494
+ // 마우슀 이동
495
  });
496
 
497
 
 
540
  this.tank.move(direction);
541
  }
542
 
543
+ // 카메라 μœ„μΉ˜ μ—…λ°μ΄νŠΈ - 항상 탱크 λ’€μ—μ„œ 바라보도둝 μ„€μ •
544
  const tankPos = this.tank.getPosition();
545
+ const tankRotation = this.tank.body.rotation.y;
546
  const cameraDistance = 30;
547
  const cameraHeight = 15;
548
 
549
+ // νƒ±ν¬μ˜ λ’€μͺ½ μœ„μΉ˜μ— 카메라 배치
550
  this.camera.position.set(
551
+ tankPos.x - Math.sin(tankRotation) * cameraDistance,
552
  tankPos.y + cameraHeight,
553
+ tankPos.z - Math.cos(tankRotation) * cameraDistance
 
 
 
 
 
 
 
554
  );
555
 
556
+ // 카메라가 항상 탱크λ₯Ό 바라보도둝 μ„€μ •
557
+ this.camera.lookAt(new THREE.Vector3(
558
+ tankPos.x,
559
+ tankPos.y,
560
+ tankPos.z
561
+ ));
562
+ //}
563
  this.camera.lookAt(lookAtPoint);
564
  }
565