feat: generate procedural world on load via WorldGenerator

This commit is contained in:
MDietermann
2026-05-31 22:11:27 +02:00
parent 03cf0ceb07
commit f5787dc9cc

View File

@@ -6,6 +6,7 @@ using OpenTK.Windowing.GraphicsLibraryFramework;
using VoxelEngine.Rendering; using VoxelEngine.Rendering;
using VoxelEngine.World.Blocks; using VoxelEngine.World.Blocks;
using VoxelEngine.World.Chunk; using VoxelEngine.World.Chunk;
using VoxelEngine.World.WorldGen;
namespace VoxelEngine.Core; namespace VoxelEngine.Core;
@@ -13,12 +14,14 @@ public sealed class Game : GameWindow
{ {
private const float MoveSpeed = 8f; // units per second private const float MoveSpeed = 8f; // units per second
private const float Sensitivity = 0.15f; // degrees per pixel private const float Sensitivity = 0.15f; // degrees per pixel
private const int Seed = 1337;
private readonly BlockRegistry _registry = new(); private readonly BlockRegistry _registry = new();
private Shader _shader = null!; private Shader _shader = null!;
private Texture _texture = null!; private Texture _texture = null!;
private readonly Dictionary<Vector3i, ChunkRenderer> _renderer = new(); private readonly Dictionary<Vector3i, ChunkRenderer> _renderer = new();
private Camera _camera = null!; private Camera _camera = null!;
private WorldGenerator _generator = null!;
private World.World _world; private World.World _world;
@@ -37,11 +40,14 @@ public sealed class Game : GameWindow
this._texture = new Texture("Assets/textures/VoxelShadeAtlas.png"); this._texture = new Texture("Assets/textures/VoxelShadeAtlas.png");
this._world = new World.World(this._registry); this._world = new World.World(this._registry);
this._generator = new WorldGenerator(Game.Seed, this._registry);
for (int x = -2; x < 2; x++) for (int cx = -2; cx < 2; cx++)
for (int z = -2; z < 2; z++) for (int cz = -2; cz < 2; cz++)
for (int cy = 0; cy < WorldConstants.WorldHeightChunks; cy++)
{ {
this._world.SetChunk(new Vector3i(x, 0, z), this.BuildChunk()); Vector3i pos = new(cx, cy, cz);
this._world.SetChunk(pos, this._generator.Generate(pos));
} }
ChunkMesher mesher = new(this._texture.Width, this._texture.Height); ChunkMesher mesher = new(this._texture.Width, this._texture.Height);
@@ -59,28 +65,13 @@ public sealed class Game : GameWindow
this._renderer[pos] = new ChunkRenderer(mesh); this._renderer[pos] = new ChunkRenderer(mesh);
} }
this._camera = new Camera(new Vector3(8f, 20f, 40f), this.ClientSize.X / (float)this.ClientSize.Y); this._camera = new Camera(
new Vector3(8f, WorldConstants.SeaLevel + 30f, 40f),
this.ClientSize.X / (float)this.ClientSize.Y);
this.CursorState = CursorState.Grabbed; this.CursorState = CursorState.Grabbed;
} }
private Chunk BuildChunk()
{
Chunk chunk = new();
for (int x = 0; x < Chunk.Size; x++)
for (int z = 0; z < Chunk.Size; z++)
for (int y = 0; y < Chunk.Size; y++)
{
chunk[x, y, z] = y switch
{
Chunk.Size - 1 => this._registry.Grass.Id,
>= Chunk.Size - 4 => this._registry.Dirt.Id,
_ => this._registry.Stone.Id
};
}
return chunk;
}
protected override void OnUpdateFrame(FrameEventArgs args) protected override void OnUpdateFrame(FrameEventArgs args)
{ {
base.OnUpdateFrame(args); base.OnUpdateFrame(args);