Spaces:
Running
Running
File size: 63,357 Bytes
a30e88c c82a71e 0fd1d06 a30e88c c82a71e a30e88c 89d8144 a30e88c f027a73 0fd1d06 96897f0 f027a73 accbf92 9c39181 accbf92 22a2c1a 7bb432f f027a73 8495bf4 20b339e 8495bf4 20b339e 8495bf4 20b339e 8495bf4 20b339e 8495bf4 20b339e 8495bf4 20b339e f027a73 080df83 9676487 080df83 762fa4e 080df83 762fa4e 080df83 f027a73 9676487 762fa4e 9676487 080df83 03060e2 080df83 f027a73 080df83 7827e8b ad7f1aa b2a23a4 590c2f2 b2a23a4 590c2f2 b2a23a4 ad7f1aa e9310dc b2a23a4 ad7f1aa 9c39181 b2a23a4 5eeb328 ad7f1aa e9310dc 989a374 0e11cb1 989a374 4f3870f 0e11cb1 4f3870f 0e11cb1 4f3870f 0e11cb1 4f3870f 0e11cb1 4f3870f 0e11cb1 4f3870f 0e11cb1 5eeb328 4f3870f 7aaeb16 0e11cb1 4f3870f 5eeb328 0e11cb1 4f3870f 0e11cb1 4f3870f 0e11cb1 5eeb328 accbf92 e9310dc 1506433 4f3870f e9310dc 4f3870f e9310dc 4f3870f e9310dc 9c39181 03060e2 9c39181 03060e2 168be5c f027a73 49edc42 7fea2cd 49edc42 89d8144 49edc42 89d8144 49edc42 f027a73 49edc42 f027a73 49edc42 f027a73 49edc42 a30e88c 49edc42 f027a73 49edc42 f027a73 49edc42 9c39181 49edc42 9c39181 f027a73 7827e8b 89d8144 96897f0 f027a73 96897f0 89d8144 96897f0 f027a73 89d8144 96897f0 a30e88c 96897f0 53ab759 0fd1d06 96897f0 f027a73 96897f0 f027a73 a30e88c 96897f0 a30e88c 96897f0 b2a23a4 49edc42 b2a23a4 02a151f b2a23a4 49edc42 02a151f 49edc42 02a151f 49edc42 02a151f 49edc42 02a151f 49edc42 02a151f 49edc42 02a151f b2a23a4 02a151f 96897f0 b2a23a4 a30e88c 96897f0 f027a73 89d8144 0fd1d06 a30e88c f027a73 0fd1d06 f027a73 0fd1d06 96897f0 a30e88c 96897f0 f027a73 0fd1d06 96897f0 a30e88c 96897f0 7827e8b 96897f0 f027a73 a30e88c f027a73 89d8144 f027a73 89d8144 4b521cc 96897f0 0c1c5d6 96897f0 e4939a8 989a374 accbf92 0c1c5d6 a30e88c 9c39181 96897f0 accbf92 96897f0 accbf92 220212e accbf92 7827e8b 590c2f2 96897f0 accbf92 96897f0 2609750 a30e88c 2a3c664 d9404e3 022d403 e4939a8 0c1c5d6 e4939a8 2609750 e4939a8 1506433 e4939a8 2609750 e4939a8 080df83 2609750 e4939a8 2609750 e4939a8 c0a7f64 e4939a8 2609750 c0a7f64 e4939a8 2609750 c0a7f64 e4939a8 2609750 e4939a8 2609750 e4939a8 2609750 e4939a8 2609750 e4939a8 3bf28e4 2609750 9c39181 dbfb9bd 9c39181 15a66ff 9c39181 7aa0006 9c39181 e40e2dd 9c39181 4b521cc 9c39181 4b521cc 9c39181 4b521cc 9c39181 4b521cc 9c39181 7bb432f 9c345a3 4b521cc 9c345a3 ce037c6 9c39181 9c345a3 22a2c1a ce037c6 22a2c1a 9c345a3 4b521cc 080df83 c0b45d7 080df83 9c39181 7827e8b 9c39181 1506433 9c39181 4b521cc 9c39181 0c1c5d6 3bb416a 0c1c5d6 4b521cc 9c39181 4b521cc 9c39181 4b521cc 9c39181 e43a395 9c39181 e43a395 9c39181 4b521cc 9c39181 7827e8b 9c39181 4b521cc 9c39181 7827e8b 9c39181 89d8144 9c39181 4b521cc 9c39181 080df83 9c39181 080df83 9c39181 080df83 9c39181 080df83 9c39181 080df83 9c39181 080df83 9c39181 03060e2 7827e8b 9c39181 7827e8b 9c39181 b2a23a4 ffe7897 0c1c5d6 3bb416a 0c1c5d6 3bb416a ffe7897 a7148ee 02a151f a7148ee 02a151f ffe7897 192b9c4 02a151f a7148ee 9c345a3 b2a23a4 a7148ee 9c345a3 a7148ee 9c345a3 b2a23a4 9c345a3 a7148ee 9c345a3 b2a23a4 9c39181 02a151f 9c345a3 a7148ee 9c39181 9c345a3 a7148ee 9c345a3 b2a23a4 a7148ee 9c345a3 a7148ee b2a23a4 a7148ee b2a23a4 9c345a3 b2a23a4 9c345a3 b2a23a4 9c345a3 a30e88c 7827e8b accbf92 9c39181 accbf92 7827e8b 9c39181 7827e8b |
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 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 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 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 |
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 = 1000;
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 = 3;
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 = 1; // ๋ณ๊ฒฝ: 10 -> 1
this.maxAmmo = 1; // ์ถ๊ฐ: ์ต๋ ํฌํ ์
this.isReloading = false; // ์ถ๊ฐ: ์ฌ์ฅ์ ์ํ
this.reloadTime = 3000; // ์ถ๊ฐ: 3์ด ์ฌ์ฅ์ ์๊ฐ
this.lastShootTime = 0;
this.bullets = [];
this.obstacles = [];
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 });
}
// ๋ณ๋์ ๋ฉ์๋๋ก ๋ถ๋ฆฌ
createExplosionEffect(scene, position) {
// ํญ๋ฐ ์ค์ฌ ํ๋์
const flashGeometry = new THREE.SphereGeometry(3);
const flashMaterial = new THREE.MeshBasicMaterial({
color: 0xffff00,
transparent: true,
opacity: 1
});
const flash = new THREE.Mesh(flashGeometry, flashMaterial);
flash.position.copy(position);
scene.add(flash);
// ํญ๋ฐ ํํฐํด
for (let i = 0; i < 30; i++) {
const size = Math.random() * 0.5 + 0.3;
const geometry = new THREE.SphereGeometry(size);
// ๋ค์ํ ์์์ ํํฐํด
const colors = [0xff4500, 0xff8c00, 0xff0000, 0xffd700];
const material = new THREE.MeshBasicMaterial({
color: colors[Math.floor(Math.random() * colors.length)],
transparent: true,
opacity: 1
});
const particle = new THREE.Mesh(geometry, material);
particle.position.copy(position);
// ํํฐํด ์๋์ ๋ฐฉํฅ ์ค์
const speed = Math.random() * 0.5 + 0.3;
const angle = Math.random() * Math.PI * 2;
const elevation = Math.random() * Math.PI - Math.PI / 2;
particle.velocity = new THREE.Vector3(
Math.cos(angle) * Math.cos(elevation) * speed,
Math.sin(elevation) * speed,
Math.sin(angle) * Math.cos(elevation) * speed
);
particle.gravity = -0.015;
particle.life = Math.random() * 30 + 30;
particle.fadeRate = 0.97;
scene.add(particle);
window.gameInstance.particles.push({
mesh: particle,
velocity: particle.velocity,
gravity: particle.gravity,
life: particle.life,
fadeRate: particle.fadeRate
});
}
// ํญ๋ฐ ๋ง ์ดํํธ
const ringGeometry = new THREE.RingGeometry(0.1, 2, 32);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xff8c00,
transparent: true,
opacity: 1,
side: THREE.DoubleSide
});
const ring = new THREE.Mesh(ringGeometry, ringMaterial);
ring.position.copy(position);
ring.lookAt(new THREE.Vector3(0, 1, 0));
scene.add(ring);
// ๋ง ํ์ฅ ์ ๋๋ฉ์ด์
const expandRing = () => {
ring.scale.x += 0.2;
ring.scale.y += 0.2;
ring.material.opacity *= 0.95;
if (ring.material.opacity > 0.01) {
requestAnimationFrame(expandRing);
} else {
scene.remove(ring);
}
};
expandRing();
// ํญ๋ฐ์ ํจ๊ณผ
const explosionSound = new Audio('sounds/explosion.ogg');
explosionSound.volume = 0.4;
explosionSound.play();
// ์นด๋ฉ๋ผ ํ๋ค๋ฆผ ํจ๊ณผ
if (window.gameInstance && window.gameInstance.camera) {
const camera = window.gameInstance.camera;
const originalPosition = camera.position.clone();
let shakeTime = 0;
const shakeIntensity = 0.3;
const shakeDuration = 500;
const shakeCamera = () => {
if (shakeTime < shakeDuration) {
camera.position.x = originalPosition.x + (Math.random() - 0.5) * shakeIntensity;
camera.position.y = originalPosition.y + (Math.random() - 0.5) * shakeIntensity;
camera.position.z = originalPosition.z + (Math.random() - 0.5) * shakeIntensity;
shakeTime += 16;
requestAnimationFrame(shakeCamera);
} else {
camera.position.copy(originalPosition);
}
};
shakeCamera();
}
// ์ค์ฌ ํ๋์ ์ ๊ฑฐ
setTimeout(() => {
scene.remove(flash);
}, 100);
}
async initialize(scene, loader) {
try {
const bodyResult = await loader.loadAsync('/models/abramsBody.glb');
this.body = bodyResult.scene;
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;
child.material.shadowSide = THREE.BackSide;
child.material.needsUpdate = true;
}
});
this.turret.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
child.material.shadowSide = THREE.BackSide;
child.material.needsUpdate = true;
}
});
// ๊ทธ๋ฆผ์ ํ๋ฉด
const shadowPlaneGeometry = new THREE.PlaneGeometry(8, 8);
const shadowPlaneMaterial = new THREE.ShadowMaterial({
opacity: 0.3
});
this.shadowPlane = new THREE.Mesh(shadowPlaneGeometry, shadowPlaneMaterial);
this.shadowPlane.receiveShadow = true;
this.shadowPlane.rotation.x = -Math.PI / 2;
this.shadowPlane.position.y = 0.1;
this.body.add(this.shadowPlane);
// ๊ฐ๋จํ ์คํฐ ์์น ์ค์
const spawnPosition = new THREE.Vector3(
(Math.random() - 0.5) * (MAP_SIZE * 0.8), // MAP_SIZE์ 80%๋ง ์ฌ์ฉ
TANK_HEIGHT, // ๊ณ ์ ๋ ๋์ด ์ฌ์ฉ
(Math.random() - 0.5) * (MAP_SIZE * 0.8)
);
this.body.position.copy(spawnPosition);
// ํญ๋ฐ ์ดํํธ ๋ฉ์๋ ์ถ๊ฐ
this.createExplosionEffect = (scene, position) => {
// ํญ๋ฐ ํํฐํด
for (let i = 0; i < 15; i++) {
const size = Math.random() * 0.2 + 0.1;
const geometry = new THREE.SphereGeometry(size);
const material = new THREE.MeshBasicMaterial({
color: Math.random() < 0.5 ? 0xff4500 : 0xff8c00
});
const particle = new THREE.Mesh(geometry, material);
particle.position.copy(position);
const speed = Math.random() * 0.3 + 0.2;
const angle = Math.random() * Math.PI * 2;
const elevation = Math.random() * Math.PI - Math.PI / 2;
particle.velocity = new THREE.Vector3(
Math.cos(angle) * Math.cos(elevation) * speed,
Math.sin(elevation) * speed,
Math.sin(angle) * Math.cos(elevation) * speed
);
particle.gravity = -0.01;
particle.life = Math.random() * 20 + 20;
particle.fadeRate = 1 / particle.life;
scene.add(particle);
window.gameInstance.particles.push({
mesh: particle,
velocity: particle.velocity,
gravity: particle.gravity,
life: particle.life,
fadeRate: particle.fadeRate
});
}
// ์ถฉ๋ ์ฌ์ด๋ ์ฌ์
const explosionSound = new Audio('sounds/explosion.ogg');
explosionSound.volume = 0.3;
explosionSound.play();
};
scene.add(this.body);
this.isLoaded = true;
this.updateAmmoDisplay();
} catch (error) {
console.error('Error loading tank models:', error);
this.isLoaded = false;
}
}
shoot(scene) {
// ์ฌ์ฅ์ ์ค์ด๊ฑฐ๋ ํ์ฝ์ด ์์ผ๋ฉด ๋ฐ์ฌํ์ง ์์
if (this.isReloading || this.ammo <= 0) {
return null;
}
// ๋ฐ์ฌ ๋๋ ์ด ์ฒดํฌ (์ฐ์ ๋ฐ์ฌ ๋ฐฉ์ง)
const currentTime = Date.now();
if (currentTime - this.lastShootTime < 100) { // 100ms์ ๋ฐ์ฌ ๋๋ ์ด
return null;
}
this.lastShootTime = currentTime;
// ๋ฐ์ฌ์ ํจ๊ณผ (ํ ๋ฒ๋ง ์ฌ์)
const sounds = ['sounds/mbtfire1.ogg', 'sounds/mbtfire2.ogg', 'sounds/mbtfire3.ogg', 'sounds/mbtfire4.ogg'];
const randomSound = sounds[Math.floor(Math.random() * sounds.length)];
const audio = new Audio(randomSound);
audio.volume = 0.5;
// ์ด์ ์ค๋์ค ์ธ์คํด์ค๊ฐ ์๋ค๋ฉด ์ค์ง
if (this.lastAudio) {
this.lastAudio.pause();
this.lastAudio.currentTime = 0;
}
this.lastAudio = audio;
audio.play();
// ๋ฐ์ฌ ์ดํํธ
this.createMuzzleFlash(scene);
// ํฌํ ์์ฑ
const bullet = this.createBullet(scene);
if (bullet) {
this.ammo--;
this.updateAmmoDisplay();
// ํ์ฝ์ ๋ชจ๋ ์์งํ์ ๋๋ง ์ฌ์ฅ์ ์์
if (this.ammo <= 0) {
this.startReload();
}
}
return bullet;
}
startReload() {
if (this.isReloading) return; // ์ด๋ฏธ ์ฌ์ฅ์ ์ค์ด๋ฉด ๋ฌด์
this.isReloading = true;
const reloadingText = document.getElementById('reloadingText');
reloadingText.style.display = 'block';
setTimeout(() => {
this.ammo = this.maxAmmo;
this.isReloading = false;
reloadingText.style.display = 'none';
this.updateAmmoDisplay();
}, this.reloadTime);
}
createMuzzleFlash(scene) {
if (!this.turret) return;
const flashGroup = new THREE.Group();
// ํ์ผ ํฌ๊ธฐ ์ฆ๊ฐ
const flameGeometry = new THREE.SphereGeometry(1.0, 8, 8);
const flameMaterial = new THREE.MeshBasicMaterial({
color: 0xffa500,
transparent: true,
opacity: 0.8
});
const flame = new THREE.Mesh(flameGeometry, flameMaterial);
flame.scale.set(2, 2, 3);
flashGroup.add(flame);
// ์ฐ๊ธฐ ํจ๊ณผ ํฌ๊ธฐ ์ฆ๊ฐ
const smokeGeometry = new THREE.SphereGeometry(0.8, 8, 8);
const smokeMaterial = new THREE.MeshBasicMaterial({
color: 0x555555,
transparent: true,
opacity: 0.5
});
for (let i = 0; i < 5; i++) { // ์ฐ๊ธฐ ํํฐํด ์ ์ฆ๊ฐ
const smoke = new THREE.Mesh(smokeGeometry, smokeMaterial);
smoke.position.set(
Math.random() * 1 - 0.5,
Math.random() * 1 - 0.5,
-1 - Math.random()
);
smoke.scale.set(1.5, 1.5, 1.5);
flashGroup.add(smoke);
}
// ํฌ๊ตฌ ์์น ๊ณ์ฐ
const muzzleOffset = new THREE.Vector3(0, 0.5, 4);
const muzzlePosition = new THREE.Vector3();
const turretWorldQuaternion = new THREE.Quaternion();
this.turret.getWorldPosition(muzzlePosition);
this.turret.getWorldQuaternion(turretWorldQuaternion);
muzzleOffset.applyQuaternion(turretWorldQuaternion);
muzzlePosition.add(muzzleOffset);
flashGroup.position.copy(muzzlePosition);
flashGroup.quaternion.copy(turretWorldQuaternion);
scene.add(flashGroup);
// ์ดํํธ ์ง์ ์๊ฐ ์ฆ๊ฐ
setTimeout(() => {
scene.remove(flashGroup);
}, 500);
}
createBullet(scene) {
if (!this.turret) return null; // ํฐ๋ ์ด ์์ผ๋ฉด null ๋ฐํ
// ํฌํ ํฌ๊ธฐ ์ฆ๊ฐ
const bulletGeometry = new THREE.CylinderGeometry(0.2, 0.2, 2, 8);
const bulletMaterial = new THREE.MeshBasicMaterial({ color: 0xffd700 });
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
// ํฌํ์ ์๋ ์์น์ ๋ฐฉํฅ ๊ฐ์ ธ์ค๊ธฐ
const muzzleOffset = new THREE.Vector3(0, 0.5, 4); // ํฌ๊ตฌ ์์น ์กฐ์ (์์ชฝ์ผ๋ก ๋ ์ด๋)
const muzzlePosition = new THREE.Vector3();
const turretWorldQuaternion = new THREE.Quaternion();
// ํฌํ์ ์๋ ๋ณํ ํ๋ ฌ ๊ฐ์ ธ์ค๊ธฐ
this.turret.getWorldPosition(muzzlePosition);
this.turret.getWorldQuaternion(turretWorldQuaternion);
// ํฌ๊ตฌ ์คํ์
์ ์ฉ
muzzleOffset.applyQuaternion(turretWorldQuaternion);
muzzlePosition.add(muzzleOffset);
// ํฌํ ์์น์ ํ์ ์ค์
bullet.position.copy(muzzlePosition);
bullet.quaternion.copy(turretWorldQuaternion);
// ๋ฐ์ฌ ๋ฐฉํฅ ์ค์
const direction = new THREE.Vector3(0, 0, 1);
direction.applyQuaternion(turretWorldQuaternion);
bullet.velocity = direction.multiplyScalar(5);
scene.add(bullet);
this.bullets.push(bullet);
return bullet;
}
update(mouseX, mouseY, scene) {
if (!this.body || !this.turretGroup) return;
const absoluteTurretRotation = mouseX;
this.turretGroup.rotation.y = absoluteTurretRotation - this.body.rotation.y;
this.turretRotation = absoluteTurretRotation;
// ์ด์ ์
๋ฐ์ดํธ ๋ฐ ์ถฉ๋ ์ฒดํฌ
for (let i = this.bullets.length - 1; i >= 0; i--) {
const bullet = this.bullets[i];
const oldPosition = bullet.position.clone();
bullet.position.add(bullet.velocity);
// ์งํ ๋์ด ์ฒดํฌ
const terrainHeight = window.gameInstance.getHeightAtPosition(
bullet.position.x,
bullet.position.z
);
if (bullet.position.y < terrainHeight ||
Math.abs(bullet.position.x) > MAP_SIZE / 2 ||
Math.abs(bullet.position.z) > MAP_SIZE / 2) {
// ํญ๋ฐ ์ดํํธ ์์ฑ
this.createExplosionEffect(scene, bullet.position);
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;
// ์๋ก์ด ์์น ๊ณ์ฐ
const newPosition = this.body.position.clone().add(moveVector);
// ์๋ก์ด ์์น์ ์งํ ๋์ด ๊ฐ์ ธ์ค๊ธฐ
const heightAtNewPos = window.gameInstance.getHeightAtPosition(newPosition.x, newPosition.z);
// ํฑํฌ ๋์ด๋งํผ ๋ํด์ ์งํ ์์ ์์น์ํด
newPosition.y = heightAtNewPos + TANK_HEIGHT;
// ๊ฒฝ์ฌ๊ฐ ๋๋ฌด ๊ฐํ๋ฅธ์ง ์ฒดํฌ
const currentHeight = this.body.position.y;
const heightDifference = Math.abs(newPosition.y - currentHeight);
const maxClimbAngle = 0.5; // ์ต๋ ๋ฑ๋ฐ ๊ฐ๋
if (heightDifference / this.moveSpeed < maxClimbAngle) {
this.body.position.copy(newPosition);
// ํฑํฌ์ ๋ฐฉํฅ ๋ฒกํฐ ๊ณ์ฐ
const forwardVector = new THREE.Vector3(0, 0, 1).applyQuaternion(this.body.quaternion);
const rightVector = new THREE.Vector3(1, 0, 0).applyQuaternion(this.body.quaternion);
// ํฑํฌ ์ฃผ๋ณ์ ๋์ด ๊ณ์ฐ
const frontHeight = window.gameInstance.getHeightAtPosition(
newPosition.x + forwardVector.x,
newPosition.z + forwardVector.z
);
const backHeight = window.gameInstance.getHeightAtPosition(
newPosition.x - forwardVector.x,
newPosition.z - forwardVector.z
);
const rightHeight = window.gameInstance.getHeightAtPosition(
newPosition.x + rightVector.x,
newPosition.z + rightVector.z
);
const leftHeight = window.gameInstance.getHeightAtPosition(
newPosition.x - rightVector.x,
newPosition.z - rightVector.z
);
// ํฑํฌ์ ๊ธฐ์ธ๊ธฐ ๊ณ์ฐ ๋ฐ ์ ์ฉ
const pitch = Math.atan2(frontHeight - backHeight, 2);
const roll = Math.atan2(rightHeight - leftHeight, 2);
// ๊ธฐ์กด y์ถ ํ์ ์ ์ ์งํ๋ฉด์ ์๋ก์ด ๊ธฐ์ธ๊ธฐ ์ ์ฉ
const currentYRotation = this.body.rotation.y;
this.body.rotation.set(pitch, currentYRotation, roll);
}
}
rotate(angle) {
if (!this.body) return;
// y์ถ ํ์ ์ ์ฉ
this.body.rotation.y += angle * this.turnSpeed;
// ํ์ฌ ์์น์์์ ์งํ์ ๋ง์ถฐ ํฑํฌ ๊ธฐ์ธ๊ธฐ ์กฐ์
const position = this.body.position;
const forwardVector = new THREE.Vector3(0, 0, 1).applyQuaternion(this.body.quaternion);
const rightVector = new THREE.Vector3(1, 0, 0).applyQuaternion(this.body.quaternion);
// ํฑํฌ ์ฃผ๋ณ์ ๋์ด ๊ณ์ฐ
const frontHeight = window.gameInstance.getHeightAtPosition(
position.x + forwardVector.x,
position.z + forwardVector.z
);
const backHeight = window.gameInstance.getHeightAtPosition(
position.x - forwardVector.x,
position.z - forwardVector.z
);
const rightHeight = window.gameInstance.getHeightAtPosition(
position.x + rightVector.x,
position.z + rightVector.z
);
const leftHeight = window.gameInstance.getHeightAtPosition(
position.x - rightVector.x,
position.z - rightVector.z
);
// ํฑํฌ์ ๊ธฐ์ธ๊ธฐ ๊ณ์ฐ ๋ฐ ์ ์ฉ
const pitch = Math.atan2(frontHeight - backHeight, 2);
const roll = Math.atan2(rightHeight - leftHeight, 2);
// ํ์ฌ y์ถ ํ์ ์ ์ ์งํ๋ฉด์ ์๋ก์ด ๊ธฐ์ธ๊ธฐ ์ ์ฉ
const currentYRotation = this.body.rotation.y;
this.body.rotation.set(pitch, currentYRotation, roll);
}
getPosition() {
return this.body ? this.body.position : new THREE.Vector3();
}
takeDamage(damage) {
this.health -= damage;
return this.health <= 0;
}
startReload() {
this.isReloading = true;
const reloadingText = document.getElementById('reloadingText');
reloadingText.style.display = 'block';
setTimeout(() => {
this.ammo = this.maxAmmo;
this.isReloading = false;
reloadingText.style.display = 'none';
this.updateAmmoDisplay();
}, this.reloadTime);
}
updateAmmoDisplay() {
document.getElementById('ammoDisplay').textContent = `APFSDS: ${this.ammo}/${this.maxAmmo}`;
}
}
// Enemy ํด๋์ค
class Enemy {
constructor(scene, position, type = 'tank') {
this.scene = scene;
this.position = position;
this.mesh = null;
this.type = type;
this.health = type === 'tank' ? 100 : 200;
this.lastAttackTime = 0;
this.bullets = [];
this.isLoaded = false;
this.moveSpeed = type === 'tank' ? ENEMY_MOVE_SPEED : ENEMY_MOVE_SPEED * 0.7;
}
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();
const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
const minDistance = 50;
// ์ด์ ์์น ์ ์ฅ
const previousPosition = this.mesh.position.clone();
if (distanceToPlayer > minDistance) {
const moveVector = direction.multiplyScalar(this.moveSpeed);
const newPosition = this.mesh.position.clone().add(moveVector);
// ์งํ ๋์ด ๊ฐ์ ธ์ค๊ธฐ
const heightAtNewPos = window.gameInstance.getHeightAtPosition(
newPosition.x,
newPosition.z
);
newPosition.y = heightAtNewPos + TANK_HEIGHT;
// ์์๋ก ์์น ์ด๋ํ์ฌ ์ถฉ๋ ์ฒดํฌ
const originalPosition = this.mesh.position.clone();
this.mesh.position.copy(newPosition);
// ์ฅ์ ๋ฌผ๊ณผ ์ถฉ๋ ์ฒดํฌ
const enemyBox = new THREE.Box3().setFromObject(this.mesh);
let hasCollision = false;
// ๋ชจ๋ ์ฅ์ ๋ฌผ์ ๋ํด ์ถฉ๋ ๊ฒ์ฌ
for (const obstacle of window.gameInstance.obstacles) {
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
if (enemyBox.intersectsBox(obstacleBox)) {
hasCollision = true;
break;
}
}
// ๋ค๋ฅธ ์ ํฑํฌ์์ ์ถฉ๋ ๊ฒ์ฌ
if (!hasCollision) {
for (const otherEnemy of window.gameInstance.enemies) {
if (otherEnemy !== this && otherEnemy.mesh) {
const otherEnemyBox = new THREE.Box3().setFromObject(otherEnemy.mesh);
if (enemyBox.intersectsBox(otherEnemyBox)) {
hasCollision = true;
break;
}
}
}
}
// ๋งต ๊ฒฝ๊ณ ์ฒดํฌ
const mapBoundary = MAP_SIZE / 2;
if (Math.abs(newPosition.x) > mapBoundary ||
Math.abs(newPosition.z) > mapBoundary) {
hasCollision = true;
}
// ์ถฉ๋์ด ์์ผ๋ฉด ์ด์ ์์น๋ก ๋ณต๊ท, ์์ผ๋ฉด ์ ์์น ์ ์ง
if (hasCollision) {
this.mesh.position.copy(previousPosition);
// ์ถฉ๋ ์ ์ฐํ ๊ฒฝ๋ก ์๋
const alternateDirections = [
new THREE.Vector3(-direction.z, 0, direction.x), // ์ผ์ชฝ์ผ๋ก 90๋
new THREE.Vector3(direction.z, 0, -direction.x), // ์ค๋ฅธ์ชฝ์ผ๋ก 90๋
new THREE.Vector3(-direction.x, 0, -direction.z) // 180๋ ํ์
];
for (const altDirection of alternateDirections) {
const altMoveVector = altDirection.multiplyScalar(this.moveSpeed);
const altNewPosition = previousPosition.clone().add(altMoveVector);
this.mesh.position.copy(altNewPosition);
const altEnemyBox = new THREE.Box3().setFromObject(this.mesh);
let altHasCollision = false;
// ์๋ก์ด ๋ฐฉํฅ์ ๋ํ ์ถฉ๋ ๊ฒ์ฌ
for (const obstacle of window.gameInstance.obstacles) {
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
if (altEnemyBox.intersectsBox(obstacleBox)) {
altHasCollision = true;
break;
}
}
if (!altHasCollision) {
// ์ฐํ ๊ฒฝ๋ก๊ฐ ๊ฐ๋ฅํ๋ฉด ๊ทธ ๋ฐฉํฅ์ผ๋ก ์ด๋
break;
} else {
// ์ฐํ๋ ๋ถ๊ฐ๋ฅํ๋ฉด ์ด์ ์์น๋ก ๋ณต๊ท
this.mesh.position.copy(previousPosition);
}
}
}
// ์งํ์ ๋ฐ๋ฅธ ๊ธฐ์ธ๊ธฐ ์กฐ์
const forwardVector = new THREE.Vector3(0, 0, 1).applyQuaternion(this.mesh.quaternion);
const rightVector = new THREE.Vector3(1, 0, 0).applyQuaternion(this.mesh.quaternion);
const frontHeight = window.gameInstance.getHeightAtPosition(
this.mesh.position.x + forwardVector.x,
this.mesh.position.z + forwardVector.z
);
const backHeight = window.gameInstance.getHeightAtPosition(
this.mesh.position.x - forwardVector.x,
this.mesh.position.z - forwardVector.z
);
const rightHeight = window.gameInstance.getHeightAtPosition(
this.mesh.position.x + rightVector.x,
this.mesh.position.z + rightVector.z
);
const leftHeight = window.gameInstance.getHeightAtPosition(
this.mesh.position.x - rightVector.x,
this.mesh.position.z - rightVector.z
);
const pitch = Math.atan2(frontHeight - backHeight, 2);
const roll = Math.atan2(rightHeight - leftHeight, 2);
// ํ์ฌ ํ์ ์ ์งํ๋ฉด์ ๊ธฐ์ธ๊ธฐ๋ง ์ ์ฉ
const currentRotation = this.mesh.rotation.y;
this.mesh.rotation.set(pitch, currentRotation, roll);
}
// ํ๋ ์ด์ด๋ฅผ ํฅํด ํฌํ ํ์
this.mesh.lookAt(playerPosition);
// ์ด์ ์
๋ฐ์ดํธ
if (this.bullets) {
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) {
this.scene.remove(bullet);
this.bullets.splice(i, 1);
continue;
}
// ์ด์๊ณผ ์ฅ์ ๋ฌผ ์ถฉ๋ ์ฒดํฌ
const bulletBox = new THREE.Box3().setFromObject(bullet);
for (const obstacle of window.gameInstance.obstacles) {
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
if (bulletBox.intersectsBox(obstacleBox)) {
this.scene.remove(bullet);
this.bullets.splice(i, 1);
break;
}
}
}
}
}
shoot(playerPosition) {
const currentTime = Date.now();
const attackInterval = this.type === 'tank' ?
ENEMY_CONFIG.ATTACK_INTERVAL :
ENEMY_CONFIG.ATTACK_INTERVAL * 1.5;
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() {
// ์ค๋์ค ๊ด๋ จ ์์ฑ ์ถ๊ฐ
this.engineSound = null;
this.engineStopSound = null;
this.isEngineRunning = false;
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;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap; // ๋ถ๋๋ฌ์ด ๊ทธ๋ฆผ์
this.renderer.outputColorSpace = THREE.SRGBColorSpace;
document.getElementById('gameContainer').appendChild(this.renderer.domElement);
// ๋ ์ด๋ ๊ด๋ จ ์์ฑ ์ถ๊ฐ
this.radarUpdateInterval = 100; // 100ms๋ง๋ค ๋ ์ด๋ ์
๋ฐ์ดํธ
this.lastRadarUpdate = 0;
this.radarRange = 200; // ๋ ์ด๋ ๊ฐ์ง ๋ฒ์
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.gameTimer = null;
this.animationFrameId = null;
this.lastAudio = null; // ๋ง์ง๋ง ๋ฐ์ฌ์ ์ถ์ ์ ์ํ ์์ฑ ์ถ๊ฐ
this.mouse = { x: 0, y: 0 };
this.keys = {
forward: false,
backward: false,
left: false,
right: false
};
this.setupEventListeners();
this.initialize();
this.obstacles = []; // obstacles ๋ฐฐ์ด ์ถ๊ฐ
}
setupScene() {
// ์ฌ ์ด๊ธฐํ
this.scene.background = new THREE.Color(0x87CEEB); // ํ๋์ ๋ฐฐ๊ฒฝ
// ์๊ฐ ์ค์
this.scene.fog = new THREE.FogExp2(0x87CEEB, 0.0008);
// ๋ ๋๋ฌ ์ค์
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
this.renderer.setPixelRatio(window.devicePixelRatio);
// ์นด๋ฉ๋ผ ์ด๊ธฐ ์ค์
this.camera.position.set(0, 15, -30);
this.camera.lookAt(0, 0, 0);
// ์ฌ ๊ฒฝ๊ณ ์ค์
const mapBoundary = MAP_SIZE / 2;
this.sceneBounds = {
minX: -mapBoundary,
maxX: mapBoundary,
minZ: -mapBoundary,
maxZ: mapBoundary
};
}
async initialize() {
try {
// ์์ ์ฌ์ด๋ ์ฌ์
const startSounds = ['sounds/start1.ogg', 'sounds/start2.ogg', 'sounds/start3.ogg'];
const randomStartSound = startSounds[Math.floor(Math.random() * startSounds.length)];
const startAudio = new Audio(randomStartSound);
startAudio.volume = 0.5;
startAudio.play();
// ๋ ๋๋ฌ ์ค์
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
this.renderer.outputColorSpace = THREE.SRGBColorSpace;
// ๊ธฐ๋ณธ ์ฌ ์ค์
this.setupScene();
// ์๊ฐ ํจ๊ณผ
this.scene.fog = new THREE.FogExp2(0x87CEEB, 0.0008);
this.scene.background = new THREE.Color(0x87CEEB);
// ์กฐ๋ช
์ค์
// ์ฃผ๋ณ๊ด
const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
this.scene.add(ambientLight);
// ๋ฉ์ธ ํ์๊ด
const mainLight = new THREE.DirectionalLight(0xffffff, 1.0);
mainLight.position.set(MAP_SIZE/2, MAP_SIZE/2, MAP_SIZE/2);
mainLight.castShadow = true;
// ๊ทธ๋ฆผ์ ์ค์
mainLight.shadow.mapSize.width = 4096;
mainLight.shadow.mapSize.height = 4096;
mainLight.shadow.camera.near = 0.5;
mainLight.shadow.camera.far = MAP_SIZE * 2;
mainLight.shadow.camera.left = -MAP_SIZE;
mainLight.shadow.camera.right = MAP_SIZE;
mainLight.shadow.camera.top = MAP_SIZE;
mainLight.shadow.camera.bottom = -MAP_SIZE;
mainLight.shadow.bias = -0.001;
mainLight.shadow.radius = 2;
mainLight.shadow.normalBias = 0.02;
this.scene.add(mainLight);
// ๋ณด์กฐ ํ์๊ด
const secondaryLight = new THREE.DirectionalLight(0xffffff, 0.3);
secondaryLight.position.set(-50, 50, -50);
this.scene.add(secondaryLight);
// ํ๊ฒฝ๊ด
const hemisphereLight = new THREE.HemisphereLight(
0x87CEEB,
0xFFE87C,
0.3
);
this.scene.add(hemisphereLight);
// ์งํ ์์ฑ
const groundGeometry = new THREE.PlaneGeometry(MAP_SIZE, MAP_SIZE, 100, 100);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xD2B48C,
roughness: 0.8,
metalness: 0.2,
envMapIntensity: 1.0
});
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
// ์งํ ๋์ด ์ค์
const vertices = ground.geometry.attributes.position.array;
for (let i = 0; i < vertices.length; i += 3) {
vertices[i + 2] = 0; // ๋ชจ๋ ๋์ด๋ฅผ 0์ผ๋ก ์ค์
}
ground.geometry.attributes.position.needsUpdate = true;
ground.geometry.computeVertexNormals();
this.ground = ground;
this.scene.add(ground);
// ๊ฒฉ์ ํจ๊ณผ ์ถ๊ฐ
const gridHelper = new THREE.GridHelper(MAP_SIZE, 50, 0x000000, 0x000000);
gridHelper.material.opacity = 0.1;
gridHelper.material.transparent = true;
gridHelper.position.y = 0.1;
this.scene.add(gridHelper);
// ์ฌ๋ง ์ฅ์ ์ถ๊ฐ
await this.addDesertDecorations();
// ํฑํฌ ์ด๊ธฐํ
await this.tank.initialize(this.scene, this.loader);
if (!this.tank.isLoaded) {
throw new Error('Tank loading failed');
}
// ์คํฐ ์์น ๊ฒ์ฆ
const spawnPos = this.findValidSpawnPosition();
const heightAtSpawn = this.getHeightAtPosition(spawnPos.x, spawnPos.z);
const slopeCheckPoints = [
{ x: spawnPos.x + 2, z: spawnPos.z },
{ x: spawnPos.x - 2, z: spawnPos.z },
{ x: spawnPos.x, z: spawnPos.z + 2 },
{ x: spawnPos.x, z: spawnPos.z - 2 }
];
const slopes = slopeCheckPoints.map(point => {
const pointHeight = this.getHeightAtPosition(point.x, point.z);
return Math.abs(pointHeight - heightAtSpawn) / 2;
});
const maxSlope = Math.max(...slopes);
if (maxSlope > 0.3) {
location.reload();
return;
}
// ์นด๋ฉ๋ผ ์ด๊ธฐ ์ค์
const tankPosition = this.tank.getPosition();
this.camera.position.set(
tankPosition.x,
tankPosition.y + 15,
tankPosition.z - 30
);
this.camera.lookAt(tankPosition);
// ๋ก๋ฉ ์๋ฃ ์ฒ๋ฆฌ
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();
}
}
// ๋ ์ด๋ ์
๋ฐ์ดํธ ๋ฉ์๋ ์ถ๊ฐ
updateRadar() {
const currentTime = Date.now();
if (currentTime - this.lastRadarUpdate < this.radarUpdateInterval) return;
const radar = document.getElementById('radar');
const radarRect = radar.getBoundingClientRect();
const radarCenter = {
x: radarRect.width / 2,
y: radarRect.height / 2
};
// ๊ธฐ์กด ์ ๋ํธ ์ ๊ฑฐ
const oldDots = radar.getElementsByClassName('enemy-dot');
while (oldDots[0]) {
oldDots[0].remove();
}
// ํฑํฌ ์์น ๊ฐ์ ธ์ค๊ธฐ
const tankPos = this.tank.getPosition();
// ๋ชจ๋ ์ ์ ๋ํด ๋ ์ด๋์ ํ์
this.enemies.forEach(enemy => {
if (!enemy.mesh || !enemy.isLoaded) return;
const enemyPos = enemy.mesh.position;
const distance = tankPos.distanceTo(enemyPos);
// ๋ ์ด๋ ๋ฒ์ ๋ด์ ์๋ ๊ฒฝ์ฐ๋ง ํ์
if (distance <= this.radarRange) {
// ํฑํฌ ๊ธฐ์ค ์๋ ๊ฐ๋ ๊ณ์ฐ
const angle = Math.atan2(
enemyPos.x - tankPos.x,
enemyPos.z - tankPos.z
);
// ์๋ ๊ฑฐ๋ฆฌ๋ฅผ ๋ ์ด๋ ํฌ๊ธฐ์ ๋ง๊ฒ ์ค์ผ์ผ๋ง
const relativeDistance = distance / this.radarRange;
const dotX = radarCenter.x + Math.sin(angle) * (radarCenter.x * relativeDistance);
const dotY = radarCenter.y + Math.cos(angle) * (radarCenter.y * relativeDistance);
// ์ ๋ํธ ์์ฑ ๋ฐ ์ถ๊ฐ
const dot = document.createElement('div');
dot.className = 'enemy-dot';
dot.style.left = `${dotX}px`;
dot.style.top = `${dotY}px`;
radar.appendChild(dot);
}
});
this.lastRadarUpdate = currentTime;
}
async addDesertDecorations() {
if (!this.obstacles) {
this.obstacles = []; // ๋ฐฐ์ด์ด ์์ผ๋ฉด ์ด๊ธฐํ
}
// ๋ฐ์ ์์ฑ
const rockGeometries = [
new THREE.DodecahedronGeometry(3),
new THREE.DodecahedronGeometry(2),
new THREE.DodecahedronGeometry(4)
];
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x8B4513,
roughness: 0.9,
metalness: 0.1
});
for (let i = 0; i < 100; i++) {
const rockGeometry = rockGeometries[Math.floor(Math.random() * rockGeometries.length)];
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(
(Math.random() - 0.5) * MAP_SIZE * 0.9,
Math.random() * 2,
(Math.random() - 0.5) * MAP_SIZE * 0.9
);
rock.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
rock.scale.set(
1 + Math.random() * 0.5,
1 + Math.random() * 0.5,
1 + Math.random() * 0.5
);
rock.castShadow = true;
rock.receiveShadow = true;
rock.userData.type = 'rock'; // ์ฅ์ ๋ฌผ ํ์
์ง์
this.obstacles.push(rock);
this.scene.add(rock);
}
// ์ ์ธ์ฅ ์ถ๊ฐ
const cactusGeometry = new THREE.CylinderGeometry(0.5, 0.7, 4, 8);
const cactusMaterial = new THREE.MeshStandardMaterial({
color: 0x2F4F2F,
roughness: 0.8
});
for (let i = 0; i < 50; i++) {
const cactus = new THREE.Mesh(cactusGeometry, cactusMaterial);
cactus.position.set(
(Math.random() - 0.5) * MAP_SIZE * 0.8,
2,
(Math.random() - 0.5) * MAP_SIZE * 0.8
);
cactus.castShadow = true;
cactus.receiveShadow = true;
cactus.userData.type = 'cactus'; // ์ฅ์ ๋ฌผ ํ์
์ง์
this.obstacles.push(cactus);
this.scene.add(cactus);
}
}
getHeightAtPosition(x, z) {
return 0; // ํญ์ ๋์ด 0 ๋ฐํ
}
findValidSpawnPosition() {
const margin = 50;
let position;
let attempts = 0;
const maxAttempts = 50;
const maxSlope = 0.3; // ์ต๋ ํ์ฉ ๊ฒฝ์ฌ
while (attempts < maxAttempts) {
position = new THREE.Vector3(
(Math.random() - 0.5) * (MAP_SIZE - margin * 2),
0,
(Math.random() - 0.5) * (MAP_SIZE - margin * 2)
);
// ํ์ฌ ์์น์ ๋์ด ๊ฐ์ ธ์ค๊ธฐ
const height = this.getHeightAtPosition(position.x, position.z);
position.y = height + TANK_HEIGHT;
// ์ฃผ๋ณ ์งํ์ ๊ฒฝ์ฌ ์ฒดํฌ
const checkPoints = [
{ x: position.x + 2, z: position.z },
{ x: position.x - 2, z: position.z },
{ x: position.x, z: position.z + 2 },
{ x: position.x, z: position.z - 2 }
];
const slopes = checkPoints.map(point => {
const pointHeight = this.getHeightAtPosition(point.x, point.z);
return Math.abs(pointHeight - height) / 2;
});
const maxCurrentSlope = Math.max(...slopes);
if (maxCurrentSlope <= maxSlope) {
return position;
}
attempts++;
}
// ์คํจ ์ ๊ธฐ๋ณธ ์์น ๋ฐํ
return new THREE.Vector3(0, TANK_HEIGHT, 0);
}
setupEventListeners() {
document.addEventListener('keydown', (event) => {
if (this.isLoading || this.isGameOver) 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 || this.isGameOver) 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 || this.isGameOver || !document.pointerLockElement) return;
const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
this.mouse.x += movementX * 0.002;
this.mouse.y = 0;
while (this.mouse.x > Math.PI) this.mouse.x -= Math.PI * 2;
while (this.mouse.x < -Math.PI) this.mouse.x += Math.PI * 2;
});
document.addEventListener('click', () => {
if (!document.pointerLockElement) {
document.body.requestPointerLock();
} else if (!this.isGameOver && this.tank && this.tank.isLoaded) {
const bullet = this.tank.shoot(this.scene);
if (bullet) {
// Shooting effects...
}
}
});
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 || this.isGameOver) return;
const direction = new THREE.Vector3();
const isMoving = this.keys.forward || this.keys.backward;
// ์ด๋ ์์ํ ๋ ์ฌ์ด๋ ์ฒ๋ฆฌ
if (isMoving && !this.isEngineRunning) {
this.isEngineRunning = true;
// ์ด์ ์ฌ์ด๋ ์ ์ง
if (this.engineStopSound) {
this.engineStopSound.pause();
}
if (this.engineSound) {
this.engineSound.pause();
}
// ์์ง ์ ์ง ์ฌ์ด๋ ์ฌ์
this.engineStopSound = new Audio('sounds/engine.ogg');
this.engineStopSound.play();
// ์์ง ์ ์ง ์ฌ์ด๋ ์ข
๋ฃ ํ ์์ง ์ฌ์ด๋ ์์
this.engineStopSound.onended = () => {
this.engineSound = new Audio('sounds/engine.ogg');
this.engineSound.loop = true;
this.engineSound.play();
};
}
// ์ด๋ ๋ฉ์ถ ๋ ์ฌ์ด๋ ์ฒ๋ฆฌ
else if (!isMoving && this.isEngineRunning) {
this.isEngineRunning = false;
if (this.engineSound) {
this.engineSound.pause();
this.engineSound = null;
}
if (this.engineStopSound) {
this.engineStopSound.pause();
this.engineStopSound = null;
}
const stopSound = new Audio('sounds/enginestop.ogg');
stopSound.play();
}
// ์ฐจ์ฒด ์ด๋๊ณผ ํ์ ์ ์ ์ง
if (this.keys.forward) direction.z += 1;
if (this.keys.backward) direction.z -= 1;
if (this.keys.left) this.tank.rotate(-1);
if (this.keys.right) this.tank.rotate(1);
if (direction.length() > 0) {
direction.normalize();
direction.applyEuler(this.tank.body.rotation);
this.tank.move(direction);
}
// ํฑํฌ ์์น ๊ฐ์ ธ์ค๊ธฐ
const tankPos = this.tank.getPosition();
// ์นด๋ฉ๋ผ๋ ๋ง์ฐ์ค X ํ์ ์๋ง ๋ฐ๋ผ๊ฐ
const cameraDistance = 15;
const cameraHeight = 8;
const cameraAngle = this.mouse.x + Math.PI; // ํญ์ ํฌํ์ ๋ค์ชฝ์ ์์น
const cameraX = tankPos.x + Math.sin(cameraAngle) * cameraDistance;
const cameraZ = tankPos.z + Math.cos(cameraAngle) * cameraDistance;
this.camera.position.set(
cameraX,
tankPos.y + cameraHeight,
cameraZ
);
// ์นด๋ฉ๋ผ๊ฐ ํฑํฌ๋ฅผ ๋ฐ๋ผ๋ณด๋๋ก ์ค์
const lookAtPoint = new THREE.Vector3(
tankPos.x,
tankPos.y + 1,
tankPos.z
);
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);
}
}
return Promise.resolve();
}
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>
`;
}
}
startGameTimer() {
if (this.gameTimer) {
clearInterval(this.gameTimer);
}
this.gameTimer = setInterval(() => {
if (this.isLoading || this.isGameOver) {
clearInterval(this.gameTimer);
return;
}
this.gameTime--;
document.getElementById('time').textContent = `Time: ${this.gameTime}s`;
if (this.gameTime <= 0) {
clearInterval(this.gameTimer);
this.endGame();
}
}, 1000);
}
spawnEnemies() {
const spawnEnemy = () => {
if (this.enemies.length < 3 && !this.isGameOver) { // ์ต๋ 3๋๋ก ์ ํ
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);
this.enemies.push(enemy);
}
}
if (!this.isGameOver) {
setTimeout(spawnEnemy, 10000); // 10์ด๋ง๋ค ์คํฐ
}
};
spawnEnemy();
}
getValidEnemySpawnPosition() {
const margin = 50;
let position;
let attempts = 0;
const maxAttempts = 50;
const maxSlope = 0.3;
do {
position = new THREE.Vector3(
(Math.random() - 0.5) * (MAP_SIZE - margin * 2),
0,
(Math.random() - 0.5) * (MAP_SIZE - margin * 2)
);
const height = this.getHeightAtPosition(position.x, position.z);
position.y = height + TANK_HEIGHT;
// ์ฃผ๋ณ ๊ฒฝ์ฌ ์ฒดํฌ
const checkPoints = [
{ x: position.x + 2, z: position.z },
{ x: position.x - 2, z: position.z },
{ x: position.x, z: position.z + 2 },
{ x: position.x, z: position.z - 2 }
];
const slopes = checkPoints.map(point => {
const pointHeight = this.getHeightAtPosition(point.x, point.z);
return Math.abs(pointHeight - height) / 2;
});
const maxCurrentSlope = Math.max(...slopes);
// ํ๋ ์ด์ด์์ ๊ฑฐ๋ฆฌ ์ฒดํฌ
const distanceToPlayer = position.distanceTo(this.tank.getPosition());
if (distanceToPlayer > 100 && maxCurrentSlope <= maxSlope) {
return position;
}
attempts++;
} while (attempts < maxAttempts);
return null;
}
updateParticles() {
for (let i = this.particles.length - 1; i >= 0; i--) {
const particle = this.particles[i];
// ์ค๋ ฅ ์ ์ฉ
particle.velocity.y += particle.gravity;
particle.mesh.position.add(particle.velocity);
// ํฌ๋ช
๋ ๊ฐ์
particle.mesh.material.opacity -= particle.fadeRate;
particle.life--;
// ํํฐํด ์ ๊ฑฐ
if (particle.life <= 0 || particle.mesh.material.opacity <= 0) {
this.scene.remove(particle.mesh);
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 hitSounds = [
'sounds/hit1.ogg', 'sounds/hit2.ogg', 'sounds/hit3.ogg',
'sounds/hit4.ogg', 'sounds/hit5.ogg', 'sounds/hit6.ogg', 'sounds/hit7.ogg'
];
// ํผ๊ฒฉ ์ฌ์ด๋ ๋ฐฐ์ด ์ ์
const beatSounds = ['sounds/beat1.ogg', 'sounds/beat2.ogg', 'sounds/beat3.ogg'];
const tankPosition = this.tank.getPosition();
const tankBoundingBox = new THREE.Box3().setFromObject(this.tank.body);
// ํฑํฌ์ ์ฅ์ ๋ฌผ ์ถฉ๋ ์ฒดํฌ (๊ฐ์ )
this.obstacles.forEach(obstacle => {
const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
if (tankBoundingBox.intersectsBox(obstacleBoundingBox)) {
// ์ถฉ๋ ์ ์ด์ ์์น๋ก ๋๋๋ฆฌ๊ธฐ
this.tank.body.position.copy(this.previousTankPosition);
// ์ถฉ๋ ์ฌ์ด๋ ์ฌ์
//const collisionSound = new Audio('sounds/collision.ogg');
//collisionSound.volume = 0.3;
//collisionSound.play();
}
});
// ์ ํฑํฌ์ ์ฅ์ ๋ฌผ ์ถฉ๋ ์ฒดํฌ (์ถ๊ฐ)
this.enemies.forEach(enemy => {
if (!enemy.mesh || !enemy.isLoaded) return;
const enemyBoundingBox = new THREE.Box3().setFromObject(enemy.mesh);
const enemyPreviousPosition = enemy.mesh.position.clone();
this.obstacles.forEach(obstacle => {
const obstacleBoundingBox = new THREE.Box3().setFromObject(obstacle);
if (enemyBoundingBox.intersectsBox(obstacleBoundingBox)) {
enemy.mesh.position.copy(enemyPreviousPosition);
}
});
});
// ์ ์ด์๊ณผ ํ๋ ์ด์ด ํฑํฌ ์ถฉ๋ ์ฒดํฌ
this.enemies.forEach(enemy => {
if (!enemy.mesh || !enemy.isLoaded) return;
enemy.bullets.forEach((bullet, bulletIndex) => {
const distance = bullet.position.distanceTo(tankPosition);
if (distance < 1) {
// ํ๋ ์ด์ด ํผ๊ฒฉ ์ฌ์ด๋ ์ฌ์
const randomBeatSound = beatSounds[Math.floor(Math.random() * beatSounds.length)];
const beatAudio = new Audio(randomBeatSound);
beatAudio.play();
if (this.tank.takeDamage(250)) {
this.endGame();
}
this.scene.remove(bullet);
enemy.bullets.splice(bulletIndex, 1); // filter ๋์ splice ์ฌ์ฉ
this.createExplosion(bullet.position);
document.getElementById('health').style.width =
`${(this.tank.health / MAX_HEALTH) * 100}%`;
}
});
});
// ํฌํ๊ณผ ์ฅ์ ๋ฌผ ์ถฉ๋ ์ฒดํฌ (์ถ๊ฐ)
// ํ๋ ์ด์ด ํฌํ
for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
const bullet = this.tank.bullets[i];
const bulletBox = new THREE.Box3().setFromObject(bullet);
for (const obstacle of this.obstacles) {
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
if (bulletBox.intersectsBox(obstacleBox)) {
// ํญ๋ฐ ์ดํํธ ์์ฑ
this.tank.createExplosionEffect(this.scene, bullet.position);
// ํฌํ ์ ๊ฑฐ
this.scene.remove(bullet);
this.tank.bullets.splice(i, 1);
break;
}
}
}
// ์ ํฌํ
this.enemies.forEach(enemy => {
if (!enemy.bullets) return;
for (let i = enemy.bullets.length - 1; i >= 0; i--) {
const bullet = enemy.bullets[i];
const bulletBox = new THREE.Box3().setFromObject(bullet);
for (const obstacle of this.obstacles) {
const obstacleBox = new THREE.Box3().setFromObject(obstacle);
if (bulletBox.intersectsBox(obstacleBox)) {
// ํญ๋ฐ ์ดํํธ ์์ฑ
this.tank.createExplosionEffect(this.scene, bullet.position);
// ํฌํ ์ ๊ฑฐ
this.scene.remove(bullet);
enemy.bullets.splice(i, 1);
break;
}
}
}
});
// ํ๋ ์ด์ด ์ด์๊ณผ ์ ์ถฉ๋ ์ฒดํฌ
for (let i = this.tank.bullets.length - 1; i >= 0; i--) {
const bullet = this.tank.bullets[i];
for (let j = this.enemies.length - 1; j >= 0; j--) {
const enemy = this.enemies[j];
if (!enemy.mesh || !enemy.isLoaded) continue;
const distance = bullet.position.distanceTo(enemy.mesh.position);
if (distance < 2) {
// ์ ํผ๊ฒฉ ์ฌ์ด๋ ์ฌ์
const randomHitSound = hitSounds[Math.floor(Math.random() * hitSounds.length)];
const hitAudio = new Audio(randomHitSound);
hitAudio.play();
if (enemy.takeDamage(50)) {
enemy.destroy();
this.enemies.splice(j, 1);
this.score += 100;
document.getElementById('score').textContent = `Score: ${this.score}`;
}
this.scene.remove(bullet);
this.tank.bullets.splice(i, 1);
this.createExplosion(bullet.position);
break; // ํ๋์ ์ด์์ด ์ฌ๋ฌ ์ ์ ๊ดํตํ์ง ์๋๋ก
}
}
}
// ํ๋ ์ด์ด ํฑํฌ์ ์ ์ ์ฐจ ์ถฉ๋ ์ฒดํฌ
this.enemies.forEach(enemy => {
if (!enemy.mesh || !enemy.isLoaded) return;
const enemyBoundingBox = new THREE.Box3().setFromObject(enemy.mesh);
if (tankBoundingBox.intersectsBox(enemyBoundingBox)) {
this.tank.body.position.copy(this.previousTankPosition);
}
});
// ์ด์ ์์น ์ ์ฅ
this.previousTankPosition.copy(this.tank.body.position);
}
endGame() {
if (this.isGameOver) return;
this.isGameOver = true;
// ์ฌ๋ง ์ฌ์ด๋ ์ฌ์
const deathSounds = ['sounds/death1.ogg', 'sounds/death2.ogg'];
const randomDeathSound = deathSounds[Math.floor(Math.random() * deathSounds.length)];
const deathAudio = new Audio(randomDeathSound);
deathAudio.play();
if (this.gameTimer) {
clearInterval(this.gameTimer);
}
if (this.animationFrameId) {
cancelAnimationFrame(this.animationFrameId);
}
document.exitPointerLock();
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 = '#0f0';
gameOverDiv.style.fontSize = '48px';
gameOverDiv.style.backgroundColor = 'rgba(0, 20, 0, 0.7)';
gameOverDiv.style.padding = '20px';
gameOverDiv.style.borderRadius = '10px';
gameOverDiv.style.textAlign = 'center';
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: #0f0; border: none;
color: black; border-radius: 5px;">
Play Again
</button>
`;
document.body.appendChild(gameOverDiv);
}
updateUI() {
if (!this.isGameOver) {
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}`;
}
}
}
animate() {
if (this.isGameOver) {
if (this.animationFrameId) {
cancelAnimationFrame(this.animationFrameId);
}
return;
}
this.animationFrameId = requestAnimationFrame(() => this.animate());
const currentTime = performance.now();
const deltaTime = (currentTime - this.lastTime) / 1000;
this.lastTime = currentTime;
if (!this.isLoading) {
this.handleMovement();
this.tank.update(this.mouse.x, this.mouse.y, this.scene);
const tankPosition = this.tank.getPosition();
this.enemies.forEach(enemy => {
enemy.update(tankPosition);
if (enemy.isLoaded && enemy.mesh.position.distanceTo(tankPosition) < ENEMY_CONFIG.ATTACK_RANGE) {
enemy.shoot(tankPosition);
}
});
this.updateParticles();
this.checkCollisions();
this.updateUI();
this.updateRadar(); // ๋ ์ด๋ ์
๋ฐ์ดํธ ์ถ๊ฐ
}
this.renderer.render(this.scene, this.camera);
}
}
// Start game
window.startGame = function() {
document.getElementById('startScreen').style.display = 'none';
document.body.requestPointerLock();
// Create new game instance or restart existing one
if (!window.gameInstance) {
window.gameInstance = new Game();
}
};
// Initialize game
document.addEventListener('DOMContentLoaded', () => {
const game = new Game();
}); |