cutechicken commited on
Commit
e40e2dd
โ€ข
1 Parent(s): 53ab759

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +49 -51
game.js CHANGED
@@ -102,27 +102,29 @@ class TankPlayer {
102
  return bullet;
103
  }
104
 
105
- update(mouseX, mouseY, scene) {
106
- if (!this.body || !this.turretGroup) return;
107
-
108
- for (let i = this.bullets.length - 1; i >= 0; i--) {
109
- const bullet = this.bullets[i];
110
- bullet.position.add(bullet.velocity);
111
-
112
- if (Math.abs(bullet.position.x) > MAP_SIZE/2 ||
113
- Math.abs(bullet.position.z) > MAP_SIZE/2) {
114
- scene.remove(bullet);
115
- this.bullets.splice(i, 1);
116
- }
117
- }
118
-
119
- // ํฌํƒ‘ ํšŒ์ „ ๋ฐฉํ–ฅ ์ˆ˜์ •
120
- if (this.turretGroup) {
121
- const rotationAngle = -Math.atan2(mouseX, mouseY); // ๋ถ€ํ˜ธ๋ฅผ ๋ฐ˜๋Œ€๋กœ ํ•˜์—ฌ ํšŒ์ „ ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
122
- this.turretRotation = rotationAngle;
123
- this.turretGroup.rotation.y = this.turretRotation;
124
  }
125
  }
 
 
126
 
127
  move(direction) {
128
  if (!this.body) return;
@@ -479,10 +481,13 @@ async addDesertDecorations() {
479
  });
480
 
481
  document.addEventListener('mousemove', (event) => {
482
- if (this.isLoading || this.isGameOver || !document.pointerLockElement) return;
483
- this.mouse.x += event.movementX * 0.002;
484
- this.mouse.y += event.movementY * 0.002;
485
- });
 
 
 
486
 
487
  document.addEventListener('click', () => {
488
  if (!document.pointerLockElement) {
@@ -511,54 +516,47 @@ async addDesertDecorations() {
511
 
512
  handleMovement() {
513
  if (!this.tank.isLoaded || this.isGameOver) return;
514
-
515
  const direction = new THREE.Vector3();
516
-
517
  if (this.keys.forward) direction.z += 1;
518
  if (this.keys.backward) direction.z -= 1;
519
  if (this.keys.left) direction.x -= 1;
520
  if (this.keys.right) direction.x += 1;
521
-
522
  if (direction.length() > 0) {
523
  direction.normalize();
524
-
525
  if (this.keys.left) this.tank.rotate(-1);
526
  if (this.keys.right) this.tank.rotate(1);
527
-
528
  direction.applyEuler(this.tank.body.rotation);
529
  this.tank.move(direction);
530
  }
531
 
532
- // ํฌํƒ‘ ํšŒ์ „ ์—…๋ฐ์ดํŠธ
533
- const mouseVector = new THREE.Vector2(this.mouse.x, -this.mouse.y);
534
- const rotationAngle = -Math.atan2(mouseVector.x, mouseVector.y); // ๋ถ€ํ˜ธ ๋ณ€๊ฒฝ
535
-
536
- if (this.tank.turretGroup) {
537
- this.tank.turretGroup.rotation.y = rotationAngle;
538
- }
539
-
540
  // ์นด๋ฉ”๋ผ ์œ„์น˜ ์—…๋ฐ์ดํŠธ
541
  const tankPos = this.tank.getPosition();
 
542
  const cameraDistance = 30;
543
  const cameraHeight = 15;
544
- const lookAtHeight = 5;
545
-
546
- const turretRotation = this.tank.turretGroup.rotation.y + this.tank.body.rotation.y;
547
- this.camera.position.set(
548
- tankPos.x - Math.sin(turretRotation) * cameraDistance,
549
- tankPos.y + cameraHeight,
550
- tankPos.z - Math.cos(turretRotation) * cameraDistance
551
- );
552
-
553
- const lookAtPoint = new THREE.Vector3(
554
- tankPos.x + Math.sin(turretRotation) * 10,
555
- tankPos.y + lookAtHeight,
556
- tankPos.z + Math.cos(turretRotation) * 10
557
- );
558
-
559
  this.camera.lookAt(lookAtPoint);
560
  }
561
 
 
562
  createBuildings() {
563
  const buildingTypes = [
564
  { width: 10, height: 30, depth: 10, color: 0x808080 },
 
102
  return bullet;
103
  }
104
 
105
+ update(mouseX, mouseY, scene) {
106
+ if (!this.body || !this.turretGroup) return;
107
+
108
+ // ํฌํƒ‘ ํšŒ์ „ ๋ฐฉํ–ฅ ์„ค์ •
109
+ const turretDirection = new THREE.Vector3(mouseX, 0, -mouseY).normalize();
110
+ const angle = Math.atan2(turretDirection.x, turretDirection.z);
111
+ this.turretGroup.rotation.y = angle;
112
+
113
+ // ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ ์ด๋™ ์ฒ˜๋ฆฌ
114
+ for (let i = this.bullets.length - 1; i >= 0; i--) {
115
+ const bullet = this.bullets[i];
116
+ bullet.position.add(bullet.velocity);
117
+
118
+ if (
119
+ Math.abs(bullet.position.x) > MAP_SIZE / 2 ||
120
+ Math.abs(bullet.position.z) > MAP_SIZE / 2
121
+ ) {
122
+ scene.remove(bullet);
123
+ this.bullets.splice(i, 1);
124
  }
125
  }
126
+ }
127
+
128
 
129
  move(direction) {
130
  if (!this.body) return;
 
481
  });
482
 
483
  document.addEventListener('mousemove', (event) => {
484
+ if (this.isLoading || this.isGameOver || !document.pointerLockElement) return;
485
+
486
+ // ์ปค์„œ ์ด๋™์— ๋”ฐ๋ผ ๊ฐ’ ์—…๋ฐ์ดํŠธ (ํšŒ์ „ ๊ฐ๋„๋ฅผ ์ œํ•œ)
487
+ this.mouse.x = Math.max(Math.min(this.mouse.x + event.movementX * 0.002, 1), -1);
488
+ this.mouse.y = Math.max(Math.min(this.mouse.y + event.movementY * 0.002, 1), -1);
489
+ });
490
+
491
 
492
  document.addEventListener('click', () => {
493
  if (!document.pointerLockElement) {
 
516
 
517
  handleMovement() {
518
  if (!this.tank.isLoaded || this.isGameOver) return;
519
+
520
  const direction = new THREE.Vector3();
521
+
522
  if (this.keys.forward) direction.z += 1;
523
  if (this.keys.backward) direction.z -= 1;
524
  if (this.keys.left) direction.x -= 1;
525
  if (this.keys.right) direction.x += 1;
526
+
527
  if (direction.length() > 0) {
528
  direction.normalize();
529
+
530
  if (this.keys.left) this.tank.rotate(-1);
531
  if (this.keys.right) this.tank.rotate(1);
532
+
533
  direction.applyEuler(this.tank.body.rotation);
534
  this.tank.move(direction);
535
  }
536
 
 
 
 
 
 
 
 
 
537
  // ์นด๋ฉ”๋ผ ์œ„์น˜ ์—…๋ฐ์ดํŠธ
538
  const tankPos = this.tank.getPosition();
539
+ const turretRotation = this.tank.turretGroup.rotation.y + this.tank.body.rotation.y;
540
  const cameraDistance = 30;
541
  const cameraHeight = 15;
542
+
543
+ this.camera.position.set(
544
+ tankPos.x - Math.sin(turretRotation) * cameraDistance,
545
+ tankPos.y + cameraHeight,
546
+ tankPos.z - Math.cos(turretRotation) * cameraDistance
547
+ );
548
+
549
+ // ์นด๋ฉ”๋ผ๊ฐ€ ํ•ญ์ƒ ํฌํƒ‘์˜ ์•ž ๋ฐฉํ–ฅ์„ ๋ฐ”๋ผ๋ณด๊ฒŒ ์„ค์ •
550
+ const lookAtPoint = new THREE.Vector3(
551
+ tankPos.x + Math.sin(turretRotation) * 10,
552
+ tankPos.y + 5,
553
+ tankPos.z + Math.cos(turretRotation) * 10
554
+ );
555
+
 
556
  this.camera.lookAt(lookAtPoint);
557
  }
558
 
559
+
560
  createBuildings() {
561
  const buildingTypes = [
562
  { width: 10, height: 30, depth: 10, color: 0x808080 },