Spaces:
Running
Running
cutechicken
commited on
Commit
β’
b16b51c
1
Parent(s):
632d61f
Update game.js
Browse files
game.js
CHANGED
@@ -570,7 +570,6 @@ startReload() {
|
|
570 |
// Enemy ν΄λμ€
|
571 |
class Enemy {
|
572 |
constructor(scene, position, type = 'tank') {
|
573 |
-
// κΈ°λ³Έ μμ±
|
574 |
this.scene = scene;
|
575 |
this.position = position;
|
576 |
this.mesh = null;
|
@@ -591,25 +590,22 @@ class Enemy {
|
|
591 |
canSeePlayer: false,
|
592 |
lastKnownPlayerPosition: null,
|
593 |
searchStartTime: null,
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
aimingTime: 0,
|
598 |
-
requiredAimTime: 1000 // μ‘°μ€μ νμν μκ°
|
599 |
};
|
600 |
|
601 |
-
// κ²½λ‘ νμ
|
602 |
this.pathfinding = {
|
603 |
currentPath: [],
|
604 |
pathUpdateInterval: 1000,
|
605 |
lastPathUpdate: 0,
|
606 |
isAvoidingObstacle: false,
|
607 |
avoidanceDirection: null,
|
608 |
-
obstacleCheckDistance:
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
sensorDistance: 15 // κ°μ§ 거리
|
613 |
};
|
614 |
|
615 |
// μ ν¬ μμ€ν
|
@@ -617,473 +613,503 @@ class Enemy {
|
|
617 |
minEngagementRange: 30,
|
618 |
maxEngagementRange: 150,
|
619 |
optimalRange: 80,
|
620 |
-
aimThreshold: 0.1,
|
621 |
lastShotAccuracy: 0,
|
622 |
-
|
623 |
-
|
|
|
624 |
};
|
625 |
}
|
626 |
|
627 |
-
|
628 |
-
|
629 |
-
const obstacles = [];
|
630 |
-
const position = this.mesh.position.clone();
|
631 |
-
position.y += 1; // μΌμ λμ΄ μ‘°μ
|
632 |
|
633 |
-
this.
|
634 |
-
|
635 |
-
|
636 |
-
|
|
|
|
|
637 |
|
638 |
-
|
639 |
-
|
640 |
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
});
|
647 |
-
}
|
648 |
-
});
|
649 |
|
650 |
-
return
|
651 |
}
|
652 |
|
653 |
-
|
654 |
-
|
655 |
-
if (obstacles.length === 0) return null;
|
656 |
|
657 |
-
|
658 |
-
const
|
659 |
-
obstacles.forEach(obstacle => {
|
660 |
-
const avoidDir = new THREE.Vector3()
|
661 |
-
.subVectors(this.mesh.position, obstacle.point)
|
662 |
-
.normalize()
|
663 |
-
.multiplyScalar(1 / obstacle.distance); // 거리μ λ°λΉλ‘νλ κ°μ€μΉ
|
664 |
-
avoidanceVector.add(avoidDir);
|
665 |
-
});
|
666 |
-
|
667 |
-
return avoidanceVector.normalize();
|
668 |
-
}
|
669 |
-
|
670 |
-
// μ‘°μ€ μμ€ν
|
671 |
-
updateAiming(playerPosition) {
|
672 |
-
const targetDirection = new THREE.Vector3()
|
673 |
-
.subVectors(playerPosition, this.mesh.position)
|
674 |
-
.normalize();
|
675 |
|
676 |
-
//
|
677 |
-
|
|
|
|
|
678 |
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
// λ©μ νμ μ μ©
|
685 |
-
this.mesh.rotation.y = this.aiState.currentRotation;
|
686 |
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
|
|
|
|
|
691 |
|
692 |
-
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
moveDirection.subVectors(playerPosition, this.mesh.position).normalize();
|
707 |
-
if (distanceToPlayer > targetDistance) {
|
708 |
-
moveDirection.multiplyScalar(1);
|
709 |
-
} else {
|
710 |
-
moveDirection.multiplyScalar(-1);
|
711 |
}
|
712 |
}
|
713 |
|
714 |
-
|
715 |
-
|
|
|
|
|
|
|
716 |
|
717 |
-
|
718 |
-
canShoot(playerPosition) {
|
719 |
-
const distance = this.mesh.position.distanceTo(playerPosition);
|
720 |
-
const hasLineOfSight = this.checkLineOfSight(playerPosition);
|
721 |
-
const isAimed = this.updateAiming(playerPosition);
|
722 |
-
|
723 |
-
return distance <= this.combat.maxEngagementRange &&
|
724 |
-
distance >= this.combat.minEngagementRange &&
|
725 |
-
hasLineOfSight &&
|
726 |
-
isAimed;
|
727 |
}
|
728 |
|
729 |
-
// λ©μΈ μ
λ°μ΄νΈ ν¨μ
|
730 |
update(playerPosition) {
|
731 |
if (!this.mesh || !this.isLoaded) return;
|
732 |
|
733 |
-
|
734 |
-
this.
|
735 |
|
736 |
-
//
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
this.
|
742 |
-
|
743 |
-
|
|
|
|
|
744 |
}
|
745 |
|
746 |
-
//
|
747 |
-
if (this.
|
748 |
-
this.
|
749 |
-
|
750 |
-
|
|
|
|
|
751 |
} else {
|
752 |
-
|
753 |
-
|
|
|
|
|
|
|
|
|
|
|
754 |
}
|
755 |
-
|
756 |
-
|
|
|
|
|
|
|
|
|
757 |
switch (this.aiState.mode) {
|
758 |
case 'pursue':
|
759 |
-
this.
|
760 |
-
this.moveAlongPath();
|
761 |
-
break;
|
762 |
-
case 'flank':
|
763 |
-
const flankPosition = this.calculateFlankPosition(playerPosition);
|
764 |
-
this.findPathToTarget(flankPosition);
|
765 |
-
this.moveAlongPath();
|
766 |
break;
|
767 |
case 'retreat':
|
768 |
-
|
769 |
-
this.findPathToTarget(retreatPosition);
|
770 |
-
this.moveAlongPath();
|
771 |
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
772 |
}
|
773 |
}
|
774 |
|
775 |
-
//
|
776 |
-
|
777 |
-
if (combatMove.length() > 0) {
|
778 |
-
this.mesh.position.add(combatMove.multiplyScalar(this.moveSpeed));
|
779 |
-
}
|
780 |
|
781 |
-
// λ°μ¬
|
782 |
if (this.canShoot(playerPosition)) {
|
783 |
this.shoot(playerPosition);
|
784 |
}
|
785 |
|
786 |
// μ΄μ μ
λ°μ΄νΈ
|
787 |
this.updateBullets();
|
788 |
-
|
789 |
-
// ν±ν¬ κΈ°μΈκΈ° μ‘°μ
|
790 |
-
this.adjustTankTilt();
|
791 |
-
}
|
792 |
-
async initialize(loader) {
|
793 |
-
try {
|
794 |
-
const modelPath = this.type === 'tank' ? '/models/t90.glb' : '/models/t90.glb';
|
795 |
-
const result = await loader.loadAsync(modelPath);
|
796 |
-
this.mesh = result.scene;
|
797 |
-
this.mesh.position.copy(this.position);
|
798 |
-
this.mesh.scale.set(ENEMY_SCALE, ENEMY_SCALE, ENEMY_SCALE);
|
799 |
-
|
800 |
-
this.mesh.traverse((child) => {
|
801 |
-
if (child.isMesh) {
|
802 |
-
child.castShadow = true;
|
803 |
-
child.receiveShadow = true;
|
804 |
-
}
|
805 |
-
});
|
806 |
-
|
807 |
-
this.scene.add(this.mesh);
|
808 |
-
this.isLoaded = true;
|
809 |
-
} catch (error) {
|
810 |
-
console.error('Error loading enemy model:', error);
|
811 |
-
this.isLoaded = false;
|
812 |
-
}
|
813 |
}
|
814 |
|
815 |
-
|
816 |
-
if (!this.mesh) return
|
817 |
-
|
818 |
-
const startPos = this.mesh.position.clone();
|
819 |
-
startPos.y += 2;
|
820 |
-
const direction = new THREE.Vector3().subVectors(playerPosition, startPos).normalize();
|
821 |
-
const distance = startPos.distanceTo(playerPosition);
|
822 |
-
|
823 |
-
const raycaster = new THREE.Raycaster(startPos, direction, 0, distance);
|
824 |
-
const intersects = raycaster.intersectObjects(window.gameInstance.obstacles, true);
|
825 |
-
|
826 |
-
return intersects.length === 0;
|
827 |
-
}
|
828 |
-
|
829 |
-
updateAIState(playerPosition) {
|
830 |
-
const currentTime = Date.now();
|
831 |
-
const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
|
832 |
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
|
837 |
-
|
838 |
-
|
839 |
-
|
|
|
|
|
|
|
|
|
840 |
}
|
841 |
}
|
842 |
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
this.aiState.mode = 'pursue';
|
850 |
-
}
|
851 |
-
this.aiState.lastStateChange = currentTime;
|
852 |
-
}
|
853 |
}
|
854 |
|
855 |
-
|
856 |
-
|
857 |
-
if (currentTime - this.pathfinding.lastPathUpdate < this.pathfinding.pathUpdateInterval) {
|
858 |
-
return;
|
859 |
-
}
|
860 |
|
861 |
-
|
862 |
-
|
|
|
|
|
863 |
}
|
864 |
|
865 |
-
|
866 |
-
|
867 |
-
const direction = new THREE.Vector3().subVectors(end, start).normalize();
|
868 |
-
const distance = start.distanceTo(end);
|
869 |
-
const steps = Math.ceil(distance / 10);
|
870 |
|
871 |
-
|
872 |
-
|
873 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
874 |
}
|
875 |
-
|
876 |
-
return points;
|
877 |
}
|
878 |
|
879 |
-
|
880 |
-
if (this.
|
881 |
|
882 |
-
const targetPoint = this.pathfinding.currentPath[0];
|
883 |
const direction = new THREE.Vector3()
|
884 |
-
.subVectors(
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
this.
|
|
|
|
|
|
|
|
|
889 |
|
890 |
-
|
891 |
-
|
|
|
892 |
}
|
893 |
}
|
894 |
|
895 |
-
|
896 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
}
|
904 |
|
905 |
-
|
906 |
-
|
907 |
-
.subVectors(this.mesh.position, playerPosition)
|
908 |
-
.normalize();
|
909 |
-
return this.mesh.position.clone().add(direction.multiplyScalar(50));
|
910 |
-
}
|
911 |
|
912 |
-
|
913 |
-
const
|
914 |
-
const
|
915 |
-
|
916 |
-
|
917 |
-
this.
|
918 |
-
|
919 |
-
|
920 |
-
|
921 |
-
this.
|
922 |
-
this.mesh.position.z - forwardVector.z
|
923 |
-
);
|
924 |
-
const rightHeight = window.gameInstance.getHeightAtPosition(
|
925 |
-
this.mesh.position.x + rightVector.x,
|
926 |
-
this.mesh.position.z + rightVector.z
|
927 |
-
);
|
928 |
-
const leftHeight = window.gameInstance.getHeightAtPosition(
|
929 |
-
this.mesh.position.x - rightVector.x,
|
930 |
-
this.mesh.position.z - rightVector.z
|
931 |
);
|
932 |
-
|
933 |
-
const pitch = Math.atan2(frontHeight - backHeight, 2);
|
934 |
-
const roll = Math.atan2(rightHeight - leftHeight, 2);
|
935 |
-
|
936 |
-
const currentRotation = this.mesh.rotation.y;
|
937 |
-
this.mesh.rotation.set(pitch, currentRotation, roll);
|
938 |
}
|
939 |
|
940 |
-
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
if (Math.abs(bullet.position.x) > MAP_SIZE / 2 ||
|
946 |
-
Math.abs(bullet.position.z) > MAP_SIZE / 2) {
|
947 |
-
this.scene.remove(bullet);
|
948 |
-
this.bullets.splice(i, 1);
|
949 |
-
continue;
|
950 |
-
}
|
951 |
-
|
952 |
-
const bulletBox = new THREE.Box3().setFromObject(bullet);
|
953 |
-
for (const obstacle of window.gameInstance.obstacles) {
|
954 |
-
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
|
955 |
-
if (bulletBox.intersectsBox(obstacleBox)) {
|
956 |
-
this.scene.remove(bullet);
|
957 |
-
this.bullets.splice(i, 1);
|
958 |
-
break;
|
959 |
-
}
|
960 |
-
}
|
961 |
-
}
|
962 |
-
}
|
963 |
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
const flameGeometry = new THREE.SphereGeometry(1.0, 8, 8);
|
970 |
-
const flameMaterial = new THREE.MeshBasicMaterial({
|
971 |
-
color: 0xffa500,
|
972 |
-
transparent: true,
|
973 |
-
opacity: 0.8
|
974 |
-
});
|
975 |
-
const flame = new THREE.Mesh(flameGeometry, flameMaterial);
|
976 |
-
flame.scale.set(2, 2, 3);
|
977 |
-
flashGroup.add(flame);
|
978 |
|
979 |
-
|
980 |
-
|
981 |
-
|
982 |
-
|
983 |
-
|
|
|
|
|
|
|
|
|
|
|
984 |
});
|
985 |
-
|
986 |
-
for (let i = 0; i < 5; i++) {
|
987 |
-
const smoke = new THREE.Mesh(smokeGeometry, smokeMaterial);
|
988 |
-
smoke.position.set(
|
989 |
-
Math.random() * 1 - 0.5,
|
990 |
-
Math.random() * 1 - 0.5,
|
991 |
-
-1 - Math.random()
|
992 |
-
);
|
993 |
-
smoke.scale.set(1.5, 1.5, 1.5);
|
994 |
-
flashGroup.add(smoke);
|
995 |
-
}
|
996 |
|
997 |
-
|
998 |
-
|
999 |
-
const meshWorldQuaternion = new THREE.Quaternion();
|
1000 |
-
|
1001 |
-
this.mesh.getWorldPosition(muzzlePosition);
|
1002 |
-
this.mesh.getWorldQuaternion(meshWorldQuaternion);
|
1003 |
-
|
1004 |
-
muzzleOffset.applyQuaternion(meshWorldQuaternion);
|
1005 |
-
muzzlePosition.add(muzzleOffset);
|
1006 |
|
1007 |
-
|
1008 |
-
|
1009 |
|
1010 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1011 |
|
1012 |
-
|
1013 |
-
this.scene.remove(flashGroup);
|
1014 |
-
}, 500);
|
1015 |
}
|
1016 |
|
1017 |
shoot(playerPosition) {
|
1018 |
-
|
1019 |
-
|
1020 |
-
|
1021 |
-
|
|
|
|
|
|
|
|
|
1022 |
|
1023 |
-
|
|
|
|
|
1024 |
|
1025 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1026 |
|
1027 |
-
|
1028 |
-
|
1029 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1030 |
|
1031 |
-
|
1032 |
-
|
|
|
|
|
|
|
|
|
1033 |
color: 0xff0000,
|
1034 |
-
|
1035 |
-
|
1036 |
});
|
1037 |
-
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
|
1038 |
-
|
1039 |
-
const muzzleOffset = new THREE.Vector3(0, 0.5, 4);
|
1040 |
-
const muzzlePosition = new THREE.Vector3();
|
1041 |
-
this.mesh.getWorldPosition(muzzlePosition);
|
1042 |
-
muzzleOffset.applyQuaternion(this.mesh.quaternion);
|
1043 |
-
muzzlePosition.add(muzzleOffset);
|
1044 |
-
|
1045 |
-
bullet.position.copy(muzzlePosition);
|
1046 |
-
bullet.quaternion.copy(this.mesh.quaternion);
|
1047 |
-
|
1048 |
-
const direction = new THREE.Vector3()
|
1049 |
-
.subVectors(playerPosition, muzzlePosition)
|
1050 |
-
.normalize();
|
1051 |
-
|
1052 |
-
const bulletSpeed = this.type === 'tank' ?
|
1053 |
-
ENEMY_CONFIG.BULLET_SPEED :
|
1054 |
-
ENEMY_CONFIG.BULLET_SPEED * 0.8;
|
1055 |
|
1056 |
-
|
|
|
|
|
|
|
|
|
|
|
1057 |
|
1058 |
-
|
1059 |
-
|
1060 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1061 |
transparent: true,
|
1062 |
-
opacity: 0.
|
1063 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1064 |
|
1065 |
-
|
1066 |
-
|
1067 |
-
bullet.
|
|
|
|
|
1068 |
|
1069 |
-
|
1070 |
-
|
1071 |
-
|
|
|
1072 |
}
|
|
|
1073 |
|
1074 |
-
|
1075 |
-
|
1076 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1077 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1078 |
|
1079 |
-
|
1080 |
-
|
1081 |
-
|
1082 |
-
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1087 |
}
|
1088 |
|
1089 |
// Particle ν΄λμ€
|
|
|
570 |
// Enemy ν΄λμ€
|
571 |
class Enemy {
|
572 |
constructor(scene, position, type = 'tank') {
|
|
|
573 |
this.scene = scene;
|
574 |
this.position = position;
|
575 |
this.mesh = null;
|
|
|
590 |
canSeePlayer: false,
|
591 |
lastKnownPlayerPosition: null,
|
592 |
searchStartTime: null,
|
593 |
+
pathfindingAttempts: 0,
|
594 |
+
maxPathfindingAttempts: 5,
|
595 |
+
alternativePath: null
|
|
|
|
|
596 |
};
|
597 |
|
598 |
+
// κ²½λ‘ νμ μμ€ν
|
599 |
this.pathfinding = {
|
600 |
currentPath: [],
|
601 |
pathUpdateInterval: 1000,
|
602 |
lastPathUpdate: 0,
|
603 |
isAvoidingObstacle: false,
|
604 |
avoidanceDirection: null,
|
605 |
+
obstacleCheckDistance: 15,
|
606 |
+
sensorAngles: [-45, -30, -15, 0, 15, 30, 45],
|
607 |
+
sensorDistance: 20,
|
608 |
+
alternativeRoutes: []
|
|
|
609 |
};
|
610 |
|
611 |
// μ ν¬ μμ€ν
|
|
|
613 |
minEngagementRange: 30,
|
614 |
maxEngagementRange: 150,
|
615 |
optimalRange: 80,
|
616 |
+
aimThreshold: 0.1,
|
617 |
lastShotAccuracy: 0,
|
618 |
+
turretRotation: 0,
|
619 |
+
targetTurretRotation: 0,
|
620 |
+
turretRotationSpeed: 0.05
|
621 |
};
|
622 |
}
|
623 |
|
624 |
+
checkLineOfSight(playerPosition) {
|
625 |
+
if (!this.mesh) return false;
|
|
|
|
|
|
|
626 |
|
627 |
+
const startPos = this.mesh.position.clone();
|
628 |
+
startPos.y += 2; // ν¬ν λμ΄μμ μμ
|
629 |
+
const direction = new THREE.Vector3()
|
630 |
+
.subVectors(playerPosition, startPos)
|
631 |
+
.normalize();
|
632 |
+
const distance = startPos.distanceTo(playerPosition);
|
633 |
|
634 |
+
const raycaster = new THREE.Raycaster(startPos, direction, 0, distance);
|
635 |
+
const intersects = raycaster.intersectObjects(window.gameInstance.obstacles, true);
|
636 |
|
637 |
+
// μ₯μ λ¬Όκ³Όμ 거리 νμΈ
|
638 |
+
if (intersects.length > 0) {
|
639 |
+
const obstacleDistance = intersects[0].distance;
|
640 |
+
return obstacleDistance > distance; // μ₯μ λ¬Όμ΄ νλ μ΄μ΄λ³΄λ€ λ©λ¦¬ μμΌλ©΄ true
|
641 |
+
}
|
|
|
|
|
|
|
642 |
|
643 |
+
return true; // μ₯μ λ¬Όμ΄ μμΌλ©΄ μμΌκ° ν보λ κ²
|
644 |
}
|
645 |
|
646 |
+
findAlternativePath(playerPosition) {
|
647 |
+
if (!this.mesh) return null;
|
|
|
648 |
|
649 |
+
const currentPos = this.mesh.position.clone();
|
650 |
+
const possibleRoutes = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
651 |
|
652 |
+
// μ¬λ¬ κ°λλ‘ κ²½λ‘ νμ
|
653 |
+
for (let angle = 0; angle < 360; angle += 45) {
|
654 |
+
const radians = angle * Math.PI / 180;
|
655 |
+
const checkDistance = 30; // νμ 거리
|
656 |
|
657 |
+
const testPoint = new THREE.Vector3(
|
658 |
+
currentPos.x + Math.cos(radians) * checkDistance,
|
659 |
+
currentPos.y,
|
660 |
+
currentPos.z + Math.sin(radians) * checkDistance
|
661 |
+
);
|
|
|
|
|
662 |
|
663 |
+
// ν΄λΉ μ§μ κΉμ§μ κ²½λ‘ μ₯μ λ¬Ό 체ν¬
|
664 |
+
const direction = new THREE.Vector3()
|
665 |
+
.subVectors(testPoint, currentPos)
|
666 |
+
.normalize();
|
667 |
+
const raycaster = new THREE.Raycaster(currentPos, direction, 0, checkDistance);
|
668 |
+
const intersects = raycaster.intersectObjects(window.gameInstance.obstacles, true);
|
669 |
|
670 |
+
if (intersects.length === 0) {
|
671 |
+
// ν΄λΉ μ§μ μμ νλ μ΄μ΄κΉμ§μ μμΌ μ²΄ν¬
|
672 |
+
const toPlayerDirection = new THREE.Vector3()
|
673 |
+
.subVectors(playerPosition, testPoint)
|
674 |
+
.normalize();
|
675 |
+
const toPlayerRaycaster = new THREE.Raycaster(testPoint, toPlayerDirection);
|
676 |
+
const playerIntersects = toPlayerRaycaster.intersectObjects(window.gameInstance.obstacles, true);
|
677 |
+
|
678 |
+
if (playerIntersects.length === 0) {
|
679 |
+
possibleRoutes.push({
|
680 |
+
point: testPoint,
|
681 |
+
distance: testPoint.distanceTo(playerPosition)
|
682 |
+
});
|
683 |
+
}
|
|
|
|
|
|
|
|
|
|
|
684 |
}
|
685 |
}
|
686 |
|
687 |
+
// κ°μ₯ ν¨μ¨μ μΈ κ²½λ‘ μ ν
|
688 |
+
if (possibleRoutes.length > 0) {
|
689 |
+
possibleRoutes.sort((a, b) => a.distance - b.distance);
|
690 |
+
return possibleRoutes[0].point;
|
691 |
+
}
|
692 |
|
693 |
+
return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
694 |
}
|
695 |
|
|
|
696 |
update(playerPosition) {
|
697 |
if (!this.mesh || !this.isLoaded) return;
|
698 |
|
699 |
+
const currentTime = Date.now();
|
700 |
+
const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
|
701 |
|
702 |
+
// μμΌ μ²΄ν¬
|
703 |
+
if (currentTime - this.aiState.lastVisibilityCheck > this.aiState.visibilityCheckInterval) {
|
704 |
+
this.aiState.canSeePlayer = this.checkLineOfSight(playerPosition);
|
705 |
+
this.aiState.lastVisibilityCheck = currentTime;
|
706 |
+
|
707 |
+
if (this.aiState.canSeePlayer) {
|
708 |
+
this.aiState.lastKnownPlayerPosition = playerPosition.clone();
|
709 |
+
this.aiState.pathfindingAttempts = 0;
|
710 |
+
this.aiState.alternativePath = null;
|
711 |
+
}
|
712 |
}
|
713 |
|
714 |
+
// AI μν μ
λ°μ΄νΈ
|
715 |
+
if (currentTime - this.aiState.lastStateChange > this.aiState.stateChangeCooldown) {
|
716 |
+
if (!this.aiState.canSeePlayer) {
|
717 |
+
if (!this.aiState.alternativePath) {
|
718 |
+
this.aiState.alternativePath = this.findAlternativePath(playerPosition);
|
719 |
+
this.aiState.pathfindingAttempts++;
|
720 |
+
}
|
721 |
} else {
|
722 |
+
if (distanceToPlayer < this.combat.minEngagementRange) {
|
723 |
+
this.aiState.mode = 'retreat';
|
724 |
+
} else if (distanceToPlayer > this.combat.maxEngagementRange) {
|
725 |
+
this.aiState.mode = 'pursue';
|
726 |
+
} else {
|
727 |
+
this.aiState.mode = 'engage';
|
728 |
+
}
|
729 |
}
|
730 |
+
this.aiState.lastStateChange = currentTime;
|
731 |
+
}
|
732 |
+
|
733 |
+
// μ΄λ λ° μ ν¬ λ‘μ§
|
734 |
+
if (this.aiState.canSeePlayer) {
|
735 |
+
// μ μμ μΈ μ ν¬ νλ
|
736 |
switch (this.aiState.mode) {
|
737 |
case 'pursue':
|
738 |
+
this.moveTowards(playerPosition);
|
|
|
|
|
|
|
|
|
|
|
|
|
739 |
break;
|
740 |
case 'retreat':
|
741 |
+
this.moveAway(playerPosition);
|
|
|
|
|
742 |
break;
|
743 |
+
case 'engage':
|
744 |
+
this.engageTarget(playerPosition);
|
745 |
+
break;
|
746 |
+
}
|
747 |
+
} else {
|
748 |
+
// μ°ν κ²½λ‘λ‘ μ΄λ
|
749 |
+
if (this.aiState.alternativePath) {
|
750 |
+
this.moveTowards(this.aiState.alternativePath);
|
751 |
+
if (this.mesh.position.distanceTo(this.aiState.alternativePath) < 5) {
|
752 |
+
this.aiState.alternativePath = null;
|
753 |
+
}
|
754 |
+
} else if (this.aiState.pathfindingAttempts < this.aiState.maxPathfindingAttempts) {
|
755 |
+
this.aiState.alternativePath = this.findAlternativePath(playerPosition);
|
756 |
+
this.aiState.pathfindingAttempts++;
|
757 |
}
|
758 |
}
|
759 |
|
760 |
+
// ν¬ν νμ μ
λ°μ΄νΈ
|
761 |
+
this.updateTurretRotation(playerPosition);
|
|
|
|
|
|
|
762 |
|
763 |
+
// λ°μ¬ κ°λ₯ μ¬λΆ νμΈ λ° λ°μ¬
|
764 |
if (this.canShoot(playerPosition)) {
|
765 |
this.shoot(playerPosition);
|
766 |
}
|
767 |
|
768 |
// μ΄μ μ
λ°μ΄νΈ
|
769 |
this.updateBullets();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
770 |
}
|
771 |
|
772 |
+
moveTowards(target) {
|
773 |
+
if (!this.mesh) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
774 |
|
775 |
+
const direction = new THREE.Vector3()
|
776 |
+
.subVectors(target, this.mesh.position)
|
777 |
+
.normalize();
|
778 |
|
779 |
+
// μ₯μ λ¬Ό κ°μ§
|
780 |
+
const obstacles = this.detectObstacles();
|
781 |
+
if (obstacles.length > 0) {
|
782 |
+
// ννΌ λ°©ν₯ κ³μ°
|
783 |
+
const avoidanceDirection = this.calculateAvoidanceDirection(obstacles);
|
784 |
+
if (avoidanceDirection) {
|
785 |
+
direction.add(avoidanceDirection).normalize();
|
786 |
}
|
787 |
}
|
788 |
|
789 |
+
// μ΄λ μ μ©
|
790 |
+
this.mesh.position.add(direction.multiplyScalar(this.moveSpeed));
|
791 |
+
|
792 |
+
// μ΄λ λ°©ν₯μΌλ‘ 차체 νμ
|
793 |
+
const targetRotation = Math.atan2(direction.x, direction.z);
|
794 |
+
this.mesh.rotation.y = this.smoothRotation(this.mesh.rotation.y, targetRotation, 0.1);
|
|
|
|
|
|
|
|
|
795 |
}
|
796 |
|
797 |
+
moveAway(target) {
|
798 |
+
if (!this.mesh) return;
|
|
|
|
|
|
|
799 |
|
800 |
+
const direction = new THREE.Vector3()
|
801 |
+
.subVectors(this.mesh.position, target)
|
802 |
+
.normalize();
|
803 |
+
this.mesh.position.add(direction.multiplyScalar(this.moveSpeed));
|
804 |
}
|
805 |
|
806 |
+
engageTarget(playerPosition) {
|
807 |
+
if (!this.mesh) return;
|
|
|
|
|
|
|
808 |
|
809 |
+
// μ΅μ μ ν¬ κ±°λ¦¬ μ μ§
|
810 |
+
const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
|
811 |
+
const optimalRange = this.combat.optimalRange;
|
812 |
+
|
813 |
+
if (Math.abs(distanceToPlayer - optimalRange) > 5) {
|
814 |
+
if (distanceToPlayer < optimalRange) {
|
815 |
+
this.moveAway(playerPosition);
|
816 |
+
} else {
|
817 |
+
this.moveTowards(playerPosition);
|
818 |
+
}
|
819 |
}
|
|
|
|
|
820 |
}
|
821 |
|
822 |
+
updateTurretRotation(playerPosition) {
|
823 |
+
if (!this.mesh) return;
|
824 |
|
|
|
825 |
const direction = new THREE.Vector3()
|
826 |
+
.subVectors(playerPosition, this.mesh.position);
|
827 |
+
this.combat.targetTurretRotation = Math.atan2(direction.x, direction.z);
|
828 |
+
|
829 |
+
// λΆλλ¬μ΄ ν¬ν νμ
|
830 |
+
this.combat.turretRotation = this.smoothRotation(
|
831 |
+
this.combat.turretRotation,
|
832 |
+
this.combat.targetTurretRotation,
|
833 |
+
this.combat.turretRotationSpeed
|
834 |
+
);
|
835 |
|
836 |
+
// ν¬ν λ©μκ° μλ€λ©΄ νμ μ μ©
|
837 |
+
if (this.mesh.getObjectByName('turret')) {
|
838 |
+
this.mesh.getObjectByName('turret').rotation.y = this.combat.turretRotation;
|
839 |
}
|
840 |
}
|
841 |
|
842 |
+
smoothRotation(current, target, speed) {
|
843 |
+
let difference = target - current;
|
844 |
+
|
845 |
+
// κ°λ μ°¨μ΄λ₯Ό -PIμμ PI μ¬μ΄λ‘ μ κ·ν
|
846 |
+
while (difference > Math.PI) difference -= Math.PI * 2;
|
847 |
+
while (difference < -Math.PI) difference += Math.PI * 2;
|
848 |
+
|
849 |
+
return current + difference * speed;
|
850 |
}
|
851 |
|
852 |
+
canShoot(playerPosition) {
|
853 |
+
if (!this.mesh) return false;
|
|
|
|
|
|
|
|
|
854 |
|
855 |
+
const currentTime = Date.now();
|
856 |
+
const timeSinceLastShot = currentTime - this.lastAttackTime;
|
857 |
+
const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
|
858 |
+
|
859 |
+
return (
|
860 |
+
this.aiState.canSeePlayer &&
|
861 |
+
timeSinceLastShot >= ENEMY_CONFIG.ATTACK_INTERVAL &&
|
862 |
+
distanceToPlayer <= this.combat.maxEngagementRange &&
|
863 |
+
distanceToPlayer >= this.combat.minEngagementRange &&
|
864 |
+
Math.abs(this.combat.turretRotation - this.combat.targetTurretRotation) < this.combat.aimThreshold
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
865 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
866 |
}
|
867 |
|
868 |
+
detectObstacles() {
|
869 |
+
const obstacles = [];
|
870 |
+
const position = this.mesh.position.clone();
|
871 |
+
position.y += 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
872 |
|
873 |
+
this.pathfinding.sensorAngles.forEach(angle => {
|
874 |
+
const direction = new THREE.Vector3(0, 0, 1)
|
875 |
+
.applyAxisAngle(new THREE.Vector3(0, 1, 0), angle * Math.PI / 180)
|
876 |
+
.applyQuaternion(this.mesh.quaternion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
877 |
|
878 |
+
const raycaster = new THREE.Raycaster(position, direction, 0, this.pathfinding.sensorDistance);
|
879 |
+
const intersects = raycaster.intersectObjects(window.gameInstance.obstacles, true);
|
880 |
+
|
881 |
+
if (intersects.length > 0) {
|
882 |
+
obstacles.push({
|
883 |
+
angle: angle,
|
884 |
+
distance: intersects[0].distance,
|
885 |
+
point: intersects[0].point
|
886 |
+
});
|
887 |
+
}
|
888 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
889 |
|
890 |
+
return obstacles;
|
891 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
892 |
|
893 |
+
calculateAvoidanceDirection(obstacles) {
|
894 |
+
if (obstacles.length === 0) return null;
|
895 |
|
896 |
+
const avoidanceVector = new THREE.Vector3();
|
897 |
+
obstacles.forEach(obstacle => {
|
898 |
+
const avoidDir = new THREE.Vector3()
|
899 |
+
.subVectors(this.mesh.position, obstacle.point)
|
900 |
+
.normalize()
|
901 |
+
.multiplyScalar(1 / obstacle.distance);
|
902 |
+
avoidanceVector.add(avoidDir);
|
903 |
+
});
|
904 |
|
905 |
+
return avoidanceVector.normalize();
|
|
|
|
|
906 |
}
|
907 |
|
908 |
shoot(playerPosition) {
|
909 |
+
const currentTime = Date.now();
|
910 |
+
const attackInterval = this.type === 'tank' ?
|
911 |
+
ENEMY_CONFIG.ATTACK_INTERVAL :
|
912 |
+
ENEMY_CONFIG.ATTACK_INTERVAL * 1.5;
|
913 |
+
|
914 |
+
if (currentTime - this.lastAttackTime < attackInterval) return;
|
915 |
+
|
916 |
+
this.createMuzzleFlash();
|
917 |
|
918 |
+
const enemyFireSound = new Audio('sounds/mbtfire5.ogg');
|
919 |
+
enemyFireSound.volume = 0.3;
|
920 |
+
enemyFireSound.play();
|
921 |
|
922 |
+
const bulletGeometry = new THREE.CylinderGeometry(0.2, 0.2, 2, 8);
|
923 |
+
const bulletMaterial = new THREE.MeshBasicMaterial({
|
924 |
+
color: 0xff0000,
|
925 |
+
emissive: 0xff0000,
|
926 |
+
emissiveIntensity: 0.5
|
927 |
+
});
|
928 |
+
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
|
929 |
|
930 |
+
const muzzleOffset = new THREE.Vector3(0, 0.5, 4);
|
931 |
+
const muzzlePosition = new THREE.Vector3();
|
932 |
+
this.mesh.getWorldPosition(muzzlePosition);
|
933 |
+
muzzleOffset.applyQuaternion(this.mesh.quaternion);
|
934 |
+
muzzlePosition.add(muzzleOffset);
|
935 |
+
|
936 |
+
bullet.position.copy(muzzlePosition);
|
937 |
+
bullet.quaternion.copy(this.mesh.quaternion);
|
938 |
+
|
939 |
+
const direction = new THREE.Vector3()
|
940 |
+
.subVectors(playerPosition, muzzlePosition)
|
941 |
+
.normalize();
|
942 |
+
|
943 |
+
const bulletSpeed = this.type === 'tank' ?
|
944 |
+
ENEMY_CONFIG.BULLET_SPEED :
|
945 |
+
ENEMY_CONFIG.BULLET_SPEED * 0.8;
|
946 |
+
|
947 |
+
bullet.velocity = direction.multiplyScalar(bulletSpeed);
|
948 |
+
|
949 |
+
const trailGeometry = new THREE.CylinderGeometry(0.1, 0.1, 1, 8);
|
950 |
+
const trailMaterial = new THREE.MeshBasicMaterial({
|
951 |
+
color: 0xff4444,
|
952 |
+
transparent: true,
|
953 |
+
opacity: 0.5
|
954 |
+
});
|
955 |
+
|
956 |
+
const trail = new THREE.Mesh(trailGeometry, trailMaterial);
|
957 |
+
trail.position.z = -1;
|
958 |
+
bullet.add(trail);
|
959 |
+
|
960 |
+
this.scene.add(bullet);
|
961 |
+
this.bullets.push(bullet);
|
962 |
+
this.lastAttackTime = currentTime;
|
963 |
+
}
|
964 |
|
965 |
+
takeDamage(damage) {
|
966 |
+
this.health -= damage;
|
967 |
+
|
968 |
+
// νΌκ²© ν¨κ³Ό μμ±
|
969 |
+
if (this.mesh) {
|
970 |
+
const flashMaterial = new THREE.MeshBasicMaterial({
|
971 |
color: 0xff0000,
|
972 |
+
transparent: true,
|
973 |
+
opacity: 0.5
|
974 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
975 |
|
976 |
+
this.mesh.traverse((child) => {
|
977 |
+
if (child.isMesh) {
|
978 |
+
child.originalMaterial = child.material;
|
979 |
+
child.material = flashMaterial;
|
980 |
+
}
|
981 |
+
});
|
982 |
|
983 |
+
// 0.1μ΄ ν μλ μ¬μ§λ‘ 볡ꡬ
|
984 |
+
setTimeout(() => {
|
985 |
+
this.mesh.traverse((child) => {
|
986 |
+
if (child.isMesh && child.originalMaterial) {
|
987 |
+
child.material = child.originalMaterial;
|
988 |
+
}
|
989 |
+
});
|
990 |
+
}, 100);
|
991 |
+
}
|
992 |
+
|
993 |
+
return this.health <= 0;
|
994 |
+
}
|
995 |
+
|
996 |
+
destroy() {
|
997 |
+
if (this.mesh) {
|
998 |
+
// νλ° ν¨κ³Ό μμ±
|
999 |
+
const explosionGeometry = new THREE.SphereGeometry(2, 32, 32);
|
1000 |
+
const explosionMaterial = new THREE.MeshBasicMaterial({
|
1001 |
+
color: 0xff4500,
|
1002 |
transparent: true,
|
1003 |
+
opacity: 0.8
|
1004 |
});
|
1005 |
+
const explosion = new THREE.Mesh(explosionGeometry, explosionMaterial);
|
1006 |
+
explosion.position.copy(this.mesh.position);
|
1007 |
+
this.scene.add(explosion);
|
1008 |
+
|
1009 |
+
// νλ° νν°ν΄ μμ±
|
1010 |
+
for (let i = 0; i < 20; i++) {
|
1011 |
+
const particleGeometry = new THREE.SphereGeometry(0.2, 8, 8);
|
1012 |
+
const particleMaterial = new THREE.MeshBasicMaterial({
|
1013 |
+
color: Math.random() < 0.5 ? 0xff4500 : 0xff8c00,
|
1014 |
+
transparent: true,
|
1015 |
+
opacity: 1
|
1016 |
+
});
|
1017 |
+
const particle = new THREE.Mesh(particleGeometry, particleMaterial);
|
1018 |
+
particle.position.copy(this.mesh.position);
|
1019 |
+
|
1020 |
+
// λλ€ λ°©ν₯μΌλ‘ νμ΄λκ°λ ν¨κ³Ό
|
1021 |
+
const angle = Math.random() * Math.PI * 2;
|
1022 |
+
const speed = Math.random() * 0.5 + 0.5;
|
1023 |
+
particle.velocity = new THREE.Vector3(
|
1024 |
+
Math.cos(angle) * speed,
|
1025 |
+
Math.random() * speed,
|
1026 |
+
Math.sin(angle) * speed
|
1027 |
+
);
|
1028 |
+
|
1029 |
+
this.scene.add(particle);
|
1030 |
+
window.gameInstance.particles.push({
|
1031 |
+
mesh: particle,
|
1032 |
+
velocity: particle.velocity,
|
1033 |
+
life: Math.random() * 60 + 60
|
1034 |
+
});
|
1035 |
+
}
|
1036 |
+
|
1037 |
+
// νλ°μ μ¬μ
|
1038 |
+
const explosionSound = new Audio('sounds/explosion.ogg');
|
1039 |
+
explosionSound.volume = 0.4;
|
1040 |
+
explosionSound.play();
|
1041 |
|
1042 |
+
// λ©μμ μ΄μ μ κ±°
|
1043 |
+
this.scene.remove(this.mesh);
|
1044 |
+
this.bullets.forEach(bullet => this.scene.remove(bullet));
|
1045 |
+
this.bullets = [];
|
1046 |
+
this.isLoaded = false;
|
1047 |
|
1048 |
+
// 0.5μ΄ ν νλ° ν¨κ³Ό μ κ±°
|
1049 |
+
setTimeout(() => {
|
1050 |
+
this.scene.remove(explosion);
|
1051 |
+
}, 500);
|
1052 |
}
|
1053 |
+
}
|
1054 |
|
1055 |
+
// μΆκ° μ νΈλ¦¬ν° λ©μλλ€
|
1056 |
+
createMuzzleFlash() {
|
1057 |
+
if (!this.mesh) return;
|
1058 |
+
|
1059 |
+
const flashGroup = new THREE.Group();
|
1060 |
+
|
1061 |
+
const flameGeometry = new THREE.SphereGeometry(0.5, 8, 8);
|
1062 |
+
const flameMaterial = new THREE.MeshBasicMaterial({
|
1063 |
+
color: 0xffa500,
|
1064 |
+
transparent: true,
|
1065 |
+
opacity: 0.8
|
1066 |
+
});
|
1067 |
+
const flame = new THREE.Mesh(flameGeometry, flameMaterial);
|
1068 |
+
flashGroup.add(flame);
|
1069 |
+
|
1070 |
+
const smokeGeometry = new THREE.SphereGeometry(0.3, 8, 8);
|
1071 |
+
const smokeMaterial = new THREE.MeshBasicMaterial({
|
1072 |
+
color: 0x555555,
|
1073 |
+
transparent: true,
|
1074 |
+
opacity: 0.5
|
1075 |
+
});
|
1076 |
+
|
1077 |
+
for (let i = 0; i < 3; i++) {
|
1078 |
+
const smoke = new THREE.Mesh(smokeGeometry, smokeMaterial);
|
1079 |
+
smoke.position.z = -0.5 - Math.random();
|
1080 |
+
flashGroup.add(smoke);
|
1081 |
}
|
1082 |
+
|
1083 |
+
const muzzleOffset = new THREE.Vector3(0, 0.5, 4);
|
1084 |
+
const muzzlePosition = new THREE.Vector3();
|
1085 |
+
this.mesh.getWorldPosition(muzzlePosition);
|
1086 |
+
muzzleOffset.applyQuaternion(this.mesh.quaternion);
|
1087 |
+
muzzlePosition.add(muzzleOffset);
|
1088 |
+
|
1089 |
+
flashGroup.position.copy(muzzlePosition);
|
1090 |
+
flashGroup.quaternion.copy(this.mesh.quaternion);
|
1091 |
+
|
1092 |
+
this.scene.add(flashGroup);
|
1093 |
+
|
1094 |
+
setTimeout(() => {
|
1095 |
+
this.scene.remove(flashGroup);
|
1096 |
+
}, 100);
|
1097 |
+
}
|
1098 |
|
1099 |
+
checkLineOfSight(playerPosition) {
|
1100 |
+
if (!this.mesh) return false;
|
1101 |
+
|
1102 |
+
const startPos = this.mesh.position.clone();
|
1103 |
+
startPos.y += 1;
|
1104 |
+
const direction = new THREE.Vector3()
|
1105 |
+
.subVectors(playerPosition, startPos)
|
1106 |
+
.normalize();
|
1107 |
+
|
1108 |
+
const raycaster = new THREE.Raycaster(startPos, direction);
|
1109 |
+
const intersects = raycaster.intersectObjects(window.gameInstance.obstacles, true);
|
1110 |
+
|
1111 |
+
return intersects.length === 0;
|
1112 |
+
}
|
1113 |
}
|
1114 |
|
1115 |
// Particle ν΄λμ€
|