You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
1.3 KiB

using System;
using gaemstone.ECS;
using static gaemstone.Bloxel.Components.CoreComponents;
using static gaemstone.Bloxel.Constants;
namespace gaemstone.Bloxel.Systems;
[Module]
[DependsOn<gaemstone.Bloxel.Components.CoreComponents>]
public class BasicWorldGenerator
{
private readonly FastNoiseLite _noise;
public BasicWorldGenerator()
{
_noise = new(new Random().Next());
_noise.SetNoiseType(FastNoiseLite.NoiseType.OpenSimplex2);
_noise.SetFractalType(FastNoiseLite.FractalType.Ridged);
_noise.SetFractalOctaves(4);
_noise.SetFractalGain(0.6f);
}
[Tag]
public struct HasBasicWorldGeneration { }
[System]
public void Populate(Universe universe, EntityRef entity,
in Chunk chunk, ChunkStoreBlocks blocks,
[Not] HasBasicWorldGeneration _)
{
var stone = universe.LookupOrThrow("Stone");
for (var lx = 0; lx < ChunkLength; lx++)
for (var ly = 0; ly < ChunkLength; ly++)
for (var lz = 0; lz < ChunkLength; lz++) {
var gx = chunk.Position.X << ChunkBitShift | lx;
var gy = chunk.Position.Y << ChunkBitShift | ly;
var gz = chunk.Position.Z << ChunkBitShift | lz;
var bias = Math.Clamp(gy / 32.0f + 1.0f, 0.0f, 1.0f);
if (_noise.GetNoise(gx, gy, gz) > bias)
blocks[lx, ly, lz] = stone;
}
entity.Add<HasBasicWorldGeneration>();
}
}