Kaynağa Gözat

Refactored ChunkSystem positioning

James Peret 2 yıl önce
ebeveyn
işleme
375ab3e5cb
1 değiştirilmiş dosya ile 41 ekleme ve 21 silme
  1. 41 21
      Runtime/ChunkSystem.cs

+ 41 - 21
Runtime/ChunkSystem.cs

@@ -27,58 +27,78 @@ namespace KairoEngine.Chunks
             chunkList.Add(globalPosition);
         }
 
-        public Vector3Int GetVoxelFromPosition(Vector3 position, Chunk<TChunkBlock> chunk)
+        public void DestroyChunk(Vector3 globalPosition)
+        {
+            Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(globalPosition);
+            chunks.Remove(chunkPos);
+            chunkList.Remove(globalPosition);
+        }
+
+        public Vector3Int GetVoxelFromGlobalPosition(Vector3 position)
         {
             Vector3 voxelPos = new Vector3();
-            voxelPos = position - chunk.globalPosition;
+            Vector3 chunkPos = GetChunkGlobalPositionFromGlobalPosition(position);
+            voxelPos = position - chunkPos;
             // 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})");
+            Vector3Int pos = new Vector3Int(Mathf.FloorToInt(voxelPos.x), Mathf.FloorToInt(voxelPos.y), Mathf.FloorToInt(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} | {chunkPos})");
             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 Vector3Int GetChunkPositionFromGlobalPosition(Vector3 position)
+        {
+            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;
+            return chunkPos;
+        }
+
+        public Vector3 GetChunkGlobalPositionFromGlobalPosition(Vector3 position)
+        {
+            Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(position);
+            Vector3 pos = new Vector3(chunkPos.x * voxelSize.x, chunkPos.y * voxelSize.y, chunkPos.z * voxelSize.z);
+            return pos;
+        }
+
         public TChunkBlock GetBlock(Vector3 position)
         {
             Chunk<TChunkBlock> chunk = GetChunk(position);
             if(!chunk.isInitialized()) return default(TChunkBlock);
-            return chunk.GetBlock(GetVoxelFromPosition(position, chunk));
+            return chunk.GetBlock(GetVoxelFromGlobalPosition(position));
         }
 
         public void SetBlock(Vector3 position, TChunkBlock data)
         {
             Chunk<TChunkBlock> chunk = GetChunk(position);
             if(!chunk.isInitialized()) return;
-            Vector3Int pos = GetVoxelFromPosition(position, chunk);
+            Vector3Int pos = GetVoxelFromGlobalPosition(position);
             if(pos.x >= chunkSize.x || pos.y >= chunkSize.y || pos.z >= chunkSize.z) return;
             chunk.SetBlock(pos, data);
         }
 
         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 * 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));
+            Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(position);
             Chunk<TChunkBlock> chunk;
             //if(!chunks.TryGetValue(chunkPos, out chunk)) Debug.LogWarning($"Chunk not found for position {chunkPos}");
             chunks.TryGetValue(chunkPos, out chunk);
             return chunk;
         }
+
+        public bool ChunkExists(Vector3 position)
+        {
+            Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(position);
+            Chunk<TChunkBlock> chunk;
+            bool value = chunks.TryGetValue(chunkPos, out chunk);
+            return value;
+        }
     }
 }