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.

45 lines
1.3 KiB

2 years ago
using System;
using gaemstone.ECS;
using static gaemstone.Bloxel.Components.CoreComponents;
using static gaemstone.Bloxel.Constants;
2 years ago
namespace gaemstone.Bloxel.Systems;
2 years ago
[Module]
[DependsOn<gaemstone.Bloxel.Components.CoreComponents>]
2 years ago
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,
2 years ago
[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;
2 years ago
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;
2 years ago
}
entity.Add<HasBasicWorldGeneration>();
}
}