Random world gen

This commit is contained in:
brausjonas
2022-05-15 19:58:14 +02:00
parent 7ccaebaf06
commit 0a0340d316
5 changed files with 42 additions and 17 deletions
@@ -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()
{
+30 -6
View File
@@ -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<Player.PlayerRotationsUpdateData> playersRotationsToUpdate =
new Queue<Player.PlayerRotationsUpdateData>();
private bool backToMenuRequest = false;
//chunks
private Chunk[,] chunks;
private Queue<ChunkCoord> chunksToUpdate = new Queue<ChunkCoord>();
@@ -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;
+6 -1
View File
@@ -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 ||
Binary file not shown.
-7
View File
@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 940041af630d8d944ac3b74467aa4ae7
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: