Spaces:
Running
Running
File size: 34,852 Bytes
b5f5087 436fe15 b5f5087 6c5795b e0245e2 cacc3d1 6c5795b cacc3d1 6c5795b cacc3d1 e0245e2 cacc3d1 436fe15 6c5795b cacc3d1 6c5795b cacc3d1 436fe15 6c5795b 436fe15 420114b 436fe15 cacc3d1 436fe15 cacc3d1 436fe15 cacc3d1 6c5795b cacc3d1 436fe15 cacc3d1 6c5795b e0245e2 6c5795b f48879f cacc3d1 e0245e2 cacc3d1 436fe15 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 436fe15 cacc3d1 e0245e2 cacc3d1 436fe15 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 6c5795b cacc3d1 436fe15 cacc3d1 e0245e2 cacc3d1 436fe15 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 e0245e2 cacc3d1 436fe15 cacc3d1 6c5795b b5f5087 cacc3d1 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Doom-Style 3D Shooter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/PointerLockControls.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Courier New', monospace;
touch-action: none;
}
body {
overflow: hidden;
background: #000;
color: #ff5555;
height: 100vh;
perspective: 1px;
}
#gameContainer {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
#gameCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
#ui {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2;
pointer-events: none;
}
#crosshair {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 30px;
height: 30px;
border: 2px solid #ff5555;
border-radius: 50%;
pointer-events: none;
}
#crosshair::before, #crosshair::after {
content: '';
position: absolute;
background: #ff5555;
}
#crosshair::before {
width: 2px;
height: 10px;
top: -12px;
left: 50%;
transform: translateX(-50%);
}
#crosshair::after {
width: 10px;
height: 2px;
top: 50%;
right: -12px;
transform: translateY(-50%);
}
#healthBar {
position: absolute;
bottom: 30px;
left: 30px;
width: 200px;
height: 30px;
background: rgba(0, 0, 0, 0.7);
border: 2px solid #ff5555;
border-radius: 5px;
overflow: hidden;
}
#healthFill {
height: 100%;
width: 100%;
background: linear-gradient(90deg, #ff0000, #ff5555);
transition: width 0.3s;
}
#ammoCounter {
position: absolute;
bottom: 30px;
right: 30px;
font-size: 24px;
color: #ff5555;
text-shadow: 0 0 5px #ff0000;
background: rgba(0, 0, 0, 0.7);
padding: 10px 20px;
border: 2px solid #ff5555;
border-radius: 5px;
}
#score {
position: absolute;
top: 30px;
left: 30px;
font-size: 24px;
color: #ff5555;
text-shadow: 0 0 5px #ff0000;
}
#startScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(rgba(0, 0, 0, 0.8), rgba(20, 0, 0, 0.9)),
url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect width="100" height="100" fill="%23110000"/><path d="M0 0L100 100M100 0L0 100" stroke="%23300" stroke-width="1"/></svg>');
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 10;
text-align: center;
}
#title {
font-size: 72px;
margin-bottom: 30px;
text-shadow: 0 0 10px #ff0000, 0 0 20px #ff0000;
letter-spacing: 5px;
}
#subtitle {
font-size: 24px;
margin-bottom: 50px;
color: #ff9999;
}
#startButton {
background: #ff0000;
color: #fff;
border: none;
padding: 15px 50px;
font-size: 24px;
cursor: pointer;
border-radius: 5px;
text-transform: uppercase;
letter-spacing: 3px;
font-weight: bold;
box-shadow: 0 0 15px #ff0000;
transition: all 0.3s;
pointer-events: auto;
}
#startButton:hover {
background: #ff5555;
transform: scale(1.05);
box-shadow: 0 0 25px #ff0000;
}
#gameOverScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.85);
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 10;
}
#gameOverTitle {
font-size: 60px;
margin-bottom: 20px;
color: #ff0000;
text-shadow: 0 0 10px #ff0000;
}
#finalScore {
font-size: 36px;
margin-bottom: 40px;
color: #ff9999;
}
#restartButton {
background: #ff0000;
color: #fff;
border: none;
padding: 15px 50px;
font-size: 24px;
cursor: pointer;
border-radius: 5px;
text-transform: uppercase;
letter-spacing: 3px;
font-weight: bold;
box-shadow: 0 0 15px #ff0000;
transition: all 0.3s;
}
#restartButton:hover {
background: #ff5555;
transform: scale(1.05);
box-shadow: 0 0 25px #ff0000;
}
#weapon {
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 300px;
height: 200px;
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 200"><rect x="50" y="100" width="200" height="30" fill="%23333"/><rect x="100" y="50" width="100" height="50" fill="%23444"/><rect x="130" y="30" width="40" height="20" fill="%23555"/><circle cx="150" cy="115" r="15" fill="%23222"/></svg>') no-repeat center bottom;
background-size: contain;
z-index: 3;
transition: transform 0.1s;
}
.hitEffect {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 0, 0, 0.3);
pointer-events: none;
opacity: 0;
z-index: 4;
}
#bloodEffect {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: radial-gradient(circle, rgba(255,0,0,0.7) 0%, rgba(255,0,0,0) 70%);
pointer-events: none;
opacity: 0;
z-index: 5;
}
/* Mobile Controls */
#mobileControls {
position: absolute;
bottom: 20px;
width: 100%;
display: none;
justify-content: space-between;
padding: 0 20px;
z-index: 10;
pointer-events: none;
}
.controlPad {
width: 120px;
height: 120px;
background: rgba(255, 0, 0, 0.3);
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
pointer-events: auto;
}
.controlStick {
width: 50px;
height: 50px;
background: rgba(255, 85, 85, 0.7);
border-radius: 50%;
position: relative;
}
#shootButton {
width: 80px;
height: 80px;
background: rgba(255, 0, 0, 0.5);
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
color: white;
pointer-events: auto;
border: 2px solid #ff5555;
}
#instructions {
position: absolute;
bottom: 160px;
width: 100%;
text-align: center;
color: #ff9999;
font-size: 18px;
padding: 0 20px;
}
@media (max-width: 768px) {
#mobileControls {
display: flex;
}
#instructions {
display: block;
}
#title {
font-size: 48px;
}
#subtitle {
font-size: 18px;
}
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="gameCanvas"></div>
<div id="ui">
<div id="crosshair"></div>
<div id="healthBar">
<div id="healthFill"></div>
</div>
<div id="ammoCounter">AMMO: 30</div>
<div id="score">SCORE: 0</div>
<div id="weapon"></div>
<div class="hitEffect" id="hitEffect"></div>
<div id="bloodEffect"></div>
</div>
<div id="mobileControls">
<div class="controlPad" id="movementPad">
<div class="controlStick" id="movementStick"></div>
</div>
<div id="shootButton">FIRE</div>
</div>
<div id="instructions">Use joystick to move, tap FIRE to shoot</div>
<div id="startScreen">
<h1 id="title">DEMON SLAYER</h1>
<p id="subtitle">Eliminate all demons to survive</p>
<button id="startButton">START MISSION</button>
</div>
<div id="gameOverScreen">
<h1 id="gameOverTitle">MISSION FAILED</h1>
<p id="finalScore">SCORE: 0</p>
<button id="restartButton">TRY AGAIN</button>
</div>
</div>
<script>
// Game variables
let scene, camera, renderer, controls;
let player, enemies = [], bullets = [];
let playerHealth = 100;
let ammo = 30;
let score = 0;
let gameActive = false;
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
let canShoot = true;
let lastShotTime = 0;
let shotCooldown = 300; // ms
let enemySpawnTimer = 0;
let clock = new THREE.Clock();
let isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
// Mobile controls
let movementStickActive = false;
let movementStickPosition = { x: 0, y: 0 };
let movementStickRadius = 35;
// Initialize the game
function init() {
// Create scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x110000);
scene.fog = new THREE.Fog(0x110000, 10, 50);
// Create camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.y = 1.6;
// Create renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.getElementById('gameCanvas').appendChild(renderer.domElement);
// Add lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xff5555, 0.8);
directionalLight.position.set(10, 20, 10);
directionalLight.castShadow = true;
scene.add(directionalLight);
// Create floor
const floorGeometry = new THREE.PlaneGeometry(100, 100);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x222222,
roughness: 0.8,
metalness: 0.2
});
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
// Create walls
createWalls();
// Create pillars
createPillars();
// Create player
player = new THREE.Object3D();
player.position.set(0, 1.6, 0);
scene.add(player);
player.add(camera);
// Add pointer lock controls for desktop
if (!isMobile) {
controls = new THREE.PointerLockControls(camera, document.body);
}
// Event listeners
if (!isMobile) {
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
document.addEventListener('mousedown', onMouseDown, false);
document.addEventListener('mousemove', onMouseMove, false);
} else {
setupMobileControls();
}
window.addEventListener('resize', onWindowResize, false);
// Start button event
document.getElementById('startButton').addEventListener('click', startGame);
document.getElementById('restartButton').addEventListener('click', restartGame);
// Start animation loop
animate();
}
function createWalls() {
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0x330000,
roughness: 0.9,
metalness: 0.1
});
// Create a simple maze-like structure
const walls = [
// Outer walls
{ x: 0, y: 2, z: -25, width: 50, height: 4, depth: 1 },
{ x: 0, y: 2, z: 25, width: 50, height: 4, depth: 1 },
{ x: -25, y: 2, z: 0, width: 1, height: 4, depth: 50 },
{ x: 25, y: 2, z: 0, width: 1, height: 4, depth: 50 },
// Inner walls
{ x: -10, y: 2, z: -10, width: 1, height: 4, depth: 20 },
{ x: 10, y: 2, z: 10, width: 1, height: 4, depth: 20 },
{ x: 0, y: 2, z: 0, width: 20, height: 4, depth: 1 },
{ x: 0, y: 2, z: -20, width: 20, height: 4, depth: 1 }
];
walls.forEach(wall => {
const wallGeometry = new THREE.BoxGeometry(wall.width, wall.height, wall.depth);
const wallMesh = new THREE.Mesh(wallGeometry, wallMaterial);
wallMesh.position.set(wall.x, wall.y, wall.z);
wallMesh.castShadow = true;
wallMesh.receiveShadow = true;
scene.add(wallMesh);
});
}
function createPillars() {
const pillarMaterial = new THREE.MeshStandardMaterial({
color: 0x440000,
roughness: 0.7,
metalness: 0.3
});
const pillarGeometry = new THREE.CylinderGeometry(1, 1, 4, 16);
const positions = [
{ x: -20, z: -20 },
{ x: 20, z: -20 },
{ x: -20, z: 20 },
{ x: 20, z: 20 },
{ x: 0, z: -15 },
{ x: 0, z: 15 },
{ x: -15, z: 0 },
{ x: 15, z: 0 }
];
positions.forEach(pos => {
const pillar = new THREE.Mesh(pillarGeometry, pillarMaterial);
pillar.position.set(pos.x, 2, pos.z);
pillar.castShadow = true;
pillar.receiveShadow = true;
scene.add(pillar);
});
}
// Create a detailed demon model
function createDemon() {
const demonGroup = new THREE.Group();
// Body
const bodyGeometry = new THREE.SphereGeometry(0.8, 16, 16);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x880000,
roughness: 0.7,
metalness: 0.3
});
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.position.y = 1;
body.castShadow = true;
demonGroup.add(body);
// Head
const headGeometry = new THREE.SphereGeometry(0.6, 16, 16);
const headMaterial = new THREE.MeshStandardMaterial({
color: 0xaa0000,
roughness: 0.7,
metalness: 0.3
});
const head = new THREE.Mesh(headGeometry, headMaterial);
head.position.y = 1.8;
head.castShadow = true;
demonGroup.add(head);
// Horns
const hornGeometry = new THREE.ConeGeometry(0.1, 0.8, 8);
const hornMaterial = new THREE.MeshStandardMaterial({ color: 0x333333 });
const horn1 = new THREE.Mesh(hornGeometry, hornMaterial);
horn1.position.set(-0.2, 2.3, 0);
horn1.rotation.z = Math.PI / 6;
horn1.castShadow = true;
demonGroup.add(horn1);
const horn2 = new THREE.Mesh(hornGeometry, hornMaterial);
horn2.position.set(0.2, 2.3, 0);
horn2.rotation.z = -Math.PI / 6;
horn2.castShadow = true;
demonGroup.add(horn2);
// Eyes
const eyeGeometry = new THREE.SphereGeometry(0.15, 8, 8);
const eyeMaterial = new THREE.MeshStandardMaterial({ color: 0xffff00 });
const eye1 = new THREE.Mesh(eyeGeometry, eyeMaterial);
eye1.position.set(-0.25, 1.9, 0.4);
demonGroup.add(eye1);
const eye2 = new THREE.Mesh(eyeGeometry, eyeMaterial);
eye2.position.set(0.25, 1.9, 0.4);
demonGroup.add(eye2);
// Arms
const armGeometry = new THREE.CylinderGeometry(0.15, 0.15, 1.2, 8);
const armMaterial = new THREE.MeshStandardMaterial({ color: 0x770000 });
const arm1 = new THREE.Mesh(armGeometry, armMaterial);
arm1.position.set(-0.9, 1, 0);
arm1.rotation.z = Math.PI / 4;
arm1.castShadow = true;
demonGroup.add(arm1);
const arm2 = new THREE.Mesh(armGeometry, armMaterial);
arm2.position.set(0.9, 1, 0);
arm2.rotation.z = -Math.PI / 4;
arm2.castShadow = true;
demonGroup.add(arm2);
// Legs
const legGeometry = new THREE.CylinderGeometry(0.2, 0.2, 1, 8);
const legMaterial = new THREE.MeshStandardMaterial({ color: 0x660000 });
const leg1 = new THREE.Mesh(legGeometry, legMaterial);
leg1.position.set(-0.4, 0.3, 0);
leg1.castShadow = true;
demonGroup.add(leg1);
const leg2 = new THREE.Mesh(legGeometry, legMaterial);
leg2.position.set(0.4, 0.3, 0);
leg2.castShadow = true;
demonGroup.add(leg2);
return demonGroup;
}
function spawnEnemy() {
const demon = createDemon();
demon.position.set(
(Math.random() - 0.5) * 40,
0,
(Math.random() - 0.5) * 40
);
demon.rotation.y = Math.random() * Math.PI * 2;
scene.add(demon);
enemies.push({
mesh: demon,
health: 100,
speed: 0.02 + Math.random() * 0.03,
lastAttack: 0,
attackCooldown: 1000 + Math.random() * 2000
});
}
function startGame() {
document.getElementById('startScreen').style.display = 'none';
if (!isMobile) {
controls.lock();
}
gameActive = true;
// Spawn initial enemies
for (let i = 0; i < 5; i++) {
spawnEnemy();
}
}
function restartGame() {
document.getElementById('gameOverScreen').style.display = 'none';
playerHealth = 100;
ammo = 30;
score = 0;
updateUI();
// Remove all enemies
enemies.forEach(enemy => {
scene.remove(enemy.mesh);
});
enemies = [];
// Remove all bullets
bullets.forEach(bullet => {
scene.remove(bullet.mesh);
});
bullets = [];
// Reset player position
player.position.set(0, 1.6, 0);
// Spawn initial enemies
for (let i = 0; i < 5; i++) {
spawnEnemy();
}
if (!isMobile) {
controls.lock();
}
gameActive = true;
}
function gameOver() {
gameActive = false;
if (!isMobile) {
controls.unlock();
}
document.getElementById('finalScore').textContent = `SCORE: ${score}`;
document.getElementById('gameOverScreen').style.display = 'flex';
}
function updateUI() {
document.getElementById('healthFill').style.width = `${playerHealth}%`;
document.getElementById('ammoCounter').textContent = `AMMO: ${ammo}`;
document.getElementById('score').textContent = `SCORE: ${score}`;
}
function onKeyDown(event) {
switch (event.code) {
case 'KeyW': moveForward = true; break;
case 'KeyS': moveBackward = true; break;
case 'KeyA': moveLeft = true; break;
case 'KeyD': moveRight = true; break;
}
}
function onKeyUp(event) {
switch (event.code) {
case 'KeyW': moveForward = false; break;
case 'KeyS': moveBackward = false; break;
case 'KeyA': moveLeft = false; break;
case 'KeyD': moveRight = false; break;
}
}
function onMouseDown() {
if (!gameActive) return;
if (ammo > 0 && canShoot) {
shoot();
canShoot = false;
setTimeout(() => { canShoot = true; }, shotCooldown);
}
}
function onMouseMove(event) {
if (!gameActive || isMobile) return;
// Add weapon sway effect
const weapon = document.getElementById('weapon');
const moveX = (event.movementX || 0) * 0.05;
const moveY = (event.movementY || 0) * 0.05;
weapon.style.transform = `translateX(calc(-50% + ${moveX}px)) translateY(${moveY}px)`;
}
function shoot() {
ammo--;
updateUI();
// Create bullet
const bulletGeometry = new THREE.SphereGeometry(0.1, 8, 8);
const bulletMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
// Position bullet at camera
bullet.position.set(0, 0, 0);
camera.getWorldPosition(bullet.position);
// Direction vector
const direction = new THREE.Vector3(0, 0, -1);
direction.applyQuaternion(camera.quaternion);
scene.add(bullet);
bullets.push({
mesh: bullet,
direction: direction,
speed: 1.0,
distance: 0
});
// Weapon recoil effect
const weapon = document.getElementById('weapon');
weapon.style.transform = 'translateX(-50%) translateY(20px)';
setTimeout(() => {
weapon.style.transform = 'translateX(-50%) translateY(0)';
}, 100);
// Muzzle flash effect
const flash = document.createElement('div');
flash.style.position = 'absolute';
flash.style.width = '50px';
flash.style.height = '50px';
flash.style.background = 'radial-gradient(circle, #ffff00, #ff5500, transparent)';
flash.style.borderRadius = '50%';
flash.style.top = '50%';
flash.style.left = '50%';
flash.style.transform = 'translate(-50%, -50%)';
flash.style.pointerEvents = 'none';
flash.style.zIndex = '10';
document.getElementById('ui').appendChild(flash);
setTimeout(() => {
document.getElementById('ui').removeChild(flash);
}, 50);
}
function setupMobileControls() {
const movementPad = document.getElementById('movementPad');
const movementStick = document.getElementById('movementStick');
const shootButton = document.getElementById('shootButton');
// Movement pad touch events
movementPad.addEventListener('touchstart', (e) => {
movementStickActive = true;
updateMovementStick(e.touches[0]);
});
movementPad.addEventListener('touchmove', (e) => {
if (movementStickActive) {
e.preventDefault();
updateMovementStick(e.touches[0]);
}
});
movementPad.addEventListener('touchend', () => {
movementStickActive = false;
movementStickPosition = { x: 0, y: 0 };
movementStick.style.transform = 'translate(0, 0)';
moveForward = false;
moveBackward = false;
moveLeft = false;
moveRight = false;
});
// Shoot button touch events
shootButton.addEventListener('touchstart', (e) => {
e.preventDefault();
if (gameActive && ammo > 0 && canShoot) {
shoot();
canShoot = false;
setTimeout(() => { canShoot = true; }, shotCooldown);
}
});
function updateMovementStick(touch) {
const padRect = movementPad.getBoundingClientRect();
const centerX = padRect.left + padRect.width / 2;
const centerY = padRect.top + padRect.height / 2;
let deltaX = touch.clientX - centerX;
let deltaY = touch.clientY - centerY;
// Limit to pad radius
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > movementStickRadius) {
deltaX = deltaX * movementStickRadius / distance;
deltaY = deltaY * movementStickRadius / distance;
}
movementStickPosition = { x: deltaX, y: deltaY };
movementStick.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
// Update movement flags
moveForward = deltaY < -10;
moveBackward = deltaY > 10;
moveLeft = deltaX < -10;
moveRight = deltaX > 10;
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
if (gameActive) {
// Player movement
const speed = 0.1;
if (isMobile) {
// Mobile movement based on joystick
if (moveForward) player.translateZ(-speed);
if (moveBackward) player.translateZ(speed);
if (moveLeft) player.translateX(-speed);
if (moveRight) player.translateX(speed);
} else {
// Desktop movement
if (moveForward) player.translateZ(-speed);
if (moveBackward) player.translateZ(speed);
if (moveLeft) player.translateX(-speed);
if (moveRight) player.translateX(speed);
}
// Keep player within bounds
player.position.x = Math.max(-24, Math.min(24, player.position.x));
player.position.z = Math.max(-24, Math.min(24, player.position.z));
// Enemy AI
enemies.forEach((enemy, index) => {
// Move towards player
const direction = new THREE.Vector3();
direction.subVectors(player.position, enemy.mesh.position).normalize();
enemy.mesh.position.add(direction.multiplyScalar(enemy.speed));
// Rotate to face player
enemy.mesh.lookAt(player.position);
enemy.mesh.rotation.x = 0; // Keep upright
enemy.mesh.rotation.z = 0;
// Attack player if close
const distance = player.position.distanceTo(enemy.mesh.position);
if (distance < 3 && Date.now() - enemy.lastAttack > enemy.attackCooldown) {
playerHealth -= 10;
updateUI();
enemy.lastAttack = Date.now();
// Show hit effect
const hitEffect = document.getElementById('hitEffect');
hitEffect.style.opacity = '1';
setTimeout(() => {
hitEffect.style.opacity = '0';
}, 100);
// Show blood effect
const bloodEffect = document.getElementById('bloodEffect');
bloodEffect.style.opacity = '0.7';
setTimeout(() => {
bloodEffect.style.opacity = '0';
}, 200);
if (playerHealth <= 0) {
gameOver();
}
}
// Remove dead enemies
if (enemy.health <= 0) {
scene.remove(enemy.mesh);
enemies.splice(index, 1);
score += 100;
updateUI();
// Spawn new enemy
if (Math.random() > 0.7) {
spawnEnemy();
}
}
});
// Update bullets
for (let i = bullets.length - 1; i >= 0; i--) {
const bullet = bullets[i];
bullet.mesh.position.add(bullet.direction.clone().multiplyScalar(bullet.speed));
bullet.distance += bullet.speed;
// Remove bullets that travel too far
if (bullet.distance > 50) {
scene.remove(bullet.mesh);
bullets.splice(i, 1);
continue;
}
// Check for collisions with enemies
for (let j = enemies.length - 1; j >= 0; j--) {
const enemy = enemies[j];
const distance = bullet.mesh.position.distanceTo(enemy.mesh.position);
if (distance < 1.5) {
// Hit enemy
enemy.health -= 25;
scene.remove(bullet.mesh);
bullets.splice(i, 1);
// Enemy hit effect
enemy.mesh.children[0].material.color.set(0xff0000);
setTimeout(() => {
if (enemy.mesh && enemy.mesh.children[0]) {
enemy.mesh.children[0].material.color.set(0x880000);
}
}, 100);
break;
}
}
}
// Spawn enemies periodically
enemySpawnTimer += delta;
if (enemySpawnTimer > 5) {
spawnEnemy();
enemySpawnTimer = 0;
}
}
renderer.render(scene, camera);
}
// Initialize the game when the page loads
window.onload = init;
</script>
</body>
</html> |