Spaces:
Running
Running
Download src/main.js from Afredbilli/Cilo: direct link, hf CLI and curl.
- Browser
- Download file 8.06 kB
-
https://huggingface.co/spaces/Afredbilli/Cilo/resolve/main/src/main.js
- Command line
-
hf download hf://spaces/Afredbilli/Cilo/src/main.js
-
curl -L -o main.js https://huggingface.co/spaces/Afredbilli/Cilo/resolve/main/src/main.js
8.06 kB
| import * as THREE from "three"; | |
| import { createWorld } from "./world.js"; | |
| import { createCamera } from "./camera.js"; | |
| import { createCharacter } from "./character.js"; | |
| import { createControls } from "./controls.js"; | |
| // ============================================= | |
| // DOM ELEMENTS | |
| // ============================================= | |
| const canvas = document.getElementById("game-canvas"); | |
| const loadingScreen = document.getElementById("loading-screen"); | |
| const loadingProgress = document.getElementById("loading-progress"); | |
| const statusIndicator = document.getElementById("status-indicator"); | |
| const statusText = document.getElementById("status-text"); | |
| const combatMessage = document.getElementById("combat-message"); | |
| // ============================================= | |
| // GAME STATE | |
| // ============================================= | |
| const game = { | |
| scene: null, | |
| renderer: null, | |
| camera: null, | |
| world: null, | |
| character: null, | |
| controls: null, | |
| clock: new THREE.Clock(), | |
| running: false, | |
| disposed: false, | |
| delta: 0, | |
| elapsed: 0, | |
| showDebug: false | |
| }; | |
| // ============================================= | |
| // LOADING UI | |
| // ============================================= | |
| function setProgress(percent) { | |
| loadingProgress.style.width = | |
| `${THREE.MathUtils.clamp(percent, 0, 100)}%`; | |
| } | |
| function setStatus(message, state = "loading") { | |
| statusText.textContent = message; | |
| statusIndicator.classList.remove("ready", "error"); | |
| if (state === "ready") { | |
| statusIndicator.classList.add("ready"); | |
| } | |
| if (state === "error") { | |
| statusIndicator.classList.add("error"); | |
| } | |
| } | |
| function hideLoadingScreen() { | |
| loadingScreen.classList.add("hidden"); | |
| } | |
| let combatMessageTimer; | |
| function showCombatMessage(message, duration = 1400) { | |
| combatMessage.textContent = message; | |
| combatMessage.classList.add("visible"); | |
| clearTimeout(combatMessageTimer); | |
| combatMessageTimer = setTimeout(() => { | |
| combatMessage.classList.remove("visible"); | |
| }, duration); | |
| } | |
| // ============================================= | |
| // THREE.JS INITIALIZATION | |
| // ============================================= | |
| function createRenderer() { | |
| const renderer = new THREE.WebGLRenderer({ | |
| canvas, | |
| antialias: true, | |
| alpha: false, | |
| powerPreference: "high-performance" | |
| }); | |
| renderer.setPixelRatio( | |
| Math.min(window.devicePixelRatio || 1, 1.75) | |
| ); | |
| renderer.setSize(window.innerWidth, window.innerHeight); | |
| renderer.outputColorSpace = THREE.SRGBColorSpace; | |
| renderer.toneMapping = THREE.ACESFilmicToneMapping; | |
| renderer.toneMappingExposure = 1.15; | |
| renderer.shadowMap.enabled = true; | |
| renderer.shadowMap.type = THREE.PCFSoftShadowMap; | |
| return renderer; | |
| } | |
| function createScene() { | |
| const scene = new THREE.Scene(); | |
| scene.background = new THREE.Color(0xaaa28b); | |
| scene.fog = new THREE.FogExp2( | |
| 0xaaa28b, | |
| 0.009 | |
| ); | |
| return scene; | |
| } | |
| // ============================================= | |
| // LIGHTING | |
| // ============================================= | |
| function createLighting(scene) { | |
| const ambient = new THREE.HemisphereLight( | |
| 0xdce4ed, | |
| 0x65523a, | |
| 2.0 | |
| ); | |
| scene.add(ambient); | |
| const sunlight = new THREE.DirectionalLight( | |
| 0xffe0ad, | |
| 3.0 | |
| ); | |
| sunlight.position.set(-12, 20, 8); | |
| sunlight.castShadow = true; | |
| sunlight.shadow.mapSize.set(1024, 1024); | |
| sunlight.shadow.camera.left = -20; | |
| sunlight.shadow.camera.right = 20; | |
| sunlight.shadow.camera.top = 20; | |
| sunlight.shadow.camera.bottom = -20; | |
| sunlight.shadow.bias = -0.0003; | |
| scene.add(sunlight); | |
| return { ambient, sunlight }; | |
| } | |
| // ============================================= | |
| // GAME INITIALIZATION | |
| // ============================================= | |
| async function initializeGame() { | |
| try { | |
| setProgress(5); | |
| setStatus("INITIALIZING"); | |
| game.renderer = createRenderer(); | |
| game.scene = createScene(); | |
| createLighting(game.scene); | |
| setProgress(15); | |
| // Create terrain and environmental scenery. | |
| game.world = createWorld(game.scene); | |
| setProgress(35); | |
| // Create the third-person camera. | |
| game.camera = createCamera( | |
| game.scene, | |
| game.renderer | |
| ); | |
| setProgress(50); | |
| // Load the player character. | |
| game.character = await createCharacter( | |
| game.scene | |
| ); | |
| setProgress(75); | |
| // Initialize keyboard and touch controls. | |
| game.controls = createControls(game); | |
| setProgress(90); | |
| // Wait for the browser to paint the completed scene. | |
| await new Promise(resolve => requestAnimationFrame(resolve)); | |
| setProgress(100); | |
| game.running = true; | |
| setStatus("READY", "ready"); | |
| hideLoadingScreen(); | |
| showCombatMessage( | |
| "Welcome to the battlefield", | |
| 2200 | |
| ); | |
| game.clock.start(); | |
| requestAnimationFrame(gameLoop); | |
| } catch (error) { | |
| console.error("Game initialization failed:", error); | |
| setStatus("INITIALIZATION FAILED", "error"); | |
| loadingScreen.classList.remove("hidden"); | |
| const description = document.createElement("p"); | |
| description.style.marginTop = "18px"; | |
| description.style.color = "#e5a093"; | |
| description.style.lineHeight = "1.6"; | |
| description.textContent = | |
| error instanceof Error | |
| ? error.message | |
| : "An unknown error occurred."; | |
| document.querySelector(".loading-content") | |
| .appendChild(description); | |
| } | |
| } | |
| // ============================================= | |
| // MAIN GAME LOOP | |
| // ============================================= | |
| function gameLoop() { | |
| if (!game.running || game.disposed) { | |
| return; | |
| } | |
| requestAnimationFrame(gameLoop); | |
| // Prevent large simulation jumps after a stalled frame. | |
| game.delta = Math.min(game.clock.getDelta(), 0.05); | |
| game.elapsed += game.delta; | |
| // Process keyboard and mobile input. | |
| game.controls?.update(game.delta); | |
| // Update character animation and movement. | |
| game.character?.update(game.delta); | |
| // Update the third-person camera. | |
| game.camera?.update( | |
| game.delta, | |
| game.character | |
| ); | |
| // Update world objects. | |
| game.world?.update?.(game.delta); | |
| // Draw the scene. | |
| game.renderer.render( | |
| game.scene, | |
| game.camera.camera | |
| ); | |
| } | |
| // ============================================= | |
| // RESPONSIVE RENDERING | |
| // ============================================= | |
| function handleResize() { | |
| if (!game.renderer || !game.camera) { | |
| return; | |
| } | |
| game.camera.resize( | |
| window.innerWidth, | |
| window.innerHeight | |
| ); | |
| game.renderer.setSize( | |
| window.innerWidth, | |
| window.innerHeight | |
| ); | |
| game.renderer.setPixelRatio( | |
| Math.min(window.devicePixelRatio || 1, 1.75) | |
| ); | |
| } | |
| window.addEventListener("resize", handleResize); | |
| // ============================================= | |
| // CLEANUP | |
| // ============================================= | |
| function disposeGame() { | |
| game.disposed = true; | |
| game.running = false; | |
| clearTimeout(combatMessageTimer); | |
| window.removeEventListener("resize", handleResize); | |
| game.controls?.dispose?.(); | |
| game.world?.dispose?.(); | |
| game.character?.dispose?.(); | |
| game.renderer?.dispose(); | |
| game.scene?.traverse(object => { | |
| if (!object.isMesh) { | |
| return; | |
| } | |
| object.geometry?.dispose(); | |
| const materials = Array.isArray(object.material) | |
| ? object.material | |
| : [object.material]; | |
| materials.forEach(material => { | |
| material?.dispose(); | |
| }); | |
| }); | |
| } | |
| window.addEventListener("pagehide", disposeGame, { | |
| once: true | |
| }); | |
| // ============================================= | |
| // START | |
| // ============================================= | |
| initializeGame(); |