From d53457a0eb4ced9a6f795881c509a920ddb40a64 Mon Sep 17 00:00:00 2001 From: Mochatitan <119437575+Mochatitan@users.noreply.github.com> Date: Fri, 19 Apr 2024 10:47:11 -0400 Subject: [PATCH] added stone --- scripts/blocks.js | 7 +++++++ scripts/ui.js | 9 ++++++++- scripts/world.js | 34 +++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/scripts/blocks.js b/scripts/blocks.js index 7ae067b..909cde0 100644 --- a/scripts/blocks.js +++ b/scripts/blocks.js @@ -12,5 +12,12 @@ export const blocks = { id: 2, name: 'dirt', color: 0x807020 + }, + stone: { + id:3, + name: 'stone', + color: 0x808080, + scale: { x: 30, y: 30, z: 30}, + scarcity: 0.5 } } \ No newline at end of file diff --git a/scripts/ui.js b/scripts/ui.js index d889c20..38ebfb6 100644 --- a/scripts/ui.js +++ b/scripts/ui.js @@ -1,4 +1,6 @@ import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; +import { blocks } from './blocks'; + export function createUI(world){ const gui = new GUI(); @@ -14,8 +16,13 @@ export function createUI(world){ terrainFolder.add(world.params.terrain, 'magnitude', 0, 1, 0.02).name("Magnitude"); terrainFolder.add(world.params.terrain, 'offset', 0, 1, 0.02).name("Offset"); + const resourcesFolder = gui.addFolder("Resources"); + resourcesFolder.add(blocks.stone, 'scarcity', 0, 1).name("Scarcity"); - + const scaleFolder = resourcesFolder.addFolder("Scale"); + scaleFolder.add(blocks.stone.scale, "x", 0, 100).name("X"); + scaleFolder.add(blocks.stone.scale, "y", 0, 100).name("Y"); + scaleFolder.add(blocks.stone.scale, "z", 0, 100).name("Z"); //gui.add(world, 'generate'); diff --git a/scripts/world.js b/scripts/world.js index 1608b90..7e079cf 100644 --- a/scripts/world.js +++ b/scripts/world.js @@ -34,8 +34,11 @@ export class World extends THREE.Group { } generate(){ + const rng = new RNG(this.params.seed); + this.initializeTerrain(); - this.generateTerrain(); + this.generateResources(rng); + this.generateTerrain(rng); this.generateMeshes(); } @@ -62,9 +65,30 @@ export class World extends THREE.Group { } } + /** + * generates resources (coal, stone, etc.) for the rest of the world + */ + generateResources(rng) { + const simplex = new SimplexNoise(rng); - generateTerrain() { - const rng = new RNG(this.params.seed); + for(let x = 0; x < this.size.width; x++){ + for(let y = 0; y < this.size.height; y++){ + for(let z = 0; z < this.size.width; z++){ + const value = simplex.noise3d( + x/blocks.stone.scale.x, + y/blocks.stone.scale.y, + z/blocks.stone.scale.z + ); + + if (value > blocks.stone.scarcity){ + this.setBlockId(x, y, z, blocks.stone.id); + } + } + } + } + } + + generateTerrain(rng) { const simplex = new SimplexNoise(rng); for (let x = 0; x < this.size.width; x++) { for (let z = 0; z < this.size.width; z++){ @@ -84,11 +108,11 @@ export class World extends THREE.Group { // Fill in all blocks at or below the terrain height for (let y = 0; y <= this.size.height; y++){ - if (y < height){ + if (y < height && this.getBlock(x, y, z).id === blocks.empty.id){ this.setBlockId(x, y, z, blocks.dirt.id); } else if (y === height){ this.setBlockId(x, y, z, blocks.grass.id); - } else { + } else if(y > height){ this.setBlockId(x, y, z, blocks.empty.id); } }