feat: Enhance terrain generation with dynamic chunk spreading and seeded randomness
This commit is contained in:
parent
c35f6cd448
commit
a31f401378
62
js/world.js
62
js/world.js
@ -41,9 +41,12 @@ function getOrCreateChunk(chunkX, chunkY) {
|
||||
}
|
||||
}
|
||||
|
||||
// Special generation for the first chunk (0,0)
|
||||
if (chunkX === 0 && chunkY === 0) {
|
||||
generateFirstChunk(chunkData);
|
||||
// Get the current player chunk position
|
||||
const playerChunkX = Math.floor(worldOffsetX / CHUNK_SIZE);
|
||||
|
||||
// Special generation for chunks within 3 chunks of the player's position
|
||||
if (chunkY === 0 && Math.abs(chunkX - playerChunkX) <= 3) {
|
||||
generateSpecialChunk(chunkData, chunkX, playerChunkX);
|
||||
}
|
||||
|
||||
chunks.set(key, chunkData);
|
||||
@ -52,17 +55,21 @@ function getOrCreateChunk(chunkX, chunkY) {
|
||||
return chunks.get(key);
|
||||
}
|
||||
|
||||
// Generate special terrain for the first chunk
|
||||
function generateFirstChunk(chunkData) {
|
||||
// Generate special terrain for chunks near the player
|
||||
function generateSpecialChunk(chunkData, chunkX, playerChunkX) {
|
||||
// 1. Create a base layer of sand above the floor
|
||||
const floorY = CHUNK_SIZE - 1;
|
||||
const baseHeight = 10; // Base height of sand
|
||||
|
||||
// Use the chunk position as part of the seed for consistent generation
|
||||
const seed = chunkX * 10000;
|
||||
const random = createSeededRandom(seed);
|
||||
|
||||
// Create two random hill points
|
||||
const hill1X = Math.floor(CHUNK_SIZE * 0.3);
|
||||
const hill2X = Math.floor(CHUNK_SIZE * 0.7);
|
||||
const hill1Height = baseHeight + Math.floor(Math.random() * 10) + 5; // 5-15 blocks higher
|
||||
const hill2Height = baseHeight + Math.floor(Math.random() * 10) + 5;
|
||||
const hill1X = Math.floor(CHUNK_SIZE * (0.2 + random() * 0.2));
|
||||
const hill2X = Math.floor(CHUNK_SIZE * (0.6 + random() * 0.2));
|
||||
const hill1Height = baseHeight + Math.floor(random() * 10) + 5; // 5-15 blocks higher
|
||||
const hill2Height = baseHeight + Math.floor(random() * 10) + 5;
|
||||
|
||||
// Generate height map for sand
|
||||
const heightMap = new Array(CHUNK_SIZE).fill(0);
|
||||
@ -79,6 +86,14 @@ function generateFirstChunk(chunkData) {
|
||||
|
||||
// Take the maximum height contribution
|
||||
heightMap[x] = Math.floor(baseHeight + Math.max(h1, h2));
|
||||
|
||||
// Add some variation based on distance from player's chunk
|
||||
const distanceFromPlayer = Math.abs(chunkX - playerChunkX);
|
||||
if (distanceFromPlayer > 0) {
|
||||
// Make terrain more extreme as we move away from player
|
||||
const factor = 1 + (distanceFromPlayer * 0.2);
|
||||
heightMap[x] = Math.floor(heightMap[x] * factor);
|
||||
}
|
||||
}
|
||||
|
||||
// Find the lowest points for water
|
||||
@ -112,6 +127,35 @@ function generateFirstChunk(chunkData) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add some random elements based on the chunk position
|
||||
if (random() < 0.3) {
|
||||
// Add a small tree or plant cluster
|
||||
const plantX = Math.floor(random() * CHUNK_SIZE);
|
||||
const plantY = floorY - heightMap[plantX] - 1;
|
||||
|
||||
if (plantY > 0 && chunkData[plantY * CHUNK_SIZE + plantX] === GRASS) {
|
||||
// Add a small tree
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (plantY - i > 0) {
|
||||
chunkData[(plantY - i) * CHUNK_SIZE + plantX] = WOOD;
|
||||
}
|
||||
}
|
||||
|
||||
// Add some leaves
|
||||
for (let dy = -2; dy <= 0; dy++) {
|
||||
for (let dx = -2; dx <= 2; dx++) {
|
||||
const leafX = plantX + dx;
|
||||
const leafY = plantY - 3 + dy;
|
||||
|
||||
if (leafX >= 0 && leafX < CHUNK_SIZE && leafY >= 0 &&
|
||||
Math.abs(dx) + Math.abs(dy) < 3) {
|
||||
chunkData[leafY * CHUNK_SIZE + leafX] = LEAF;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getChunkCoordinates(worldX, worldY) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user