cutechicken commited on
Commit
71a155b
โ€ข
1 Parent(s): bf05d71

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +119 -97
game.js CHANGED
@@ -1717,42 +1717,29 @@ class Game {
1717
 
1718
  checkCollisions() {
1719
  if (this.isLoading || !this.tank.isLoaded) return;
1720
-
1721
- // ๋ช…์ค‘ ์‚ฌ์šด๋“œ์™€ ํ”ผ๊ฒฉ ์‚ฌ์šด๋“œ ๋ฐฐ์—ด ์ •์˜
1722
  const hitSounds = [
1723
  'sounds/hit1.ogg', 'sounds/hit2.ogg', 'sounds/hit3.ogg',
1724
  'sounds/hit4.ogg', 'sounds/hit5.ogg', 'sounds/hit6.ogg', 'sounds/hit7.ogg'
1725
  ];
 
 
1726
  const beatSounds = ['sounds/beat1.ogg', 'sounds/beat2.ogg', 'sounds/beat3.ogg'];
1727
 
1728
- // ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ์˜ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์ƒ์„ฑ
1729
  const tankPosition = this.tank.getPosition();
1730
  const tankBoundingBox = new THREE.Box3().setFromObject(this.tank.body);
1731
- tankBoundingBox.expandByScalar(1); // ์ถฉ๋Œ ์—ฌ์œ  ๊ณต๊ฐ„
1732
-
1733
- // 1. ํƒฑํฌ์™€ ์  ํƒฑํฌ ๊ฐ„์˜ ์ถฉ๋Œ ์ฒดํฌ
1734
- this.enemies.forEach(enemy => {
1735
- if (!enemy.mesh || !enemy.isLoaded) return;
1736
-
1737
- const enemyBoundingBox = new THREE.Box3().setFromObject(enemy.mesh);
1738
- enemyBoundingBox.expandByScalar(1);
1739
 
1740
- if (tankBoundingBox.intersectsBox(enemyBoundingBox)) {
1741
- this.tank.body.position.copy(this.previousTankPosition);
1742
- }
1743
- });
1744
-
1745
- // 2. ํƒฑํฌ์™€ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ
1746
  this.obstacles.forEach(obstacle => {
1747
- if (obstacle.userData.isCollidable) {
1748
- const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
1749
- if (tankBoundingBox.intersectsBox(obstacleBoundingBox)) {
1750
- this.tank.body.position.copy(this.previousTankPosition);
1751
- }
1752
  }
1753
- });
1754
-
1755
- // 3. ์  ํƒฑํฌ์™€ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ
1756
  this.enemies.forEach(enemy => {
1757
  if (!enemy.mesh || !enemy.isLoaded) return;
1758
 
@@ -1760,97 +1747,128 @@ class Game {
1760
  const enemyPreviousPosition = enemy.mesh.position.clone();
1761
 
1762
  this.obstacles.forEach(obstacle => {
1763
- if (obstacle.userData.isCollidable) {
1764
- const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
1765
- if (enemyBoundingBox.intersectsBox(obstacleBoundingBox)) {
1766
- enemy.mesh.position.copy(enemyPreviousPosition);
1767
- }
1768
  }
1769
  });
1770
  });
1771
 
1772
- // 4. ์  ์ด์•Œ๊ณผ ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ ์ถฉ๋Œ ์ฒดํฌ
1773
  this.enemies.forEach(enemy => {
1774
  if (!enemy.mesh || !enemy.isLoaded) return;
1775
 
1776
  enemy.bullets.forEach((bullet, bulletIndex) => {
1777
- const bulletBox = new THREE.Box3().setFromObject(bullet);
1778
- const prevPosition = bullet.position.clone().sub(bullet.velocity);
1779
-
1780
- const raycaster = new THREE.Raycaster();
1781
- raycaster.set(prevPosition, bullet.velocity.clone().normalize());
1782
-
1783
- const intersects = raycaster.intersectObject(this.tank.body, true);
1784
-
1785
- if (intersects.length > 0 || tankBoundingBox.intersectsBox(bulletBox)) {
1786
- // ํ”ผ๊ฒฉ ์‚ฌ์šด๋“œ ์žฌ์ƒ
1787
  const randomBeatSound = beatSounds[Math.floor(Math.random() * beatSounds.length)];
1788
- new Audio(randomBeatSound).play();
 
1789
 
1790
- // ๋ฐ๋ฏธ์ง€ ์ฒ˜๋ฆฌ
1791
  if (this.tank.takeDamage(250)) {
1792
  this.endGame();
1793
  }
1794
-
1795
- // ํญ๋ฐœ ํšจ๊ณผ ๋ฐ ์ด์•Œ ์ œ๊ฑฐ
1796
- this.tank.createExplosionEffect(this.scene, bullet.position);
1797
  this.scene.remove(bullet);
1798
- enemy.bullets.splice(bulletIndex, 1);
1799
 
1800
- // ์ฒด๋ ฅ๋ฐ” ์—…๋ฐ์ดํŠธ
1801
  document.getElementById('health').style.width =
1802
  `${(this.tank.health / MAX_HEALTH) * 100}%`;
1803
  }
1804
  });
1805
  });
1806
 
1807
- // 5. ํ”Œ๋ ˆ์ด์–ด ํฌํƒ„๊ณผ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ
1808
- for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
1809
- const bullet = this.tank.bullets[i];
1810
- const bulletBox = new THREE.Box3().setFromObject(bullet);
 
1811
 
1812
- for (const obstacle of this.obstacles) {
1813
- if (obstacle.userData.isCollidable) {
1814
- const obstacleBox = new THREE.Box3().setFromObject(obstacle);
1815
- if (bulletBox.intersectsBox(obstacleBox)) {
1816
- this.tank.createExplosionEffect(this.scene, bullet.position);
1817
- this.scene.remove(bullet);
1818
- this.tank.bullets.splice(i, 1);
1819
- break;
1820
- }
 
 
1821
  }
1822
  }
1823
  }
 
 
 
 
 
1824
 
1825
- // 6. ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ๊ณผ ์  ์ถฉ๋Œ ์ฒดํฌ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1826
  for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
1827
- const bullet = this.tank.bullets[i];
1828
- for (let j = this.enemies.length - 1; j >= 0; j--) {
1829
- const enemy = this.enemies[j];
1830
- if (!enemy.mesh || !enemy.isLoaded) continue;
1831
-
1832
- const distance = bullet.position.distanceTo(enemy.mesh.position);
1833
- if (distance < 2) {
1834
- // ๋ช…์ค‘ ์‚ฌ์šด๋“œ ์žฌ์ƒ
1835
- const randomHitSound = hitSounds[Math.floor(Math.random() * hitSounds.length)];
1836
- new Audio(randomHitSound).play();
1837
-
1838
- // ์  ํƒฑํฌ ๋ฐ๋ฏธ์ง€ ์ฒ˜๋ฆฌ
1839
- if (enemy.takeDamage(50)) {
1840
- enemy.destroy();
1841
- this.enemies.splice(j, 1);
1842
- this.score += 100;
1843
- document.getElementById('score').textContent = `Score: ${this.score}`;
1844
- }
1845
-
1846
- // ํญ๋ฐœ ํšจ๊ณผ ๋ฐ ์ด์•Œ ์ œ๊ฑฐ
1847
- this.tank.createExplosionEffect(this.scene, bullet.position);
1848
- this.scene.remove(bullet);
1849
- this.tank.bullets.splice(i, 1);
1850
- break;
1851
  }
 
 
 
 
 
 
 
1852
  }
1853
  }
 
 
 
 
 
 
 
 
 
 
 
1854
 
1855
  // ์ด์ „ ์œ„์น˜ ์ €์žฅ
1856
  this.previousTankPosition.copy(this.tank.body.position);
@@ -1927,19 +1945,23 @@ class Game {
1927
  }
1928
  // ํฌ๋กœ์Šคํ—ค์–ด ์—…๋ฐ์ดํŠธ ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€
1929
  updateCrosshair() {
1930
- // ํ™”๋ฉด ์ค‘์•™ ์ขŒํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ ˆ์ด์บ์Šคํ„ฐ ์ƒ์„ฑ
1931
- this.raycaster.setFromCamera(new THREE.Vector2(0, 0), this.camera);
 
1932
 
1933
- // ์  ์ „์ฐจ ๋ฉ”์‹œ๋“ค๋งŒ ๋ชจ์•„์„œ ๋ฐฐ์—ด ์ƒ์„ฑ
1934
- const enemyMeshes = this.enemies
1935
- .filter(enemy => enemy.mesh && enemy.isLoaded)
1936
- .map(enemy => enemy.mesh);
1937
-
1938
- // ๋ ˆ์ด์™€ ์  ์ „์ฐจ ๋ฉ”์‹œ๋“ค์˜ ๊ต์ฐจ ๊ฒ€์‚ฌ
1939
- const intersects = this.raycaster.intersectObjects(enemyMeshes, true);
 
 
 
 
1940
 
1941
- // ๊ต์ฐจ์ ์ด ์žˆ์œผ๋ฉด ํฌ๋กœ์Šคํ—ค์–ด ์ƒํƒœ ๋ณ€๊ฒฝ
1942
- if (intersects.length > 0) {
1943
  this.crosshair.classList.add('target-detected');
1944
  } else {
1945
  this.crosshair.classList.remove('target-detected');
 
1717
 
1718
  checkCollisions() {
1719
  if (this.isLoading || !this.tank.isLoaded) return;
1720
+
1721
+ // ๋ช…์ค‘ ์‚ฌ์šด๋“œ ๋ฐฐ์—ด ์ •์˜
1722
  const hitSounds = [
1723
  'sounds/hit1.ogg', 'sounds/hit2.ogg', 'sounds/hit3.ogg',
1724
  'sounds/hit4.ogg', 'sounds/hit5.ogg', 'sounds/hit6.ogg', 'sounds/hit7.ogg'
1725
  ];
1726
+
1727
+ // ํ”ผ๊ฒฉ ์‚ฌ์šด๋“œ ๋ฐฐ์—ด ์ •์˜
1728
  const beatSounds = ['sounds/beat1.ogg', 'sounds/beat2.ogg', 'sounds/beat3.ogg'];
1729
 
 
1730
  const tankPosition = this.tank.getPosition();
1731
  const tankBoundingBox = new THREE.Box3().setFromObject(this.tank.body);
 
 
 
 
 
 
 
 
1732
 
1733
+ // ํƒฑํฌ์™€ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ (๊ฐœ์„ )
 
 
 
 
 
1734
  this.obstacles.forEach(obstacle => {
1735
+ if (obstacle.userData.isCollidable) { // ์ถฉ๋Œ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋งŒ ๊ฒ€์‚ฌ
1736
+ const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
1737
+ if (tankBoundingBox.intersectsBox(obstacleBoundingBox)) {
1738
+ this.tank.body.position.copy(this.previousTankPosition);
 
1739
  }
1740
+ }
1741
+ });
1742
+ // ์  ํƒฑํฌ์™€ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ (์ถ”๊ฐ€)
1743
  this.enemies.forEach(enemy => {
1744
  if (!enemy.mesh || !enemy.isLoaded) return;
1745
 
 
1747
  const enemyPreviousPosition = enemy.mesh.position.clone();
1748
 
1749
  this.obstacles.forEach(obstacle => {
1750
+ const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
1751
+ if (enemyBoundingBox.intersectsBox(obstacleBoundingBox)) {
1752
+ enemy.mesh.position.copy(enemyPreviousPosition);
 
 
1753
  }
1754
  });
1755
  });
1756
 
1757
+ // ์  ์ด์•Œ๊ณผ ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ ์ถฉ๋Œ ์ฒดํฌ
1758
  this.enemies.forEach(enemy => {
1759
  if (!enemy.mesh || !enemy.isLoaded) return;
1760
 
1761
  enemy.bullets.forEach((bullet, bulletIndex) => {
1762
+ const distance = bullet.position.distanceTo(tankPosition);
1763
+ if (distance < 1) {
1764
+ // ํ”Œ๋ ˆ์ด์–ด ํ”ผ๊ฒฉ ์‚ฌ์šด๋“œ ์žฌ์ƒ
 
 
 
 
 
 
 
1765
  const randomBeatSound = beatSounds[Math.floor(Math.random() * beatSounds.length)];
1766
+ const beatAudio = new Audio(randomBeatSound);
1767
+ beatAudio.play();
1768
 
 
1769
  if (this.tank.takeDamage(250)) {
1770
  this.endGame();
1771
  }
 
 
 
1772
  this.scene.remove(bullet);
1773
+ enemy.bullets.splice(bulletIndex, 1); // filter ๋Œ€์‹  splice ์‚ฌ์šฉ
1774
 
1775
+ this.createExplosion(bullet.position);
1776
  document.getElementById('health').style.width =
1777
  `${(this.tank.health / MAX_HEALTH) * 100}%`;
1778
  }
1779
  });
1780
  });
1781
 
1782
+ // ํ”Œ๋ ˆ์ด์–ด ํฌํƒ„
1783
+ // ํฌํƒ„๊ณผ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ
1784
+ for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
1785
+ const bullet = this.tank.bullets[i];
1786
+ const bulletBox = new THREE.Box3().setFromObject(bullet);
1787
 
1788
+ for (const obstacle of this.obstacles) {
1789
+ if (obstacle.userData.isCollidable) { // ์ถฉ๋Œ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋งŒ ๊ฒ€์‚ฌ
1790
+ const obstacleBox = new THREE.Box3().setFromObject(obstacle);
1791
+ if (bulletBox.intersectsBox(obstacleBox)) {
1792
+ // ํญ๋ฐœ ์ดํŽ™ํŠธ ์ƒ์„ฑ
1793
+ this.tank.createExplosionEffect(this.scene, bullet.position);
1794
+
1795
+ // ํฌํƒ„ ์ œ๊ฑฐ
1796
+ this.scene.remove(bullet);
1797
+ this.tank.bullets.splice(i, 1);
1798
+ break;
1799
  }
1800
  }
1801
  }
1802
+ }
1803
+
1804
+ // ์  ํƒฑํฌ์™€ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ
1805
+ this.enemies.forEach(enemy => {
1806
+ if (!enemy.mesh || !enemy.isLoaded) return;
1807
 
1808
+ enemy.bullets.forEach((bullet, bulletIndex) => {
1809
+ const distance = bullet.position.distanceTo(tankPosition);
1810
+ if (distance < 1) {
1811
+ // ํ”ผ๊ฒฉ ์‚ฌ์šด๋“œ ์žฌ์ƒ
1812
+ const randomBeatSound = beatSounds[Math.floor(Math.random() * beatSounds.length)];
1813
+ const beatAudio = new Audio(randomBeatSound);
1814
+ beatAudio.play();
1815
+
1816
+ if (this.tank.takeDamage(250)) {
1817
+ this.endGame();
1818
+ }
1819
+
1820
+ // ๊ธฐ์กด์˜ createExplosion ๋Œ€์‹  createExplosionEffect ์‚ฌ์šฉ
1821
+ this.tank.createExplosionEffect(this.scene, bullet.position);
1822
+
1823
+ this.scene.remove(bullet);
1824
+ enemy.bullets.splice(bulletIndex, 1);
1825
+
1826
+ document.getElementById('health').style.width =
1827
+ `${(this.tank.health / MAX_HEALTH) * 100}%`;
1828
+ }
1829
+ });
1830
+ });
1831
+
1832
+
1833
+ // ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ๊ณผ ์  ์ถฉ๋Œ ์ฒดํฌ
1834
  for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
1835
+ const bullet = this.tank.bullets[i];
1836
+ for (let j = this.enemies.length - 1; j >= 0; j--) {
1837
+ const enemy = this.enemies[j];
1838
+ if (!enemy.mesh || !enemy.isLoaded) continue;
1839
+
1840
+ const distance = bullet.position.distanceTo(enemy.mesh.position);
1841
+ if (distance < 2) {
1842
+ const randomHitSound = hitSounds[Math.floor(Math.random() * hitSounds.length)];
1843
+ const hitAudio = new Audio(randomHitSound);
1844
+ hitAudio.play();
1845
+
1846
+ if (enemy.takeDamage(50)) {
1847
+ enemy.destroy();
1848
+ this.enemies.splice(j, 1);
1849
+ this.score += 100;
1850
+ document.getElementById('score').textContent = `Score: ${this.score}`;
 
 
 
 
 
 
 
 
1851
  }
1852
+
1853
+ // ์—ฌ๊ธฐ๋„ ๋™์ผํ•˜๊ฒŒ ์ˆ˜์ •
1854
+ this.tank.createExplosionEffect(this.scene, bullet.position);
1855
+
1856
+ this.scene.remove(bullet);
1857
+ this.tank.bullets.splice(i, 1);
1858
+ break;
1859
  }
1860
  }
1861
+ }
1862
+
1863
+ // ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ์™€ ์  ์ „์ฐจ ์ถฉ๋Œ ์ฒดํฌ
1864
+ this.enemies.forEach(enemy => {
1865
+ if (!enemy.mesh || !enemy.isLoaded) return;
1866
+
1867
+ const enemyBoundingBox = new THREE.Box3().setFromObject(enemy.mesh);
1868
+ if (tankBoundingBox.intersectsBox(enemyBoundingBox)) {
1869
+ this.tank.body.position.copy(this.previousTankPosition);
1870
+ }
1871
+ });
1872
 
1873
  // ์ด์ „ ์œ„์น˜ ์ €์žฅ
1874
  this.previousTankPosition.copy(this.tank.body.position);
 
1945
  }
1946
  // ํฌ๋กœ์Šคํ—ค์–ด ์—…๋ฐ์ดํŠธ ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€
1947
  updateCrosshair() {
1948
+ // ํ™”๋ฉด ์ค‘์•™์—์„œ ์•ฝ๊ฐ„์˜ ์—ฌ์œ ๋ฅผ ๋‘๊ณ  ๋ ˆ์ด์บ์ŠคํŒ…
1949
+ const raycasterDirection = new THREE.Vector2();
1950
+ this.raycaster.setFromCamera(raycasterDirection, this.camera);
1951
 
1952
+ // ์  ์ „์ฐจ์˜ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๋„ ํฌํ•จํ•˜์—ฌ ๊ฒ€์‚ฌ
1953
+ const detectEnemy = this.enemies.some(enemy => {
1954
+ if (!enemy.mesh || !enemy.isLoaded) return false;
1955
+
1956
+ // ์  ์ „์ฐจ์˜ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์ƒ์„ฑ
1957
+ const boundingBox = new THREE.Box3().setFromObject(enemy.mesh);
1958
+ const intersects = this.raycaster.ray.intersectsBox(boundingBox);
1959
+
1960
+ // ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค์™€์˜ ๊ต์ฐจ ์—ฌ๋ถ€๋กœ ํŒ๋‹จ
1961
+ return intersects;
1962
+ });
1963
 
1964
+ if (detectEnemy) {
 
1965
  this.crosshair.classList.add('target-detected');
1966
  } else {
1967
  this.crosshair.classList.remove('target-detected');