cutechicken commited on
Commit
ca7a3bc
Β·
verified Β·
1 Parent(s): 01a4765

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +20 -10
game.js CHANGED
@@ -106,13 +106,21 @@ class TankPlayer {
106
  update(mouseX, mouseY, scene) {
107
  if (!this.body || !this.turretGroup) return;
108
 
109
- // 마우슀 이동에 λ”°λ₯Έ 포탑 νšŒμ „ - 360도 νšŒμ „ κ°€λŠ₯ν•˜λ„λ‘ μˆ˜μ •
110
- let angle = Math.atan2(mouseX, mouseY);
111
- // 각도λ₯Ό 0~2Ο€ λ²”μœ„λ‘œ μ •κ·œν™”
112
- if (angle < 0) angle += Math.PI * 2;
 
 
 
113
 
114
- this.turretGroup.rotation.y = angle;
115
- this.turretRotation = angle;
 
 
 
 
 
116
 
117
  // ν”Œλ ˆμ΄μ–΄ μ΄μ•Œ μ—…λ°μ΄νŠΈ
118
  for (let i = this.bullets.length - 1; i >= 0; i--) {
@@ -547,8 +555,9 @@ async addDesertDecorations() {
547
  const cameraDistance = 30;
548
  const cameraHeight = 15;
549
 
550
- // ν¬νƒ‘μ˜ ν˜„μž¬ νšŒμ „ κ°λ„μ—μ„œ 180도λ₯Ό 더해 항상 ν¬νƒ‘μ˜ 뒀에 μœ„μΉ˜ν•˜λ„λ‘ 함
551
- const cameraAngle = this.tank.turretRotation + Math.PI;
 
552
 
553
  const cameraX = tankPos.x + Math.sin(cameraAngle) * cameraDistance;
554
  const cameraZ = tankPos.z + Math.cos(cameraAngle) * cameraDistance;
@@ -561,11 +570,12 @@ async addDesertDecorations() {
561
  );
562
 
563
  // 카메라가 탱크λ₯Ό 바라보도둝 μ„€μ •
564
- this.camera.lookAt(new THREE.Vector3(
565
  tankPos.x,
566
  tankPos.y + 2,
567
  tankPos.z
568
- ));
 
569
  }
570
  //this.camera.lookAt(lookAtPoint);
571
 
 
106
  update(mouseX, mouseY, scene) {
107
  if (!this.body || !this.turretGroup) return;
108
 
109
+ // 마우슀 이동에 λ”°λ₯Έ 포탑 νšŒμ „
110
+ // 차체의 νšŒμ „μ„ κ³ λ €ν•˜μ—¬ μƒλŒ€μ μΈ 포탑 νšŒμ „ 계산
111
+ let targetAngle = Math.atan2(mouseX, mouseY);
112
+ if (targetAngle < 0) targetAngle += Math.PI * 2;
113
+
114
+ // 차체의 ν˜„μž¬ νšŒμ „κ°’μ„ κ³ λ €ν•˜μ—¬ ν¬νƒ‘μ˜ μƒλŒ€ νšŒμ „ 계산
115
+ let relativeAngle = targetAngle - this.body.rotation.y;
116
 
117
+ // 각도λ₯Ό -Ο€μ—μ„œ Ο€ μ‚¬μ΄λ‘œ μ •κ·œν™”
118
+ while (relativeAngle > Math.PI) relativeAngle -= Math.PI * 2;
119
+ while (relativeAngle < -Math.PI) relativeAngle += Math.PI * 2;
120
+
121
+ // 포탑 νšŒμ „ μ œν•œ μ—†μŒ (360도 νšŒμ „ κ°€λŠ₯)
122
+ this.turretGroup.rotation.y = relativeAngle;
123
+ this.turretRotation = targetAngle; // 전체 νšŒμ „κ° μ €μž₯
124
 
125
  // ν”Œλ ˆμ΄μ–΄ μ΄μ•Œ μ—…λ°μ΄νŠΈ
126
  for (let i = this.bullets.length - 1; i >= 0; i--) {
 
555
  const cameraDistance = 30;
556
  const cameraHeight = 15;
557
 
558
+ // 포탑 전체 νšŒμ „κ° μ‚¬μš© (차체 νšŒμ „ + 포탑 μƒλŒ€ νšŒμ „)
559
+ const totalRotation = this.tank.body.rotation.y + this.tank.turretGroup.rotation.y;
560
+ const cameraAngle = totalRotation + Math.PI;
561
 
562
  const cameraX = tankPos.x + Math.sin(cameraAngle) * cameraDistance;
563
  const cameraZ = tankPos.z + Math.cos(cameraAngle) * cameraDistance;
 
570
  );
571
 
572
  // 카메라가 탱크λ₯Ό 바라보도둝 μ„€μ •
573
+ const lookAtPoint = new THREE.Vector3(
574
  tankPos.x,
575
  tankPos.y + 2,
576
  tankPos.z
577
+ );
578
+ this.camera.lookAt(lookAtPoint);
579
  }
580
  //this.camera.lookAt(lookAtPoint);
581