Browse Source

Added system for saving and loading chunk updates

James Peret 2 years ago
parent
commit
64b3060d1a
2 changed files with 46 additions and 2 deletions
  1. 1 1
      Runtime/ChunkTerrainCullingSystem.cs
  2. 45 1
      Runtime/ChunkTerrainGenerator.cs

+ 1 - 1
Runtime/ChunkTerrainCullingSystem.cs

@@ -68,7 +68,7 @@ namespace KairoEngine.TerrainEngine
                 }
                 targets[i].lastPosition = chunkPos;
             }
-            //DestroyChunks();
+            DestroyChunks();
             #if UNITY_EDITOR
                 chunkTerrainGenerator.UpdateEditor ();
             #endif

+ 45 - 1
Runtime/ChunkTerrainGenerator.cs

@@ -33,6 +33,19 @@ namespace KairoEngine.TerrainEngine
 
         private bool initialized = false;
 
+        public class VoxelUpdateData
+        {
+            public Vector3 position;
+            public BlockBase block;
+
+            public VoxelUpdateData(Vector3 position, BlockBase block)
+            {
+                this.position = position;
+                this.block = block;
+            }
+        }
+        private Dictionary<Vector3,List<VoxelUpdateData>> chunkUpdates = new Dictionary<Vector3, List<VoxelUpdateData>>();
+
         public void Initialize ()
         {
             chunkSystem = new ChunkSystem<BlockBase>(chunkSize,voxelSize, true);
@@ -116,6 +129,12 @@ namespace KairoEngine.TerrainEngine
                     }
                 }
             }
+            chunkUpdates.TryGetValue(initialPos, out List<VoxelUpdateData> updates);
+            if(updates == null) return;
+            for (int i = 0; i < updates.Count; i++)
+            {
+                chunkSystem.SetBlock(updates[i].position, updates[i].block);
+            }
         }
 
         public void GenerateChunkData(Vector3 position)
@@ -306,9 +325,28 @@ namespace KairoEngine.TerrainEngine
                     block.code = code;
                     block.value = (uint)targetHeigth;
                     chunkSystem.SetBlock(voxelPos, block);
+                    // Get positions
                     Vector3Int localVoxelPos = chunkSystem.GetVoxelFromGlobalPosition(voxelPos);
-                    List<Vector3> chunks = new List<Vector3>();
                     Vector3 chunkPos = chunkSystem.GetChunkGlobalPositionFromGlobalPosition(voxelPos);
+                    // Save data update
+                    VoxelUpdateData updateData = new VoxelUpdateData(voxelPos, block);
+                    chunkUpdates.TryGetValue(chunkPos, out List<VoxelUpdateData> chunkUpdate);
+                    if(chunkUpdate != null) 
+                    {
+                        bool exists = false;
+                        for (int h = 0; h < chunkUpdate.Count; h++)
+                        {
+                            if(chunkUpdate[h].position.Equals(updateData.position))
+                            {
+                                exists = true;
+                                chunkUpdate[h].block = updateData.block;
+                            }
+                        }
+                        if(!exists) chunkUpdate.Add(updateData);
+                    }
+                    else chunkUpdates.Add(chunkPos, new List<VoxelUpdateData>{updateData});
+                    // Check if changed voxels are in the edge of the chunk and if true add the neightboring chunk to the update list
+                    List<Vector3> chunks = new List<Vector3>();
                     chunks.Add(chunkPos);
                     if(localVoxelPos.x == 0) chunks.Add(new Vector3(chunkPos.x - chunkSize.x * voxelSize.x, chunkPos.y, chunkPos.z));
                     if(localVoxelPos.x == 16) chunks.Add(new Vector3(chunkPos.x + chunkSize.x * voxelSize.x, chunkPos.y, chunkPos.z));
@@ -351,5 +389,11 @@ namespace KairoEngine.TerrainEngine
                 terrainMeshes[chunkPosition] = meshObj;
             }
         }
+
+        [Button("Save"), ButtonGroup("Buttons")]
+        public void SaveChunkUpdateData()
+        {
+
+        }
     }
 }