diff --git a/Minecraft Client/Assets/Scripts/Game/Client.cs b/Minecraft Client/Assets/Scripts/Game/Client.cs index f9df7f2..97e03c1 100644 --- a/Minecraft Client/Assets/Scripts/Game/Client.cs +++ b/Minecraft Client/Assets/Scripts/Game/Client.cs @@ -1,5 +1,7 @@ +using System; using System.Collections; using System.Collections.Generic; +using System.Data; using System.IO; using System.Net; using System.Net.Sockets; @@ -17,7 +19,7 @@ public class Client private UdpClient client; private int port; private Thread receiveThread; - + public Client(string hostname, int port, World world, string name) { this.port = port; @@ -27,7 +29,7 @@ public class Client //setup client client = new UdpClient(); client.DontFragment = false; - + if (IPAddress.TryParse(hostname, out IPAddress address)) { address = IPAddress.Parse(hostname); @@ -48,7 +50,7 @@ public class Client { sendList.Add(b); } - + Send(sendList.ToArray()); IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8051); byte[] data = client.Receive(ref endPoint); @@ -257,6 +259,7 @@ public class Client { client.Send(data, data.Length); } + public void Disconnect() { diff --git a/Minecraft Client/Assets/Scripts/Game/World.cs b/Minecraft Client/Assets/Scripts/Game/World.cs index db77556..38015e4 100644 --- a/Minecraft Client/Assets/Scripts/Game/World.cs +++ b/Minecraft Client/Assets/Scripts/Game/World.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.SceneManagement; public class World : MonoBehaviour { @@ -17,6 +18,8 @@ public class World : MonoBehaviour private Queue playersRotationsToUpdate = new Queue(); + private bool backToMenuRequest = false; + //chunks private Chunk[,] chunks; private Queue chunksToUpdate = new Queue(); @@ -91,6 +94,12 @@ public class World : MonoBehaviour g.transform.rotation = data.rotation; } } + + //go bock to menu + if (backToMenuRequest) + { + SceneManager.LoadScene("Scenes/Login", LoadSceneMode.Single); + } } //enqueue player position update to list @@ -217,15 +226,25 @@ public class World : MonoBehaviour return blockData[blockID].textures; } - //get the perlin noise height + //get the height public byte GetHeight(int x, int z) { - float scale = 0.025f; - byte perlinHeight = 10; - byte groundHeight = 10; + int xChunk = x / Data.chunkWidth; + int zChunk = z / Data.chunkWidth; - byte height = (byte)(Mathf.PerlinNoise((x) * scale + 0.1f, (z) * scale + 0.1f) * perlinHeight + groundHeight); - return height; + int xInChunk = x - (xChunk * Data.chunkWidth); + int zInChunk = z - (zChunk * Data.chunkWidth); + + Chunk c = chunks[xChunk, zChunk]; + for (int y = 0; y < Data.chunkHeight; y++) + { + if (c.blocks[xInChunk, y, zInChunk] == 0) + { + return (byte)(y - 1); + } + } + + return 250; } //edit a block in local chunk storage @@ -270,6 +289,11 @@ public class World : MonoBehaviour } + public void BackToMenu() + { + SceneManager.LoadScene("Scenes/Login", LoadSceneMode.Single); + } + public Client GetClient() { return client; diff --git a/Minecraft Server/Assets/Game/World.cs b/Minecraft Server/Assets/Game/World.cs index d465fc0..7fd2783 100644 --- a/Minecraft Server/Assets/Game/World.cs +++ b/Minecraft Server/Assets/Game/World.cs @@ -4,11 +4,13 @@ using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; +using Random = UnityEngine.Random; public class World : MonoBehaviour { //basic [SerializeField] public int worldSize; + private int randomOffsetX, randomOffsetZ; //chunks public Chunk[,] chunks; @@ -18,6 +20,9 @@ public class World : MonoBehaviour [SerializeField] private int transferDelay; private void Start() { + randomOffsetX = Random.Range(0, 10000); + randomOffsetZ = Random.Range(0, 10000); + if (File.Exists(Application.dataPath + "\\save.world")) { chunks = new Chunk[worldSize, worldSize]; @@ -88,7 +93,7 @@ public class World : MonoBehaviour //return the ID of a block public byte GetBlockID(Vector3 positionInWorld) { - byte height = GetHeight((int)positionInWorld.x, (int)positionInWorld.z); + byte height = GetHeight((int)positionInWorld.x + randomOffsetX, (int)positionInWorld.z + randomOffsetZ); //world pass if (positionInWorld.x < 0 || positionInWorld.y < 0 || positionInWorld.z < 0 || diff --git a/Minecraft Server/Assets/save.world b/Minecraft Server/Assets/save.world index e69de29..bf5a15f 100644 Binary files a/Minecraft Server/Assets/save.world and b/Minecraft Server/Assets/save.world differ diff --git a/Minecraft Server/Assets/save.world.meta b/Minecraft Server/Assets/save.world.meta deleted file mode 100644 index 69f851e..0000000 --- a/Minecraft Server/Assets/save.world.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 940041af630d8d944ac3b74467aa4ae7 -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: