Spaces:
Running
Running
Update game.js
Browse files
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 |
-
|
| 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 |
-
|
| 1753 |
-
|
| 1754 |
-
|
| 1755 |
-
|
|
|
|
| 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 |
-
|
| 1768 |
-
|
| 1769 |
-
|
|
|
|
|
|
|
| 1770 |
}
|
| 1771 |
});
|
| 1772 |
});
|
| 1773 |
|
| 1774 |
-
// ์ ์ด์๊ณผ ํ๋ ์ด์ด ํฑํฌ ์ถฉ๋ ์ฒดํฌ
|
| 1775 |
-
this.enemies.forEach(enemy => {
|
| 1776 |
-
|
| 1777 |
|
| 1778 |
-
|
| 1779 |
-
|
| 1780 |
-
|
| 1781 |
-
// ๋ ์ด์บ์คํธ๋ฅผ ์ํ ์ด์ ์์น ๊ณ์ฐ
|
| 1782 |
-
const prevPosition = bullet.position.clone().sub(bullet.velocity);
|
| 1783 |
|
| 1784 |
-
|
| 1785 |
-
|
| 1786 |
-
raycaster.set(prevPosition, bullet.velocity.clone().normalize());
|
| 1787 |
|
| 1788 |
-
|
| 1789 |
-
const intersects = raycaster.intersectObject(this.tank.body, true);
|
| 1790 |
|
| 1791 |
-
|
| 1792 |
-
|
| 1793 |
-
|
| 1794 |
-
|
| 1795 |
-
|
| 1796 |
-
|
| 1797 |
-
|
| 1798 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 1814 |
-
|
| 1815 |
-
const bulletBox = new THREE.Box3().setFromObject(bullet);
|
| 1816 |
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
|
| 1820 |
-
|
| 1821 |
-
|
| 1822 |
-
|
| 1823 |
-
|
| 1824 |
-
|
| 1825 |
-
|
| 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 |
-
|
| 1838 |
-
|
| 1839 |
-
|
| 1840 |
-
|
| 1841 |
-
|
| 1842 |
-
|
| 1843 |
-
|
| 1844 |
-
|
| 1845 |
-
|
| 1846 |
-
|
| 1847 |
-
|
| 1848 |
-
|
| 1849 |
-
enemy.
|
| 1850 |
-
|
| 1851 |
-
|
| 1852 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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);
|