Skip to content

Commit

Permalink
added stone
Browse files Browse the repository at this point in the history
  • Loading branch information
Mochatitan committed Apr 19, 2024
1 parent ac88f6c commit d53457a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
7 changes: 7 additions & 0 deletions scripts/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
9 changes: 8 additions & 1 deletion scripts/ui.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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');
Expand Down
34 changes: 29 additions & 5 deletions scripts/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Expand All @@ -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++){
Expand All @@ -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);
}
}
Expand Down

0 comments on commit d53457a

Please sign in to comment.