<!DOCTYPE html>
<html>
<head>
<title>Хроно-Инженер</title>
<style>
body {
background-color: #000022;
color: #ffffff;
font-family: sans-serif;
margin: 0;
overflow: hidden; /* Prevent scrollbars */
}
canvas {
display: block;
margin: 0 auto;
}
#ui {
position: absolute;
top: 10px;
left: 10px;
color: #ffffff;
padding: 10px;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 5px;
}
#controls {
position: absolute;
bottom: 10px;
left: 10px;
color: #ffffff;
padding: 10px;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 5px;
}
button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 5px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 12px;
margin: 2px;
cursor: pointer;
border-radius: 3px;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="ui">
Level: <span id="level">1</span><br>
Score: <span id="score">0</span>
</div>
<div id="controls">
Tool: <span id="currentTool">Accelerate</span><br>
<button onclick="setTool('accelerate')">Accelerate</button>
<button onclick="setTool('decelerate')">Decelerate</button>
<button onclick="setTool('reverse')">Reverse</button>
<button onclick="setTool('barrier')">Barrier</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let level = 1;
let score = 0;
let currentTool = 'accelerate';
const gridCellSize = 20;
let gridWidth = Math.floor(canvas.width / gridCellSize);
let gridHeight = Math.floor(canvas.height / gridCellSize);
let timeFlowGrid = [];
let particles = [];
let barriers = [];
function initializeGrid() {
timeFlowGrid = [];
for (let i = 0; i < gridWidth; i++) {
timeFlowGrid[i] = [];
for (let j = 0; j < gridHeight; j++) {
timeFlowGrid[i][j] = {
type: 'neutral', // 'accelerate', 'decelerate', 'reverse', 'neutral'
strength: 0.1,
directionX: 0,
directionY: 0
};
}
}
generateRandomFlows();
}
function generateRandomFlows() {
for (let i = 0; i < gridWidth; i++) {
for (let j = 0; j < gridHeight; j++) {
if (Math.random() < 0.05) {
const flowType = ['accelerate', 'decelerate', 'reverse'][Math.floor(Math.random() * 3)];
const strength = Math.random() * 0.5 + 0.1;
const directionX = Math.random() * 2 - 1;
const directionY = Math.random() * 2 - 1;
timeFlowGrid[i][j] = { type: flowType, strength: strength, directionX: directionX, directionY: directionY };
}
}
}
}
function createParticle() {
particles.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
velocityX: 0,
velocityY: 0,
history: []
});
}
function setTool(tool) {
currentTool = tool;
document.getElementById('currentTool').innerText = tool;
}
canvas.addEventListener('click', (event) => {
const x = event.offsetX;
const y = event.offsetY;
const gridX = Math.floor(x / gridCellSize);
const gridY = Math.floor(y / gridCellSize);
if (currentTool === 'barrier') {
barriers.push({x: gridX, y: gridY});
} else {
timeFlowGrid[gridX][gridY] = {type: currentTool, strength: 0.5, directionX: Math.random() * 2 - 1, directionY: Math.random() * 2 - 1};
}
});
function update() {
particles.forEach(particle => {
const gridX = Math.floor(particle.x / gridCellSize);
const gridY = Math.floor(particle.y / gridCellSize);
if (gridX >= 0 && gridX < gridWidth && gridY >= 0 && gridY < gridHeight) {
const flow = timeFlowGrid[gridX][gridY];
let timeFactor = 1;
let flowColor = "white";
switch (flow.type) {
case 'accelerate':
timeFactor = 1 + flow.strength;
flowColor = "green";
break;
case 'decelerate':
timeFactor = 1 - flow.strength;
flowColor = "blue";
break;
case 'reverse':
timeFactor = -1 * flow.strength;
flowColor = "red";
break;
case 'neutral':
default:
timeFactor = 1;
}
particle.velocityX += flow.directionX * flow.strength;
particle.velocityY += flow.directionY * flow.strength;
particle.x += particle.velocityX * timeFactor;
particle.y += particle.velocityY * timeFactor;
particle.velocityX *= 0.95; // Friction
particle.velocityY *= 0.95;
// Keep track of particle's path for trails
particle.history.push({ x: particle.x, y: particle.y, color:flowColor });
if (particle.history.length > 50) {
particle.history.shift();
}
// Boundary collision
if (particle.x < 0 || particle.x > canvas.width || particle.y < 0 || particle.y > canvas.height) {
particle.x = Math.random() * canvas.width;
particle.y = Math.random() * canvas.height;
particle.velocityX = 0;
particle.velocityY = 0;
particle.history = [];
}
//Barrier collision
barriers.forEach(barrier => {
let barrierX = barrier.x * gridCellSize + gridCellSize/2;
let barrierY = barrier.y * gridCellSize + gridCellSize/2;
let distance = Math.sqrt((particle.x - barrierX)**2 + (particle.y - barrierY)**2);
if (distance < gridCellSize) {
particle.velocityX = 0;
particle.velocityY = 0;
// Bounce back: simple approach
particle.velocityX = -(particle.x - barrierX) * 0.1;
particle.velocityY = -(particle.y - barrierY) * 0.1;
}
});
}
});
}
function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Time Flow Grid
for (let i = 0; i < gridWidth; i++) {
for (let j = 0; j < gridHeight; j++) {
const flow = timeFlowGrid[i][j];
if (flow.type !== 'neutral') {
let color = 'rgba(255, 255, 255, 0.1)';
switch (flow.type) {
case 'accelerate': color = 'rgba(0, 255, 0, 0.2)'; break;
case 'decelerate': color = 'rgba(0, 0, 255, 0.2)'; break;
case 'reverse': color = 'rgba(255, 0, 0, 0.2)'; break;
}
ctx.fillStyle = color;
ctx.fillRect(i * gridCellSize, j * gridCellSize, gridCellSize, gridCellSize);
// Draw a line to indicate flow direction
ctx.beginPath();
ctx.moveTo(i * gridCellSize + gridCellSize / 2, j * gridCellSize + gridCellSize / 2);
ctx.lineTo(i * gridCellSize + gridCellSize / 2 + flow.directionX * gridCellSize/2, j * gridCellSize + gridCellSize / 2 + flow.directionY * gridCellSize/2);
ctx.strokeStyle = color;
ctx.stroke();
}
}
}
//Draw barriers
barriers.forEach(barrier => {
ctx.fillStyle = 'gold';
ctx.fillRect(barrier.x * gridCellSize, barrier.y * gridCellSize, gridCellSize, gridCellSize);
});
// Draw Particles
particles.forEach(particle => {
// Draw trail
for (let i = 1; i < particle.history.length; i++) {
const p1 = particle.history[i - 1];
const p2 = particle.history[i];
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.strokeStyle = particle.history[i].color; // Use color from the history
ctx.lineWidth = 2;
ctx.stroke();
}
ctx.beginPath();
ctx.arc(particle.x, particle.y, 5, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.fill();
});
}
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
function startGame() {
initializeGrid();
createParticle();
gameLoop();
}
startGame();
</script>
</body>
</html>
This revised code provides a functional base for the "Chrono-Engineer" game concept, incorporating the core mechanics and visual style elements described in the prompt. You can further expand on this foundation by adding more levels, puzzles, tools, and visual effects. Remember to focus on making the gameplay intuitive and engaging.