feat: Add procedural generation for first chunk with sand hills, water, and grass
This commit is contained in:
parent
8ef18f52ab
commit
c35f6cd448
67
js/world.js
67
js/world.js
@ -41,12 +41,79 @@ function getOrCreateChunk(chunkX, chunkY) {
|
||||
}
|
||||
}
|
||||
|
||||
// Special generation for the first chunk (0,0)
|
||||
if (chunkX === 0 && chunkY === 0) {
|
||||
generateFirstChunk(chunkData);
|
||||
}
|
||||
|
||||
chunks.set(key, chunkData);
|
||||
}
|
||||
|
||||
return chunks.get(key);
|
||||
}
|
||||
|
||||
// Generate special terrain for the first chunk
|
||||
function generateFirstChunk(chunkData) {
|
||||
// 1. Create a base layer of sand above the floor
|
||||
const floorY = CHUNK_SIZE - 1;
|
||||
const baseHeight = 10; // Base height of sand
|
||||
|
||||
// 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;
|
||||
|
||||
// Generate height map for sand
|
||||
const heightMap = new Array(CHUNK_SIZE).fill(0);
|
||||
|
||||
// Calculate heights based on distance from the two hills
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
// Distance from each hill (using a simple distance function)
|
||||
const dist1 = Math.abs(x - hill1X);
|
||||
const dist2 = Math.abs(x - hill2X);
|
||||
|
||||
// Height contribution from each hill (inverse to distance)
|
||||
const h1 = hill1Height * Math.max(0, 1 - dist1 / (CHUNK_SIZE * 0.3));
|
||||
const h2 = hill2Height * Math.max(0, 1 - dist2 / (CHUNK_SIZE * 0.3));
|
||||
|
||||
// Take the maximum height contribution
|
||||
heightMap[x] = Math.floor(baseHeight + Math.max(h1, h2));
|
||||
}
|
||||
|
||||
// Find the lowest points for water
|
||||
let minHeight = Math.min(...heightMap);
|
||||
|
||||
// Place sand according to the height map
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
const height = heightMap[x];
|
||||
for (let y = floorY - height; y < floorY; y++) {
|
||||
chunkData[y * CHUNK_SIZE + x] = SAND;
|
||||
}
|
||||
|
||||
// 3. Add grass on top of the hills
|
||||
if (height > baseHeight + 3) { // Only on higher parts
|
||||
chunkData[(floorY - height) * CHUNK_SIZE + x] = GRASS;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Add water in the lowest areas
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
const height = heightMap[x];
|
||||
// Add water where the height is close to the minimum
|
||||
if (height <= minHeight + 2) {
|
||||
// Add a few layers of water
|
||||
const waterDepth = 3;
|
||||
for (let d = 0; d < waterDepth; d++) {
|
||||
const y = floorY - height - d - 1;
|
||||
if (y >= 0) {
|
||||
chunkData[y * CHUNK_SIZE + x] = WATER;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getChunkCoordinates(worldX, worldY) {
|
||||
const chunkX = Math.floor(worldX / CHUNK_SIZE);
|
||||
const chunkY = Math.floor(worldY / CHUNK_SIZE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user