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

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +53 -53
game.js CHANGED
@@ -40,11 +40,11 @@ class TankPlayer {
40
 
41
  async initialize(scene, loader) {
42
  try {
43
- const bodyResult = await loader.loadAsync('/models/abramsBlow.glb');
44
  this.body = bodyResult.scene;
45
  this.body.position.copy(this.position);
46
 
47
- const turretResult = await loader.loadAsync('/models/abramsTlow.glb');
48
  this.turret = turretResult.scene;
49
 
50
  this.turretGroup.position.y = 0.2;
@@ -105,12 +105,6 @@ class TankPlayer {
105
  update(mouseX, mouseY, scene) {
106
  if (!this.body || !this.turretGroup) return;
107
 
108
- // ํฌํƒ‘ ํšŒ์ „ ๊ณ„์‚ฐ ์ˆ˜์ •
109
- const rotationAngle = Math.atan2(mouseX, mouseY);
110
- this.turretRotation = rotationAngle;
111
- this.turretGroup.rotation.y = this.turretRotation;
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);
@@ -121,6 +115,13 @@ class TankPlayer {
121
  this.bullets.splice(i, 1);
122
  }
123
  }
 
 
 
 
 
 
 
124
  }
125
 
126
  move(direction) {
@@ -164,7 +165,7 @@ class Enemy {
164
 
165
  async initialize(loader) {
166
  try {
167
- const modelPath = this.type === 'tank' ? '/models/enemy1.glb' : '/models/enemy1.glb';
168
  const result = await loader.loadAsync(modelPath);
169
  this.mesh = result.scene;
170
  this.mesh.position.copy(this.position);
@@ -509,54 +510,53 @@ async addDesertDecorations() {
509
  }
510
 
511
  handleMovement() {
512
- if (!this.tank.isLoaded || this.isGameOver) return;
513
-
514
- const direction = new THREE.Vector3();
515
-
516
- if (this.keys.forward) direction.z += 1;
517
- if (this.keys.backward) direction.z -= 1;
518
- if (this.keys.left) direction.x -= 1;
519
- if (this.keys.right) direction.x += 1;
520
-
521
- if (direction.length() > 0) {
522
- direction.normalize();
523
-
524
- if (this.keys.left) this.tank.rotate(-1);
525
- if (this.keys.right) this.tank.rotate(1);
526
-
527
- direction.applyEuler(this.tank.body.rotation);
528
- this.tank.move(direction);
529
- }
530
-
531
- // ๋งˆ์šฐ์Šค ์›€์ง์ž„์— ๋”ฐ๋ฅธ ํฌํƒ‘ ํšŒ์ „ ์ฒ˜๋ฆฌ
532
- const tankRotation = this.tank.body.rotation.y;
533
- const mouseAngle = Math.atan2(this.mouse.x, -this.mouse.y);
534
- const finalRotation = mouseAngle + tankRotation;
535
 
536
- if (this.tank.turretGroup) {
537
- this.tank.turretGroup.rotation.y = finalRotation;
538
- }
539
-
540
- // ์นด๋ฉ”๋ผ ์œ„์น˜ ์—…๋ฐ์ดํŠธ
541
- const tankPos = this.tank.getPosition();
542
- const cameraDistance = 30;
543
- const cameraHeight = 15;
544
- const lookAtHeight = 5;
545
 
546
- this.camera.position.set(
547
- tankPos.x - Math.sin(finalRotation) * cameraDistance,
548
- tankPos.y + cameraHeight,
549
- tankPos.z - Math.cos(finalRotation) * cameraDistance
550
- );
551
 
552
- const lookAtPoint = new THREE.Vector3(
553
- tankPos.x + Math.sin(finalRotation) * 10,
554
- tankPos.y + lookAtHeight,
555
- tankPos.z + Math.cos(finalRotation) * 10
556
- );
557
-
558
- this.camera.lookAt(lookAtPoint);
559
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  }
561
 
562
  createBuildings() {
 
40
 
41
  async initialize(scene, loader) {
42
  try {
43
+ const bodyResult = await loader.loadAsync('/models/abramsBody.glb');
44
  this.body = bodyResult.scene;
45
  this.body.position.copy(this.position);
46
 
47
+ const turretResult = await loader.loadAsync('/models/abramsTurret.glb');
48
  this.turret = turretResult.scene;
49
 
50
  this.turretGroup.position.y = 0.2;
 
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);
 
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) {
 
165
 
166
  async initialize(loader) {
167
  try {
168
+ const modelPath = this.type === 'tank' ? '/models/enemy1.glb' : '/models/enemy4.glb';
169
  const result = await loader.loadAsync(modelPath);
170
  this.mesh = result.scene;
171
  this.mesh.position.copy(this.position);
 
510
  }
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() {