added block breaking
This commit is contained in:
104
World.cs
Normal file
104
World.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using OpenTK.Mathematics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Voxel
|
||||
{
|
||||
public class World
|
||||
{
|
||||
private Dictionary<(int, int), Chunk> _chunks;
|
||||
|
||||
public World()
|
||||
{
|
||||
_chunks = new Dictionary<(int, int), Chunk>();
|
||||
}
|
||||
|
||||
// Get a chunk at world coordinates, returns null if not loaded
|
||||
public Chunk GetChunk(int chunkX, int chunkZ)
|
||||
{
|
||||
_chunks.TryGetValue((chunkX, chunkZ), out Chunk chunk);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
// Add a chunk
|
||||
public void AddChunk(Chunk chunk)
|
||||
{
|
||||
_chunks[(chunk.PositionX, chunk.PositionY)] = chunk;
|
||||
}
|
||||
|
||||
// Remove a chunk
|
||||
public void RemoveChunk(int chunkX, int chunkZ)
|
||||
{
|
||||
_chunks.Remove((chunkX, chunkZ));
|
||||
}
|
||||
|
||||
// Iterate over all chunks
|
||||
public IEnumerable<Chunk> GetAllChunks()
|
||||
{
|
||||
return _chunks.Values;
|
||||
}
|
||||
|
||||
// Optional: get a block at world coordinates
|
||||
public Blocks GetBlock(int worldX, int worldY, int worldZ)
|
||||
{
|
||||
int chunkX = worldX / Chunk.Size;
|
||||
int chunkZ = worldZ / Chunk.Size;
|
||||
Chunk chunk = GetChunk(chunkX, chunkZ);
|
||||
if (chunk == null) return 0; // air if chunk not loaded
|
||||
|
||||
int localX = worldX % Chunk.Size;
|
||||
int localZ = worldZ % Chunk.Size;
|
||||
|
||||
return chunk.GetBlock(localX, worldY, localZ);
|
||||
}
|
||||
|
||||
// Optional: set a block at world coordinates
|
||||
public void SetBlock(int worldX, int worldY, int worldZ, Blocks block)
|
||||
{
|
||||
int chunkX = worldX / Chunk.Size;
|
||||
int chunkZ = worldZ / Chunk.Size;
|
||||
Chunk chunk = GetChunk(chunkX, chunkZ);
|
||||
if (chunk == null) return;
|
||||
|
||||
int localX = worldX % Chunk.Size;
|
||||
int localZ = worldZ % Chunk.Size;
|
||||
|
||||
chunk.SetBlock(localX, worldY, localZ, block);
|
||||
Renderer.ClearFaces();
|
||||
ChunkMesh chunkMesh = chunk.GetChunkMesh();
|
||||
Renderer.AddFaces(chunkMesh.Faces);
|
||||
}
|
||||
|
||||
public (Blocks block, int x, int y, int z) Raycast(float length)
|
||||
{
|
||||
Vector3 start = Camera.Position;
|
||||
Vector3 dir = Camera.Front.Normalized();
|
||||
|
||||
float stepSize = 0.01f;
|
||||
float distanceTraveled = 0f;
|
||||
|
||||
while (distanceTraveled < length)
|
||||
{
|
||||
Vector3 point = start + dir * distanceTraveled;
|
||||
|
||||
int bx = (int)MathF.Floor(point.X);
|
||||
int by = (int)MathF.Floor(point.Y);
|
||||
int bz = (int)MathF.Floor(point.Z);
|
||||
|
||||
Blocks block = GetBlock(bx, by, bz);
|
||||
if (block != Blocks.Air)
|
||||
{
|
||||
return (block, bx, by, bz);
|
||||
}
|
||||
|
||||
distanceTraveled += stepSize;
|
||||
}
|
||||
|
||||
return (Blocks.Air, 0, 0, 0);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user