refactor: Spread game code into modular files for better maintainability
This commit reorganizes the game's source code into multiple files within a `js/` directory, creating a more modular and maintainable structure. The changes include:
- Created separate files for different game components:
- `constants.js`: Game constants and element types
- `world.js`: World management functions
- `terrain.js`: Terrain generation logic
- `physics.js`: Physics simulation
- `render.js`: Rendering functions
- `input.js`: Input handling
- `main.js`: Main game initialization and loop
- Element-specific files in `js/elements/`:
- `basic.js`: Sand, water, dirt behaviors
- `plants.js`: Grass, seeds, flowers
- `trees.js`: Tree growth and leaf generation
- `fire.js`: Fire and lava behaviors
- Updated `index.html` to load modules in the correct order
- Removed the monolithic `script.js`
The modular approach improves code readability, makes future extensions easier, and separates concerns more effectively.
This commit is contained in:
76
js/main.js
Normal file
76
js/main.js
Normal file
@@ -0,0 +1,76 @@
|
||||
// Main game variables and initialization
|
||||
let canvas, ctx;
|
||||
let currentTool = SAND;
|
||||
let lastFrameTime = 0;
|
||||
let fps = 0;
|
||||
let debugMode = false;
|
||||
|
||||
// Initialize the simulation
|
||||
window.onload = function() {
|
||||
canvas = document.getElementById('simulation-canvas');
|
||||
ctx = canvas.getContext('2d');
|
||||
|
||||
// Set canvas size to fill the screen
|
||||
resizeCanvas();
|
||||
window.addEventListener('resize', resizeCanvas);
|
||||
|
||||
// Tool selection
|
||||
document.getElementById('sand-btn').addEventListener('click', () => setTool(SAND));
|
||||
document.getElementById('water-btn').addEventListener('click', () => setTool(WATER));
|
||||
document.getElementById('dirt-btn').addEventListener('click', () => setTool(DIRT));
|
||||
document.getElementById('stone-btn').addEventListener('click', () => setTool(STONE));
|
||||
document.getElementById('grass-btn').addEventListener('click', () => setTool(GRASS));
|
||||
document.getElementById('wood-btn').addEventListener('click', () => setTool(WOOD));
|
||||
document.getElementById('seed-btn').addEventListener('click', () => setTool(SEED));
|
||||
document.getElementById('tree-seed-btn').addEventListener('click', () => setTool(TREE_SEED));
|
||||
document.getElementById('fire-btn').addEventListener('click', () => setTool(FIRE));
|
||||
document.getElementById('lava-btn').addEventListener('click', () => setTool(LAVA));
|
||||
document.getElementById('eraser-btn').addEventListener('click', () => setTool(EMPTY));
|
||||
|
||||
// Navigation controls
|
||||
document.getElementById('move-left').addEventListener('click', () => moveWorld(-CHUNK_SIZE/2, 0));
|
||||
document.getElementById('move-right').addEventListener('click', () => moveWorld(CHUNK_SIZE/2, 0));
|
||||
document.getElementById('move-up').addEventListener('click', () => moveWorld(0, -CHUNK_SIZE/2));
|
||||
document.getElementById('move-down').addEventListener('click', () => moveWorld(0, CHUNK_SIZE/2));
|
||||
document.getElementById('debug-btn').addEventListener('click', toggleDebug);
|
||||
|
||||
// Drawing events
|
||||
canvas.addEventListener('mousedown', handleMouseDown);
|
||||
canvas.addEventListener('mousemove', handleMouseMove);
|
||||
canvas.addEventListener('mouseup', handleMouseUp);
|
||||
canvas.addEventListener('mouseleave', handleMouseUp);
|
||||
|
||||
// Touch events for mobile
|
||||
canvas.addEventListener('touchstart', handleTouchStart);
|
||||
canvas.addEventListener('touchmove', handleTouchMove);
|
||||
canvas.addEventListener('touchend', handleMouseUp);
|
||||
|
||||
// Initialize the first chunk and generate terrain around it
|
||||
getOrCreateChunk(0, 0);
|
||||
generateChunksAroundPlayer();
|
||||
|
||||
// Start the simulation loop
|
||||
requestAnimationFrame(simulationLoop);
|
||||
};
|
||||
|
||||
function resizeCanvas() {
|
||||
canvas.width = window.innerWidth;
|
||||
canvas.height = window.innerHeight - document.querySelector('.controls').offsetHeight;
|
||||
}
|
||||
|
||||
function simulationLoop(timestamp) {
|
||||
// Calculate FPS
|
||||
const deltaTime = timestamp - lastFrameTime;
|
||||
lastFrameTime = timestamp;
|
||||
fps = Math.round(1000 / deltaTime);
|
||||
document.getElementById('fps').textContent = `FPS: ${fps}`;
|
||||
|
||||
// Update physics
|
||||
updatePhysics();
|
||||
|
||||
// Render
|
||||
render();
|
||||
|
||||
// Continue the loop
|
||||
requestAnimationFrame(simulationLoop);
|
||||
}
|
||||
Reference in New Issue
Block a user