sandsim/js/elements/trees.js
Kacper Kostka (aider) a86acfff3a 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.
2025-04-04 12:15:30 +02:00

109 lines
3.8 KiB
JavaScript

// Tree element behaviors
function updateTreeSeed(x, y) {
// Tree seeds fall like other seeds
if (getPixel(x, y + 1) === EMPTY) {
setPixel(x, y, EMPTY);
setPixel(x, y + 1, TREE_SEED);
moveMetadata(x, y, x, y + 1);
}
else if (getPixel(x - 1, y + 1) === EMPTY) {
setPixel(x, y, EMPTY);
setPixel(x - 1, y + 1, TREE_SEED);
moveMetadata(x, y, x - 1, y + 1);
}
else if (getPixel(x + 1, y + 1) === EMPTY) {
setPixel(x, y, EMPTY);
setPixel(x + 1, y + 1, TREE_SEED);
moveMetadata(x, y, x + 1, y + 1);
}
// Seeds can float on water
else if (getPixel(x, y + 1) === WATER) {
// Just float, don't do anything
}
// If seed is on dirt or grass, it can grow into a tree
else if (getPixel(x, y + 1) === DIRT || getPixel(x, y + 1) === GRASS) {
// Start growing a tree
growTree(x, y);
}
}
function growTree(x, y) {
// Replace the seed with the trunk
setPixel(x, y, WOOD);
// Determine tree height (50-80 blocks, 10x bigger)
const treeHeight = 50 + Math.floor(Math.random() * 31);
// Grow the trunk upward
for (let i = 1; i < treeHeight; i++) {
if (getPixel(x, y - i) === EMPTY) {
setPixel(x, y - i, WOOD);
} else {
break; // Stop if we hit something
}
}
// Add leaves at the top (10x bigger radius)
addLeaves(x, y - treeHeight + 1, 20 + Math.floor(Math.random() * 10));
// Add some branches
addBranches(x, y, treeHeight);
}
function addBranches(x, y, treeHeight) {
// Add 2-4 branches at different heights
const numBranches = 2 + Math.floor(Math.random() * 3);
for (let i = 0; i < numBranches; i++) {
// Position branch at different heights along the trunk
const branchY = y - Math.floor(treeHeight * (0.3 + 0.4 * i / numBranches));
// Choose left or right direction
const direction = Math.random() > 0.5 ? 1 : -1;
// Branch length (10-15 blocks)
const branchLength = 10 + Math.floor(Math.random() * 6);
// Create the branch
for (let j = 1; j <= branchLength; j++) {
// Branch goes out horizontally with some upward angle
const branchX = x + (j * direction);
const upwardAngle = Math.floor(j * 0.3);
if (getPixel(branchX, branchY - upwardAngle) === EMPTY) {
setPixel(branchX, branchY - upwardAngle, WOOD);
} else {
break; // Stop if we hit something
}
// Add small leaf clusters at the end of branches
if (j === branchLength) {
addLeaves(branchX, branchY - upwardAngle, 8 + Math.floor(Math.random() * 4));
}
}
}
}
function addLeaves(x, y, radius) {
// Add a cluster of leaves around the point
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
// Skip the exact center (trunk position)
if (dx === 0 && dy === 0) continue;
// Make it more circular by checking distance
const distance = Math.sqrt(dx*dx + dy*dy);
if (distance <= radius) {
// Random chance to place a leaf based on distance from center
// More dense leaves for larger trees
const density = radius > 10 ? 0.8 : 0.6;
if (Math.random() < (1 - distance/radius/density)) {
if (getPixel(x + dx, y + dy) === EMPTY) {
setPixel(x + dx, y + dy, LEAF);
}
}
}
}
}
}