Spaces:
Running
Running
File size: 7,939 Bytes
1390db3 fe3647a 1390db3 874a51f 1390db3 118fa8d 1390db3 fd12cd0 1390db3 09a3a37 1390db3 bf6239d 1390db3 d6dfcf1 1390db3 874a51f 1390db3 09a3a37 1390db3 bf6239d 1390db3 d6dfcf1 1390db3 fe3647a 874a51f 1390db3 5cf582c 1390db3 a297171 1f43352 1390db3 5cf582c a646668 1390db3 118fa8d 1390db3 118fa8d 1390db3 fe3647a 1390db3 874a51f 1390db3 fd12cd0 09a3a37 9b606b6 09a3a37 fd12cd0 1390db3 |
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 |
import * as THREE from 'three';
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
import { CFG } from './config.js';
import { G } from './globals.js';
import { makeRandom } from './utils.js';
import { setupLights } from './lighting.js';
import { setupGround, generateForest, generateGroundCover, getTerrainHeight, tickForest } from './world.js';
import { setupWeapon, updateWeaponAnchor, beginReload, updateWeapon } from './weapon.js';
import { setupEvents } from './events.js';
import { updatePlayer } from './player.js';
import { updateEnemies } from './enemies.js';
import { startNextWave, updateWaves } from './waves.js';
import { updateHUD, showOverlay, updateDamageEffect, updateHealEffect, updateCrosshair, updateHitMarker } from './hud.js';
import { updateFX } from './fx.js';
import { updateCasings } from './casings.js';
import { updateEnemyProjectiles } from './projectiles.js';
import { updateGrenades } from './grenades.js';
import { updateDayNight } from './daynight.js';
import { performShooting } from './combat.js';
import { updatePickups } from './pickups.js';
import { updateHelmets } from './helmets.js';
import { setupClouds, updateClouds } from './clouds.js';
import { startMusic, stopMusic } from './audio.js';
import { setupMountains, updateMountains } from './mountains.js';
init();
animate();
function init() {
// Renderer
G.renderer = new THREE.WebGLRenderer({ antialias: true });
G.renderer.setSize(window.innerWidth, window.innerHeight);
// Lower pixel ratio cap for significant fill-rate savings
G.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.0));
G.renderer.shadowMap.enabled = true;
// Use the cheapest shadow filter for CPU savings
G.renderer.shadowMap.type = THREE.BasicShadowMap;
document.body.appendChild(G.renderer.domElement);
// Scene
G.scene = new THREE.Scene();
G.scene.background = new THREE.Color(0x0a1015);
G.scene.fog = new THREE.FogExp2(0x05070a, CFG.fogDensity);
// Camera
G.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Controls
G.controls = new PointerLockControls(G.camera, document.body);
// Clock
G.clock = new THREE.Clock();
// Seeded random
G.random = makeRandom(CFG.seed);
// Player
const startY = getTerrainHeight(0, 0) + (CFG.player.eyeHeight || 1.8);
G.player = {
pos: new THREE.Vector3(0, startY, 0),
vel: new THREE.Vector3(),
speed: CFG.player.speed,
radius: CFG.player.radius,
health: CFG.player.health,
alive: true,
score: 0,
yVel: 0,
grounded: true,
// Apex-like movement state
sliding: false,
jumpBuffer: 0,
coyoteTimer: 0,
lastWallNormal: new THREE.Vector3(),
wallContactTimer: 0
, jumpHeld: false
};
G.weapon.ammo = CFG.gun.magSize;
G.weapon.reserve = Infinity;
G.grenadeCount = 0;
// Add camera to scene
G.camera.position.copy(G.player.pos);
// Lights
setupLights();
// Ground and world
setupGround();
// Ground cover before or after trees — independent
generateGroundCover();
generateForest();
setupClouds();
setupMountains();
// Weapon
setupWeapon();
updateWeaponAnchor();
// Events
setupEvents({
startGame,
restartGame,
beginReload,
updateWeaponAnchor
});
// Pre-warm shaders to avoid first-frame hitches
if (G.renderer && G.scene && G.camera) {
try { G.renderer.compile(G.scene, G.camera); } catch (_) {}
}
}
function startGame() {
G.state = 'playing';
G.player.health = CFG.player.health;
G.player.score = 0;
G.player.alive = true;
G.player.pos.set(0, getTerrainHeight(0, 0) + (CFG.player.eyeHeight || 1.8), 0);
G.player.vel.set(0, 0, 0);
G.player.yVel = 0;
G.player.grounded = true;
G.player.sliding = false;
G.player.jumpBuffer = 0;
G.player.coyoteTimer = 0;
G.player.lastWallNormal.set(0, 0, 0);
G.player.wallContactTimer = 0;
G.camera.position.copy(G.player.pos);
G.damageFlash = 0;
G.healFlash = 0;
G.hitFlash = 0;
// Grenades reset
G.grenades.length = 0;
G.heldGrenade = null;
G.grenadeCount = 0;
// Clear enemies
for (const enemy of G.enemies) {
G.scene.remove(enemy.mesh);
}
G.enemies.length = 0;
// Clear enemy projectiles
for (const p of G.enemyProjectiles) {
G.scene.remove(p.mesh);
}
G.enemyProjectiles.length = 0;
// Clear pickups/orbs
for (const o of G.orbs) {
G.scene.remove(o.mesh);
}
G.orbs.length = 0;
// Clear wave powerups
for (const p of G.powerups || []) {
if (p && p.mesh) G.scene.remove(p.mesh);
}
G.powerups.length = 0;
// Clear any detached helmets
for (const h of G.helmets) {
G.scene.remove(h.mesh);
}
G.helmets.length = 0;
// Clear ejected casings
for (const c of G.casings) {
G.scene.remove(c.mesh);
}
G.casings.length = 0;
// Reset waves
G.waves.current = 1;
G.waves.aliveCount = 0;
G.waves.spawnQueue = 0;
G.waves.nextSpawnTimer = 0;
G.waves.breakTimer = 0;
G.waves.inBreak = false;
G.waves.wolvesToSpawn = 0;
G.waves.shamansToSpawn = 0;
G.waves.golemsToSpawn = 0;
// Reset weapon
G.weapon.ammo = CFG.gun.magSize;
G.weapon.reloading = false;
G.weapon.reloadTimer = 0;
G.weapon.recoil = 0;
G.weapon.spread = CFG.gun.spreadMin ?? CFG.gun.bloom ?? 0;
G.weapon.targetSpread = G.weapon.spread;
G.weapon.viewPitch = 0;
G.weapon.viewYaw = 0;
G.weapon.appliedPitch = 0;
G.weapon.appliedYaw = 0;
G.weapon.rofMult = 1;
G.weapon.rofBuffTimer = 0;
G.weapon.rofBuffTotal = 0;
G.movementMult = 1;
G.movementBuffTimer = 0;
G.weapon.infiniteAmmoTimer = 0;
G.weapon.infiniteAmmoTotal = 0;
G.weapon.ammoBeforeInf = null;
G.weapon.reserveBeforeInf = null;
const overlay = document.getElementById('overlay');
if (overlay) overlay.classList.add('hidden');
updateHUD();
// Initialize day/night visuals immediately
updateDayNight(0);
startNextWave();
// Start background music (once audio is unlocked)
startMusic();
}
function restartGame() {
G.controls.lock();
}
function gameOver() {
G.state = 'gameover';
G.controls.unlock();
showOverlay('gameover');
// Gracefully fade out music
stopMusic(0.6);
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(G.clock.getDelta(), 0.1);
if (G.state === 'playing') {
updatePlayer(delta);
updateEnemies(delta, gameOver);
updateEnemyProjectiles(delta, gameOver);
updateWaves(delta);
performShooting(delta);
updateWeapon(delta);
updatePickups(delta);
updateHUD();
updateCrosshair(delta);
updateHitMarker(delta);
updateDamageEffect(delta);
updateHealEffect(delta);
}
updateDayNight(delta);
updateFX(delta);
updateHelmets(delta);
updateCasings(delta);
// Update grenades and previews last among gameplay
if (G.state === 'playing') {
updateGrenades(delta);
if (!G.player.alive) {
gameOver();
}
}
updateClouds(delta);
updateMountains(delta);
// Update subtle foliage wind sway using elapsedTime (avoid double-advancing clock)
tickForest(G.clock.elapsedTime);
G.renderer.render(G.scene, G.camera);
// Lightweight FPS meter (updates ~2x/sec)
if (!G._fpsAccum) { G._fpsAccum = 0; G._fpsFrames = 0; G._fpsNext = 0.5; }
G._fpsAccum += delta; G._fpsFrames++;
if (G._fpsAccum >= G._fpsNext) {
const fps = Math.round(G._fpsFrames / G._fpsAccum);
const el = document.getElementById('fps');
if (el) {
el.textContent = String(fps);
}
G._fpsAccum = 0; G._fpsFrames = 0;
}
// Process a small budget of deferred disposals to avoid spikes
if (G.disposeQueue && G.disposeQueue.length) {
const budget = 24; // dispose up to N geometries per frame
for (let i = 0; i < budget && G.disposeQueue.length; i++) {
const geom = G.disposeQueue.pop();
if (geom && geom.dispose) {
try { geom.dispose(); } catch (e) { /* noop */ }
}
}
}
}
|