feat: add Chunk as a dense 16-cubed id array
This commit is contained in:
40
VoxelEngine.Tests/ChunkTests.cs
Normal file
40
VoxelEngine.Tests/ChunkTests.cs
Normal file
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
19
World/Chunk.cs
Normal file
19
World/Chunk.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user