Da się 🙂 – w przeglądarce, w czystym pliku HTML z dosłownie kilkudziesięcioma liniami kodu (plus biblioteka Three.js) możesz mieć działającą, „namiastkową” strzelankę 3D z widokiem FPP, chodzeniem klawiszami WASD i „zestrzeliwaniem” sześcianów kliknięciem myszy.
Poniżej kompletna, samowystarczalna strona. Skopiuj-wklej do pliku index.html, odpal w przeglądarce (najlepiej Chrome / Firefox) i już możesz biegać i strzelać.
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Mini FPS – Three.js Demo</title>
<style>
html,body{margin:0;height:100%;overflow:hidden;background:#000;}
#info{position:fixed;top:10px;left:50%;transform:translateX(-50%);
color:#fff;font-family:sans-serif;font-size:14px}
#crosshair{
position:fixed;top:50%;left:50%;width:10px;height:10px;
margin:-5px 0 0 -5px;border:2px solid #fff;border-radius:50%;
pointer-events:none;opacity:.7
}
</style>
</head>
<body>
<div id="info">Kliknij, by przejąć mysz – chodź WASD, podskok SPACJA, strzał LPM</div>
<div id="crosshair"></div>
<!-- Three.js z CDN -->
<script src="https://cdn.jsdelivr.net/npm/three@0.155/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.155/examples/js/controls/PointerLockControls.js"></script>
<script>
// --- Scena, kamera, renderer ---
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x121212);
const camera = new THREE.PerspectiveCamera(75, innerWidth/innerHeight, 1, 1000);
const renderer= new THREE.WebGLRenderer({antialias:true});
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
// --- Podłoga ---
const floorGeo = new THREE.PlaneGeometry(200,200);
const floorMat = new THREE.MeshPhongMaterial({color:0x555555});
const floor = new THREE.Mesh(floorGeo,floorMat);
floor.rotation.x = -Math.PI/2;
floor.receiveShadow = true;
scene.add(floor);
// --- Światło ---
const light = new THREE.DirectionalLight(0xffffff,1);
light.position.set(10,20,10);
scene.add(light);
scene.add(new THREE.AmbientLight(0x404040));
// --- Losowe cele (kostki) ---
const cubes = [];
const cubeGeo = new THREE.BoxGeometry(2,2,2);
const cubeMat = new THREE.MeshLambertMaterial({color:0xff4444});
for (let i=0;i<30;i++){
const c = new THREE.Mesh(cubeGeo,cubeMat.clone());
c.position.set(
(Math.random()-0.5)*100,
1,
(Math.random()-0.5)*100
);
scene.add(c);
cubes.push(c);
}
// --- Kontroler FPP z blokadą kursora ---
const controls = new THREE.PointerLockControls(camera, document.body);
document.addEventListener('click', () => controls.lock());
scene.add(controls.getObject());
// Ruch gracza
const velocity = new THREE.Vector3();
const direction= new THREE.Vector3();
const key = {};
addEventListener('keydown',e=>key[e.code]=true);
addEventListener('keyup' ,e=>key[e.code]=false);
// --- Strzał ---
window.addEventListener('mousedown', shoot);
const raycaster = new THREE.Raycaster();
function shoot(e){
if(e.button!==0) return; // tylko LPM
raycaster.set(
camera.getWorldPosition(new THREE.Vector3()),
camera.getWorldDirection(new THREE.Vector3())
);
const hit = raycaster.intersectObjects(cubes,false)[0];
if(hit){
hit.object.material.color.set(0x00ff00); // trafienie -> zielony
scene.remove(hit.object); // usuń z gry
}
}
// --- Pętla gry ---
const clock = new THREE.Clock();
function animate(){
requestAnimationFrame(animate);
const dt = Math.min(0.05, clock.getDelta()); // max 50 ms żeby fizyka nie "wybuchła"
// ruch
velocity.x -= velocity.x * 10.0 * dt;
velocity.z -= velocity.z * 10.0 * dt;
direction.set(
(key['KeyD']?1:0) - (key['KeyA']?1:0),
0,
(key['KeyS']?1:0) - (key['KeyW']?1:0)
).normalize();
const speed = 100;
if(key['Space']) velocity.y = 15; // prosty "skok"
velocity.x += direction.x * speed * dt;
velocity.z += direction.z * speed * dt;
controls.moveRight (-velocity.x * dt);
controls.moveForward(-velocity.z * dt);
controls.getObject().position.y += velocity.y*dt;
if(controls.getObject().position.y < 2){ // prosta grawitacja
velocity.y = 0; controls.getObject().position.y = 2;
}
renderer.render(scene,camera);
}
animate();
// --- Reakcja na zmianę wielkości okna ---
addEventListener('resize', ()=>{
camera.aspect = innerWidth/innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>