Cleaned up chunk and world code
This commit is contained in:
83
World.cs
83
World.cs
@@ -8,6 +8,14 @@ namespace Voxel
|
||||
{
|
||||
private Dictionary<(int, int), Chunk> _chunks;
|
||||
|
||||
private static readonly Dictionary<Orientation, (int x, int y)> _neighborOffsets = new()
|
||||
{
|
||||
{ Orientation.West, (1, 0) },
|
||||
{ Orientation.East, (-1, 0) },
|
||||
{ Orientation.North, (0, 1) },
|
||||
{ Orientation.South, (0, -1) }
|
||||
};
|
||||
|
||||
public World()
|
||||
{
|
||||
_chunks = new Dictionary<(int, int), Chunk>();
|
||||
@@ -23,26 +31,53 @@ namespace Voxel
|
||||
{
|
||||
_chunks[(chunk.X, chunk.Y)] = chunk;
|
||||
|
||||
Dictionary<Orientation, Orientation> oppositeOrientation = new()
|
||||
{
|
||||
{Orientation.West, Orientation.East},
|
||||
{Orientation.East, Orientation.West},
|
||||
{Orientation.North, Orientation.South},
|
||||
{Orientation.South, Orientation.North},
|
||||
};
|
||||
|
||||
foreach (var (orientation, neighbor) in GetChunkNeighbors(chunk))
|
||||
{
|
||||
neighbor.Neighbors[oppositeOrientation[orientation]] = chunk;
|
||||
chunk.Neighbors[orientation] = neighbor;
|
||||
neighbor.UpdateChunkMesh();
|
||||
}
|
||||
UpdateNeighboringChunks(chunk);
|
||||
chunk.UpdateChunkMesh();
|
||||
}
|
||||
|
||||
public void RemoveChunk(int chunkX, int chunkZ)
|
||||
{
|
||||
_chunks.Remove((chunkX, chunkZ));
|
||||
if (_chunks.TryGetValue((chunkX, chunkZ), out Chunk chunk))
|
||||
{
|
||||
var neighbors = GetChunkNeighbors(chunk).ToList();
|
||||
_chunks.Remove((chunkX, chunkZ));
|
||||
|
||||
// 3. For each neighbor, remove reference to this chunk
|
||||
foreach (var (orientation, neighbor) in neighbors)
|
||||
{
|
||||
var opposite = GetOppositeOrientation(orientation);
|
||||
neighbor.Neighbors[opposite] = null;
|
||||
neighbor.UpdateChunkMesh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateNeighboringChunks(Chunk chunk)
|
||||
{
|
||||
chunk.Neighbors[Orientation.West] = null;
|
||||
chunk.Neighbors[Orientation.East] = null;
|
||||
chunk.Neighbors[Orientation.North] = null;
|
||||
chunk.Neighbors[Orientation.South] = null;
|
||||
|
||||
foreach (var (orientation, neighbor) in GetChunkNeighbors(chunk))
|
||||
{
|
||||
Orientation opposite = GetOppositeOrientation(orientation);
|
||||
neighbor.Neighbors[opposite] = chunk;
|
||||
chunk.Neighbors[orientation] = neighbor;
|
||||
neighbor.UpdateChunkMesh();
|
||||
}
|
||||
}
|
||||
|
||||
private Orientation GetOppositeOrientation(Orientation orientation)
|
||||
{
|
||||
return orientation switch
|
||||
{
|
||||
Orientation.West => Orientation.East,
|
||||
Orientation.East => Orientation.West,
|
||||
Orientation.North => Orientation.South,
|
||||
Orientation.South => Orientation.North,
|
||||
_ => orientation
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerable<Chunk> GetAllChunks()
|
||||
@@ -87,7 +122,7 @@ namespace Voxel
|
||||
|
||||
chunk.SetBlock(localX, worldY, localZ, block);
|
||||
|
||||
if (block == Blocks.Air && (localX == 15 || localX == 0) || (localZ == 15 || localZ == 0))
|
||||
if (block == Blocks.Air && ((localX == Chunk.Size - 1 || localX == 0) || (localZ == Chunk.Size - 1 || localZ == 0)))
|
||||
{
|
||||
foreach (var orientation in GetEdgeOrientations(localX, localZ, Chunk.Size))
|
||||
{
|
||||
@@ -101,20 +136,12 @@ namespace Voxel
|
||||
|
||||
public IEnumerable<(Orientation orientation, Chunk neighbor)> GetChunkNeighbors(Chunk chunk)
|
||||
{
|
||||
Dictionary<Orientation, (int x, int y)> offsets = new()
|
||||
{
|
||||
{ Orientation.West, (1, 0) },
|
||||
{ Orientation.East, (-1, 0) },
|
||||
{ Orientation.North, (0, 1) },
|
||||
{ Orientation.South, (0, -1) }
|
||||
};
|
||||
int chunkX = chunk.X;
|
||||
int chunkY = chunk.Y;
|
||||
|
||||
foreach (var kv in offsets)
|
||||
foreach (var kv in _neighborOffsets)
|
||||
{
|
||||
int nx = chunk.X + kv.Value.x;
|
||||
int ny = chunk.Y + kv.Value.y;
|
||||
|
||||
Chunk neighbor = GetChunk(nx, ny);
|
||||
Chunk neighbor = GetChunk(chunkX + kv.Value.x, chunkY + kv.Value.y);
|
||||
if (neighbor != null)
|
||||
yield return (kv.Key, neighbor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user