<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>THE DARKNESS</title>
<style>
body {
margin: 0;
overflow: hidden;
background: black;
font-family: Arial;
color: white;
}
#intro {
position: absolute;
inset: 0;
background: #050505;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 5;
}
h1 {
color: darkred;
font-size: 55px;
letter-spacing: 8px;
}
button {
background: darkred;
color: white;
padding: 15px 35px;
border: none;
cursor: pointer;
font-size: 20px;
}
#hud {
position: absolute;
top: 20px;
left: 20px;
}
</style>
</head>
<body>
 
<div id="intro">
<h1>THE DARKNESS</h1>
<p>Sei solo. O almeno così pensi...</p>
<p>Trova la chiave e scappa.</p>
<button onclick="startGame()">GIOCA</button>
</div>
 
<div id="hud">🔦 Torcia: ON</div>
 
<script type="module">
import * as THREE from
'https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js';
 
import { PointerLockControls } from
'https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/controls/PointerLockControls.js';
 
let scene, camera, renderer, controls;
let flashlight, monster;
let keyFound = false;
let flashlightOn = true;
let gameStarted = false;
 
function startGame() {
document.getElementById("intro").style.display = "none";
init();
}
 
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x020202);
scene.fog = new THREE.Fog(0x020202, 2, 20);
 
camera = new THREE.PerspectiveCamera(
75, innerWidth / innerHeight, 0.1, 100
);
camera.position.set(0, 1.6, 5);
 
renderer = new THREE.WebGLRenderer();
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
 
controls = new PointerLockControls(camera, document.body);
document.body.addEventListener("click", () => {
if (!controls.isLocked) controls.lock();
});
 
scene.add(new THREE.AmbientLight(0x222222));
 
flashlight = new THREE.SpotLight(
0xffffff, 30, 18, Math.PI / 7, 0.5
);
camera.add(flashlight);
flashlight.position.set(0, 0, 0);
flashlight.target.position.set(0, 0, -1);
camera.add(flashlight.target);
scene.add(camera);
 
// Pavimento
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({color: 0x292929})
);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
 
// Pareti
function wall(x,y,z,w,h,d) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w,h,d),
new THREE.MeshStandardMaterial({color: 0x303030})
);
mesh.position.set(x,y,z);
scene.add(mesh);
}
 
wall(0,2,-10,20,4,0.5);
wall(-10,2,0,0.5,4,20);
wall(10,2,0,0.5,4,20);
wall(0,2,10,20,4,0.5);
 
// Mostro
monster = new THREE.Group();
 
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.8,1.8,0.5),
new THREE.MeshStandardMaterial({color:0x050505})
);
body.position.y = 1;
monster.add(body);
 
const eyes = new THREE.Mesh(
new THREE.BoxGeometry(0.4,0.12,0.1),
new THREE.MeshBasicMaterial({color:0xff0000})
);
eyes.position.set(0,1.55,-0.27);
monster.add(eyes);
 
monster.position.set(0,0,-8);
scene.add(monster);
 
// Chiave
const key = new THREE.Mesh(
new THREE.TorusGeometry(0.2,0.05,8,20),
new THREE.MeshStandardMaterial({color:0xffff00})
);
key.position.set(3,0.5,-4);
scene.add(key);
 
window.addEventListener("keydown", e => {
if(e.key.toLowerCase() === "f") {
flashlightOn = !flashlightOn;
flashlight.visible = flashlightOn;
document.getElementById("hud").innerText =
"🔦 Torcia: " + (flashlightOn ? "ON" : "OFF");
}
 
if(e.key.toLowerCase() === "e" &&
camera.position.distanceTo(key.position) < 2) {
keyFound = true;
scene.remove(key);
document.getElementById("hud").innerText =
"🔑 Chiave trovata! Cerca l'uscita!";
}
});
 
gameStarted = true;
animate();
}
 
const velocity = new THREE.Vector3();
const direction = new THREE.Vector3();
const keys = {};
 
window.addEventListener("keydown", e => keys[e.key.toLowerCase()] = true);
window.addEventListener("keyup", e => keys[e.key.toLowerCase()] = false);
 
function animate() {
requestAnimationFrame(animate);
 
if(!gameStarted) return;
 
if(controls.isLocked) {
direction.set(0,0,0);
if(keys.w) direction.z -= 1;
if(keys.s) direction.z += 1;
if(keys.a) direction.x -= 1;
if(keys.d) direction.x += 1;
 
direction.normalize();
controls.moveForward(-direction.z * 0.07);
controls.moveRight(direction.x * 0.07);
 
// Il mostro ti segue
const target = camera.position.clone();
target.y = 0;
monster.position.lerp(target, 0.0015);
monster.lookAt(camera.position.x, 1, camera.position.z);
 
if(monster.position.distanceTo(camera.position) < 1) {
document.getElementById("intro").style.display = "flex";
document.querySelector("#intro h1").innerText = "TI HA TROVATO";
document.querySelector("#intro p").innerText = "GAME OVER";
gameStarted = false;
controls.unlock();
}
}
 
renderer.render(scene,camera);
}
</script>
</body>
</html>