cutechicken commited on
Commit
b2a23a4
โ€ข
1 Parent(s): 9c39181

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +96 -79
game.js CHANGED
@@ -78,16 +78,23 @@ class TankPlayer {
78
  }
79
 
80
  shoot(scene) {
81
- if (this.isReloading || this.ammo <= 0) return null;
82
-
83
- const bullet = this.createBullet(scene);
84
- if (bullet) {
85
- this.ammo--;
86
- this.updateAmmoDisplay();
87
- this.startReload();
88
- }
89
- return bullet;
 
 
 
 
 
90
  }
 
 
91
 
92
  createBullet(scene) {
93
  const bulletGeometry = new THREE.SphereGeometry(0.2);
@@ -212,26 +219,36 @@ class Enemy {
212
  }
213
 
214
  update(playerPosition) {
215
- if (!this.mesh || !this.isLoaded) return;
216
-
217
- const direction = new THREE.Vector3()
218
- .subVectors(playerPosition, this.mesh.position)
219
- .normalize();
220
-
221
- this.mesh.lookAt(playerPosition);
 
 
 
 
 
 
 
222
  this.mesh.position.add(direction.multiplyScalar(this.moveSpeed));
 
223
 
224
- for (let i = this.bullets.length - 1; i >= 0; i--) {
225
- const bullet = this.bullets[i];
226
- bullet.position.add(bullet.velocity);
 
227
 
228
- if (Math.abs(bullet.position.x) > MAP_SIZE ||
229
- Math.abs(bullet.position.z) > MAP_SIZE) {
230
- this.scene.remove(bullet);
231
- this.bullets.splice(i, 1);
232
- }
233
  }
234
  }
 
 
235
 
236
  shoot(playerPosition) {
237
  const currentTime = Date.now();
@@ -801,63 +818,63 @@ class Game {
801
  }
802
 
803
  checkCollisions() {
804
- if (this.isLoading || !this.tank.isLoaded) return;
805
-
806
- const tankPosition = this.tank.getPosition();
807
-
808
- // ์  ์ด์•Œ๊ณผ ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ ์ถฉ๋Œ ์ฒดํฌ
809
- this.enemies.forEach(enemy => {
810
- if (!enemy.mesh || !enemy.isLoaded) return;
811
-
812
- enemy.bullets.forEach(bullet => {
813
- const distance = bullet.position.distanceTo(tankPosition);
814
- if (distance < 1) {
815
- if (this.tank.takeDamage(10)) {
816
- this.endGame();
817
- }
818
- this.scene.remove(bullet);
819
- enemy.bullets = enemy.bullets.filter(b => b !== bullet);
820
-
821
- this.createExplosion(bullet.position);
822
- document.getElementById('health').style.width =
823
- `${(this.tank.health / MAX_HEALTH) * 100}%`;
824
- }
825
- });
826
- });
827
 
828
- // ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ๊ณผ ์  ์ถฉ๋Œ ์ฒดํฌ
829
- this.tank.bullets.forEach((bullet, bulletIndex) => {
830
- this.enemies.forEach((enemy, enemyIndex) => {
831
- if (!enemy.mesh || !enemy.isLoaded) return;
832
-
833
- const distance = bullet.position.distanceTo(enemy.mesh.position);
834
- if (distance < 2) {
835
- if (enemy.takeDamage(50)) {
836
- enemy.destroy();
837
- this.enemies.splice(enemyIndex, 1);
838
- this.score += 100;
839
- document.getElementById('score').textContent = `Score: ${this.score}`;
840
- }
841
- this.scene.remove(bullet);
842
- this.tank.bullets.splice(bulletIndex, 1);
843
- this.createExplosion(bullet.position);
844
- }
845
- });
846
- });
847
 
848
- // ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ์™€ ๊ฑด๋ฌผ ์ถฉ๋Œ ์ฒดํฌ
849
- const tankBoundingBox = new THREE.Box3().setFromObject(this.tank.body);
850
- for (const building of this.buildings) {
851
- const buildingBox = new THREE.Box3().setFromObject(building);
852
- if (tankBoundingBox.intersectsBox(buildingBox)) {
853
- this.tank.body.position.copy(this.previousTankPosition);
854
- break;
855
- }
856
- }
857
-
858
- // ์ด์ „ ์œ„์น˜ ์ €์žฅ
859
- this.previousTankPosition.copy(this.tank.body.position);
860
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
861
  endGame() {
862
  if (this.isGameOver) return;
863
 
 
78
  }
79
 
80
  shoot(scene) {
81
+ if (this.isReloading || this.ammo <= 0) return null;
82
+
83
+ // ๋ฐœ์‚ฌ์Œ ํšจ๊ณผ ์ถ”๊ฐ€
84
+ const sounds = ['sounds/mbtfire1.ogg', 'sounds/mbtfire2.ogg', 'sounds/mbtfire3.ogg', 'sounds/mbtfire4.ogg'];
85
+ const randomSound = sounds[Math.floor(Math.random() * sounds.length)];
86
+ const audio = new Audio(randomSound);
87
+ audio.volume = 0.5;
88
+ audio.play();
89
+
90
+ const bullet = this.createBullet(scene);
91
+ if (bullet) {
92
+ this.ammo--;
93
+ this.updateAmmoDisplay();
94
+ this.startReload();
95
  }
96
+ return bullet;
97
+ }
98
 
99
  createBullet(scene) {
100
  const bulletGeometry = new THREE.SphereGeometry(0.2);
 
219
  }
220
 
221
  update(playerPosition) {
222
+ if (!this.mesh || !this.isLoaded) return;
223
+
224
+ const direction = new THREE.Vector3()
225
+ .subVectors(playerPosition, this.mesh.position)
226
+ .normalize();
227
+
228
+ // ํ”Œ๋ ˆ์ด์–ด์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
229
+ const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
230
+ const minDistance = 50; // ์ตœ์†Œ ์ ‘๊ทผ ๊ฑฐ๋ฆฌ
231
+
232
+ this.mesh.lookAt(playerPosition);
233
+
234
+ // ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ๋ฉ€๋ฆฌ ์žˆ์„ ๋•Œ๋งŒ ์ด๋™
235
+ if (distanceToPlayer > minDistance) {
236
  this.mesh.position.add(direction.multiplyScalar(this.moveSpeed));
237
+ }
238
 
239
+ // ์ด์•Œ ์—…๋ฐ์ดํŠธ ๋ถ€๋ถ„
240
+ for (let i = this.bullets.length - 1; i >= 0; i--) {
241
+ const bullet = this.bullets[i];
242
+ bullet.position.add(bullet.velocity);
243
 
244
+ if (Math.abs(bullet.position.x) > MAP_SIZE ||
245
+ Math.abs(bullet.position.z) > MAP_SIZE) {
246
+ this.scene.remove(bullet);
247
+ this.bullets.splice(i, 1);
 
248
  }
249
  }
250
+ }
251
+
252
 
253
  shoot(playerPosition) {
254
  const currentTime = Date.now();
 
818
  }
819
 
820
  checkCollisions() {
821
+ if (this.isLoading || !this.tank.isLoaded) return;
822
+
823
+ const tankPosition = this.tank.getPosition();
824
+ // ์  ์ด์•Œ๊ณผ ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ ์ถฉ๋Œ ์ฒดํฌ
825
+ this.enemies.forEach(enemy => {
826
+ if (!enemy.mesh || !enemy.isLoaded) return;
827
+
828
+ enemy.bullets.forEach(bullet => {
829
+ const distance = bullet.position.distanceTo(tankPosition);
830
+ if (distance < 1) {
831
+ if (this.tank.takeDamage(250)) { // ๋ฐ๋ฏธ์ง€๋ฅผ 250์œผ๋กœ ์ˆ˜์ •
832
+ this.endGame();
833
+ }
834
+ this.scene.remove(bullet);
835
+ enemy.bullets = enemy.bullets.filter(b => b !== bullet);
836
+
837
+ this.createExplosion(bullet.position);
838
+ document.getElementById('health').style.width =
839
+ `${(this.tank.health / MAX_HEALTH) * 100}%`;
840
+ }
841
+ });
842
+ });
 
843
 
844
+ // ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ๊ณผ ์  ์ถฉ๋Œ ์ฒดํฌ
845
+ this.tank.bullets.forEach((bullet, bulletIndex) => {
846
+ this.enemies.forEach((enemy, enemyIndex) => {
847
+ if (!enemy.mesh || !enemy.isLoaded) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
848
 
849
+ const distance = bullet.position.distanceTo(enemy.mesh.position);
850
+ if (distance < 2) {
851
+ if (enemy.takeDamage(50)) {
852
+ enemy.destroy();
853
+ this.enemies.splice(enemyIndex, 1);
854
+ this.score += 100;
855
+ document.getElementById('score').textContent = `Score: ${this.score}`;
856
+ }
857
+ this.scene.remove(bullet);
858
+ this.tank.bullets.splice(bulletIndex, 1);
859
+ this.createExplosion(bullet.position);
860
+ }
861
+ });
862
+ });
863
+
864
+ // ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ์™€ ์  ์ „์ฐจ ์ถฉ๋Œ ์ฒดํฌ
865
+ const tankBoundingBox = new THREE.Box3().setFromObject(this.tank.body);
866
+ this.enemies.forEach(enemy => {
867
+ if (!enemy.mesh || !enemy.isLoaded) return;
868
+
869
+ const enemyBoundingBox = new THREE.Box3().setFromObject(enemy.mesh);
870
+ if (tankBoundingBox.intersectsBox(enemyBoundingBox)) {
871
+ this.tank.body.position.copy(this.previousTankPosition);
872
+ }
873
+ });
874
+
875
+ // ์ด์ „ ์œ„์น˜ ์ €์žฅ
876
+ this.previousTankPosition.copy(this.tank.body.position);
877
+ }
878
  endGame() {
879
  if (this.isGameOver) return;
880