Below is a self-contained HTML file that implements a basic first-person game using inline CSS, pure JavaScript, and the gl-matrix library for matrix operations. The game includes WASD movement, mouse-based rotation, collision detection, and procedurally generated textures.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>First-Person Game</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/3.4.2/gl-matrix.min.js"></script>
<script>
const canvas = document.getElementById('canvas');
const gl = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const mat4 = glMatrix.mat4;
const vec3 = glMatrix.vec3;
// Camera settings
const camera = {
position: vec3.fromValues(3, 0, 3),
rotation: vec3.fromValues(0, 0, 0),
fov: Math.PI / 3,
speed: 0.1,
sensitivity: 0.002
};
// Map: 6x6 meters, walls are 1 meter high
const map = [
[1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1]
];
// Procedural texture generation
function generateTexture(width, height) {
const texture = new Uint8ClampedArray(width * height * 4);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
const c = Math.sin(x * 0.1) * Math.cos(y * 0.1) * 128 + 128;
texture[idx] = c;
texture[idx + 1] = c;
texture[idx + 2] = c;
texture[idx + 3] = 255;
}
}
return new ImageData(texture, width, height);
}
const wallTexture = generateTexture(64, 64);
const floorTexture = generateTexture(32, 32);
const ceilingTexture = generateTexture(32, 32);
// Input handling
let keys = {};
let mouseLocked = false;
let lastMouseX = 0;
document.addEventListener('keydown', (e) => keys[e.key] = true);
document.addEventListener('keyup', (e) => keys[e.key] = false);
document.addEventListener('mousemove', (e) => {
if (mouseLocked) {
camera.rotation[1] -= e.movementX * camera.sensitivity;
camera.rotation[0] -= e.movementY * camera.sensitivity;
camera.rotation[0] = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, camera.rotation[0]));
}
});
document.addEventListener('mousedown', () => {
if (!mouseLocked) {
document.body.requestPointerLock();
mouseLocked = true;
}
});
document.addEventListener('mouseup', () => {
document.exitPointerLock();
mouseLocked = false;
});
// Collision detection
function collide(x, y) {
if (x < 0 || x >= map.length || y < 0 || y >= map[0].length) return true;
return map[Math.floor(x)][Math.floor(y)] === 1;
}
// Game loop
function update() {
const forward = vec3.fromValues(
Math.sin(camera.rotation[1]),
0,
Math.cos(camera.rotation[1])
);
const right = vec3.fromValues(
Math.sin(camera.rotation[1] - Math.PI / 2),
0,
Math.cos(camera.rotation[1] - Math.PI / 2)
);
if (keys['w']) {
if (!collide(camera.position[0] + forward[0] * camera.speed, camera.position[2] + forward[2] * camera.speed)) {
camera.position[0] += forward[0] * camera.speed;
camera.position[2] += forward[2] * camera.speed;
}
}
if (keys['s']) {
if (!collide(camera.position[0] - forward[0] * camera.speed, camera.position[2] - forward[2] * camera.speed)) {
camera.position[0] -= forward[0] * camera.speed;
camera.position[2] -= forward[2] * camera.speed;
}
}
if (keys['a']) {
if (!collide(camera.position[0] - right[0] * camera.speed, camera.position[2] - right[2] * camera.speed)) {
camera.position[0] -= right[0] * camera.speed;
camera.position[2] -= right[2] * camera.speed;
}
}
if (keys['d']) {
if (!collide(camera.position[0] + right[0] * camera.speed, camera.position[2] + right[2] * camera.speed)) {
camera.position[0] += right[0] * camera.speed;
camera.position[2] += right[2] * camera.speed;
}
}
}
// Rendering
function render() {
gl.clearRect(0, 0, canvas.width, canvas.height);
const halfWidth = canvas.width / 2;
const halfHeight = canvas.height / 2;
const numRays = canvas.width;
const fovHalf = camera.fov / 2;
for (let x = 0; x < numRays; x++) {
const rayAngle = camera.rotation[1] - fovHalf + (x / canvas.width) * camera.fov;
const rayDir = vec3.fromValues(Math.sin(rayAngle), 0, Math.cos(rayAngle));
let distance = 0;
let hit = false;
let hitSide = 0;
while (!hit && distance < 10) {
distance += 0.1;
const testX = camera.position[0] + rayDir[0] * distance;
const testY = camera.position[2] + rayDir[2] * distance;
if (collide(testX, testY)) {
hit = true;
const deltaX = testX - Math.floor(testX);
const deltaY = testY - Math.floor(testY);
hitSide = (deltaX > deltaY) ? (rayDir[0] < 0 ? 3 : 2) : (rayDir[2] < 0 ? 1 : 0);
}
}
const lineHeight = (distance === 0) ? 0 : (canvas.height / distance);
const lineOffset = halfHeight - lineHeight / 2;
if (hit) {
gl.fillStyle = 'gray';
gl.fillRect(x, lineOffset, 1, lineHeight);
}
}
}
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>
This implementation provides a basic first-person game experience with minimal dependencies and purely mathematical texture generation.