Spaces:
Running
Running
File size: 27,978 Bytes
a30e88c 0fd1d06 a30e88c 0fd1d06 a30e88c f027a73 0fd1d06 96897f0 f027a73 accbf92 f027a73 accbf92 f027a73 accbf92 f027a73 accbf92 75e71ec accbf92 75e71ec accbf92 75e71ec accbf92 75e71ec accbf92 75e71ec accbf92 75e71ec accbf92 f027a73 678cd65 f027a73 678cd65 f027a73 a30e88c f027a73 0fd1d06 96897f0 f027a73 96897f0 f027a73 96897f0 f027a73 96897f0 a30e88c 96897f0 f027a73 0fd1d06 96897f0 f027a73 96897f0 f027a73 a30e88c 96897f0 a30e88c 96897f0 f027a73 a30e88c f027a73 96897f0 a30e88c f027a73 a30e88c 96897f0 a30e88c f027a73 96897f0 a30e88c 96897f0 f027a73 0fd1d06 a30e88c f027a73 0fd1d06 f027a73 0fd1d06 96897f0 a30e88c 96897f0 f027a73 0fd1d06 96897f0 a30e88c 96897f0 a30e88c 96897f0 f027a73 a30e88c f027a73 96897f0 f027a73 96897f0 accbf92 a30e88c f027a73 96897f0 accbf92 96897f0 accbf92 220212e accbf92 96897f0 accbf92 96897f0 f027a73 96897f0 57a9965 a30e88c 57a9965 96897f0 678cd65 fa41962 678cd65 a30e88c accbf92 b96edd4 678cd65 accbf92 b96edd4 678cd65 b96edd4 accbf92 b96edd4 678cd65 b96edd4 678cd65 b96edd4 accbf92 678cd65 accbf92 b96edd4 678cd65 612e991 678cd65 68a71f4 678cd65 accbf92 678cd65 d50b5f8 68a71f4 678cd65 612e991 678cd65 612e991 678cd65 612e991 68a71f4 612e991 fa41962 678cd65 612e991 678cd65 accbf92 f027a73 0fd1d06 f027a73 accbf92 f027a73 0fd1d06 f027a73 0fd1d06 f027a73 a30e88c 57a9965 96897f0 57a9965 a30e88c 57a9965 f027a73 96897f0 f027a73 96897f0 a30e88c 96897f0 a30e88c 96897f0 a30e88c 96897f0 a30e88c 96897f0 57a9965 f027a73 57a9965 96897f0 57a9965 f027a73 57a9965 f027a73 a30e88c 57a9965 96897f0 f027a73 accbf92 57a9965 a30e88c 57a9965 a30e88c 57a9965 accbf92 57a9965 f027a73 57a9965 accbf92 57a9965 96897f0 57a9965 f027a73 a30e88c accbf92 f027a73 96897f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 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 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 |
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
// ๊ฒ์ ์์
const GAME_DURATION = 180;
const MAP_SIZE = 2000;
const TANK_HEIGHT = 0.5;
const ENEMY_GROUND_HEIGHT = 0;
const ENEMY_SCALE = 10;
const MAX_HEALTH = 1000;
const ENEMY_MOVE_SPEED = 0.1;
const ENEMY_COUNT_MAX = 5;
const PARTICLE_COUNT = 15;
const BUILDING_COUNT = 30; // ๊ฑด๋ฌผ ์ ์ถ๊ฐ
const ENEMY_CONFIG = {
ATTACK_RANGE: 100,
ATTACK_INTERVAL: 2000,
BULLET_SPEED: 2
};
// TankPlayer ํด๋์ค
class TankPlayer {
constructor() {
this.body = null;
this.turret = null;
this.position = new THREE.Vector3(0, 0, 0);
this.rotation = new THREE.Euler(0, 0, 0);
this.turretRotation = 0;
this.moveSpeed = 0.5;
this.turnSpeed = 0.03;
this.turretGroup = new THREE.Group();
this.health = MAX_HEALTH;
this.isLoaded = false;
this.ammo = 10;
this.lastShootTime = 0;
this.shootInterval = 1000;
this.bullets = [];
}
async initialize(scene, loader) {
try {
const bodyResult = await loader.loadAsync('/models/abramsBody.glb');
this.body = bodyResult.scene;
this.body.position.copy(this.position);
const turretResult = await loader.loadAsync('/models/abramsTurret.glb');
this.turret = turretResult.scene;
this.turretGroup.position.y = 0.2;
this.turretGroup.add(this.turret);
this.body.add(this.turretGroup);
this.body.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
this.turret.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
scene.add(this.body);
this.isLoaded = true;
} catch (error) {
console.error('Error loading tank models:', error);
this.isLoaded = false;
}
}
shoot(scene) {
const currentTime = Date.now();
if (currentTime - this.lastShootTime < this.shootInterval || this.ammo <= 0) return null;
// ์ด์ ์์ฑ
const bulletGeometry = new THREE.SphereGeometry(0.2);
const bulletMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
// ์ด์ ์์ ์์น (ํฌํ ๋)
const bulletOffset = new THREE.Vector3(0, 0.5, 2);
// ํฌํ์ ํ์ ์ ์ ์ฉ
bulletOffset.applyQuaternion(this.turretGroup.quaternion);
bulletOffset.applyQuaternion(this.body.quaternion);
bullet.position.copy(this.body.position).add(bulletOffset);
// ์ด์ ์๋ (ํฌํ ๋ฐฉํฅ)
const direction = new THREE.Vector3(0, 0, 1);
direction.applyQuaternion(this.turretGroup.quaternion);
direction.applyQuaternion(this.body.quaternion);
bullet.velocity = direction.multiplyScalar(2);
scene.add(bullet);
this.bullets.push(bullet);
this.ammo--;
this.lastShootTime = currentTime;
document.getElementById('ammo').textContent = `Ammo: ${this.ammo}/10`;
return bullet;
}
update(mouseX, mouseY) {
if (!this.body || !this.turretGroup) return;
// ์ด์ ์
๋ฐ์ดํธ๋ง ์ํํ๊ณ ํฌํ ํ์ ์ Game ํด๋์ค์ handleMovement์์ ์ฒ๋ฆฌ
for (let i = this.bullets.length - 1; i >= 0; i--) {
const bullet = this.bullets[i];
bullet.position.add(bullet.velocity);
// ์ด์์ด ๋งต ๋ฐ์ผ๋ก ๋๊ฐ๋ฉด ์ ๊ฑฐ
if (Math.abs(bullet.position.x) > MAP_SIZE/2 ||
Math.abs(bullet.position.z) > MAP_SIZE/2) {
scene.remove(bullet);
this.bullets.splice(i, 1);
}
}
}
move(direction) {
if (!this.body) return;
const moveVector = new THREE.Vector3();
moveVector.x = direction.x * this.moveSpeed;
moveVector.z = direction.z * this.moveSpeed;
this.body.position.add(moveVector);
}
rotate(angle) {
if (!this.body) return;
this.body.rotation.y += angle * this.turnSpeed;
}
getPosition() {
return this.body ? this.body.position : new THREE.Vector3();
}
takeDamage(damage) {
this.health -= damage;
return this.health <= 0;
}
}
// Enemy ํด๋์ค ์์
class Enemy {
constructor(scene, position, type = 'tank') {
this.scene = scene;
this.position = position;
this.mesh = null;
this.type = type; // 'tank' ๋๋ 'heavy'
this.health = type === 'tank' ? 100 : 200; // heavy๋ ์ฒด๋ ฅ์ด ๋ ๋์
this.lastAttackTime = 0;
this.bullets = [];
this.isLoaded = false;
this.moveSpeed = type === 'tank' ? ENEMY_MOVE_SPEED : ENEMY_MOVE_SPEED * 0.7; // heavy๋ ๋ ๋๋ฆผ
}
async initialize(loader) {
try {
// ํ์
์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ชจ๋ธ ๋ก๋
const modelPath = this.type === 'tank' ? '/models/enemy1.glb' : '/models/enemy4.glb';
const result = await loader.loadAsync(modelPath);
this.mesh = result.scene;
this.mesh.position.copy(this.position);
this.mesh.scale.set(ENEMY_SCALE, ENEMY_SCALE, ENEMY_SCALE);
this.mesh.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
this.scene.add(this.mesh);
this.isLoaded = true;
} catch (error) {
console.error('Error loading enemy model:', error);
this.isLoaded = false;
}
}
update(playerPosition) {
if (!this.mesh || !this.isLoaded) return;
// ํ๋ ์ด์ด ๋ฐฉํฅ์ผ๋ก ํ์
const direction = new THREE.Vector3()
.subVectors(playerPosition, this.mesh.position)
.normalize();
this.mesh.lookAt(playerPosition);
// ํ๋ ์ด์ด ๋ฐฉํฅ์ผ๋ก ์ด๋ (ํ์
์ ๋ฐ๋ผ ๋ค๋ฅธ ์๋)
this.mesh.position.add(direction.multiplyScalar(this.moveSpeed));
// ์ด์ ์
๋ฐ์ดํธ
for (let i = this.bullets.length - 1; i >= 0; i--) {
const bullet = this.bullets[i];
bullet.position.add(bullet.velocity);
// ์ด์์ด ๋งต ๋ฐ์ผ๋ก ๋๊ฐ๋ฉด ์ ๊ฑฐ
if (Math.abs(bullet.position.x) > MAP_SIZE ||
Math.abs(bullet.position.z) > MAP_SIZE) {
this.scene.remove(bullet);
this.bullets.splice(i, 1);
}
}
}
shoot(playerPosition) {
const currentTime = Date.now();
const attackInterval = this.type === 'tank' ?
ENEMY_CONFIG.ATTACK_INTERVAL :
ENEMY_CONFIG.ATTACK_INTERVAL * 1.5; // heavy๋ ๋ฐ์ฌ ๊ฐ๊ฒฉ์ด ๋ ๊น
if (currentTime - this.lastAttackTime < attackInterval) return;
const bulletGeometry = new THREE.SphereGeometry(this.type === 'tank' ? 0.2 : 0.3);
const bulletMaterial = new THREE.MeshBasicMaterial({
color: this.type === 'tank' ? 0xff0000 : 0xff6600
});
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
bullet.position.copy(this.mesh.position);
const direction = new THREE.Vector3()
.subVectors(playerPosition, this.mesh.position)
.normalize();
const bulletSpeed = this.type === 'tank' ?
ENEMY_CONFIG.BULLET_SPEED :
ENEMY_CONFIG.BULLET_SPEED * 0.8;
bullet.velocity = direction.multiplyScalar(bulletSpeed);
this.scene.add(bullet);
this.bullets.push(bullet);
this.lastAttackTime = currentTime;
}
takeDamage(damage) {
this.health -= damage;
return this.health <= 0;
}
destroy() {
if (this.mesh) {
this.scene.remove(this.mesh);
this.bullets.forEach(bullet => this.scene.remove(bullet));
this.bullets = [];
this.isLoaded = false;
}
}
}
// Particle ํด๋์ค๋ ๊ทธ๋๋ก ์ ์ง
class Particle {
constructor(scene, position) {
const geometry = new THREE.SphereGeometry(0.1);
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.position.copy(position);
this.velocity = new THREE.Vector3(
(Math.random() - 0.5) * 0.3,
Math.random() * 0.2,
(Math.random() - 0.5) * 0.3
);
this.gravity = -0.01;
this.lifetime = 60;
this.age = 0;
scene.add(this.mesh);
}
update() {
this.velocity.y += this.gravity;
this.mesh.position.add(this.velocity);
this.age++;
return this.age < this.lifetime;
}
destroy(scene) {
scene.remove(this.mesh);
}
}
// Game ํด๋์ค
class Game {
constructor() {
// ๊ธฐ๋ณธ Three.js ์ค์
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.shadowMap.enabled = true;
document.getElementById('gameContainer').appendChild(this.renderer.domElement);
// ๊ฒ์ ์์ ์ด๊ธฐํ
this.tank = new TankPlayer();
this.enemies = [];
this.particles = [];
this.buildings = [];
this.loader = new GLTFLoader();
this.controls = null;
this.gameTime = GAME_DURATION;
this.score = 0;
this.isGameOver = false;
this.isLoading = true;
this.previousTankPosition = new THREE.Vector3();
this.lastTime = performance.now();
// ๋ง์ฐ์ค/ํค๋ณด๋ ์ํ
this.mouse = { x: 0, y: 0 };
this.keys = {
forward: false,
backward: false,
left: false,
right: false
};
// ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ค์
this.setupEventListeners();
this.initialize();
this.disposedObjects = new Set(); // ์ ๊ฑฐ๋ ๊ฐ์ฒด ์ถ์
}
// ๋ฆฌ์์ค ์ ๋ฆฌ๋ฅผ ์ํ ๋ฉ์๋ ์ถ๊ฐ
dispose(object) {
if (this.disposedObjects.has(object)) return;
if (object.geometry) object.geometry.dispose();
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach(material => material.dispose());
} else {
object.material.dispose();
}
}
if (object.parent) object.parent.remove(object);
this.disposedObjects.add(object);
}
// ์ด์ ์ ๊ฑฐ ์ ๋ฆฌ์์ค ์ ๋ฆฌ
cleanupBullets() {
// ํ๋ ์ด์ด ์ด์ ์ ๋ฆฌ
for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
const bullet = this.tank.bullets[i];
if (Math.abs(bullet.position.x) > MAP_SIZE/2 ||
Math.abs(bullet.position.z) > MAP_SIZE/2) {
this.dispose(bullet);
this.tank.bullets.splice(i, 1);
}
}
// ์ ์ด์ ์ ๋ฆฌ
this.enemies.forEach(enemy => {
for (let i = enemy.bullets.length - 1; i >= 0; i--) {
const bullet = enemy.bullets[i];
if (Math.abs(bullet.position.x) > MAP_SIZE/2 ||
Math.abs(bullet.position.z) > MAP_SIZE/2) {
this.dispose(bullet);
enemy.bullets.splice(i, 1);
}
}
});
}
}
async initialize() {
try {
// ์กฐ๋ช
์ค์
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
this.scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(50, 50, 50);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
this.scene.add(directionalLight);
// ์งํ ์์ฑ
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(MAP_SIZE, MAP_SIZE),
new THREE.MeshStandardMaterial({
color: 0x333333,
roughness: 0.9,
metalness: 0.1
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
this.scene.add(ground);
// ๊ฑด๋ฌผ ์์ฑ
await this.createBuildings();
// ํฑํฌ ์ด๊ธฐํ
await this.tank.initialize(this.scene, this.loader);
if (!this.tank.isLoaded) {
throw new Error('Tank loading failed');
}
// ์นด๋ฉ๋ผ ์ค์ ์์
// ํฑํฌ์ ํ์ฌ ์์น ๊ฐ์ ธ์ค๊ธฐ
const tankPosition = this.tank.getPosition();
// ์นด๋ฉ๋ผ๋ฅผ ํฑํฌ ์์น ๊ธฐ์ค์ผ๋ก ์ค์
this.camera.position.set(
tankPosition.x,
tankPosition.y + 15, // ํฑํฌ๋ณด๋ค 15 ์ ๋ ์์
tankPosition.z - 30 // ํฑํฌ๋ณด๋ค 30 ์ ๋ ๋ค์
);
// ์นด๋ฉ๋ผ๊ฐ ํฑํฌ๋ฅผ ๋ฐ๋ผ๋ณด๋๋ก ์ค์
this.camera.lookAt(new THREE.Vector3(
tankPosition.x,
tankPosition.y + 2, // ํฑํฌ์ ์๋จ ๋ถ๋ถ์ ๋ฐ๋ผ๋ณด๋๋ก
tankPosition.z
));
// ๋ก๋ฉ ์๋ฃ
this.isLoading = false;
document.getElementById('loading').style.display = 'none';
// ๊ฒ์ ์์
this.animate();
this.spawnEnemies();
this.startGameTimer();
} catch (error) {
console.error('Game initialization error:', error);
this.handleLoadingError();
}
}
setupEventListeners() {
// ํค๋ณด๋ ์ด๋ฒคํธ๋ ๊ทธ๋๋ก ์ ์ง
document.addEventListener('keydown', (event) => {
if (this.isLoading) return;
switch(event.code) {
case 'KeyW': this.keys.forward = true; break;
case 'KeyS': this.keys.backward = true; break;
case 'KeyA': this.keys.left = true; break;
case 'KeyD': this.keys.right = true; break;
}
});
document.addEventListener('keyup', (event) => {
if (this.isLoading) return;
switch(event.code) {
case 'KeyW': this.keys.forward = false; break;
case 'KeyS': this.keys.backward = false; break;
case 'KeyA': this.keys.left = false; break;
case 'KeyD': this.keys.right = false; break;
}
});
// ๋ง์ฐ์ค ์์ง์ ์ด๋ฒคํธ๋ฅผ ์์
document.addEventListener('mousemove', (event) => {
if (this.isLoading || !document.pointerLockElement) return;
// movementX/Y๋ฅผ ์ฌ์ฉํ์ฌ ๋ง์ฐ์ค ํ์ ๊ณ์ฐ
this.mouse.x += event.movementX * 0.002;
this.mouse.y += event.movementY * 0.002;
});
// ํด๋ฆญ ์ด๋ฒคํธ ์์ - ํฌ์ธํฐ ๋ฝ๊ณผ ๋ฐ์ฌ๋ฅผ ํจ๊ป ์ฒ๋ฆฌ
document.addEventListener('click', () => {
if (!document.pointerLockElement) {
document.body.requestPointerLock();
} else {
const bullet = this.tank.shoot(this.scene);
if (bullet) {
// ์ด์ ๋ฐ์ฌ ํจ๊ณผ์์ด๋ ์๊ฐํจ๊ณผ ์ถ๊ฐ ๊ฐ๋ฅ
}
}
});
// ํฌ์ธํฐ ๋ฝ ์ํ ๋ณ๊ฒฝ ์ด๋ฒคํธ ์ถ๊ฐ
document.addEventListener('pointerlockchange', () => {
if (!document.pointerLockElement) {
// ํฌ์ธํฐ ๋ฝ์ด ํด์ ๋์์ ๋์ ์ฒ๋ฆฌ
this.mouse.x = 0;
this.mouse.y = 0;
}
});
// ์ฐฝ ํฌ๊ธฐ ๋ณ๊ฒฝ ์ด๋ฒคํธ๋ ๊ทธ๋๋ก ์ ์ง
window.addEventListener('resize', () => {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
});
}
handleMovement() {
if (!this.tank.isLoaded) return;
const direction = new THREE.Vector3();
if (this.keys.forward) direction.z += 1;
if (this.keys.backward) direction.z -= 1;
if (this.keys.left) direction.x -= 1;
if (this.keys.right) direction.x += 1;
if (direction.length() > 0) {
direction.normalize();
// A,D ํค๋ก ํฑํฌ ํ์
if (this.keys.left) this.tank.rotate(-1);
if (this.keys.right) this.tank.rotate(1);
// ํ์ฌ ํฑํฌ์ ๋ฐฉํฅ์ผ๋ก ์ด๋
direction.applyEuler(this.tank.body.rotation);
this.tank.move(direction);
}
// ๋ง์ฐ์ค ์์น๋ฅผ ํฑํฌ ๊ธฐ์ค์ผ๋ก ๋ณํ
const mouseVector = new THREE.Vector2(this.mouse.x, -this.mouse.y);
const rotationAngle = Math.atan2(mouseVector.x, mouseVector.y);
// ํฌํ ํ์
if (this.tank.turretGroup) {
this.tank.turretGroup.rotation.y = rotationAngle;
}
// ์ฌ๊ธฐ๋ถํฐ ์นด๋ฉ๋ผ ๋ก์ง์ ์์ ํฉ๋๋ค
const tankPos = this.tank.getPosition();
const cameraDistance = 30; // ์นด๋ฉ๋ผ์ ํฑํฌ ์ฌ์ด์ ๊ฑฐ๋ฆฌ
const cameraHeight = 15; // ์นด๋ฉ๋ผ์ ๋์ด
const lookAtHeight = 5; // ์นด๋ฉ๋ผ๊ฐ ๋ฐ๋ผ๋ณด๋ ๋์ด
// ํฑํฌ์ ํ์ ์ ๋ฐ๋ผ ์นด๋ฉ๋ผ ์์น ๊ณ์ฐ
const tankRotation = this.tank.body.rotation.y;
// ์นด๋ฉ๋ผ ์์น ๊ณ์ฐ ์์
this.camera.position.set(
tankPos.x - Math.sin(tankRotation) * cameraDistance,
tankPos.y + cameraHeight,
tankPos.z - Math.cos(tankRotation) * cameraDistance
);
// ์นด๋ฉ๋ผ๊ฐ ๋ฐ๋ผ๋ณด๋ ์ง์ ์ ํฑํฌ ์์น๋ณด๋ค ์ฝ๊ฐ ์์ชฝ์ผ๋ก ์ค์
const lookAtPoint = new THREE.Vector3(
tankPos.x + Math.sin(tankRotation) * 10, // ํฑํฌ ์์ชฝ 10 ์ ๋
tankPos.y + lookAtHeight, // ํฑํฌ๋ณด๋ค ์ฝ๊ฐ ์
tankPos.z + Math.cos(tankRotation) * 10 // ํฑํฌ ์์ชฝ 10 ์ ๋
);
this.camera.lookAt(lookAtPoint);
}
createBuildings() {
const buildingTypes = [
{ width: 10, height: 30, depth: 10, color: 0x808080 },
{ width: 15, height: 40, depth: 15, color: 0x606060 },
{ width: 20, height: 50, depth: 20, color: 0x404040 }
];
for (let i = 0; i < BUILDING_COUNT; i++) {
const type = buildingTypes[Math.floor(Math.random() * buildingTypes.length)];
const building = this.createBuilding(type);
let position;
let attempts = 0;
do {
position = new THREE.Vector3(
(Math.random() - 0.5) * (MAP_SIZE - type.width),
type.height / 2,
(Math.random() - 0.5) * (MAP_SIZE - type.depth)
);
attempts++;
} while (this.checkBuildingCollision(position, type) && attempts < 50);
if (attempts < 50) {
building.position.copy(position);
this.buildings.push(building);
this.scene.add(building);
}
}
}
createBuilding(type) {
const geometry = new THREE.BoxGeometry(type.width, type.height, type.depth);
const material = new THREE.MeshPhongMaterial({
color: type.color,
emissive: 0x222222,
specular: 0x111111,
shininess: 30
});
const building = new THREE.Mesh(geometry, material);
building.castShadow = true;
building.receiveShadow = true;
return building;
}
checkBuildingCollision(position, type) {
const margin = 5;
const bbox = new THREE.Box3(
new THREE.Vector3(
position.x - (type.width / 2 + margin),
0,
position.z - (type.depth / 2 + margin)
),
new THREE.Vector3(
position.x + (type.width / 2 + margin),
type.height,
position.z + (type.depth / 2 + margin)
)
);
return this.buildings.some(building => {
const buildingBox = new THREE.Box3().setFromObject(building);
return bbox.intersectsBox(buildingBox);
});
}
handleLoadingError() {
this.isLoading = false;
const loadingElement = document.getElementById('loading');
if (loadingElement) {
loadingElement.innerHTML = `
<div class="loading-text" style="color: red;">
Loading failed. Please refresh the page.
</div>
`;
}
}
spawnEnemies() {
let spawnTimeout;
const spawnEnemy = () => {
if (this.isGameOver) {
clearTimeout(spawnTimeout);
return;
}
if (this.enemies.length < ENEMY_COUNT_MAX) {
const position = this.getValidEnemySpawnPosition();
if (position) {
const type = Math.random() < 0.7 ? 'tank' : 'heavy';
const enemy = new Enemy(this.scene, position, type);
enemy.initialize(this.loader).catch(console.error);
this.enemies.push(enemy);
}
}
spawnTimeout = setTimeout(spawnEnemy, 3000);
};
spawnEnemy();
}
getValidEnemySpawnPosition() {
const margin = 20;
let position;
let attempts = 0;
const maxAttempts = 50;
do {
position = new THREE.Vector3(
(Math.random() - 0.5) * (MAP_SIZE - margin * 2),
ENEMY_GROUND_HEIGHT,
(Math.random() - 0.5) * (MAP_SIZE - margin * 2)
);
const distanceToPlayer = position.distanceTo(this.tank.getPosition());
if (distanceToPlayer < 100) continue;
let collisionFound = false;
for (const building of this.buildings) {
const buildingBox = new THREE.Box3().setFromObject(building);
if (buildingBox.containsPoint(position)) {
collisionFound = true;
break;
}
}
if (!collisionFound) return position;
attempts++;
} while (attempts < maxAttempts);
return null;
}
startGameTimer() {
const timer = setInterval(() => {
if (this.isLoading) return;
this.gameTime--;
if (this.gameTime <= 0 || this.isGameOver) {
clearInterval(timer);
this.endGame();
}
}, 1000);
}
updateParticles() {
for (let i = this.particles.length - 1; i >= 0; i--) {
const particle = this.particles[i];
if (!particle.update()) {
particle.destroy(this.scene);
this.particles.splice(i, 1);
}
}
}
createExplosion(position) {
for (let i = 0; i < PARTICLE_COUNT; i++) {
this.particles.push(new Particle(this.scene, position));
}
}
checkCollisions() {
if (this.isLoading || !this.tank.isLoaded) return;
const tankPosition = this.tank.getPosition();
const tankBoundingBox = new THREE.Box3().setFromObject(this.tank.body);
// ์ ์ด์๊ณผ์ ์ถฉ๋ ๊ฒ์ฌ๋ฅผ ์ต์ ํ
for (const enemy of this.enemies) {
if (!enemy.mesh || !enemy.isLoaded) continue;
for (let i = enemy.bullets.length - 1; i >= 0; i--) {
const bullet = enemy.bullets[i];
const distance = bullet.position.distanceTo(tankPosition);
if (distance > 50) continue; // ๋จผ ์ด์์ ๊ฒ์ฌ ์คํต
if (distance < 1) {
if (this.tank.takeDamage(10)) {
this.endGame();
return;
}
this.dispose(bullet);
enemy.bullets.splice(i, 1);
this.createExplosion(bullet.position);
document.getElementById('health').style.width =
`${(this.tank.health / MAX_HEALTH) * 100}%`;
}
}
}
// ๊ฑด๋ฌผ๊ณผ์ ์ถฉ๋ ๊ฒ์ฌ ์ต์ ํ
for (const building of this.buildings) {
const buildingBox = new THREE.Box3().setFromObject(building);
if (tankBoundingBox.intersectsBox(buildingBox)) {
this.tank.body.position.copy(this.previousTankPosition);
break;
}
}
this.previousTankPosition = this.tank.body.position.clone();
}
endGame() {
this.isGameOver = true;
const gameOverDiv = document.createElement('div');
gameOverDiv.style.position = 'absolute';
gameOverDiv.style.top = '50%';
gameOverDiv.style.left = '50%';
gameOverDiv.style.transform = 'translate(-50%, -50%)';
gameOverDiv.style.color = 'white';
gameOverDiv.style.fontSize = '48px';
gameOverDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
gameOverDiv.style.padding = '20px';
gameOverDiv.style.borderRadius = '10px';
gameOverDiv.innerHTML = `
Game Over<br>
Score: ${this.score}<br>
Time Survived: ${GAME_DURATION - this.gameTime}s<br>
<button onclick="location.reload()"
style="font-size: 24px; padding: 10px; margin-top: 20px;
cursor: pointer; background: #4CAF50; border: none;
color: white; border-radius: 5px;">
Play Again
</button>
`;
document.body.appendChild(gameOverDiv);
}
animate() {
if (this.isGameOver) return;
requestAnimationFrame(() => this.animate());
const currentTime = performance.now();
const deltaTime = (currentTime - this.lastTime) / 1000;
this.lastTime = currentTime;
if (this.isLoading) {
this.renderer.render(this.scene, this.camera);
return;
}
// ํ๋ ์ ์ ํ (60fps)
if (deltaTime < 1/60) return;
this.handleMovement();
this.tank.update(this.mouse.x, this.mouse.y);
const tankPosition = this.tank.getPosition();
// ํ๋ฉด์ ๋ณด์ด๋ ์ ๋ง ์
๋ฐ์ดํธ
this.enemies.forEach(enemy => {
if (!enemy.mesh || !enemy.isLoaded) return;
const distance = enemy.mesh.position.distanceTo(tankPosition);
if (distance > 200) return; // ๋จผ ์ ์ ์
๋ฐ์ดํธ ์คํต
enemy.update(tankPosition);
if (distance < ENEMY_CONFIG.ATTACK_RANGE) {
enemy.shoot(tankPosition);
}
});
this.updateParticles();
this.checkCollisions();
this.cleanupBullets();
this.updateUI();
this.renderer.render(this.scene, this.camera);
}
updateUI() {
const healthBar = document.getElementById('health');
if (healthBar) {
healthBar.style.width = `${(this.tank.health / MAX_HEALTH) * 100}%`;
}
const timeElement = document.getElementById('time');
if (timeElement) {
timeElement.textContent = `Time: ${this.gameTime}s`;
}
const scoreElement = document.getElementById('score');
if (scoreElement) {
scoreElement.textContent = `Score: ${this.score}`;
}
}
}
// HTML์ startGame ํจ์์ ์ฐ๊ฒฐ
window.startGame = function() {
document.getElementById('startScreen').style.display = 'none';
document.body.requestPointerLock();
};
// ๊ฒ์ ์ธ์คํด์ค ์์ฑ
const game = new Game(); |