Spaces:
Running
Running
Update game.js
Browse files
game.js
CHANGED
|
@@ -78,16 +78,23 @@ class TankPlayer {
|
|
| 78 |
}
|
| 79 |
|
| 80 |
shoot(scene) {
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
this.mesh.position.add(direction.multiplyScalar(this.moveSpeed));
|
|
|
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
}
|
| 233 |
}
|
| 234 |
}
|
|
|
|
|
|
|
| 235 |
|
| 236 |
shoot(playerPosition) {
|
| 237 |
const currentTime = Date.now();
|
|
@@ -801,63 +818,63 @@ class Game {
|
|
| 801 |
}
|
| 802 |
|
| 803 |
checkCollisions() {
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
});
|
| 827 |
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 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 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 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 |
|