|
@@ -0,0 +1,58 @@
|
|
|
|
+using System;
|
|
|
|
+using System.Collections;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using UnityEngine;
|
|
|
|
+
|
|
|
|
+namespace KairoEngine.Chunks
|
|
|
|
+{
|
|
|
|
+ public class ChunkSystem<TChunkBlock>
|
|
|
|
+ {
|
|
|
|
+ public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
|
|
|
|
+ public Dictionary<Vector3Int, Chunk<TChunkBlock>> chunks;
|
|
|
|
+
|
|
|
|
+ public ChunkSystem(Vector3Int chunkSize, bool debug = false)
|
|
|
|
+ {
|
|
|
|
+ this.chunkSize = chunkSize;
|
|
|
|
+ this.chunks = new Dictionary<Vector3Int, Chunk<TChunkBlock>>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void CreateChunk(Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock)
|
|
|
|
+ {
|
|
|
|
+ Chunk<TChunkBlock> chunk = new Chunk<TChunkBlock>(chunkSize, position, createChunkBlock);
|
|
|
|
+ chunks.Add(position, chunk);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TChunkBlock GetBlock(Vector3Int 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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void SetBlock(Vector3Int 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);
|
|
|
|
+ chunk.SetBlock(pos, data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Chunk<TChunkBlock> GetChunk(Vector3Int 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;
|
|
|
|
+ Chunk<TChunkBlock> chunk;
|
|
|
|
+ chunks.TryGetValue(pos, out chunk);
|
|
|
|
+ return chunk;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|