diff --git a/VoxelEngine.Tests/ChunkTests.cs b/VoxelEngine.Tests/ChunkTests.cs new file mode 100644 index 0000000..646a569 --- /dev/null +++ b/VoxelEngine.Tests/ChunkTests.cs @@ -0,0 +1,40 @@ +using VoxelEngine.World; +using Xunit; + +namespace VoxelEngine.Tests; + +public class ChunkTests +{ + [Fact] + public void Size_Is16() + { + Assert.Equal(16, Chunk.Size); + } + + [Fact] + public void NewChunk_IsAllAir() + { + var chunk = new Chunk(); + Assert.Equal((ushort)0, chunk[0, 0, 0]); + Assert.Equal((ushort)0, chunk[15, 15, 15]); + } + + [Fact] + public void Indexer_StoresAndReturnsId() + { + var chunk = new Chunk(); + chunk[1, 2, 3] = 7; + Assert.Equal((ushort)7, chunk[1, 2, 3]); + Assert.Equal((ushort)0, chunk[3, 2, 1]); + } + + [Theory] + [InlineData(0, 0, 0, true)] + [InlineData(15, 15, 15, true)] + [InlineData(-1, 0, 0, false)] + [InlineData(0, 16, 0, false)] + public void InBounds_ChecksCoordinates(int x, int y, int z, bool expected) + { + Assert.Equal(expected, Chunk.InBounds(x, y, z)); + } +} diff --git a/World/Chunk.cs b/World/Chunk.cs new file mode 100644 index 0000000..f628633 --- /dev/null +++ b/World/Chunk.cs @@ -0,0 +1,19 @@ +namespace VoxelEngine.World; + +public sealed class Chunk +{ + public const int Size = 16; + + private readonly ushort[] _ids = new ushort[Size * Size * Size]; + + public ushort this[int x, int y, int z] + { + get => _ids[Index(x, y, z)]; + set => _ids[Index(x, y, z)] = value; + } + + public static bool InBounds(int x, int y, int z) => + x >= 0 && x < Size && y >= 0 && y < Size && z >= 0 && z < Size; + + private static int Index(int x, int y, int z) => (x * Size + y) * Size + z; +}