FROSTEGG's picture
Теперь сделай DOOM лучше, поправь управление.
554e4cd verified
Raw History Blame Contribute Delete
15.3 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOOM Clone</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
}
canvas {
display: block;
}
#ui {
position: absolute;
bottom: 20px;
left: 20px;
color: white;
font-family: 'Courier New', monospace;
text-shadow: 2px 2px 4px #000;
}
#health, #ammo, #weapon {
margin-bottom: 10px;
}
#start-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.8);
color: #ff0000;
font-family: 'Courier New', monospace;
z-index: 100;
}
#start-screen h1 {
font-size: 4rem;
margin-bottom: 2rem;
text-shadow: 0 0 10px #ff0000;
}
#start-screen button {
padding: 1rem 2rem;
background: #ff0000;
color: white;
border: none;
font-size: 1.5rem;
cursor: pointer;
transition: all 0.3s;
}
#start-screen button:hover {
background: #cc0000;
transform: scale(1.05);
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="ui">
<div id="health">HEALTH: 100%</div>
<div id="ammo">AMMO: 50</div>
<div id="weapon">WEAPON: PISTOL</div>
</div>
<div id="start-screen">
<h1>DOOM CLONE</h1>
<p>WASD to move | SHIFT to run | Mouse to look | Left click to shoot</p>
<button id="start-btn">START GAME</button>
</div>
<script>
// Game canvas setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Game state
let gameRunning = false;
let player = {
x: 0,
y: 0,
angle: 0,
health: 100,
ammo: 50,
weapon: 'PISTOL',
speed: 0.15,
turnSpeed: 0.05
};
// Map (1 = wall, 0 = empty space)
const map = [
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
];
const mapSize = 8;
const cellSize = 64;
// Enemies
let enemies = [
{ x: 300, y: 300, type: 'IMP', health: 30 },
{ x: 100, y: 400, type: 'ZOMBIE', health: 20 }
];
// Key states
const keys = {
w: false,
a: false,
s: false,
d: false,
' ': false,
shift: false
};
// Mouse control
let mouseMovementX = 0;
let mouseMovementY = 0;
let isPointerLocked = false;
// Event listeners
document.getElementById('start-btn').addEventListener('click', startGame);
document.addEventListener('keydown', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = true;
}
});
document.addEventListener('keyup', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = false;
}
});
// Improved mouse controls with pointer lock
canvas.addEventListener('click', () => {
if (!isPointerLocked) {
canvas.requestPointerLock = canvas.requestPointerLock ||
canvas.mozRequestPointerLock ||
canvas.webkitRequestPointerLock;
canvas.requestPointerLock();
}
});
document.addEventListener('pointerlockchange', lockChangeAlert, false);
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);
document.addEventListener('webkitpointerlockchange', lockChangeAlert, false);
function lockChangeAlert() {
if (document.pointerLockElement === canvas ||
document.mozPointerLockElement === canvas ||
document.webkitPointerLockElement === canvas) {
isPointerLocked = true;
document.addEventListener("mousemove", updatePosition, false);
} else {
isPointerLocked = false;
document.removeEventListener("mousemove", updatePosition, false);
}
}
function updatePosition(e) {
if (!gameRunning) return;
mouseMovementX += e.movementX;
mouseMovementY += e.movementY;
}
document.addEventListener('mousedown', (e) => {
if (!gameRunning || !isPointerLocked) return;
if (e.button === 0 && player.ammo > 0) {
shoot();
}
});
// Game functions
function startGame() {
document.getElementById('start-screen').style.display = 'none';
gameRunning = true;
player = {
x: 100,
y: 100,
angle: 0,
health: 100,
ammo: 50,
weapon: 'PISTOL',
speed: 0.1,
turnSpeed: 0.05
};
updateUI();
gameLoop();
}
function gameLoop() {
if (!gameRunning) return;
// Clear canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Handle input
handleInput();
// Draw 3D view
draw3DView();
// Draw mini-map
drawMiniMap();
requestAnimationFrame(gameLoop);
}
function handleInput() {
// Rotation
if (mouseMovementX !== 0) {
player.angle += mouseMovementX * 0.002;
mouseMovementX = 0;
}
// Movement with proper strafing
const moveSpeed = keys.shift ? player.speed * 1.5 : player.speed;
const moveX = Math.cos(player.angle) * moveSpeed;
const moveY = Math.sin(player.angle) * moveSpeed;
const strafeX = Math.cos(player.angle + Math.PI/2) * moveSpeed;
const strafeY = Math.sin(player.angle + Math.PI/2) * moveSpeed;
// Forward/backward movement
if (keys.w) {
if (!checkWallCollision(player.x + moveX * 5, player.y + moveY * 5)) {
player.x += moveX;
player.y += moveY;
}
}
if (keys.s) {
if (!checkWallCollision(player.x - moveX * 5, player.y - moveY * 5)) {
player.x -= moveX;
player.y -= moveY;
}
}
// Strafing
if (keys.a) {
if (!checkWallCollision(player.x - strafeX * 5, player.y - strafeY * 5)) {
player.x -= strafeX;
player.y -= strafeY;
}
}
if (keys.d) {
if (!checkWallCollision(player.x + strafeX * 5, player.y + strafeY * 5)) {
player.x += strafeX;
player.y += strafeY;
}
}
}
function checkWallCollision(x, y) {
const mapX = Math.floor(x / cellSize);
const mapY = Math.floor(y / cellSize);
if (mapX < 0 || mapX >= mapSize || mapY < 0 || mapY >= mapSize) {
return true;
}
return map[mapY][mapX] === 1;
}
function draw3DView() {
// Draw sky
ctx.fillStyle = '#333366';
ctx.fillRect(0, 0, canvas.width, canvas.height / 2);
// Draw floor
ctx.fillStyle = '#663300';
ctx.fillRect(0, canvas.height / 2, canvas.width, canvas.height / 2);
// Raycasting
for (let x = 0; x < canvas.width; x++) {
const cameraX = 2 * x / canvas.width - 1;
const rayDirX = Math.cos(player.angle) + cameraX * Math.cos(player.angle + Math.PI/2);
const rayDirY = Math.sin(player.angle) + cameraX * Math.sin(player.angle + Math.PI/2);
let mapX = Math.floor(player.x / cellSize);
let mapY = Math.floor(player.y / cellSize);
let sideDistX, sideDistY;
let deltaDistX = Math.abs(1 / rayDirX);
let deltaDistY = Math.abs(1 / rayDirY);
let perpWallDist;
let stepX, stepY;
let hit = 0;
let side;
if (rayDirX < 0) {
stepX = -1;
sideDistX = (player.x / cellSize - mapX) * deltaDistX;
} else {
stepX = 1;
sideDistX = (mapX + 1.0 - player.x / cellSize) * deltaDistX;
}
if (rayDirY < 0) {
stepY = -1;
sideDistY = (player.y / cellSize - mapY) * deltaDistY;
} else {
stepY = 1;
sideDistY = (mapY + 1.0 - player.y / cellSize) * deltaDistY;
}
// DDA algorithm
while (hit === 0) {
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
} else {
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
if (mapX < 0 || mapX >= mapSize || mapY < 0 || mapY >= mapSize) {
hit = 1;
} else if (map[mapY][mapX] > 0) {
hit = 1;
}
}
// Calculate distance projected on camera direction
if (side === 0) {
perpWallDist = (mapX - player.x / cellSize + (1 - stepX) / 2) / rayDirX;
} else {
perpWallDist = (mapY - player.y / cellSize + (1 - stepY) / 2) / rayDirY;
}
// Calculate height of line to draw on screen
const lineHeight = Math.floor(canvas.height / perpWallDist);
// Calculate lowest and highest pixel to fill in current stripe
let drawStart = -lineHeight / 2 + canvas.height / 2;
if (drawStart < 0) drawStart = 0;
let drawEnd = lineHeight / 2 + canvas.height / 2;
if (drawEnd >= canvas.height) drawEnd = canvas.height - 1;
// Choose wall color
let color;
if (map[mapY][mapX] === 1) {
color = side === 0 ? '#ff0000' : '#cc0000'; // Red walls
}
// Draw the wall slice
ctx.fillStyle = color;
ctx.fillRect(x, drawStart, 1, drawEnd - drawStart);
}
}
function drawMiniMap() {
const miniMapSize = 150;
const miniMapScale = miniMapSize / (mapSize * cellSize);
const offset = 20;
// Draw background
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(offset, offset, miniMapSize, miniMapSize);
// Draw walls
for (let y = 0; y < mapSize; y++) {
for (let x = 0; x < mapSize; x++) {
if (map[y][x] === 1) {
ctx.fillStyle = '#777777';
ctx.fillRect(
x * cellSize * miniMapScale + offset,
y * cellSize * miniMapScale + offset,
cellSize * miniMapScale,
cellSize * miniMapScale
);
}
}
}
// Draw player
ctx.fillStyle = '#00ff00';
ctx.beginPath();
ctx.arc(
player.x * miniMapScale + offset,
player.y * miniMapScale + offset,
3, 0, Math.PI * 2
);
ctx.fill();
// Draw player direction
ctx.strokeStyle = '#00ff00';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(
player.x * miniMapScale + offset,
player.y * miniMapScale + offset
);
ctx.lineTo(
(player.x + Math.cos(player.angle) * 20) * miniMapScale + offset,
(player.y + Math.sin(player.angle) * 20) * miniMapScale + offset
);
ctx.stroke();
}
function shoot() {
if (player.ammo <= 0) return;
player.ammo--;
updateUI();
// Check if we hit an enemy
for (let i = 0; i < enemies.length; i++) {
const enemy = enemies[i];
const angleToEnemy = Math.atan2(enemy.y - player.y, enemy.x - player.x);
const angleDiff = Math.abs(normalizeAngle(angleToEnemy - player.angle));
if (angleDiff < 0.1) { // Within shooting cone
const dist = Math.sqrt(
Math.pow(enemy.x - player.x, 2) +
Math.pow(enemy.y - player.y, 2)
);
if (dist < 300) { // Within range
enemy.health -= 10;
if (enemy.health <= 0) {
enemies.splice(i, 1);
}
break;
}
}
}
}
function normalizeAngle(angle) {
while (angle > Math.PI) angle -= 2 * Math.PI;
while (angle < -Math.PI) angle += 2 * Math.PI;
return angle;
}
function updateUI() {
document.getElementById('health').textContent = `HEALTH: ${player.health}%`;
document.getElementById('ammo').textContent = `AMMO: ${player.ammo}`;
document.getElementById('weapon').textContent = `WEAPON: ${player.weapon}`;
}
// Handle window resize
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
</script>
</body>
</html>