diff --git a/Minecraft Server/.idea/.idea.Minecraft Server/.idea/vcs.xml b/Minecraft Server/.idea/.idea.Minecraft Server/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Minecraft Server/.idea/.idea.Minecraft Server/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Minecraft Server/Assets/Game/Server.cs b/Minecraft Server/Assets/Game/Server.cs index 8a411d3..e3b8e8a 100644 --- a/Minecraft Server/Assets/Game/Server.cs +++ b/Minecraft Server/Assets/Game/Server.cs @@ -38,7 +38,7 @@ public class Server //info: //[0] = 0: init message - //[0] = 1: world transfer [1] = 1: finished + //[0] = 1: world transfer [1] = 1: finished, to client //[0] = 2: world size //[0] = 3: edit block //[0] = 4: disconnect player diff --git a/Minecraft Server/Assets/Game/World.cs b/Minecraft Server/Assets/Game/World.cs index 146a375..597f9a8 100644 --- a/Minecraft Server/Assets/Game/World.cs +++ b/Minecraft Server/Assets/Game/World.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; +using System.Text; using UnityEngine; public class World : MonoBehaviour @@ -14,10 +16,48 @@ public class World : MonoBehaviour //network private Server server; [SerializeField] private int transferDelay; - private void Start() { - CreateChunks(); + if (File.Exists(Application.dataPath + "\\save.world")) + { + chunks = new Chunk[worldSize, worldSize]; + for (int x = 0; x < worldSize; x++) + { + for (int z = 0; z < worldSize; z++) + { + chunks[x, z] = new Chunk(new ChunkCoord(x, z), this); + chunks[x, z].gameObject.transform.SetParent(gameObject.transform); + } + } + + StreamReader reader = new StreamReader(Application.dataPath + "\\save.world"); + for (int xChunk = 0; xChunk < worldSize; xChunk++) + { + for (int zChunk = 0; zChunk < worldSize; zChunk++) + { + byte[] save = Encoding.ASCII.GetBytes(reader.ReadLine()); + + int i = 0; + + for (int y = 0; y < Data.chunkHeight; y++) + { + for (int x = 0; x < Data.chunkWidth; x++) + { + for (int z = 0; z < Data.chunkWidth; z++) + { + chunks[xChunk, zChunk].blocks[x, y, z] = save[i]; + i++; + } + } + } + } + } + reader.Close(); + } + else + { + CreateChunks(); + } server = new Server(this, transferDelay); } @@ -43,6 +83,7 @@ public class World : MonoBehaviour } } + //return the ID of a block public byte GetBlockID(Vector3 positionInWorld) { @@ -141,11 +182,37 @@ public class World : MonoBehaviour private void OnApplicationQuit() { + SaveWorld(); server.Disconnect(); } private void SaveWorld() { + StreamWriter writer = new StreamWriter(Application.dataPath + "\\save.world", false); + for (int xChunk = 0; xChunk < worldSize; xChunk++) + { + for (int zChunk = 0; zChunk < worldSize; zChunk++) + { + byte[] save = new byte[Data.chunkWidth * Data.chunkWidth * Data.chunkHeight]; + int i = 0; + + for (int y = 0; y < Data.chunkHeight; y++) + { + for (int x = 0; x < Data.chunkWidth; x++) + { + for (int z = 0; z < Data.chunkWidth; z++) + { + save[i] = chunks[xChunk, zChunk].blocks[x, y, z]; + i++; + } + } + } + + writer.WriteLine(Encoding.ASCII.GetString(save)); + writer.Flush(); + } + } + writer.Close(); } } diff --git a/Minecraft Server/Assets/save.world b/Minecraft Server/Assets/save.world new file mode 100644 index 0000000..ac9f586 Binary files /dev/null and b/Minecraft Server/Assets/save.world differ diff --git a/Minecraft Server/Assets/save.world.meta b/Minecraft Server/Assets/save.world.meta new file mode 100644 index 0000000..69f851e --- /dev/null +++ b/Minecraft Server/Assets/save.world.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 940041af630d8d944ac3b74467aa4ae7 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: