cutechicken commited on
Commit
bf05d71
โ€ข
1 Parent(s): 65ff90d

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +87 -107
game.js CHANGED
@@ -1717,46 +1717,42 @@ 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
-
1727
- // ํ”ผ๊ฒฉ ์‚ฌ์šด๋“œ ๋ฐฐ์—ด ์ •์˜
1728
  const beatSounds = ['sounds/beat1.ogg', 'sounds/beat2.ogg', 'sounds/beat3.ogg'];
1729
 
 
1730
  const tankPosition = this.tank.getPosition();
1731
- // ์ „์ฒด ํƒฑํฌ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์ƒ์„ฑ
1732
  const tankBoundingBox = new THREE.Box3().setFromObject(this.tank.body);
1733
- // ์—ฌ์œ  ๊ณต๊ฐ„์„ ์ฃผ๊ธฐ ์œ„ํ•ด ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ํ™•์žฅ
1734
- tankBoundingBox.expandByScalar(1); // ๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ 1์œ ๋‹› ํ™•์žฅ
1735
- // ์  ํƒฑํฌ๋“ค์˜ ์ถฉ๋Œ ๊ฒ€์‚ฌ
1736
  this.enemies.forEach(enemy => {
1737
  if (!enemy.mesh || !enemy.isLoaded) return;
1738
 
1739
- // ์  ํƒฑํฌ์˜ ์ „์ฒด ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์ƒ์„ฑ
1740
  const enemyBoundingBox = new THREE.Box3().setFromObject(enemy.mesh);
1741
- enemyBoundingBox.expandByScalar(1); // ์—ฌ์œ  ๊ณต๊ฐ„
1742
 
1743
- // ํƒฑํฌ์™€ ์  ํƒฑํฌ ๊ฐ„์˜ ์ถฉ๋Œ ์ฒดํฌ
1744
  if (tankBoundingBox.intersectsBox(enemyBoundingBox)) {
1745
- // ์ถฉ๋Œ ์‹œ ์ด์ „ ์œ„์น˜๋กœ ๋ณต๊ท€
1746
  this.tank.body.position.copy(this.previousTankPosition);
1747
  }
1748
  });
1749
 
1750
- // ํƒฑํฌ์™€ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ (๊ฐœ์„ )
1751
  this.obstacles.forEach(obstacle => {
1752
- if (obstacle.userData.isCollidable) { // ์ถฉ๋Œ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋งŒ ๊ฒ€์‚ฌ
1753
- const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
1754
- if (tankBoundingBox.intersectsBox(obstacleBoundingBox)) {
1755
- this.tank.body.position.copy(this.previousTankPosition);
 
1756
  }
1757
- }
1758
- });
1759
- // ์  ํƒฑํฌ์™€ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ (์ถ”๊ฐ€)
1760
  this.enemies.forEach(enemy => {
1761
  if (!enemy.mesh || !enemy.isLoaded) return;
1762
 
@@ -1764,113 +1760,97 @@ class Game {
1764
  const enemyPreviousPosition = enemy.mesh.position.clone();
1765
 
1766
  this.obstacles.forEach(obstacle => {
1767
- const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
1768
- if (enemyBoundingBox.intersectsBox(obstacleBoundingBox)) {
1769
- enemy.mesh.position.copy(enemyPreviousPosition);
 
 
1770
  }
1771
  });
1772
  });
1773
 
1774
- // ์  ์ด์•Œ๊ณผ ํ”Œ๋ ˆ์ด์–ด ํƒฑํฌ ์ถฉ๋Œ ์ฒดํฌ
1775
- this.enemies.forEach(enemy => {
1776
- if (!enemy.mesh || !enemy.isLoaded) return;
1777
 
1778
- enemy.bullets.forEach((bullet, bulletIndex) => {
1779
- // ์ด์•Œ์˜ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์ƒ์„ฑ
1780
- const bulletBox = new THREE.Box3().setFromObject(bullet);
1781
- // ๋ ˆ์ด์บ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์ด์ „ ์œ„์น˜ ๊ณ„์‚ฐ
1782
- const prevPosition = bullet.position.clone().sub(bullet.velocity);
1783
 
1784
- // ๋ ˆ์ด์บ์Šคํ„ฐ ์„ค์ •
1785
- const raycaster = new THREE.Raycaster();
1786
- raycaster.set(prevPosition, bullet.velocity.clone().normalize());
1787
 
1788
- // ํƒฑํฌ ๋ชจ๋ธ๊ณผ์˜ ๊ต์ฐจ ๊ฒ€์‚ฌ
1789
- const intersects = raycaster.intersectObject(this.tank.body, true);
1790
 
1791
- if (intersects.length > 0 || tankBoundingBox.intersectsBox(bulletBox)) {
1792
- // ํ”Œ๋ ˆ์ด์–ด ํ”ผ๊ฒฉ ์‚ฌ์šด๋“œ ์žฌ์ƒ
1793
- const randomBeatSound = beatSounds[Math.floor(Math.random() * beatSounds.length)];
1794
- const beatAudio = new Audio(randomBeatSound);
1795
- beatAudio.play();
1796
-
1797
- if (this.tank.takeDamage(250)) {
1798
- this.endGame();
 
 
 
 
 
 
 
 
 
 
1799
  }
1800
-
1801
- this.tank.createExplosionEffect(this.scene, bullet.position);
1802
- this.scene.remove(bullet);
1803
- enemy.bullets.splice(bulletIndex, 1);
1804
-
1805
- document.getElementById('health').style.width =
1806
- `${(this.tank.health / MAX_HEALTH) * 100}%`;
1807
- }
1808
  });
1809
- });
1810
 
1811
- // ํ”Œ๋ ˆ์ด์–ด ํฌํƒ„
1812
- // ํฌํƒ„๊ณผ ์žฅ์• ๋ฌผ ์ถฉ๋Œ ์ฒดํฌ
1813
- for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
1814
- const bullet = this.tank.bullets[i];
1815
- const bulletBox = new THREE.Box3().setFromObject(bullet);
1816
 
1817
- for (const obstacle of this.obstacles) {
1818
- if (obstacle.userData.isCollidable) { // ์ถฉ๋Œ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋งŒ ๊ฒ€์‚ฌ
1819
- const obstacleBox = new THREE.Box3().setFromObject(obstacle);
1820
- if (bulletBox.intersectsBox(obstacleBox)) {
1821
- // ํญ๋ฐœ ์ดํŽ™ํŠธ ์ƒ์„ฑ
1822
- this.tank.createExplosionEffect(this.scene, bullet.position);
1823
-
1824
- // ํฌํƒ„ ์ œ๊ฑฐ
1825
- this.scene.remove(bullet);
1826
- this.tank.bullets.splice(i, 1);
1827
- break;
1828
  }
1829
  }
1830
  }
1831
- }
1832
-
1833
-
1834
 
1835
- // ํ”Œ๋ ˆ์ด์–ด ์ด์•Œ๊ณผ ์  ์ถฉ๋Œ ์ฒดํฌ
1836
  for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
1837
- const bullet = this.tank.bullets[i];
1838
- for (let j = this.enemies.length - 1; j >= 0; j--) {
1839
- const enemy = this.enemies[j];
1840
- if (!enemy.mesh || !enemy.isLoaded) continue;
1841
-
1842
- const distance = bullet.position.distanceTo(enemy.mesh.position);
1843
- if (distance < 2) {
1844
- const randomHitSound = hitSounds[Math.floor(Math.random() * hitSounds.length)];
1845
- const hitAudio = new Audio(randomHitSound);
1846
- hitAudio.play();
1847
-
1848
- if (enemy.takeDamage(50)) {
1849
- enemy.destroy();
1850
- this.enemies.splice(j, 1);
1851
- this.score += 100;
1852
- document.getElementById('score').textContent = `Score: ${this.score}`;
 
 
 
 
 
 
 
 
1853
  }
1854
-
1855
- // ์—ฌ๊ธฐ๋„ ๋™์ผํ•˜๊ฒŒ ์ˆ˜์ •
1856
- this.tank.createExplosionEffect(this.scene, bullet.position);
1857
-
1858
- this.scene.remove(bullet);
1859
- this.tank.bullets.splice(i, 1);
1860
- break;
1861
  }
1862
  }
1863
- }
1864
-
1865
- // ํ”Œ๋ ˆ์ด์–ด ๏ฟฝ๏ฟฝ๏ฟฝํฌ์™€ ์  ์ „์ฐจ ์ถฉ๋Œ ์ฒดํฌ
1866
- this.enemies.forEach(enemy => {
1867
- if (!enemy.mesh || !enemy.isLoaded) return;
1868
-
1869
- const enemyBoundingBox = new THREE.Box3().setFromObject(enemy.mesh);
1870
- if (tankBoundingBox.intersectsBox(enemyBoundingBox)) {
1871
- this.tank.body.position.copy(this.previousTankPosition);
1872
- }
1873
- });
1874
 
1875
  // ์ด์ „ ์œ„์น˜ ์ €์žฅ
1876
  this.previousTankPosition.copy(this.tank.body.position);
 
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
  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);