cutechicken commited on
Commit
168be5c
โ€ข
1 Parent(s): a7b799c

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +21 -13
game.js CHANGED
@@ -106,10 +106,14 @@ class TankPlayer {
106
  update(mouseX, mouseY, scene) {
107
  if (!this.body || !this.turretGroup) return;
108
 
109
- // ๋งˆ์šฐ์Šค ๋ฐฉํ–ฅ์œผ๋กœ ํฌํƒ‘ ํšŒ์ „
110
- const angle = Math.atan2(mouseX, mouseY);
111
- this.turretGroup.rotation.y = angle;
112
- this.turretRotation = angle; // ํ˜„์žฌ ํฌํƒ‘ ํšŒ์ „ ๊ฐ๋„ ์ €์žฅ
 
 
 
 
113
 
114
  // ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ ์—…๋ฐ์ดํŠธ
115
  for (let i = this.bullets.length - 1; i >= 0; i--) {
@@ -127,6 +131,7 @@ update(mouseX, mouseY, scene) {
127
  }
128
 
129
 
 
130
  move(direction) {
131
  if (!this.body) return;
132
 
@@ -525,6 +530,7 @@ async addDesertDecorations() {
525
 
526
  const direction = new THREE.Vector3();
527
 
 
528
  if (this.keys.forward) direction.z += 1;
529
  if (this.keys.backward) direction.z -= 1;
530
  if (this.keys.left) direction.x -= 1;
@@ -533,26 +539,28 @@ async addDesertDecorations() {
533
  if (direction.length() > 0) {
534
  direction.normalize();
535
 
 
536
  if (this.keys.left) this.tank.rotate(-1);
537
  if (this.keys.right) this.tank.rotate(1);
538
 
 
539
  direction.applyEuler(this.tank.body.rotation);
540
  this.tank.move(direction);
541
  }
542
 
543
- // ํƒฑํฌ์™€ ํฌํƒ‘์˜ ํ˜„์žฌ ์œ„์น˜/ํšŒ์ „ ๊ฐ€์ ธ์˜ค๊ธฐ
544
  const tankPos = this.tank.getPosition();
545
- const turretRotation = this.tank.turretRotation;
546
-
547
- // ์นด๋ฉ”๋ผ ์œ„์น˜ ๊ณ„์‚ฐ
548
  const cameraDistance = 30;
549
  const cameraHeight = 15;
 
550
 
551
- // ํฌํƒ‘ ํšŒ์ „์„ ๊ธฐ์ค€์œผ๋กœ ์นด๋ฉ”๋ผ ์œ„์น˜ ๊ณ„์‚ฐ (ํฌํƒ‘์˜ ๋ฐ˜๋Œ€ํŽธ)
552
- const cameraX = tankPos.x - Math.sin(turretRotation + Math.PI) * cameraDistance;
553
- const cameraZ = tankPos.z - Math.cos(turretRotation + Math.PI) * cameraDistance;
554
 
555
- // ์นด๋ฉ”๋ผ ์œ„์น˜ ์„ค์ •
556
  this.camera.position.set(
557
  cameraX,
558
  tankPos.y + cameraHeight,
@@ -562,7 +570,7 @@ async addDesertDecorations() {
562
  // ์นด๋ฉ”๋ผ๊ฐ€ ํƒฑํฌ๋ฅผ ๋ฐ”๋ผ๋ณด๋„๋ก ์„ค์ •
563
  this.camera.lookAt(new THREE.Vector3(
564
  tankPos.x,
565
- tankPos.y + 2, // ์•ฝ๊ฐ„ ์œ„๋กœ ์˜ฌ๋ ค์„œ ํฌํƒ‘์„ ๋” ์ž˜ ๋ณด์ด๊ฒŒ ํ•จ
566
  tankPos.z
567
  ));
568
  }
 
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
+ // ํฌํƒ‘ ํšŒ์ „์— 180๋„(ฯ€)๋ฅผ ๋”ํ•ด์„œ ํฌ์‹ ์ด ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์„ ํ–ฅํ•˜๋„๋ก ํ•จ
115
+ this.turretGroup.rotation.y = angle + Math.PI;
116
+ this.turretRotation = angle + Math.PI;
117
 
118
  // ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ ์—…๋ฐ์ดํŠธ
119
  for (let i = this.bullets.length - 1; i >= 0; i--) {
 
131
  }
132
 
133
 
134
+
135
  move(direction) {
136
  if (!this.body) return;
137
 
 
530
 
531
  const direction = new THREE.Vector3();
532
 
533
+ // ์ด๋™ ๋ฐฉํ–ฅ ๊ณ„์‚ฐ
534
  if (this.keys.forward) direction.z += 1;
535
  if (this.keys.backward) direction.z -= 1;
536
  if (this.keys.left) direction.x -= 1;
 
539
  if (direction.length() > 0) {
540
  direction.normalize();
541
 
542
+ // ํƒฑํฌ ํšŒ์ „
543
  if (this.keys.left) this.tank.rotate(-1);
544
  if (this.keys.right) this.tank.rotate(1);
545
 
546
+ // ์ด๋™ ๋ฐฉํ–ฅ์„ ํƒฑํฌ์˜ ํšŒ์ „์— ๋งž์ถฐ ์กฐ์ •
547
  direction.applyEuler(this.tank.body.rotation);
548
  this.tank.move(direction);
549
  }
550
 
551
+ // ํƒฑํฌ ์œ„์น˜ ๊ฐ€์ ธ์˜ค๊ธฐ
552
  const tankPos = this.tank.getPosition();
553
+
554
+ // ์นด๋ฉ”๋ผ ์œ„์น˜ ๊ณ„์‚ฐ - ๊ณ ์ •๋œ ๋ฐฉํ–ฅ์œผ๋กœ ์„ค์ •
 
555
  const cameraDistance = 30;
556
  const cameraHeight = 15;
557
+ const fixedCameraAngle = Math.PI; // ์นด๋ฉ”๋ผ๋Š” ํ•ญ์ƒ ๋‚จ์ชฝ(ฯ€)์„ ํ–ฅํ•˜๋„๋ก ์„ค์ •
558
 
559
+ // ์นด๋ฉ”๋ผ ์œ„์น˜ ๊ณ„์‚ฐ (ํƒฑํฌ ํšŒ์ „๊ณผ ๋…๋ฆฝ์ )
560
+ const cameraX = tankPos.x - Math.sin(fixedCameraAngle) * cameraDistance;
561
+ const cameraZ = tankPos.z - Math.cos(fixedCameraAngle) * cameraDistance;
562
 
563
+ // ์นด๋ฉ”๋ผ ์œ„์น˜ ๋ฐ ์‹œ์  ์„ค์ •
564
  this.camera.position.set(
565
  cameraX,
566
  tankPos.y + cameraHeight,
 
570
  // ์นด๋ฉ”๋ผ๊ฐ€ ํƒฑํฌ๋ฅผ ๋ฐ”๋ผ๋ณด๋„๋ก ์„ค์ •
571
  this.camera.lookAt(new THREE.Vector3(
572
  tankPos.x,
573
+ tankPos.y + 2,
574
  tankPos.z
575
  ));
576
  }