Spaces:
Running
Running
Update game.js
Browse files
game.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
|
| 5 |
// ๊ฒ์ ์์
|
| 6 |
const GAME_DURATION = 180;
|
|
@@ -572,67 +572,31 @@ class Enemy {
|
|
| 572 |
constructor(scene, position, type = 'tank') {
|
| 573 |
this.scene = scene;
|
| 574 |
this.position = position;
|
| 575 |
-
this.
|
| 576 |
-
this.turret = null;
|
| 577 |
-
this.turretGroup = new THREE.Group();
|
| 578 |
this.type = type;
|
| 579 |
this.health = type === 'tank' ? 100 : 200;
|
| 580 |
this.lastAttackTime = 0;
|
| 581 |
this.bullets = [];
|
| 582 |
this.isLoaded = false;
|
| 583 |
this.moveSpeed = type === 'tank' ? ENEMY_MOVE_SPEED : ENEMY_MOVE_SPEED * 0.7;
|
| 584 |
-
this.turnSpeed = 0.03;
|
| 585 |
-
this.targetRotation = 0;
|
| 586 |
-
this.turretRotation = 0;
|
| 587 |
-
this.canShoot = false; // ํฌํ์ด ์ ๋ ฌ๋์๋์ง ํ์ธํ๋ ํ๋๊ทธ
|
| 588 |
}
|
| 589 |
|
| 590 |
async initialize(loader) {
|
| 591 |
try {
|
| 592 |
-
//
|
| 593 |
-
const
|
| 594 |
-
this.
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
const turretResult = await loader.loadAsync('/models/t90Turret.glb');
|
| 598 |
-
this.turret = turretResult.scene;
|
| 599 |
-
|
| 600 |
-
// ํฌํ ๊ทธ๋ฃน ์ค์
|
| 601 |
-
this.turretGroup.position.y = 0.2; // ํฌํ ๋์ด ์กฐ์
|
| 602 |
-
this.turretGroup.add(this.turret);
|
| 603 |
-
this.body.add(this.turretGroup);
|
| 604 |
-
|
| 605 |
-
// ์์น์ ์ค์ผ์ผ ์ค์
|
| 606 |
-
this.body.position.copy(this.position);
|
| 607 |
-
this.body.scale.set(ENEMY_SCALE, ENEMY_SCALE, ENEMY_SCALE);
|
| 608 |
-
|
| 609 |
-
// ๊ทธ๋ฆผ์ ์ค์
|
| 610 |
-
this.body.traverse((child) => {
|
| 611 |
-
if (child.isMesh) {
|
| 612 |
-
child.castShadow = true;
|
| 613 |
-
child.receiveShadow = true;
|
| 614 |
-
}
|
| 615 |
-
});
|
| 616 |
|
| 617 |
-
this.
|
| 618 |
if (child.isMesh) {
|
| 619 |
child.castShadow = true;
|
| 620 |
child.receiveShadow = true;
|
| 621 |
}
|
| 622 |
});
|
| 623 |
|
| 624 |
-
|
| 625 |
-
const shadowPlaneGeometry = new THREE.PlaneGeometry(8, 8);
|
| 626 |
-
const shadowPlaneMaterial = new THREE.ShadowMaterial({
|
| 627 |
-
opacity: 0.3
|
| 628 |
-
});
|
| 629 |
-
this.shadowPlane = new THREE.Mesh(shadowPlaneGeometry, shadowPlaneMaterial);
|
| 630 |
-
this.shadowPlane.receiveShadow = true;
|
| 631 |
-
this.shadowPlane.rotation.x = -Math.PI / 2;
|
| 632 |
-
this.shadowPlane.position.y = 0.1;
|
| 633 |
-
this.body.add(this.shadowPlane);
|
| 634 |
-
|
| 635 |
-
this.scene.add(this.body);
|
| 636 |
this.isLoaded = true;
|
| 637 |
} catch (error) {
|
| 638 |
console.error('Error loading enemy model:', error);
|
|
@@ -641,76 +605,38 @@ class Enemy {
|
|
| 641 |
}
|
| 642 |
|
| 643 |
update(playerPosition) {
|
| 644 |
-
|
| 645 |
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
this.body.rotation.y += Math.sign(rotationDiff) * Math.min(Math.abs(rotationDiff), this.turnSpeed);
|
| 666 |
-
|
| 667 |
-
// ์ ์ง ๋ฐฉํฅ์ผ๋ก ์ด๋
|
| 668 |
-
const moveVector = new THREE.Vector3(0, 0, this.moveSpeed);
|
| 669 |
-
moveVector.applyEuler(this.body.rotation);
|
| 670 |
-
const newPosition = this.body.position.clone().add(moveVector);
|
| 671 |
-
|
| 672 |
-
// ์งํ ๋์ด ๊ฐ์ ธ์ค๊ธฐ
|
| 673 |
-
const heightAtNewPos = window.gameInstance.getHeightAtPosition(
|
| 674 |
-
newPosition.x,
|
| 675 |
-
newPosition.z
|
| 676 |
-
);
|
| 677 |
-
newPosition.y = heightAtNewPos + TANK_HEIGHT;
|
| 678 |
-
|
| 679 |
-
// ์ถฉ๋ ๊ฒ์ฌ ๋ฐ ์ด๋ ์ฒ๋ฆฌ
|
| 680 |
-
this.handleCollisions(newPosition, previousPosition);
|
| 681 |
-
}
|
| 682 |
-
|
| 683 |
-
// ํฌํ ํ์ - ํ๋ ์ด์ด๋ฅผ ํฅํด
|
| 684 |
-
const turretTargetRotation = Math.atan2(
|
| 685 |
-
playerPosition.x - this.body.position.x,
|
| 686 |
-
playerPosition.z - this.body.position.z
|
| 687 |
);
|
|
|
|
| 688 |
|
| 689 |
-
// ์๋ ์ขํ์์์ ํฌํ ํ์ ์ ๋ก์ปฌ ํ์ ์ผ๋ก ๋ณํ
|
| 690 |
-
const localTurretRotation = turretTargetRotation - this.body.rotation.y;
|
| 691 |
-
|
| 692 |
-
// ํฌํ ํ์ ๋ณด๊ฐ
|
| 693 |
-
let turretRotationDiff = localTurretRotation - this.turretGroup.rotation.y;
|
| 694 |
-
while (turretRotationDiff > Math.PI) turretRotationDiff -= Math.PI * 2;
|
| 695 |
-
while (turretRotationDiff < -Math.PI) turretRotationDiff += Math.PI * 2;
|
| 696 |
-
|
| 697 |
-
this.turretGroup.rotation.y += Math.sign(turretRotationDiff) * Math.min(Math.abs(turretRotationDiff), this.turnSpeed);
|
| 698 |
-
|
| 699 |
-
// ํฌํ์ด ํ๋ ์ด์ด๋ฅผ ํฅํ๊ณ ์๋์ง ํ์ธ
|
| 700 |
-
this.canShoot = Math.abs(turretRotationDiff) < 0.1;
|
| 701 |
-
|
| 702 |
-
// ์ด์ ์
๋ฐ์ดํธ ๋ก์ง
|
| 703 |
-
this.updateBullets();
|
| 704 |
-
}
|
| 705 |
-
|
| 706 |
-
handleCollisions(newPosition, previousPosition) {
|
| 707 |
// ์์๋ก ์์น ์ด๋ํ์ฌ ์ถฉ๋ ์ฒดํฌ
|
| 708 |
-
this.
|
|
|
|
| 709 |
|
| 710 |
-
|
|
|
|
| 711 |
let hasCollision = false;
|
| 712 |
|
| 713 |
-
// ์ฅ์ ๋ฌผ ์ถฉ๋ ๊ฒ์ฌ
|
| 714 |
for (const obstacle of window.gameInstance.obstacles) {
|
| 715 |
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
|
| 716 |
if (enemyBox.intersectsBox(obstacleBox)) {
|
|
@@ -719,34 +645,127 @@ class Enemy {
|
|
| 719 |
}
|
| 720 |
}
|
| 721 |
|
| 722 |
-
// ๋ค๋ฅธ ์
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
|
|
|
|
|
|
| 729 |
}
|
| 730 |
}
|
| 731 |
}
|
| 732 |
|
| 733 |
-
// ๋งต ๊ฒฝ๊ณ
|
| 734 |
const mapBoundary = MAP_SIZE / 2;
|
| 735 |
if (Math.abs(newPosition.x) > mapBoundary ||
|
| 736 |
Math.abs(newPosition.z) > mapBoundary) {
|
| 737 |
hasCollision = true;
|
| 738 |
}
|
| 739 |
|
| 740 |
-
// ์ถฉ๋์ด ์์ผ๋ฉด ์ด์ ์์น๋ก ๋ณต๊ท
|
| 741 |
if (hasCollision) {
|
| 742 |
-
this.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 743 |
}
|
| 744 |
}
|
|
|
|
| 745 |
|
| 746 |
-
shoot(playerPosition) {
|
| 747 |
-
// ํฌํ์ด ์ ๋ ฌ๋์ง ์์์ผ๋ฉด ๋ฐ์ฌํ์ง ์์
|
| 748 |
-
if (!this.canShoot) return;
|
| 749 |
|
|
|
|
| 750 |
const currentTime = Date.now();
|
| 751 |
const attackInterval = this.type === 'tank' ?
|
| 752 |
ENEMY_CONFIG.ATTACK_INTERVAL :
|
|
@@ -754,28 +773,16 @@ class Enemy {
|
|
| 754 |
|
| 755 |
if (currentTime - this.lastAttackTime < attackInterval) return;
|
| 756 |
|
| 757 |
-
// ํฌํ ์์ฑ
|
| 758 |
const bulletGeometry = new THREE.SphereGeometry(this.type === 'tank' ? 0.2 : 0.3);
|
| 759 |
const bulletMaterial = new THREE.MeshBasicMaterial({
|
| 760 |
color: this.type === 'tank' ? 0xff0000 : 0xff6600
|
| 761 |
});
|
| 762 |
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
|
| 763 |
|
| 764 |
-
|
| 765 |
-
const muzzleOffset = new THREE.Vector3(0, 0.5, 4);
|
| 766 |
-
const muzzlePosition = new THREE.Vector3();
|
| 767 |
-
const turretWorldQuaternion = new THREE.Quaternion();
|
| 768 |
-
|
| 769 |
-
this.turret.getWorldPosition(muzzlePosition);
|
| 770 |
-
this.turret.getWorldQuaternion(turretWorldQuaternion);
|
| 771 |
-
|
| 772 |
-
muzzleOffset.applyQuaternion(turretWorldQuaternion);
|
| 773 |
-
muzzlePosition.add(muzzleOffset);
|
| 774 |
-
|
| 775 |
-
bullet.position.copy(muzzlePosition);
|
| 776 |
|
| 777 |
const direction = new THREE.Vector3()
|
| 778 |
-
.subVectors(playerPosition,
|
| 779 |
.normalize();
|
| 780 |
|
| 781 |
const bulletSpeed = this.type === 'tank' ?
|
|
@@ -789,40 +796,14 @@ class Enemy {
|
|
| 789 |
this.lastAttackTime = currentTime;
|
| 790 |
}
|
| 791 |
|
| 792 |
-
updateBullets() {
|
| 793 |
-
for (let i = this.bullets.length - 1; i >= 0; i--) {
|
| 794 |
-
const bullet = this.bullets[i];
|
| 795 |
-
bullet.position.add(bullet.velocity);
|
| 796 |
-
|
| 797 |
-
// ๋งต ๋ฐ์ผ๋ก ๋๊ฐ๊ฑฐ๋ ์ฅ์ ๋ฌผ๊ณผ ์ถฉ๋ํ๋ฉด ์ ๊ฑฐ
|
| 798 |
-
if (Math.abs(bullet.position.x) > MAP_SIZE / 2 ||
|
| 799 |
-
Math.abs(bullet.position.z) > MAP_SIZE / 2) {
|
| 800 |
-
this.scene.remove(bullet);
|
| 801 |
-
this.bullets.splice(i, 1);
|
| 802 |
-
continue;
|
| 803 |
-
}
|
| 804 |
-
|
| 805 |
-
// ์ฅ์ ๋ฌผ๊ณผ ์ถฉ๋ ์ฒดํฌ
|
| 806 |
-
const bulletBox = new THREE.Box3().setFromObject(bullet);
|
| 807 |
-
for (const obstacle of window.gameInstance.obstacles) {
|
| 808 |
-
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
|
| 809 |
-
if (bulletBox.intersectsBox(obstacleBox)) {
|
| 810 |
-
this.scene.remove(bullet);
|
| 811 |
-
this.bullets.splice(i, 1);
|
| 812 |
-
break;
|
| 813 |
-
}
|
| 814 |
-
}
|
| 815 |
-
}
|
| 816 |
-
}
|
| 817 |
-
|
| 818 |
takeDamage(damage) {
|
| 819 |
this.health -= damage;
|
| 820 |
return this.health <= 0;
|
| 821 |
}
|
| 822 |
|
| 823 |
destroy() {
|
| 824 |
-
if (this.
|
| 825 |
-
this.scene.remove(this.
|
| 826 |
this.bullets.forEach(bullet => this.scene.remove(bullet));
|
| 827 |
this.bullets = [];
|
| 828 |
this.isLoaded = false;
|
|
@@ -1097,56 +1078,56 @@ class Game {
|
|
| 1097 |
|
| 1098 |
// ๋ ์ด๋ ์
๋ฐ์ดํธ ๋ฉ์๋ ์ถ๊ฐ
|
| 1099 |
updateRadar() {
|
| 1100 |
-
|
| 1101 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
|
| 1110 |
-
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
|
| 1131 |
-
|
| 1132 |
-
|
| 1133 |
|
| 1134 |
-
|
| 1135 |
-
|
| 1136 |
-
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
| 1147 |
|
| 1148 |
-
|
| 1149 |
-
}
|
| 1150 |
|
| 1151 |
async addDesertDecorations() {
|
| 1152 |
if (!this.obstacles) {
|
|
@@ -1858,47 +1839,45 @@ this.enemies.forEach(enemy => {
|
|
| 1858 |
}
|
| 1859 |
|
| 1860 |
animate() {
|
| 1861 |
-
|
| 1862 |
-
|
| 1863 |
-
|
| 1864 |
-
|
| 1865 |
-
|
| 1866 |
-
|
| 1867 |
|
| 1868 |
-
|
| 1869 |
-
|
| 1870 |
-
|
| 1871 |
-
|
| 1872 |
-
|
| 1873 |
-
|
| 1874 |
|
| 1875 |
-
|
| 1876 |
-
|
| 1877 |
-
|
| 1878 |
|
| 1879 |
-
|
| 1880 |
-
|
| 1881 |
-
|
| 1882 |
-
|
| 1883 |
-
|
| 1884 |
-
|
| 1885 |
-
|
| 1886 |
-
|
| 1887 |
-
|
| 1888 |
-
|
| 1889 |
-
|
| 1890 |
-
|
| 1891 |
-
}
|
| 1892 |
-
}
|
| 1893 |
-
});
|
| 1894 |
|
| 1895 |
-
|
| 1896 |
-
|
| 1897 |
-
|
| 1898 |
-
|
| 1899 |
-
|
| 1900 |
-
|
| 1901 |
-
|
|
|
|
| 1902 |
}
|
| 1903 |
|
| 1904 |
// Start game
|
|
|
|
| 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;
|
|
|
|
| 572 |
constructor(scene, position, type = 'tank') {
|
| 573 |
this.scene = scene;
|
| 574 |
this.position = position;
|
| 575 |
+
this.mesh = null;
|
|
|
|
|
|
|
| 576 |
this.type = type;
|
| 577 |
this.health = type === 'tank' ? 100 : 200;
|
| 578 |
this.lastAttackTime = 0;
|
| 579 |
this.bullets = [];
|
| 580 |
this.isLoaded = false;
|
| 581 |
this.moveSpeed = type === 'tank' ? ENEMY_MOVE_SPEED : ENEMY_MOVE_SPEED * 0.7;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
}
|
| 583 |
|
| 584 |
async initialize(loader) {
|
| 585 |
try {
|
| 586 |
+
const modelPath = this.type === 'tank' ? '/models/enemy1.glb' : '/models/enemy4.glb';
|
| 587 |
+
const result = await loader.loadAsync(modelPath);
|
| 588 |
+
this.mesh = result.scene;
|
| 589 |
+
this.mesh.position.copy(this.position);
|
| 590 |
+
this.mesh.scale.set(ENEMY_SCALE, ENEMY_SCALE, ENEMY_SCALE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
|
| 592 |
+
this.mesh.traverse((child) => {
|
| 593 |
if (child.isMesh) {
|
| 594 |
child.castShadow = true;
|
| 595 |
child.receiveShadow = true;
|
| 596 |
}
|
| 597 |
});
|
| 598 |
|
| 599 |
+
this.scene.add(this.mesh);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
this.isLoaded = true;
|
| 601 |
} catch (error) {
|
| 602 |
console.error('Error loading enemy model:', error);
|
|
|
|
| 605 |
}
|
| 606 |
|
| 607 |
update(playerPosition) {
|
| 608 |
+
if (!this.mesh || !this.isLoaded) return;
|
| 609 |
|
| 610 |
+
const direction = new THREE.Vector3()
|
| 611 |
+
.subVectors(playerPosition, this.mesh.position)
|
| 612 |
+
.normalize();
|
| 613 |
+
|
| 614 |
+
const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
|
| 615 |
+
const minDistance = 50;
|
| 616 |
+
|
| 617 |
+
// ์ด์ ์์น ์ ์ฅ
|
| 618 |
+
const previousPosition = this.mesh.position.clone();
|
| 619 |
+
|
| 620 |
+
if (distanceToPlayer > minDistance) {
|
| 621 |
+
const moveVector = direction.multiplyScalar(this.moveSpeed);
|
| 622 |
+
const newPosition = this.mesh.position.clone().add(moveVector);
|
| 623 |
+
|
| 624 |
+
// ์งํ ๋์ด ๊ฐ์ ธ์ค๊ธฐ
|
| 625 |
+
const heightAtNewPos = window.gameInstance.getHeightAtPosition(
|
| 626 |
+
newPosition.x,
|
| 627 |
+
newPosition.z
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
);
|
| 629 |
+
newPosition.y = heightAtNewPos + TANK_HEIGHT;
|
| 630 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
// ์์๋ก ์์น ์ด๋ํ์ฌ ์ถฉ๋ ์ฒดํฌ
|
| 632 |
+
const originalPosition = this.mesh.position.clone();
|
| 633 |
+
this.mesh.position.copy(newPosition);
|
| 634 |
|
| 635 |
+
// ์ฅ์ ๋ฌผ๊ณผ ์ถฉ๋ ์ฒดํฌ
|
| 636 |
+
const enemyBox = new THREE.Box3().setFromObject(this.mesh);
|
| 637 |
let hasCollision = false;
|
| 638 |
|
| 639 |
+
// ๋ชจ๋ ์ฅ์ ๋ฌผ์ ๋ํด ์ถฉ๋ ๊ฒ์ฌ
|
| 640 |
for (const obstacle of window.gameInstance.obstacles) {
|
| 641 |
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
|
| 642 |
if (enemyBox.intersectsBox(obstacleBox)) {
|
|
|
|
| 645 |
}
|
| 646 |
}
|
| 647 |
|
| 648 |
+
// ๋ค๋ฅธ ์ ํฑํฌ์์ ์ถฉ๋ ๊ฒ์ฌ
|
| 649 |
+
if (!hasCollision) {
|
| 650 |
+
for (const otherEnemy of window.gameInstance.enemies) {
|
| 651 |
+
if (otherEnemy !== this && otherEnemy.mesh) {
|
| 652 |
+
const otherEnemyBox = new THREE.Box3().setFromObject(otherEnemy.mesh);
|
| 653 |
+
if (enemyBox.intersectsBox(otherEnemyBox)) {
|
| 654 |
+
hasCollision = true;
|
| 655 |
+
break;
|
| 656 |
+
}
|
| 657 |
}
|
| 658 |
}
|
| 659 |
}
|
| 660 |
|
| 661 |
+
// ๋งต ๊ฒฝ๊ณ ์ฒดํฌ
|
| 662 |
const mapBoundary = MAP_SIZE / 2;
|
| 663 |
if (Math.abs(newPosition.x) > mapBoundary ||
|
| 664 |
Math.abs(newPosition.z) > mapBoundary) {
|
| 665 |
hasCollision = true;
|
| 666 |
}
|
| 667 |
|
| 668 |
+
// ์ถฉ๋์ด ์์ผ๋ฉด ์ด์ ์์น๋ก ๋ณต๊ท, ์์ผ๋ฉด ์ ์์น ์ ์ง
|
| 669 |
if (hasCollision) {
|
| 670 |
+
this.mesh.position.copy(previousPosition);
|
| 671 |
+
|
| 672 |
+
// ์ถฉ๋ ์ ์ฐํ ๊ฒฝ๋ก ์๋
|
| 673 |
+
const alternateDirections = [
|
| 674 |
+
new THREE.Vector3(-direction.z, 0, direction.x), // ์ผ์ชฝ์ผ๋ก 90๋
|
| 675 |
+
new THREE.Vector3(direction.z, 0, -direction.x), // ์ค๋ฅธ์ชฝ์ผ๋ก 90๋
|
| 676 |
+
new THREE.Vector3(-direction.x, 0, -direction.z) // 180๋ ํ์
|
| 677 |
+
];
|
| 678 |
+
|
| 679 |
+
for (const altDirection of alternateDirections) {
|
| 680 |
+
const altMoveVector = altDirection.multiplyScalar(this.moveSpeed);
|
| 681 |
+
const altNewPosition = previousPosition.clone().add(altMoveVector);
|
| 682 |
+
|
| 683 |
+
this.mesh.position.copy(altNewPosition);
|
| 684 |
+
const altEnemyBox = new THREE.Box3().setFromObject(this.mesh);
|
| 685 |
+
|
| 686 |
+
let altHasCollision = false;
|
| 687 |
+
|
| 688 |
+
// ์๋ก์ด ๋ฐฉํฅ์ ๋ํ ์ถฉ๋ ๊ฒ์ฌ
|
| 689 |
+
for (const obstacle of window.gameInstance.obstacles) {
|
| 690 |
+
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
|
| 691 |
+
if (altEnemyBox.intersectsBox(obstacleBox)) {
|
| 692 |
+
altHasCollision = true;
|
| 693 |
+
break;
|
| 694 |
+
}
|
| 695 |
+
}
|
| 696 |
+
|
| 697 |
+
if (!altHasCollision) {
|
| 698 |
+
// ์ฐํ ๊ฒฝ๋ก๊ฐ ๊ฐ๋ฅํ๋ฉด ๊ทธ ๋ฐฉํฅ์ผ๋ก ์ด๋
|
| 699 |
+
break;
|
| 700 |
+
} else {
|
| 701 |
+
// ์ฐํ๋ ๋ถ๊ฐ๋ฅํ๋ฉด ์ด์ ์์น๋ก ๋ณต๊ท
|
| 702 |
+
this.mesh.position.copy(previousPosition);
|
| 703 |
+
}
|
| 704 |
+
}
|
| 705 |
+
}
|
| 706 |
+
|
| 707 |
+
// ์งํ์ ๋ฐ๋ฅธ ๊ธฐ์ธ๊ธฐ ์กฐ์
|
| 708 |
+
const forwardVector = new THREE.Vector3(0, 0, 1).applyQuaternion(this.mesh.quaternion);
|
| 709 |
+
const rightVector = new THREE.Vector3(1, 0, 0).applyQuaternion(this.mesh.quaternion);
|
| 710 |
+
|
| 711 |
+
const frontHeight = window.gameInstance.getHeightAtPosition(
|
| 712 |
+
this.mesh.position.x + forwardVector.x,
|
| 713 |
+
this.mesh.position.z + forwardVector.z
|
| 714 |
+
);
|
| 715 |
+
const backHeight = window.gameInstance.getHeightAtPosition(
|
| 716 |
+
this.mesh.position.x - forwardVector.x,
|
| 717 |
+
this.mesh.position.z - forwardVector.z
|
| 718 |
+
);
|
| 719 |
+
const rightHeight = window.gameInstance.getHeightAtPosition(
|
| 720 |
+
this.mesh.position.x + rightVector.x,
|
| 721 |
+
this.mesh.position.z + rightVector.z
|
| 722 |
+
);
|
| 723 |
+
const leftHeight = window.gameInstance.getHeightAtPosition(
|
| 724 |
+
this.mesh.position.x - rightVector.x,
|
| 725 |
+
this.mesh.position.z - rightVector.z
|
| 726 |
+
);
|
| 727 |
+
|
| 728 |
+
const pitch = Math.atan2(frontHeight - backHeight, 2);
|
| 729 |
+
const roll = Math.atan2(rightHeight - leftHeight, 2);
|
| 730 |
+
|
| 731 |
+
// ํ์ฌ ํ์ ์ ์งํ๋ฉด์ ๊ธฐ์ธ๊ธฐ๋ง ์ ์ฉ
|
| 732 |
+
const currentRotation = this.mesh.rotation.y;
|
| 733 |
+
this.mesh.rotation.set(pitch, currentRotation, roll);
|
| 734 |
+
}
|
| 735 |
+
|
| 736 |
+
// ํ๋ ์ด์ด๋ฅผ ํฅํด ํฌํ ํ์
|
| 737 |
+
this.mesh.lookAt(playerPosition);
|
| 738 |
+
|
| 739 |
+
// ์ด์ ์
๋ฐ์ดํธ
|
| 740 |
+
if (this.bullets) {
|
| 741 |
+
for (let i = this.bullets.length - 1; i >= 0; i--) {
|
| 742 |
+
const bullet = this.bullets[i];
|
| 743 |
+
bullet.position.add(bullet.velocity);
|
| 744 |
+
|
| 745 |
+
// ์ด์์ด ๋งต ๋ฐ์ผ๋ก ๋๊ฐ๊ฑฐ๋ ์ฅ์ ๋ฌผ๊ณผ ์ถฉ๋ํ๋ฉด ์ ๊ฑฐ
|
| 746 |
+
if (Math.abs(bullet.position.x) > MAP_SIZE / 2 ||
|
| 747 |
+
Math.abs(bullet.position.z) > MAP_SIZE / 2) {
|
| 748 |
+
this.scene.remove(bullet);
|
| 749 |
+
this.bullets.splice(i, 1);
|
| 750 |
+
continue;
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
// ์ด์๊ณผ ์ฅ์ ๋ฌผ ์ถฉ๋ ์ฒดํฌ
|
| 754 |
+
const bulletBox = new THREE.Box3().setFromObject(bullet);
|
| 755 |
+
for (const obstacle of window.gameInstance.obstacles) {
|
| 756 |
+
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
|
| 757 |
+
if (bulletBox.intersectsBox(obstacleBox)) {
|
| 758 |
+
this.scene.remove(bullet);
|
| 759 |
+
this.bullets.splice(i, 1);
|
| 760 |
+
break;
|
| 761 |
+
}
|
| 762 |
+
}
|
| 763 |
}
|
| 764 |
}
|
| 765 |
+
}
|
| 766 |
|
|
|
|
|
|
|
|
|
|
| 767 |
|
| 768 |
+
shoot(playerPosition) {
|
| 769 |
const currentTime = Date.now();
|
| 770 |
const attackInterval = this.type === 'tank' ?
|
| 771 |
ENEMY_CONFIG.ATTACK_INTERVAL :
|
|
|
|
| 773 |
|
| 774 |
if (currentTime - this.lastAttackTime < attackInterval) return;
|
| 775 |
|
|
|
|
| 776 |
const bulletGeometry = new THREE.SphereGeometry(this.type === 'tank' ? 0.2 : 0.3);
|
| 777 |
const bulletMaterial = new THREE.MeshBasicMaterial({
|
| 778 |
color: this.type === 'tank' ? 0xff0000 : 0xff6600
|
| 779 |
});
|
| 780 |
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
|
| 781 |
|
| 782 |
+
bullet.position.copy(this.mesh.position);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
|
| 784 |
const direction = new THREE.Vector3()
|
| 785 |
+
.subVectors(playerPosition, this.mesh.position)
|
| 786 |
.normalize();
|
| 787 |
|
| 788 |
const bulletSpeed = this.type === 'tank' ?
|
|
|
|
| 796 |
this.lastAttackTime = currentTime;
|
| 797 |
}
|
| 798 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
takeDamage(damage) {
|
| 800 |
this.health -= damage;
|
| 801 |
return this.health <= 0;
|
| 802 |
}
|
| 803 |
|
| 804 |
destroy() {
|
| 805 |
+
if (this.mesh) {
|
| 806 |
+
this.scene.remove(this.mesh);
|
| 807 |
this.bullets.forEach(bullet => this.scene.remove(bullet));
|
| 808 |
this.bullets = [];
|
| 809 |
this.isLoaded = false;
|
|
|
|
| 1078 |
|
| 1079 |
// ๋ ์ด๋ ์
๋ฐ์ดํธ ๋ฉ์๋ ์ถ๊ฐ
|
| 1080 |
updateRadar() {
|
| 1081 |
+
const currentTime = Date.now();
|
| 1082 |
+
if (currentTime - this.lastRadarUpdate < this.radarUpdateInterval) return;
|
| 1083 |
+
|
| 1084 |
+
const radar = document.getElementById('radar');
|
| 1085 |
+
const radarRect = radar.getBoundingClientRect();
|
| 1086 |
+
const radarCenter = {
|
| 1087 |
+
x: radarRect.width / 2,
|
| 1088 |
+
y: radarRect.height / 2
|
| 1089 |
+
};
|
| 1090 |
|
| 1091 |
+
// ๊ธฐ์กด ์ ๋ํธ ์ ๊ฑฐ
|
| 1092 |
+
const oldDots = radar.getElementsByClassName('enemy-dot');
|
| 1093 |
+
while (oldDots[0]) {
|
| 1094 |
+
oldDots[0].remove();
|
| 1095 |
+
}
|
| 1096 |
|
| 1097 |
+
// ํฑํฌ ์์น ๊ฐ์ ธ์ค๊ธฐ
|
| 1098 |
+
const tankPos = this.tank.getPosition();
|
| 1099 |
|
| 1100 |
+
// ๋ชจ๋ ์ ์ ๋ํด ๋ ์ด๋์ ํ์
|
| 1101 |
+
this.enemies.forEach(enemy => {
|
| 1102 |
+
if (!enemy.mesh || !enemy.isLoaded) return;
|
| 1103 |
|
| 1104 |
+
const enemyPos = enemy.mesh.position;
|
| 1105 |
+
const distance = tankPos.distanceTo(enemyPos);
|
| 1106 |
|
| 1107 |
+
// ๋ ์ด๋ ๋ฒ์ ๋ด์ ์๋ ๊ฒฝ์ฐ๋ง ํ์
|
| 1108 |
+
if (distance <= this.radarRange) {
|
| 1109 |
+
// ํฑํฌ ๊ธฐ์ค ์๋ ๊ฐ๋ ๊ณ์ฐ
|
| 1110 |
+
const angle = Math.atan2(
|
| 1111 |
+
enemyPos.x - tankPos.x,
|
| 1112 |
+
enemyPos.z - tankPos.z
|
| 1113 |
+
);
|
| 1114 |
|
| 1115 |
+
// ์๋ ๊ฑฐ๋ฆฌ๋ฅผ ๋ ์ด๋ ํฌ๊ธฐ์ ๋ง๊ฒ ์ค์ผ์ผ๋ง
|
| 1116 |
+
const relativeDistance = distance / this.radarRange;
|
| 1117 |
+
const dotX = radarCenter.x + Math.sin(angle) * (radarCenter.x * relativeDistance);
|
| 1118 |
+
const dotY = radarCenter.y + Math.cos(angle) * (radarCenter.y * relativeDistance);
|
| 1119 |
+
|
| 1120 |
+
// ์ ๋ํธ ์์ฑ ๋ฐ ์ถ๊ฐ
|
| 1121 |
+
const dot = document.createElement('div');
|
| 1122 |
+
dot.className = 'enemy-dot';
|
| 1123 |
+
dot.style.left = `${dotX}px`;
|
| 1124 |
+
dot.style.top = `${dotY}px`;
|
| 1125 |
+
radar.appendChild(dot);
|
| 1126 |
+
}
|
| 1127 |
+
});
|
| 1128 |
|
| 1129 |
+
this.lastRadarUpdate = currentTime;
|
| 1130 |
+
}
|
| 1131 |
|
| 1132 |
async addDesertDecorations() {
|
| 1133 |
if (!this.obstacles) {
|
|
|
|
| 1839 |
}
|
| 1840 |
|
| 1841 |
animate() {
|
| 1842 |
+
if (this.isGameOver) {
|
| 1843 |
+
if (this.animationFrameId) {
|
| 1844 |
+
cancelAnimationFrame(this.animationFrameId);
|
| 1845 |
+
}
|
| 1846 |
+
return;
|
| 1847 |
+
}
|
| 1848 |
|
| 1849 |
+
this.animationFrameId = requestAnimationFrame(() => this.animate());
|
| 1850 |
+
// ๊ฒ์์ด ์์๋์ง ์์์ผ๋ฉด ๋ ๋๋ง๋ง ์ํ
|
| 1851 |
+
if (!this.isStarted) {
|
| 1852 |
+
this.renderer.render(this.scene, this.camera);
|
| 1853 |
+
return;
|
| 1854 |
+
}
|
| 1855 |
|
| 1856 |
+
const currentTime = performance.now();
|
| 1857 |
+
const deltaTime = (currentTime - this.lastTime) / 1000;
|
| 1858 |
+
this.lastTime = currentTime;
|
| 1859 |
|
| 1860 |
+
if (!this.isLoading) {
|
| 1861 |
+
this.handleMovement();
|
| 1862 |
+
this.tank.update(this.mouse.x, this.mouse.y, this.scene);
|
| 1863 |
+
|
| 1864 |
+
const tankPosition = this.tank.getPosition();
|
| 1865 |
+
this.enemies.forEach(enemy => {
|
| 1866 |
+
enemy.update(tankPosition);
|
| 1867 |
+
|
| 1868 |
+
if (enemy.isLoaded && enemy.mesh.position.distanceTo(tankPosition) < ENEMY_CONFIG.ATTACK_RANGE) {
|
| 1869 |
+
enemy.shoot(tankPosition);
|
| 1870 |
+
}
|
| 1871 |
+
});
|
|
|
|
|
|
|
|
|
|
| 1872 |
|
| 1873 |
+
this.updateParticles();
|
| 1874 |
+
this.checkCollisions();
|
| 1875 |
+
this.updateUI();
|
| 1876 |
+
this.updateRadar(); // ๋ ์ด๋ ์
๋ฐ์ดํธ ์ถ๊ฐ
|
| 1877 |
+
}
|
| 1878 |
+
|
| 1879 |
+
this.renderer.render(this.scene, this.camera);
|
| 1880 |
+
}
|
| 1881 |
}
|
| 1882 |
|
| 1883 |
// Start game
|