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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@@ -258,6 +260,7 @@ public class Client
client.Send(data, data.Length); client.Send(data, data.Length);
} }
public void Disconnect() public void Disconnect()
{ {
Send(new byte[] { 4 }); Send(new byte[] { 4 });
+30 -6
View File
@@ -2,6 +2,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
public class World : MonoBehaviour public class World : MonoBehaviour
{ {
@@ -17,6 +18,8 @@ public class World : MonoBehaviour
private Queue<Player.PlayerRotationsUpdateData> playersRotationsToUpdate = private Queue<Player.PlayerRotationsUpdateData> playersRotationsToUpdate =
new Queue<Player.PlayerRotationsUpdateData>(); new Queue<Player.PlayerRotationsUpdateData>();
private bool backToMenuRequest = false;
//chunks //chunks
private Chunk[,] chunks; private Chunk[,] chunks;
private Queue<ChunkCoord> chunksToUpdate = new Queue<ChunkCoord>(); private Queue<ChunkCoord> chunksToUpdate = new Queue<ChunkCoord>();
@@ -91,6 +94,12 @@ public class World : MonoBehaviour
g.transform.rotation = data.rotation; g.transform.rotation = data.rotation;
} }
} }
//go bock to menu
if (backToMenuRequest)
{
SceneManager.LoadScene("Scenes/Login", LoadSceneMode.Single);
}
} }
//enqueue player position update to list //enqueue player position update to list
@@ -217,15 +226,25 @@ public class World : MonoBehaviour
return blockData[blockID].textures; return blockData[blockID].textures;
} }
//get the perlin noise height //get the height
public byte GetHeight(int x, int z) public byte GetHeight(int x, int z)
{ {
float scale = 0.025f; int xChunk = x / Data.chunkWidth;
byte perlinHeight = 10; int zChunk = z / Data.chunkWidth;
byte groundHeight = 10;
byte height = (byte)(Mathf.PerlinNoise((x) * scale + 0.1f, (z) * scale + 0.1f) * perlinHeight + groundHeight); int xInChunk = x - (xChunk * Data.chunkWidth);
return height; 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 //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() public Client GetClient()
{ {
return client; return client;
+6 -1
View File
@@ -4,11 +4,13 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using Random = UnityEngine.Random;
public class World : MonoBehaviour public class World : MonoBehaviour
{ {
//basic //basic
[SerializeField] public int worldSize; [SerializeField] public int worldSize;
private int randomOffsetX, randomOffsetZ;
//chunks //chunks
public Chunk[,] chunks; public Chunk[,] chunks;
@@ -18,6 +20,9 @@ public class World : MonoBehaviour
[SerializeField] private int transferDelay; [SerializeField] private int transferDelay;
private void Start() private void Start()
{ {
randomOffsetX = Random.Range(0, 10000);
randomOffsetZ = Random.Range(0, 10000);
if (File.Exists(Application.dataPath + "\\save.world")) if (File.Exists(Application.dataPath + "\\save.world"))
{ {
chunks = new Chunk[worldSize, worldSize]; chunks = new Chunk[worldSize, worldSize];
@@ -88,7 +93,7 @@ public class World : MonoBehaviour
//return the ID of a block //return the ID of a block
public byte GetBlockID(Vector3 positionInWorld) 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 //world pass
if (positionInWorld.x < 0 || positionInWorld.y < 0 || positionInWorld.z < 0 || 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: