From 39375b83ead8e65d9f162ec1be47d96bd5823225 Mon Sep 17 00:00:00 2001 From: MDietermann Date: Mon, 1 Jun 2026 10:45:58 +0200 Subject: [PATCH] refactor: extract shared ChunkMath.FloorDiv helper --- Core/ChunkMath.cs | 13 +++++++++++++ Game/Player.cs | 13 +++---------- World/World.cs | 36 ++++++++++++------------------------ 3 files changed, 28 insertions(+), 34 deletions(-) create mode 100644 Core/ChunkMath.cs diff --git a/Core/ChunkMath.cs b/Core/ChunkMath.cs new file mode 100644 index 0000000..d8f096d --- /dev/null +++ b/Core/ChunkMath.cs @@ -0,0 +1,13 @@ +namespace VoxelEngine.Core; + +internal static class ChunkMath +{ + // Floor division: floors toward negative infinity (not toward zero like C#'s /). + // Required for correct world-coord -> chunk-index mapping for negative coordinates. + internal static int FloorDiv(int value, int size) + { + int q = value / size; + if (value % size != 0 && value < 0) q--; + return q; + } +} diff --git a/Game/Player.cs b/Game/Player.cs index 94fd03e..270a37f 100644 --- a/Game/Player.cs +++ b/Game/Player.cs @@ -20,9 +20,9 @@ public sealed class Player { Vector3 p = this.Position; return new Vector3i( - Player.FloorDiv((int)MathF.Floor(p.X), VoxelChunk.Size), - Player.FloorDiv((int)MathF.Floor(p.Y), VoxelChunk.Size), - Player.FloorDiv((int)MathF.Floor(p.Z), VoxelChunk.Size)); + ChunkMath.FloorDiv((int)MathF.Floor(p.X), VoxelChunk.Size), + ChunkMath.FloorDiv((int)MathF.Floor(p.Y), VoxelChunk.Size), + ChunkMath.FloorDiv((int)MathF.Floor(p.Z), VoxelChunk.Size)); } } @@ -35,11 +35,4 @@ public sealed class Player public void Move(Vector3 delta) => this.Camera.Move(delta); public void AddRotation(float yawDelta, float pitchDelta) => this.Camera.AddRotation(yawDelta, pitchDelta); - - private static int FloorDiv(int value, int size) - { - int q = value / size; - if (value % size != 0 && value < 0) q--; - return q; - } } diff --git a/World/World.cs b/World/World.cs index 938a929..f6ea3b5 100644 --- a/World/World.cs +++ b/World/World.cs @@ -1,22 +1,17 @@ using System.Collections; using OpenTK.Mathematics; +using VoxelEngine.Core; using VoxelEngine.World.Blocks; using VoxelEngine.World.Persistence; namespace VoxelEngine.World; -public sealed class World : IWorld, IEnumerable> +public sealed class World(BlockRegistry registry, IChunkModificationStore modStore) + : IWorld, IEnumerable> { private readonly Dictionary _chunks = new(); - private readonly BlockRegistry _registry; - public IChunkModificationStore ModificationStore { get; } - - public World(BlockRegistry registry, IChunkModificationStore modStore) - { - this._registry = registry; - this.ModificationStore = modStore; - } + public IChunkModificationStore ModificationStore { get; } = modStore; public void SetChunk(Vector3i chunkPos, Chunk.Chunk chunk) => this._chunks[chunkPos] = chunk; @@ -27,25 +22,25 @@ public sealed class World : IWorld, IEnumerable> GetEnumerator() => this._chunks.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();