|
@@ -9,52 +9,75 @@ namespace KairoEngine.Chunks
|
|
|
public class ChunkSystem<TChunkBlock>
|
|
|
{
|
|
|
public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
|
|
|
+ public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
|
|
|
public Dictionary<Vector3Int, Chunk<TChunkBlock>> chunks;
|
|
|
- public List<Vector3Int> chunkList = new List<Vector3Int>();
|
|
|
+ public List<Vector3> chunkList = new List<Vector3>();
|
|
|
|
|
|
- public ChunkSystem(Vector3Int chunkSize, bool debug = false)
|
|
|
+ public ChunkSystem(Vector3Int chunkSize, Vector3 voxelSize, bool debug = false)
|
|
|
{
|
|
|
this.chunkSize = chunkSize;
|
|
|
+ this.voxelSize = voxelSize;
|
|
|
this.chunks = new Dictionary<Vector3Int, Chunk<TChunkBlock>>();
|
|
|
}
|
|
|
|
|
|
- public void CreateChunk(Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock)
|
|
|
+ public void CreateChunk(Vector3 globalPosition, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock)
|
|
|
{
|
|
|
- Chunk<TChunkBlock> chunk = new Chunk<TChunkBlock>(chunkSize, position, createChunkBlock);
|
|
|
+ Chunk<TChunkBlock> chunk = new Chunk<TChunkBlock>(chunkSize, globalPosition, position, createChunkBlock);
|
|
|
chunks.Add(position, chunk);
|
|
|
- chunkList.Add(position);
|
|
|
+ chunkList.Add(globalPosition);
|
|
|
}
|
|
|
|
|
|
- public TChunkBlock GetBlock(Vector3Int position)
|
|
|
+ public Vector3Int GetVoxelFromPosition(Vector3 position, Chunk<TChunkBlock> chunk)
|
|
|
+ {
|
|
|
+ Vector3 voxelPos = new Vector3();
|
|
|
+ voxelPos = position - chunk.globalPosition;
|
|
|
+ // voxelPos.x = position.x - (Mathf.Floor(position.x / chunkSize.x * voxelSize.x) * chunkSize.x * voxelSize.x);
|
|
|
+ // voxelPos.y = position.y - (Mathf.Floor(position.y / chunkSize.y * voxelSize.y) * chunkSize.y * voxelSize.y);
|
|
|
+ // voxelPos.z = position.z - (Mathf.Floor(position.z / chunkSize.z * voxelSize.z) * chunkSize.z * voxelSize.z);
|
|
|
+ voxelPos = new Vector3(voxelPos.x / voxelSize.x, voxelPos.y / voxelSize.y, voxelPos.z / voxelSize.z);
|
|
|
+ //voxelPos = new Vector3(voxelPos.x + 0.1f, voxelPos.y + 0.1f, voxelPos.z + 0.1f);
|
|
|
+ Vector3Int pos = new Vector3Int(Mathf.RoundToInt(voxelPos.x), Mathf.RoundToInt(voxelPos.y), Mathf.RoundToInt(voxelPos.z));
|
|
|
+ if(pos.x >= chunkSize.x || pos.y >= chunkSize.y || pos.z >= chunkSize.z) Debug.LogError($"Position is out of chunk bounds ({pos} | {position} | {chunk.position})");
|
|
|
+ pos = new Vector3Int(Mathf.Clamp(pos.x, 0, chunkSize.x - 1), Mathf.Clamp(pos.y, 0, chunkSize.y - 1), Mathf.Clamp(pos.z, 0, chunkSize.z - 1));
|
|
|
+ return pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TChunkBlock GetBlock(Vector3 position)
|
|
|
{
|
|
|
Chunk<TChunkBlock> chunk = GetChunk(position);
|
|
|
if(!chunk.isInitialized()) return default(TChunkBlock);
|
|
|
- Vector3Int pos = new Vector3Int();
|
|
|
- pos.x = position.x - (Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x);
|
|
|
- pos.y = position.y - (Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y);
|
|
|
- pos.z = position.z - (Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z);
|
|
|
- return chunk.GetBlock(pos);
|
|
|
+ return chunk.GetBlock(GetVoxelFromPosition(position, chunk));
|
|
|
}
|
|
|
|
|
|
- public void SetBlock(Vector3Int position, TChunkBlock data)
|
|
|
+ public void SetBlock(Vector3 position, TChunkBlock data)
|
|
|
{
|
|
|
Chunk<TChunkBlock> chunk = GetChunk(position);
|
|
|
if(!chunk.isInitialized()) return;
|
|
|
- Vector3Int pos = new Vector3Int();
|
|
|
- pos.x = position.x - (Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x);
|
|
|
- pos.y = position.y - (Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y);
|
|
|
- pos.z = position.z - (Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z);
|
|
|
+ Vector3Int pos = GetVoxelFromPosition(position, chunk);
|
|
|
+ if(pos.x >= chunkSize.x || pos.y >= chunkSize.y || pos.z >= chunkSize.z) return;
|
|
|
chunk.SetBlock(pos, data);
|
|
|
}
|
|
|
|
|
|
- public Chunk<TChunkBlock> GetChunk(Vector3Int position)
|
|
|
+ public Chunk<TChunkBlock> GetChunk(Vector3 position)
|
|
|
{
|
|
|
- Vector3Int pos = new Vector3Int();
|
|
|
- pos.x = Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x;
|
|
|
- pos.y = Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y;
|
|
|
- pos.z = Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z;
|
|
|
+ // Vector3Int pos = new Vector3Int();
|
|
|
+ // pos.x = Mathf.FloorToInt(position.x / chunkSize.x ) * chunkSize.x;
|
|
|
+ // pos.y = Mathf.FloorToInt(position.y / chunkSize.y ) * chunkSize.y;
|
|
|
+ // pos.z = Mathf.FloorToInt(position.z / chunkSize.z ) * chunkSize.z;
|
|
|
+ // Vector3Int pos = new Vector3Int();
|
|
|
+ // pos.x = Mathf.FloorToInt(position.x / chunkSize.x * voxelSize.x ) * chunkSize.x;
|
|
|
+ // pos.y = Mathf.FloorToInt(position.y / chunkSize.y * voxelSize.y) * chunkSize.y;
|
|
|
+ // pos.z = Mathf.FloorToInt(position.z / chunkSize.z * voxelSize.z) * chunkSize.z;
|
|
|
+
|
|
|
+ Vector3 pos = new Vector3(position.x / voxelSize.x, position.y / voxelSize.y, position.z / voxelSize.z);
|
|
|
+ Vector3Int chunkPos = new Vector3Int();
|
|
|
+ chunkPos.x = Mathf.FloorToInt(pos.x / chunkSize.x ) * chunkSize.x;
|
|
|
+ chunkPos.y = Mathf.FloorToInt(pos.y / chunkSize.y ) * chunkSize.y;
|
|
|
+ chunkPos.z = Mathf.FloorToInt(pos.z / chunkSize.z ) * chunkSize.z;
|
|
|
+ //Vector3Int chunkPos = new Vector3Int(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y), Mathf.RoundToInt(pos.z));
|
|
|
Chunk<TChunkBlock> chunk;
|
|
|
- chunks.TryGetValue(pos, out chunk);
|
|
|
+ //if(!chunks.TryGetValue(chunkPos, out chunk)) Debug.LogWarning($"Chunk not found for position {chunkPos}");
|
|
|
+ chunks.TryGetValue(chunkPos, out chunk);
|
|
|
return chunk;
|
|
|
}
|
|
|
}
|