Spaces:
Running
Running
Update game.js
Browse files
game.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
| 1 |
import * as THREE from 'three';
|
| 2 |
-
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
|
| 3 |
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
|
| 4 |
|
| 5 |
// κ²μ μμ
|
| 6 |
const GAME_DURATION = 180;
|
| 7 |
const MAP_SIZE = 2000;
|
| 8 |
-
const HELICOPTER_HEIGHT = 30;
|
| 9 |
const ENEMY_GROUND_HEIGHT = 0;
|
| 10 |
-
const ENEMY_SCALE = 10;
|
| 11 |
const MAX_HEALTH = 1000;
|
| 12 |
const ENEMY_MOVE_SPEED = 0.1;
|
| 13 |
-
const
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
'/models/enemy13.glb',
|
| 17 |
-
'/models/enemy14.glb'
|
| 18 |
-
];
|
| 19 |
const ENEMY_CONFIG = {
|
| 20 |
ATTACK_RANGE: 100,
|
| 21 |
ATTACK_INTERVAL: 2000,
|
|
@@ -32,323 +28,130 @@ let ammo = 30;
|
|
| 32 |
let currentStage = 1;
|
| 33 |
let isGameOver = false;
|
| 34 |
let lastTime = performance.now();
|
|
|
|
| 35 |
|
| 36 |
-
// μ΄μ리 μμ±κΈ°
|
| 37 |
class GunSoundGenerator {
|
| 38 |
-
constructor(
|
| 39 |
-
this.audioContext =
|
| 40 |
}
|
| 41 |
|
| 42 |
createGunshot() {
|
| 43 |
-
|
| 44 |
-
const mainOsc = this.audioContext.createOscillator();
|
| 45 |
-
mainOsc.type = 'square';
|
| 46 |
-
mainOsc.frequency.setValueAtTime(100, this.audioContext.currentTime);
|
| 47 |
-
mainOsc.frequency.exponentialRampToValueAtTime(1, this.audioContext.currentTime + 0.1);
|
| 48 |
-
|
| 49 |
-
// λ
Έμ΄μ¦ μμ±
|
| 50 |
-
const noiseBuffer = this.createNoiseBuffer();
|
| 51 |
-
const noise = this.audioContext.createBufferSource();
|
| 52 |
-
noise.buffer = noiseBuffer;
|
| 53 |
-
|
| 54 |
-
// κ²μΈ λ
Έλλ€
|
| 55 |
-
const mainGain = this.audioContext.createGain();
|
| 56 |
-
const noiseGain = this.audioContext.createGain();
|
| 57 |
-
|
| 58 |
-
// λ³Όλ₯¨ μ벨λ‘ν μ€μ
|
| 59 |
-
mainGain.gain.setValueAtTime(1, this.audioContext.currentTime);
|
| 60 |
-
mainGain.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + 0.1);
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
const bufferSize = this.audioContext.sampleRate * 0.1;
|
| 80 |
const buffer = this.audioContext.createBuffer(1, bufferSize, this.audioContext.sampleRate);
|
| 81 |
-
const
|
| 82 |
-
|
| 83 |
for (let i = 0; i < bufferSize; i++) {
|
| 84 |
-
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
return buffer;
|
| 88 |
-
}
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
// μ¬μ΄λ μμ€ν
|
| 92 |
-
class SoundSystem {
|
| 93 |
-
constructor() {
|
| 94 |
-
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
| 95 |
-
this.gunSoundGenerator = new GunSoundGenerator(this.audioContext);
|
| 96 |
-
this.buffers = new Map();
|
| 97 |
-
this.pools = new Map();
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
async loadSound(name, url) {
|
| 101 |
-
try {
|
| 102 |
-
const response = await fetch(url);
|
| 103 |
-
const arrayBuffer = await response.arrayBuffer();
|
| 104 |
-
const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
|
| 105 |
-
this.buffers.set(name, audioBuffer);
|
| 106 |
-
this.createPool(name, audioBuffer);
|
| 107 |
-
console.log(`Sound loaded: ${name}`);
|
| 108 |
-
} catch (error) {
|
| 109 |
-
console.error(`Error loading sound ${name}:`, error);
|
| 110 |
-
}
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
createPool(name, buffer, size = 10) {
|
| 114 |
-
const pool = [];
|
| 115 |
-
for (let i = 0; i < size; i++) {
|
| 116 |
-
pool.push({
|
| 117 |
-
source: null,
|
| 118 |
-
gainNode: this.audioContext.createGain(),
|
| 119 |
-
lastPlayed: 0
|
| 120 |
-
});
|
| 121 |
}
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
if (!pool || !buffer) {
|
| 133 |
-
console.warn(`Sound not found: ${name}`);
|
| 134 |
-
return;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
const now = this.audioContext.currentTime;
|
| 138 |
-
const poolItem = pool.find(item => !item.source || item.lastPlayed + buffer.duration <= now);
|
| 139 |
-
|
| 140 |
-
if (poolItem) {
|
| 141 |
-
if (poolItem.source) {
|
| 142 |
-
poolItem.source.disconnect();
|
| 143 |
-
}
|
| 144 |
-
|
| 145 |
-
poolItem.source = this.audioContext.createBufferSource();
|
| 146 |
-
poolItem.source.buffer = buffer;
|
| 147 |
-
poolItem.source.connect(poolItem.gainNode);
|
| 148 |
-
poolItem.gainNode.connect(this.audioContext.destination);
|
| 149 |
-
poolItem.source.start(0);
|
| 150 |
-
poolItem.lastPlayed = now;
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
// μ¬μ΄λ μμ€ν
μ΄κΈ°ν
|
| 156 |
-
const
|
| 157 |
-
|
| 158 |
-
// μ΄λ μν
|
| 159 |
-
const moveState = {
|
| 160 |
-
forward: false,
|
| 161 |
-
backward: false,
|
| 162 |
-
left: false,
|
| 163 |
-
right: false
|
| 164 |
-
};
|
| 165 |
-
|
| 166 |
-
async function initSounds() {
|
| 167 |
-
try {
|
| 168 |
-
await soundSystem.loadSound('explosion', 'explosion.wav');
|
| 169 |
-
console.log('All sounds loaded successfully');
|
| 170 |
-
} catch (error) {
|
| 171 |
-
console.error('Error initializing sounds:', error);
|
| 172 |
-
}
|
| 173 |
-
}
|
| 174 |
|
| 175 |
async function init() {
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
renderer.shadowMap.enabled = true;
|
| 194 |
-
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
|
| 195 |
-
document.body.appendChild(renderer.domElement);
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
try {
|
| 218 |
-
await initSounds();
|
| 219 |
-
await createTerrain();
|
| 220 |
-
await loadEnemies();
|
| 221 |
-
await testModelLoading();
|
| 222 |
-
|
| 223 |
document.getElementById('loading').style.display = 'none';
|
| 224 |
-
|
| 225 |
-
console.log('Active enemies:', enemies.length);
|
| 226 |
} catch (error) {
|
| 227 |
console.error('Initialization error:', error);
|
|
|
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
-
function
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
const outlineMesh = model.clone();
|
| 238 |
-
outlineMesh.traverse((node) => {
|
| 239 |
-
if (node.isMesh) {
|
| 240 |
-
node.material = outlineMaterial;
|
| 241 |
-
node.scale.multiplyScalar(1.05);
|
| 242 |
-
}
|
| 243 |
-
});
|
| 244 |
-
|
| 245 |
-
model.add(outlineMesh);
|
| 246 |
-
}
|
| 247 |
-
|
| 248 |
-
async function loadEnemies() {
|
| 249 |
-
console.log('Starting enemy loading...');
|
| 250 |
-
const loader = new GLTFLoader();
|
| 251 |
-
const enemyCount = 3 + currentStage;
|
| 252 |
-
const loadPromises = [];
|
| 253 |
-
|
| 254 |
-
for (let i = 0; i < enemyCount; i++) {
|
| 255 |
-
const angle = (i / enemyCount) * Math.PI * 2;
|
| 256 |
-
const radius = 200;
|
| 257 |
-
const position = new THREE.Vector3(
|
| 258 |
-
Math.cos(angle) * radius,
|
| 259 |
-
ENEMY_GROUND_HEIGHT,
|
| 260 |
-
Math.sin(angle) * radius
|
| 261 |
-
);
|
| 262 |
-
|
| 263 |
-
console.log(`Creating enemy ${i} at position:`, position);
|
| 264 |
-
|
| 265 |
-
// μμ μ μμ±
|
| 266 |
-
const tempGeometry = new THREE.BoxGeometry(5, 5, 5);
|
| 267 |
-
const tempMaterial = new THREE.MeshPhongMaterial({
|
| 268 |
-
color: 0xff0000,
|
| 269 |
-
transparent: true,
|
| 270 |
-
opacity: 0.8,
|
| 271 |
-
emissive: 0xff0000,
|
| 272 |
-
emissiveIntensity: 0.5
|
| 273 |
-
});
|
| 274 |
-
const tempEnemy = new THREE.Mesh(tempGeometry, tempMaterial);
|
| 275 |
-
tempEnemy.position.copy(position);
|
| 276 |
-
tempEnemy.castShadow = true;
|
| 277 |
-
tempEnemy.receiveShadow = true;
|
| 278 |
-
scene.add(tempEnemy);
|
| 279 |
-
|
| 280 |
-
// μμ μ μ λΉ μΆκ°
|
| 281 |
-
const tempLight = new THREE.PointLight(0xff0000, 1, 20);
|
| 282 |
-
tempLight.position.set(0, 5, 0);
|
| 283 |
-
tempEnemy.add(tempLight);
|
| 284 |
-
|
| 285 |
-
const enemy = {
|
| 286 |
-
model: tempEnemy,
|
| 287 |
-
health: 100,
|
| 288 |
-
speed: ENEMY_MOVE_SPEED,
|
| 289 |
-
lastAttackTime: 0,
|
| 290 |
-
position: position.clone()
|
| 291 |
-
};
|
| 292 |
-
enemies.push(enemy);
|
| 293 |
-
|
| 294 |
-
// GLB λͺ¨λΈ λ‘λ©
|
| 295 |
-
const modelPath = ENEMY_MODELS[i % ENEMY_MODELS.length];
|
| 296 |
-
const loadPromise = new Promise((resolve, reject) => {
|
| 297 |
-
loader.load(
|
| 298 |
-
modelPath,
|
| 299 |
-
(gltf) => {
|
| 300 |
-
console.log(`Successfully loaded model: ${modelPath}`);
|
| 301 |
-
const model = gltf.scene;
|
| 302 |
-
model.scale.set(ENEMY_SCALE, ENEMY_SCALE, ENEMY_SCALE);
|
| 303 |
-
model.position.copy(position);
|
| 304 |
-
|
| 305 |
-
model.traverse((node) => {
|
| 306 |
-
if (node.isMesh) {
|
| 307 |
-
node.castShadow = true;
|
| 308 |
-
node.receiveShadow = true;
|
| 309 |
-
node.material.metalness = 0.2;
|
| 310 |
-
node.material.roughness = 0.8;
|
| 311 |
-
node.material.emissive = new THREE.Color(0x444444);
|
| 312 |
-
node.material.emissiveIntensity = 0.5;
|
| 313 |
-
}
|
| 314 |
-
});
|
| 315 |
-
|
| 316 |
-
// μμλΌμΈ ν¨κ³Ό μΆκ°
|
| 317 |
-
createEnemyOutline(model);
|
| 318 |
-
|
| 319 |
-
// μ μ£Όλ³μ λΉ μΆκ°
|
| 320 |
-
const enemyLight = new THREE.PointLight(0xff0000, 1, 20);
|
| 321 |
-
enemyLight.position.set(0, 5, 0);
|
| 322 |
-
model.add(enemyLight);
|
| 323 |
-
|
| 324 |
-
scene.remove(tempEnemy);
|
| 325 |
-
scene.add(model);
|
| 326 |
-
enemy.model = model;
|
| 327 |
-
resolve();
|
| 328 |
-
},
|
| 329 |
-
(xhr) => {
|
| 330 |
-
console.log(`${modelPath}: ${(xhr.loaded / xhr.total * 100)}% loaded`);
|
| 331 |
-
},
|
| 332 |
-
(error) => {
|
| 333 |
-
console.error(`Error loading model ${modelPath}:`, error);
|
| 334 |
-
reject(error);
|
| 335 |
-
}
|
| 336 |
-
);
|
| 337 |
-
});
|
| 338 |
-
loadPromises.push(loadPromise);
|
| 339 |
-
}
|
| 340 |
-
|
| 341 |
-
try {
|
| 342 |
-
await Promise.all(loadPromises);
|
| 343 |
-
console.log('All enemies loaded successfully');
|
| 344 |
-
} catch (error) {
|
| 345 |
-
console.error('Error loading enemies:', error);
|
| 346 |
-
}
|
| 347 |
}
|
| 348 |
|
| 349 |
function createTerrain() {
|
| 350 |
return new Promise((resolve) => {
|
| 351 |
-
|
|
|
|
| 352 |
const material = new THREE.MeshStandardMaterial({
|
| 353 |
color: 0xD2B48C,
|
| 354 |
roughness: 0.8,
|
|
@@ -368,6 +171,7 @@ function createTerrain() {
|
|
| 368 |
terrain.receiveShadow = true;
|
| 369 |
scene.add(terrain);
|
| 370 |
|
|
|
|
| 371 |
addObstacles();
|
| 372 |
resolve();
|
| 373 |
});
|
|
@@ -380,7 +184,8 @@ function addObstacles() {
|
|
| 380 |
roughness: 0.9
|
| 381 |
});
|
| 382 |
|
| 383 |
-
|
|
|
|
| 384 |
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
|
| 385 |
rock.position.set(
|
| 386 |
(Math.random() - 0.5) * MAP_SIZE * 0.9,
|
|
@@ -398,19 +203,61 @@ function addObstacles() {
|
|
| 398 |
}
|
| 399 |
}
|
| 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
function createExplosion(position) {
|
| 402 |
-
const particleCount = 30;
|
| 403 |
const particles = [];
|
| 404 |
-
|
| 405 |
-
for (let i = 0; i < particleCount; i++) {
|
| 406 |
const particle = new THREE.Mesh(
|
| 407 |
new THREE.SphereGeometry(0.3),
|
| 408 |
new THREE.MeshBasicMaterial({
|
| 409 |
color: 0xff4400,
|
| 410 |
transparent: true,
|
| 411 |
-
opacity: 1
|
| 412 |
-
emissive: 0xff4400,
|
| 413 |
-
emissiveIntensity: 1
|
| 414 |
})
|
| 415 |
);
|
| 416 |
|
|
@@ -423,39 +270,38 @@ function createExplosion(position) {
|
|
| 423 |
|
| 424 |
particles.push(particle);
|
| 425 |
scene.add(particle);
|
| 426 |
-
|
| 427 |
-
// νλ° μ§μ μ μΌμμ μΈ λΉ μΆκ°
|
| 428 |
-
const explosionLight = new THREE.PointLight(0xff4400, 2, 20);
|
| 429 |
-
explosionLight.position.copy(position);
|
| 430 |
-
scene.add(explosionLight);
|
| 431 |
-
setTimeout(() => scene.remove(explosionLight), 100);
|
| 432 |
}
|
| 433 |
|
| 434 |
-
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
particle.position.add(particle.velocity);
|
| 437 |
-
particle.material.opacity
|
| 438 |
-
|
| 439 |
-
if (particle.material.opacity <= 0) {
|
| 440 |
-
scene.remove(particle);
|
| 441 |
-
particles.splice(i, 1);
|
| 442 |
-
}
|
| 443 |
});
|
| 444 |
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
animateExplosion();
|
| 451 |
-
soundSystem.play('explosion');
|
| 452 |
}
|
| 453 |
|
| 454 |
-
// μ΄λ²€νΈ νΈλ€λ¬
|
| 455 |
function onClick() {
|
| 456 |
if (!controls.isLocked) {
|
| 457 |
controls.lock();
|
| 458 |
-
|
| 459 |
} else if (ammo > 0) {
|
| 460 |
shoot();
|
| 461 |
}
|
|
@@ -486,6 +332,14 @@ function onWindowResize() {
|
|
| 486 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 487 |
}
|
| 488 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
function shoot() {
|
| 490 |
if (ammo <= 0) return;
|
| 491 |
|
|
@@ -495,9 +349,9 @@ function shoot() {
|
|
| 495 |
const bullet = createBullet();
|
| 496 |
bullets.push(bullet);
|
| 497 |
|
| 498 |
-
|
| 499 |
|
| 500 |
-
// μ΄κ΅¬ νμΌ ν¨κ³Ό
|
| 501 |
const muzzleFlash = new THREE.PointLight(0xffff00, 3, 10);
|
| 502 |
muzzleFlash.position.copy(camera.position);
|
| 503 |
scene.add(muzzleFlash);
|
|
@@ -505,42 +359,37 @@ function shoot() {
|
|
| 505 |
}
|
| 506 |
|
| 507 |
function createBullet() {
|
| 508 |
-
|
| 509 |
-
const
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
const bulletLight = new THREE.PointLight(0xffff00, 1, 5);
|
| 518 |
-
bullet.add(bulletLight);
|
| 519 |
|
| 520 |
bullet.position.copy(camera.position);
|
| 521 |
const direction = new THREE.Vector3();
|
| 522 |
camera.getWorldDirection(direction);
|
| 523 |
-
bullet.velocity = direction.multiplyScalar(5);
|
| 524 |
|
| 525 |
scene.add(bullet);
|
| 526 |
return bullet;
|
| 527 |
}
|
| 528 |
|
| 529 |
function createEnemyBullet(enemy) {
|
| 530 |
-
const
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
// μ μ΄μ κ΄μ ν¨κ³Ό μΆκ°
|
| 539 |
-
const bulletLight = new THREE.PointLight(0xff0000, 1, 5);
|
| 540 |
-
bullet.add(bulletLight);
|
| 541 |
|
| 542 |
bullet.position.copy(enemy.model.position);
|
| 543 |
-
bullet.position.y += 5;
|
| 544 |
|
| 545 |
const direction = new THREE.Vector3();
|
| 546 |
direction.subVectors(camera.position, enemy.model.position).normalize();
|
|
@@ -573,29 +422,28 @@ function updateBullets() {
|
|
| 573 |
|
| 574 |
bullets[i].position.add(bullets[i].velocity);
|
| 575 |
|
| 576 |
-
|
| 577 |
-
|
|
|
|
|
|
|
| 578 |
|
| 579 |
-
if (bullets[i] && bullets[i].position.distanceTo(enemy.model.position) < 10) {
|
| 580 |
scene.remove(bullets[i]);
|
| 581 |
bullets.splice(i, 1);
|
| 582 |
enemy.health -= 25;
|
| 583 |
|
| 584 |
-
// νΌκ²© ν¨κ³Ό
|
| 585 |
createExplosion(enemy.model.position.clone());
|
| 586 |
|
| 587 |
if (enemy.health <= 0) {
|
| 588 |
-
// νκ΄΄ ν¨κ³Ό
|
| 589 |
createExplosion(enemy.model.position.clone());
|
| 590 |
-
createExplosion(enemy.model.position.clone().add(new THREE.Vector3(5, 0, 5)));
|
| 591 |
-
createExplosion(enemy.model.position.clone().add(new THREE.Vector3(-5, 0, -5)));
|
| 592 |
scene.remove(enemy.model);
|
| 593 |
-
enemies
|
| 594 |
-
console.log('Enemy destroyed, remaining enemies:', enemies.length);
|
| 595 |
}
|
|
|
|
| 596 |
}
|
| 597 |
-
}
|
| 598 |
|
|
|
|
| 599 |
if (bullets[i] && bullets[i].position.distanceTo(camera.position) > 1000) {
|
| 600 |
scene.remove(bullets[i]);
|
| 601 |
bullets.splice(i, 1);
|
|
@@ -609,6 +457,7 @@ function updateEnemyBullets() {
|
|
| 609 |
|
| 610 |
enemyBullets[i].position.add(enemyBullets[i].velocity);
|
| 611 |
|
|
|
|
| 612 |
if (enemyBullets[i].position.distanceTo(camera.position) < 3) {
|
| 613 |
playerHealth -= 10;
|
| 614 |
updateHealthBar();
|
|
@@ -622,6 +471,7 @@ function updateEnemyBullets() {
|
|
| 622 |
continue;
|
| 623 |
}
|
| 624 |
|
|
|
|
| 625 |
if (enemyBullets[i].position.distanceTo(camera.position) > 1000) {
|
| 626 |
scene.remove(enemyBullets[i]);
|
| 627 |
enemyBullets.splice(i, 1);
|
|
@@ -633,14 +483,12 @@ function updateEnemies() {
|
|
| 633 |
const currentTime = Date.now();
|
| 634 |
|
| 635 |
enemies.forEach(enemy => {
|
| 636 |
-
if (!enemy || !enemy.model)
|
| 637 |
-
console.warn('Invalid enemy detected');
|
| 638 |
-
return;
|
| 639 |
-
}
|
| 640 |
|
|
|
|
| 641 |
const direction = new THREE.Vector3();
|
| 642 |
direction.subVectors(camera.position, enemy.model.position);
|
| 643 |
-
direction.y = 0;
|
| 644 |
direction.normalize();
|
| 645 |
|
| 646 |
const newPosition = enemy.model.position.clone()
|
|
@@ -648,25 +496,20 @@ function updateEnemies() {
|
|
| 648 |
newPosition.y = ENEMY_GROUND_HEIGHT;
|
| 649 |
enemy.model.position.copy(newPosition);
|
| 650 |
|
|
|
|
| 651 |
enemy.model.lookAt(new THREE.Vector3(
|
| 652 |
camera.position.x,
|
| 653 |
enemy.model.position.y,
|
| 654 |
camera.position.z
|
| 655 |
));
|
| 656 |
|
|
|
|
| 657 |
const distanceToPlayer = enemy.model.position.distanceTo(camera.position);
|
| 658 |
if (distanceToPlayer < ENEMY_CONFIG.ATTACK_RANGE &&
|
| 659 |
currentTime - enemy.lastAttackTime > ENEMY_CONFIG.ATTACK_INTERVAL) {
|
| 660 |
|
| 661 |
-
console.log('Enemy attacking!');
|
| 662 |
enemyBullets.push(createEnemyBullet(enemy));
|
| 663 |
enemy.lastAttackTime = currentTime;
|
| 664 |
-
|
| 665 |
-
// 곡격 μ λ°κ΄ ν¨κ³Ό
|
| 666 |
-
const attackFlash = new THREE.PointLight(0xff0000, 2, 20);
|
| 667 |
-
attackFlash.position.copy(enemy.model.position);
|
| 668 |
-
scene.add(attackFlash);
|
| 669 |
-
setTimeout(() => scene.remove(attackFlash), 100);
|
| 670 |
}
|
| 671 |
});
|
| 672 |
}
|
|
@@ -687,8 +530,8 @@ function updateHealthBar() {
|
|
| 687 |
}
|
| 688 |
|
| 689 |
function updateHelicopterHUD() {
|
| 690 |
-
|
| 691 |
-
|
| 692 |
|
| 693 |
const speed = Math.round(
|
| 694 |
Math.sqrt(
|
|
@@ -739,21 +582,45 @@ function checkGameStatus() {
|
|
| 739 |
document.getElementById('stage').textContent = `Stage ${currentStage}`;
|
| 740 |
setTimeout(() => {
|
| 741 |
document.getElementById('stage').style.display = 'none';
|
| 742 |
-
|
| 743 |
}, 2000);
|
| 744 |
}
|
| 745 |
}
|
| 746 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
function gameOver(won) {
|
| 748 |
isGameOver = true;
|
| 749 |
controls.unlock();
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
|
|
|
|
|
|
| 753 |
}
|
| 754 |
|
| 755 |
-
function gameLoop() {
|
| 756 |
requestAnimationFrame(gameLoop);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
|
| 758 |
if (controls.isLocked && !isGameOver) {
|
| 759 |
updateMovement();
|
|
@@ -767,24 +634,48 @@ function gameLoop() {
|
|
| 767 |
renderer.render(scene, camera);
|
| 768 |
}
|
| 769 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
// κ²μ μμ
|
| 771 |
window.addEventListener('load', async () => {
|
| 772 |
try {
|
| 773 |
await init();
|
| 774 |
console.log('Game started');
|
| 775 |
console.log('Active enemies:', enemies.length);
|
| 776 |
-
gameLoop();
|
| 777 |
} catch (error) {
|
| 778 |
console.error('Game initialization error:', error);
|
| 779 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 780 |
}
|
| 781 |
});
|
| 782 |
|
| 783 |
-
//
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import * as THREE from 'three';
|
|
|
|
| 2 |
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
|
| 3 |
|
| 4 |
// κ²μ μμ
|
| 5 |
const GAME_DURATION = 180;
|
| 6 |
const MAP_SIZE = 2000;
|
| 7 |
+
const HELICOPTER_HEIGHT = 30;
|
| 8 |
const ENEMY_GROUND_HEIGHT = 0;
|
| 9 |
+
const ENEMY_SCALE = 10;
|
| 10 |
const MAX_HEALTH = 1000;
|
| 11 |
const ENEMY_MOVE_SPEED = 0.1;
|
| 12 |
+
const ENEMY_COUNT_MAX = 5;
|
| 13 |
+
const PARTICLE_COUNT = 15;
|
| 14 |
+
const OBSTACLE_COUNT = 50;
|
|
|
|
|
|
|
|
|
|
| 15 |
const ENEMY_CONFIG = {
|
| 16 |
ATTACK_RANGE: 100,
|
| 17 |
ATTACK_INTERVAL: 2000,
|
|
|
|
| 28 |
let currentStage = 1;
|
| 29 |
let isGameOver = false;
|
| 30 |
let lastTime = performance.now();
|
| 31 |
+
let lastRender = 0;
|
| 32 |
|
| 33 |
+
// μ€μ€λ μ΄ν° κΈ°λ° μ΄μ리 μμ±κΈ°
|
| 34 |
class GunSoundGenerator {
|
| 35 |
+
constructor() {
|
| 36 |
+
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
| 37 |
}
|
| 38 |
|
| 39 |
createGunshot() {
|
| 40 |
+
const currentTime = this.audioContext.currentTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
// λ©μΈ μ€μ€λ μ΄ν°
|
| 43 |
+
const osc = this.audioContext.createOscillator();
|
| 44 |
+
const gainNode = this.audioContext.createGain();
|
| 45 |
+
|
| 46 |
+
osc.type = 'square';
|
| 47 |
+
osc.frequency.setValueAtTime(200, currentTime);
|
| 48 |
+
osc.frequency.exponentialRampToValueAtTime(50, currentTime + 0.1);
|
| 49 |
+
|
| 50 |
+
gainNode.gain.setValueAtTime(0.5, currentTime);
|
| 51 |
+
gainNode.gain.exponentialRampToValueAtTime(0.01, currentTime + 0.1);
|
| 52 |
+
|
| 53 |
+
osc.connect(gainNode);
|
| 54 |
+
gainNode.connect(this.audioContext.destination);
|
| 55 |
+
|
| 56 |
+
osc.start(currentTime);
|
| 57 |
+
osc.stop(currentTime + 0.1);
|
| 58 |
|
| 59 |
+
// λ
Έμ΄μ¦ μΆκ°
|
| 60 |
const bufferSize = this.audioContext.sampleRate * 0.1;
|
| 61 |
const buffer = this.audioContext.createBuffer(1, bufferSize, this.audioContext.sampleRate);
|
| 62 |
+
const data = buffer.getChannelData(0);
|
| 63 |
+
|
| 64 |
for (let i = 0; i < bufferSize; i++) {
|
| 65 |
+
data[i] = Math.random() * 2 - 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
+
|
| 68 |
+
const noise = this.audioContext.createBufferSource();
|
| 69 |
+
const noiseGain = this.audioContext.createGain();
|
| 70 |
+
|
| 71 |
+
noise.buffer = buffer;
|
| 72 |
+
noiseGain.gain.setValueAtTime(0.2, currentTime);
|
| 73 |
+
noiseGain.gain.exponentialRampToValueAtTime(0.01, currentTime + 0.05);
|
| 74 |
+
|
| 75 |
+
noise.connect(noiseGain);
|
| 76 |
+
noiseGain.connect(this.audioContext.destination);
|
| 77 |
+
|
| 78 |
+
noise.start(currentTime);
|
| 79 |
}
|
| 80 |
|
| 81 |
+
resume() {
|
| 82 |
+
if (this.audioContext.state === 'suspended') {
|
| 83 |
+
this.audioContext.resume();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
// μ¬μ΄λ μμ€ν
μ΄κΈ°ν
|
| 89 |
+
const gunSound = new GunSoundGenerator();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
async function init() {
|
| 92 |
+
document.getElementById('loading').style.display = 'block';
|
| 93 |
|
| 94 |
+
try {
|
| 95 |
+
// Scene μ΄κΈ°ν
|
| 96 |
+
scene = new THREE.Scene();
|
| 97 |
+
scene.background = new THREE.Color(0x87ceeb);
|
| 98 |
+
scene.fog = new THREE.Fog(0x87ceeb, 0, 1000);
|
| 99 |
+
|
| 100 |
+
// Renderer μ΅μ ν
|
| 101 |
+
renderer = new THREE.WebGLRenderer({
|
| 102 |
+
antialias: false,
|
| 103 |
+
powerPreference: "high-performance"
|
| 104 |
+
});
|
| 105 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 106 |
+
renderer.shadowMap.enabled = true;
|
| 107 |
+
renderer.shadowMap.type = THREE.BasicShadowMap;
|
| 108 |
+
document.body.appendChild(renderer.domElement);
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
// Camera μ€μ
|
| 111 |
+
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
| 112 |
+
camera.position.set(0, HELICOPTER_HEIGHT, 0);
|
| 113 |
|
| 114 |
+
// κΈ°λ³Έ μ‘°λͺ
|
| 115 |
+
scene.add(new THREE.AmbientLight(0xffffff, 0.6));
|
| 116 |
+
|
| 117 |
+
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
|
| 118 |
+
dirLight.position.set(100, 100, 50);
|
| 119 |
+
dirLight.castShadow = true;
|
| 120 |
+
dirLight.shadow.mapSize.width = 1024; // κ·Έλ¦Όμ ν΄μλ κ°μ
|
| 121 |
+
dirLight.shadow.mapSize.height = 1024;
|
| 122 |
+
scene.add(dirLight);
|
| 123 |
|
| 124 |
+
// Controls μ€μ
|
| 125 |
+
controls = new PointerLockControls(camera, document.body);
|
| 126 |
|
| 127 |
+
// μ΄λ²€νΈ 리μ€λ
|
| 128 |
+
setupEventListeners();
|
| 129 |
+
|
| 130 |
+
// κ²μ μμ μ΄κΈ°ν
|
| 131 |
+
await Promise.all([
|
| 132 |
+
createTerrain(),
|
| 133 |
+
createEnemies()
|
| 134 |
+
]);
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
document.getElementById('loading').style.display = 'none';
|
| 137 |
+
gameLoop();
|
|
|
|
| 138 |
} catch (error) {
|
| 139 |
console.error('Initialization error:', error);
|
| 140 |
+
document.getElementById('loading').textContent = 'Error loading game. Please refresh.';
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
| 144 |
+
function setupEventListeners() {
|
| 145 |
+
document.addEventListener('click', onClick);
|
| 146 |
+
document.addEventListener('keydown', onKeyDown);
|
| 147 |
+
document.addEventListener('keyup', onKeyUp);
|
| 148 |
+
window.addEventListener('resize', onWindowResize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
}
|
| 150 |
|
| 151 |
function createTerrain() {
|
| 152 |
return new Promise((resolve) => {
|
| 153 |
+
// μ΅μ νλ μ§ν μμ±
|
| 154 |
+
const geometry = new THREE.PlaneGeometry(MAP_SIZE, MAP_SIZE, 100, 100); // ν΄μλ κ°μ
|
| 155 |
const material = new THREE.MeshStandardMaterial({
|
| 156 |
color: 0xD2B48C,
|
| 157 |
roughness: 0.8,
|
|
|
|
| 171 |
terrain.receiveShadow = true;
|
| 172 |
scene.add(terrain);
|
| 173 |
|
| 174 |
+
// μ΅μ νλ μ₯μ λ¬Ό μΆκ°
|
| 175 |
addObstacles();
|
| 176 |
resolve();
|
| 177 |
});
|
|
|
|
| 184 |
roughness: 0.9
|
| 185 |
});
|
| 186 |
|
| 187 |
+
// μ₯μ λ¬Ό μ κ°μ
|
| 188 |
+
for (let i = 0; i < OBSTACLE_COUNT; i++) {
|
| 189 |
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
|
| 190 |
rock.position.set(
|
| 191 |
(Math.random() - 0.5) * MAP_SIZE * 0.9,
|
|
|
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
+
function createEnemies() {
|
| 207 |
+
const enemyCount = Math.min(3 + currentStage, ENEMY_COUNT_MAX);
|
| 208 |
+
|
| 209 |
+
for (let i = 0; i < enemyCount; i++) {
|
| 210 |
+
const angle = (i / enemyCount) * Math.PI * 2;
|
| 211 |
+
const radius = 200;
|
| 212 |
+
const position = new THREE.Vector3(
|
| 213 |
+
Math.cos(angle) * radius,
|
| 214 |
+
ENEMY_GROUND_HEIGHT,
|
| 215 |
+
Math.sin(angle) * radius
|
| 216 |
+
);
|
| 217 |
+
|
| 218 |
+
const enemy = createSimpleEnemy(position);
|
| 219 |
+
enemies.push(enemy);
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
function createSimpleEnemy(position) {
|
| 224 |
+
// κ°λ¨ν μ λͺ¨λΈ μμ±
|
| 225 |
+
const geometry = new THREE.BoxGeometry(5, 10, 5);
|
| 226 |
+
const material = new THREE.MeshPhongMaterial({
|
| 227 |
+
color: 0xff0000,
|
| 228 |
+
emissive: 0x441111,
|
| 229 |
+
emissiveIntensity: 0.5
|
| 230 |
+
});
|
| 231 |
+
|
| 232 |
+
const model = new THREE.Mesh(geometry, material);
|
| 233 |
+
model.position.copy(position);
|
| 234 |
+
model.castShadow = true;
|
| 235 |
+
model.receiveShadow = true;
|
| 236 |
+
|
| 237 |
+
// μ μ£Όλ³μ λΉ μΆκ°
|
| 238 |
+
const light = new THREE.PointLight(0xff0000, 1, 10);
|
| 239 |
+
light.position.set(0, 5, 0);
|
| 240 |
+
model.add(light);
|
| 241 |
+
|
| 242 |
+
scene.add(model);
|
| 243 |
+
|
| 244 |
+
return {
|
| 245 |
+
model: model,
|
| 246 |
+
health: 100,
|
| 247 |
+
speed: ENEMY_MOVE_SPEED,
|
| 248 |
+
lastAttackTime: 0
|
| 249 |
+
};
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
function createExplosion(position) {
|
|
|
|
| 253 |
const particles = [];
|
| 254 |
+
for (let i = 0; i < PARTICLE_COUNT; i++) {
|
|
|
|
| 255 |
const particle = new THREE.Mesh(
|
| 256 |
new THREE.SphereGeometry(0.3),
|
| 257 |
new THREE.MeshBasicMaterial({
|
| 258 |
color: 0xff4400,
|
| 259 |
transparent: true,
|
| 260 |
+
opacity: 1
|
|
|
|
|
|
|
| 261 |
})
|
| 262 |
);
|
| 263 |
|
|
|
|
| 270 |
|
| 271 |
particles.push(particle);
|
| 272 |
scene.add(particle);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
}
|
| 274 |
|
| 275 |
+
// νλ° κ΄μ ν¨κ³Ό
|
| 276 |
+
const explosionLight = new THREE.PointLight(0xff4400, 2, 20);
|
| 277 |
+
explosionLight.position.copy(position);
|
| 278 |
+
scene.add(explosionLight);
|
| 279 |
+
|
| 280 |
+
// μ΅μ νλ μ λλ©μ΄μ
|
| 281 |
+
let opacity = 1;
|
| 282 |
+
const animate = () => {
|
| 283 |
+
opacity -= 0.05;
|
| 284 |
+
if (opacity <= 0) {
|
| 285 |
+
particles.forEach(p => scene.remove(p));
|
| 286 |
+
scene.remove(explosionLight);
|
| 287 |
+
return;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
particles.forEach(particle => {
|
| 291 |
particle.position.add(particle.velocity);
|
| 292 |
+
particle.material.opacity = opacity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
});
|
| 294 |
|
| 295 |
+
requestAnimationFrame(animate);
|
| 296 |
+
};
|
| 297 |
+
|
| 298 |
+
animate();
|
|
|
|
|
|
|
|
|
|
| 299 |
}
|
| 300 |
|
|
|
|
| 301 |
function onClick() {
|
| 302 |
if (!controls.isLocked) {
|
| 303 |
controls.lock();
|
| 304 |
+
gunSound.resume();
|
| 305 |
} else if (ammo > 0) {
|
| 306 |
shoot();
|
| 307 |
}
|
|
|
|
| 332 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 333 |
}
|
| 334 |
|
| 335 |
+
// μ΄λ μν
|
| 336 |
+
const moveState = {
|
| 337 |
+
forward: false,
|
| 338 |
+
backward: false,
|
| 339 |
+
left: false,
|
| 340 |
+
right: false
|
| 341 |
+
};
|
| 342 |
+
|
| 343 |
function shoot() {
|
| 344 |
if (ammo <= 0) return;
|
| 345 |
|
|
|
|
| 349 |
const bullet = createBullet();
|
| 350 |
bullets.push(bullet);
|
| 351 |
|
| 352 |
+
gunSound.createGunshot();
|
| 353 |
|
| 354 |
+
// μ΅μ νλ μ΄κ΅¬ νμΌ ν¨κ³Ό
|
| 355 |
const muzzleFlash = new THREE.PointLight(0xffff00, 3, 10);
|
| 356 |
muzzleFlash.position.copy(camera.position);
|
| 357 |
scene.add(muzzleFlash);
|
|
|
|
| 359 |
}
|
| 360 |
|
| 361 |
function createBullet() {
|
| 362 |
+
// μ΅μ νλ μ΄μ μμ±
|
| 363 |
+
const bullet = new THREE.Mesh(
|
| 364 |
+
new THREE.SphereGeometry(0.5),
|
| 365 |
+
new THREE.MeshBasicMaterial({
|
| 366 |
+
color: 0xffff00,
|
| 367 |
+
emissive: 0xffff00,
|
| 368 |
+
emissiveIntensity: 1
|
| 369 |
+
})
|
| 370 |
+
);
|
|
|
|
|
|
|
| 371 |
|
| 372 |
bullet.position.copy(camera.position);
|
| 373 |
const direction = new THREE.Vector3();
|
| 374 |
camera.getWorldDirection(direction);
|
| 375 |
+
bullet.velocity = direction.multiplyScalar(5);
|
| 376 |
|
| 377 |
scene.add(bullet);
|
| 378 |
return bullet;
|
| 379 |
}
|
| 380 |
|
| 381 |
function createEnemyBullet(enemy) {
|
| 382 |
+
const bullet = new THREE.Mesh(
|
| 383 |
+
new THREE.SphereGeometry(0.5),
|
| 384 |
+
new THREE.MeshBasicMaterial({
|
| 385 |
+
color: 0xff0000,
|
| 386 |
+
emissive: 0xff0000,
|
| 387 |
+
emissiveIntensity: 1
|
| 388 |
+
})
|
| 389 |
+
);
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
bullet.position.copy(enemy.model.position);
|
| 392 |
+
bullet.position.y += 5;
|
| 393 |
|
| 394 |
const direction = new THREE.Vector3();
|
| 395 |
direction.subVectors(camera.position, enemy.model.position).normalize();
|
|
|
|
| 422 |
|
| 423 |
bullets[i].position.add(bullets[i].velocity);
|
| 424 |
|
| 425 |
+
// μ κ³Όμ μΆ©λ κ²μ¬
|
| 426 |
+
for (let j = enemies.length - 1; j >= 0; j--) {
|
| 427 |
+
const enemy = enemies[j];
|
| 428 |
+
if (!enemy || !enemy.model) continue;
|
| 429 |
|
| 430 |
+
if (bullets[i] && bullets[i].position.distanceTo(enemy.model.position) < 10) {
|
| 431 |
scene.remove(bullets[i]);
|
| 432 |
bullets.splice(i, 1);
|
| 433 |
enemy.health -= 25;
|
| 434 |
|
|
|
|
| 435 |
createExplosion(enemy.model.position.clone());
|
| 436 |
|
| 437 |
if (enemy.health <= 0) {
|
|
|
|
| 438 |
createExplosion(enemy.model.position.clone());
|
|
|
|
|
|
|
| 439 |
scene.remove(enemy.model);
|
| 440 |
+
enemies.splice(j, 1);
|
|
|
|
| 441 |
}
|
| 442 |
+
break;
|
| 443 |
}
|
| 444 |
+
}
|
| 445 |
|
| 446 |
+
// λ²μ λ²μ΄λ μ΄μ μ κ±°
|
| 447 |
if (bullets[i] && bullets[i].position.distanceTo(camera.position) > 1000) {
|
| 448 |
scene.remove(bullets[i]);
|
| 449 |
bullets.splice(i, 1);
|
|
|
|
| 457 |
|
| 458 |
enemyBullets[i].position.add(enemyBullets[i].velocity);
|
| 459 |
|
| 460 |
+
// νλ μ΄μ΄μμ μΆ©λ κ²μ¬
|
| 461 |
if (enemyBullets[i].position.distanceTo(camera.position) < 3) {
|
| 462 |
playerHealth -= 10;
|
| 463 |
updateHealthBar();
|
|
|
|
| 471 |
continue;
|
| 472 |
}
|
| 473 |
|
| 474 |
+
// λ²μ λ²μ΄λ μ΄μ μ κ±°
|
| 475 |
if (enemyBullets[i].position.distanceTo(camera.position) > 1000) {
|
| 476 |
scene.remove(enemyBullets[i]);
|
| 477 |
enemyBullets.splice(i, 1);
|
|
|
|
| 483 |
const currentTime = Date.now();
|
| 484 |
|
| 485 |
enemies.forEach(enemy => {
|
| 486 |
+
if (!enemy || !enemy.model) return;
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
+
// μ μ΄λ λ‘μ§
|
| 489 |
const direction = new THREE.Vector3();
|
| 490 |
direction.subVectors(camera.position, enemy.model.position);
|
| 491 |
+
direction.y = 0;
|
| 492 |
direction.normalize();
|
| 493 |
|
| 494 |
const newPosition = enemy.model.position.clone()
|
|
|
|
| 496 |
newPosition.y = ENEMY_GROUND_HEIGHT;
|
| 497 |
enemy.model.position.copy(newPosition);
|
| 498 |
|
| 499 |
+
// μ μ΄ νλ μ΄μ΄λ₯Ό λ°λΌλ³΄λλ‘ μ€μ
|
| 500 |
enemy.model.lookAt(new THREE.Vector3(
|
| 501 |
camera.position.x,
|
| 502 |
enemy.model.position.y,
|
| 503 |
camera.position.z
|
| 504 |
));
|
| 505 |
|
| 506 |
+
// 곡격 λ‘μ§
|
| 507 |
const distanceToPlayer = enemy.model.position.distanceTo(camera.position);
|
| 508 |
if (distanceToPlayer < ENEMY_CONFIG.ATTACK_RANGE &&
|
| 509 |
currentTime - enemy.lastAttackTime > ENEMY_CONFIG.ATTACK_INTERVAL) {
|
| 510 |
|
|
|
|
| 511 |
enemyBullets.push(createEnemyBullet(enemy));
|
| 512 |
enemy.lastAttackTime = currentTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
}
|
| 514 |
});
|
| 515 |
}
|
|
|
|
| 530 |
}
|
| 531 |
|
| 532 |
function updateHelicopterHUD() {
|
| 533 |
+
document.querySelector('#altitude-indicator span').textContent =
|
| 534 |
+
Math.round(camera.position.y);
|
| 535 |
|
| 536 |
const speed = Math.round(
|
| 537 |
Math.sqrt(
|
|
|
|
| 582 |
document.getElementById('stage').textContent = `Stage ${currentStage}`;
|
| 583 |
setTimeout(() => {
|
| 584 |
document.getElementById('stage').style.display = 'none';
|
| 585 |
+
createEnemies();
|
| 586 |
}, 2000);
|
| 587 |
}
|
| 588 |
}
|
| 589 |
|
| 590 |
+
function cleanupResources() {
|
| 591 |
+
// 리μμ€ μ 리
|
| 592 |
+
bullets.forEach(bullet => scene.remove(bullet));
|
| 593 |
+
bullets = [];
|
| 594 |
+
|
| 595 |
+
enemyBullets.forEach(bullet => scene.remove(bullet));
|
| 596 |
+
enemyBullets = [];
|
| 597 |
+
|
| 598 |
+
enemies.forEach(enemy => {
|
| 599 |
+
if (enemy && enemy.model) {
|
| 600 |
+
scene.remove(enemy.model);
|
| 601 |
+
}
|
| 602 |
+
});
|
| 603 |
+
enemies = [];
|
| 604 |
+
}
|
| 605 |
+
|
| 606 |
function gameOver(won) {
|
| 607 |
isGameOver = true;
|
| 608 |
controls.unlock();
|
| 609 |
+
cleanupResources();
|
| 610 |
+
setTimeout(() => {
|
| 611 |
+
alert(won ? 'Mission Complete!' : 'Game Over!');
|
| 612 |
+
location.reload();
|
| 613 |
+
}, 100);
|
| 614 |
}
|
| 615 |
|
| 616 |
+
function gameLoop(timestamp) {
|
| 617 |
requestAnimationFrame(gameLoop);
|
| 618 |
+
|
| 619 |
+
// νλ μ μ ν (60fps)
|
| 620 |
+
if (timestamp - lastRender < 16) {
|
| 621 |
+
return;
|
| 622 |
+
}
|
| 623 |
+
lastRender = timestamp;
|
| 624 |
|
| 625 |
if (controls.isLocked && !isGameOver) {
|
| 626 |
updateMovement();
|
|
|
|
| 634 |
renderer.render(scene, camera);
|
| 635 |
}
|
| 636 |
|
| 637 |
+
// λλ²κΉ
μ μν μ μ μ κ·Ό
|
| 638 |
+
window.debugGame = {
|
| 639 |
+
scene,
|
| 640 |
+
camera,
|
| 641 |
+
enemies,
|
| 642 |
+
gunSound,
|
| 643 |
+
reloadEnemies: createEnemies
|
| 644 |
+
};
|
| 645 |
+
|
| 646 |
// κ²μ μμ
|
| 647 |
window.addEventListener('load', async () => {
|
| 648 |
try {
|
| 649 |
await init();
|
| 650 |
console.log('Game started');
|
| 651 |
console.log('Active enemies:', enemies.length);
|
| 652 |
+
gameLoop(performance.now());
|
| 653 |
} catch (error) {
|
| 654 |
console.error('Game initialization error:', error);
|
| 655 |
+
document.getElementById('loading').innerHTML = `
|
| 656 |
+
<div class="loading-text" style="color: #ff0000;">
|
| 657 |
+
Error loading game. Please refresh.
|
| 658 |
+
</div>
|
| 659 |
+
`;
|
| 660 |
}
|
| 661 |
});
|
| 662 |
|
| 663 |
+
// μ±λ₯ λͺ¨λν°λ§
|
| 664 |
+
let lastFpsUpdate = 0;
|
| 665 |
+
let frameCount = 0;
|
| 666 |
+
|
| 667 |
+
function updateFPS(timestamp) {
|
| 668 |
+
frameCount++;
|
| 669 |
+
|
| 670 |
+
if (timestamp - lastFpsUpdate >= 1000) {
|
| 671 |
+
const fps = Math.round(frameCount * 1000 / (timestamp - lastFpsUpdate));
|
| 672 |
+
console.log('FPS:', fps);
|
| 673 |
+
|
| 674 |
+
frameCount = 0;
|
| 675 |
+
lastFpsUpdate = timestamp;
|
| 676 |
+
}
|
| 677 |
+
|
| 678 |
+
requestAnimationFrame(updateFPS);
|
| 679 |
+
}
|
| 680 |
+
|
| 681 |
+
updateFPS(performance.now());
|