This commit is contained in:
maxwes08
2025-09-09 10:57:28 +02:00
parent 3678eaa5f8
commit b6655d71d9
6 changed files with 148 additions and 81 deletions

View File

@@ -6,6 +6,7 @@
public static int Height = 256;
public readonly int X;
public readonly int Y;
private ChunkMesh _chunkMesh;
private Dictionary<ushort, BlockData> _blockData;
private Blocks[] _blocks;
@@ -17,14 +18,18 @@
_blockData = new Dictionary<ushort, BlockData>();
_blocks = new Blocks[Size * Size * Height];
_chunkMesh = new ChunkMesh(X, Y);
Initialize();
}
public void SetBlock(int x, int y, int z, Blocks block)
{
Console.WriteLine(x.ToString() + ", " + y.ToString());
int i = GetBlockIndex(x, y, z);
if (i == -1) return;
_blocks[i] = block;
UpdateChunkMesh();
}
public void SetBlockIndex(int i, Blocks block)
@@ -35,6 +40,7 @@
public Blocks GetBlock(int x, int y, int z)
{
int i = GetBlockIndex(x, y, z);
if (i == -1) return Blocks.Air;
return _blocks[i];
}
@@ -51,6 +57,7 @@
_blocks[i] = Blocks.Stone;
for (int i = Size * Size * 15; i < Size * Size * 16; i++)
_blocks[i] = Blocks.Grass;
UpdateChunkMesh();
}
// todo
@@ -66,7 +73,7 @@
private int GetBlockIndex(int x, int y, int z)
{
if (x < 0 || x > 15 || y < 0 || y > 255 || z < 0 || z > 15)
return 0;
return -1;
return x + z * Size + y * Size * Size;
}
@@ -81,14 +88,10 @@
( 0, 0, -1) // -Z
};
public ChunkMesh GetChunkMesh()
private void UpdateChunkMesh()
{
ChunkMesh chunkMesh = new ChunkMesh(X, Y);
List<FaceData> faces = new List<FaceData>(Size * Size * Height / 2);
// offsets table
for (int x = 0; x < Size; x++)
{
for (int z = 0; z < Size; z++)
@@ -100,6 +103,20 @@
int indexBase = y * Size * Size + z * Size + x;
Blocks block = _blocks[indexBase];
void AddFace()
{
FaceData faceData = new FaceData();
faceData.Facing = (Orientation)face;
faceData.Texture = BlockDefinitions.Blocks[block].FaceTextures[face];
faceData.X = (byte)x;
faceData.Y = (byte)y;
faceData.Z = (byte)z;
faces.Add(faceData);
}
if (block == Blocks.Air) continue; // ignore if air
int nx = x + Offsets[face].dx;
@@ -107,30 +124,29 @@
int nz = z + Offsets[face].dz;
// check neighbor, ignore if at chunk edge
if (nx >= 0 && nx < Size &&
ny >= 0 && ny < Height &&
nz >= 0 && nz < Size &&
_blocks[GetBlockIndex(nx, ny, nz)] != 0)
int ni = GetBlockIndex(nx, ny, nz);
if (GetBlockIndex(nx, ny, nz) == -1)
{
AddFace();
continue;
}
FaceData faceData = new FaceData();
faceData.Facing = (Orientation)face;
faceData.Texture = BlockDefinitions.Blocks[block].FaceTextures[face];
faceData.X = (byte)x;
faceData.Y = (byte)y;
faceData.Z = (byte)z;
faces.Add(faceData);
if (_blocks[ni] == Blocks.Air)
{
AddFace();
continue;
}
}
}
}
}
_chunkMesh.SetFaces(faces);
}
chunkMesh.SetFaces(faces);
return chunkMesh;
public ChunkMesh GetChunkMesh()
{
return _chunkMesh;
}
}
}