feat: Add natural tree and flower seed generation on grass terrain

This commit is contained in:
Kacper Kostka (aider) 2025-04-05 15:02:59 +02:00
parent 1584600d0b
commit 4716cbe692

View File

@ -173,6 +173,21 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) {
} }
} }
} }
// Randomly spawn tree seeds on grass
if (random() < 0.08) { // 8% chance for a tree seed on grass
const seedY = floorY - noiseHeight - 1; // Position above the grass
if (seedY >= 0 && chunkData[(floorY - noiseHeight) * CHUNK_SIZE + x] === GRASS) {
chunkData[seedY * CHUNK_SIZE + x] = TREE_SEED;
// Add metadata for the tree seed
const seedMetadata = {
age: Math.floor(random() * 50), // Random initial age
growthStage: 0,
type: 'oak' // Default tree type
};
// We'll set the metadata when the chunk is actually created
}
}
} }
} }
@ -246,6 +261,45 @@ function generateSpecialChunk(chunkData, chunkX, playerChunkX) {
} }
} }
} }
// Add additional tree seeds scattered throughout the terrain
for (let x = 0; x < CHUNK_SIZE; x += 5 + Math.floor(random() * 10)) { // Space them out
const height = heightMap[x];
const surfaceY = floorY - height;
// Only place seeds on grass
if (chunkData[surfaceY * CHUNK_SIZE + x] === GRASS) {
// 25% chance for a tree seed at each valid position
if (random() < 0.25) {
const seedY = surfaceY - 1; // Position above the grass
if (seedY >= 0) {
chunkData[seedY * CHUNK_SIZE + x] = TREE_SEED;
}
}
}
}
// Add some flower seeds in clusters near grass
for (let i = 0; i < 3; i++) { // Create a few flower clusters
const clusterX = Math.floor(random() * CHUNK_SIZE);
const clusterY = floorY - heightMap[clusterX];
if (chunkData[clusterY * CHUNK_SIZE + clusterX] === GRASS) {
// Create a small cluster of flower seeds
for (let dy = -1; dy <= 0; dy++) {
for (let dx = -2; dx <= 2; dx++) {
const seedX = clusterX + dx;
const seedY = clusterY + dy - 1; // Above the grass
if (seedX >= 0 && seedX < CHUNK_SIZE && seedY >= 0 &&
random() < 0.6 && // 60% chance for each position in the cluster
chunkData[(seedY+1) * CHUNK_SIZE + seedX] === GRASS) {
chunkData[seedY * CHUNK_SIZE + seedX] = SEED;
}
}
}
}
}
} }
function getChunkCoordinates(worldX, worldY) { function getChunkCoordinates(worldX, worldY) {
@ -273,6 +327,22 @@ function setPixel(worldX, worldY, type) {
const colorIndex = Math.floor(Math.random() * 10); const colorIndex = Math.floor(Math.random() * 10);
setMetadata(worldX, worldY, { ...getMetadata(worldX, worldY) || {}, colorIndex, waterColorTimer: 0 }); setMetadata(worldX, worldY, { ...getMetadata(worldX, worldY) || {}, colorIndex, waterColorTimer: 0 });
} }
else if (type === TREE_SEED) {
// Initialize tree seed metadata
setMetadata(worldX, worldY, {
age: Math.floor(Math.random() * 50), // Random initial age
growthStage: 0,
type: Math.random() < 0.8 ? 'oak' : 'pine' // 80% oak, 20% pine
});
}
else if (type === SEED) {
// Initialize flower seed metadata
setMetadata(worldX, worldY, {
age: Math.floor(Math.random() * 30),
growthStage: 0,
flowerType: Math.floor(Math.random() * 5) // Different flower types
});
}
} }
function getPixel(worldX, worldY) { function getPixel(worldX, worldY) {