Spaces:
Running
Running
Update game.js
Browse files
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 |
-
|
| 1741 |
-
this.tank.body.position.copy(this.previousTankPosition);
|
| 1742 |
-
}
|
| 1743 |
-
});
|
| 1744 |
-
|
| 1745 |
-
// 2. ํฑํฌ์ ์ฅ์ ๋ฌผ ์ถฉ๋ ์ฒดํฌ
|
| 1746 |
this.obstacles.forEach(obstacle => {
|
| 1747 |
-
|
| 1748 |
-
|
| 1749 |
-
|
| 1750 |
-
|
| 1751 |
-
}
|
| 1752 |
}
|
| 1753 |
-
}
|
| 1754 |
-
|
| 1755 |
-
|
| 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 |
-
|
| 1764 |
-
|
| 1765 |
-
|
| 1766 |
-
enemy.mesh.position.copy(enemyPreviousPosition);
|
| 1767 |
-
}
|
| 1768 |
}
|
| 1769 |
});
|
| 1770 |
});
|
| 1771 |
|
| 1772 |
-
//
|
| 1773 |
this.enemies.forEach(enemy => {
|
| 1774 |
if (!enemy.mesh || !enemy.isLoaded) return;
|
| 1775 |
|
| 1776 |
enemy.bullets.forEach((bullet, bulletIndex) => {
|
| 1777 |
-
const
|
| 1778 |
-
|
| 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)
|
|
|
|
| 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 |
-
//
|
| 1808 |
-
|
| 1809 |
-
|
| 1810 |
-
|
|
|
|
| 1811 |
|
| 1812 |
-
|
| 1813 |
-
|
| 1814 |
-
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
|
| 1820 |
-
|
|
|
|
|
|
|
| 1821 |
}
|
| 1822 |
}
|
| 1823 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1824 |
|
| 1825 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1826 |
for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
-
|
| 1831 |
-
|
| 1832 |
-
|
| 1833 |
-
|
| 1834 |
-
|
| 1835 |
-
|
| 1836 |
-
|
| 1837 |
-
|
| 1838 |
-
|
| 1839 |
-
|
| 1840 |
-
|
| 1841 |
-
|
| 1842 |
-
|
| 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 |
-
|
|
|
|
| 1932 |
|
| 1933 |
-
// ์
|
| 1934 |
-
const
|
| 1935 |
-
|
| 1936 |
-
|
| 1937 |
-
|
| 1938 |
-
|
| 1939 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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');
|