Bug Fixing

Es können keine blöcke mehr "in" einen selbst oder in andere spieler gebaut werden
This commit is contained in:
brausjonas
2022-05-13 18:00:50 +02:00
parent 04e06652f5
commit 7ccaebaf06
5 changed files with 93 additions and 10 deletions
@@ -32,7 +32,7 @@ TextureImporter:
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
filterMode: 0
aniso: 1
mipBias: 0
wrapU: 0
@@ -75,6 +75,18 @@ TextureImporter:
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
@@ -184,11 +184,13 @@ public class Client
world.EditBlock(chunk, positionInChunk, blockID);
}
//add a new player
if (data[0] == 5)
{
world.AddPlayer(data[1]);
}
//update a players position
if (data[0] == 7)
{
byte[] positionASCII = new byte[data.Length - 2];
@@ -207,11 +209,13 @@ public class Client
world.UpdatePlayerPosition(new Vector3(x, y, z), id);
}
//remove a player from local world
if (data[0] == 4)
{
world.RemovePlayer(data[1]);
}
//update a players rotation
if (data[0] == 8)
{
byte[] rotationASCII = new byte[data.Length - 2];
@@ -234,6 +238,7 @@ public class Client
}
//sends an edit block request to server
public void EditBlock(Vector3 positionInWorld, byte blockID)
{
ChunkCoord chunk = new ChunkCoord((int)(positionInWorld.x / Data.chunkWidth),
@@ -247,6 +252,7 @@ public class Client
Send(new byte[] { 3, (byte)chunk.x, (byte)chunk.z, xInChunk, yInChunk, zInChunk, blockID });
}
//default send method
private void Send(byte[] data)
{
client.Send(data, data.Length);
@@ -33,6 +33,8 @@ public class Player : MonoBehaviour
private void Start()
{
client = world.GetClient();
//lock the cursor
Cursor.lockState = CursorLockMode.Locked;
camera = GetComponentInChildren<Camera>();
transform.position = new Vector3((byte)transform.position.x,
@@ -41,6 +43,7 @@ public class Player : MonoBehaviour
private void Jump()
{
//add velocity up
verticalMomentum = jumpForce;
isGrounded = false;
jumpRequest = false;
@@ -48,6 +51,7 @@ public class Player : MonoBehaviour
private void FixedUpdate()
{
//if space was pressed
if (jumpRequest)
{
Jump();
@@ -56,6 +60,7 @@ public class Player : MonoBehaviour
CalculateVelocity();
transform.Translate(velocity, Space.World);
//check if position or rotation had changed and send an update
Vector3 position = transform.position;
Quaternion rotation = transform.rotation;
@@ -76,6 +81,7 @@ public class Player : MonoBehaviour
private void Update()
{
//get the player input
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
mouseX = Input.GetAxis("Mouse X");
@@ -86,6 +92,7 @@ public class Player : MonoBehaviour
jumpRequest = true;
}
//get the camera up and down movement and clamp it
rotX -= mouseY * 1.4f;
rotX = Mathf.Clamp(rotX, -90, 90);
@@ -93,6 +100,7 @@ public class Player : MonoBehaviour
transform.Rotate(0, mouseX, 0);
//send destroy block request to server
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit))
@@ -102,6 +110,7 @@ public class Player : MonoBehaviour
}
}
//send build block request to server
if (Input.GetMouseButtonDown(1))
{
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit))
@@ -114,24 +123,29 @@ public class Player : MonoBehaviour
private void CalculateVelocity()
{
//check if player can accelerate any faster (there is a max speed)
if (verticalMomentum > gravity)
{
verticalMomentum += Time.fixedDeltaTime * gravity;
}
//add velocity for moving, falling and jumping
velocity = ((transform.forward * vertical) + (transform.right * horizontal)) * Time.fixedDeltaTime * walkSpeed;
velocity += Vector3.up * verticalMomentum * Time.fixedDeltaTime;
//check colliders on z axis
if ((velocity.z > 0 && front) || (velocity.z < 0 && back))
{
velocity.z = 0;
}
//check colliders on x axis
if ((velocity.x > 0 && right) || (velocity.x < 0 && left))
{
velocity.x = 0;
}
//check colliders on y axis
if (velocity.y < 0)
{
velocity.y = CheckDownSpeed(velocity.y);
@@ -146,6 +160,7 @@ public class Player : MonoBehaviour
}
}
//check colliders for -y
private float CheckDownSpeed(float downSpeed)
{
if (
@@ -163,6 +178,7 @@ public class Player : MonoBehaviour
return downSpeed;
}
//check colliders for +y
private float CheckUpSpeed(float upSpeed)
{
if (
@@ -178,6 +194,7 @@ public class Player : MonoBehaviour
return upSpeed;
}
//check colliders for +z
public bool front
{
get
@@ -194,6 +211,7 @@ public class Player : MonoBehaviour
}
}
//check colliders for -z
public bool back
{
get
@@ -210,6 +228,7 @@ public class Player : MonoBehaviour
}
}
//check colliders for -x
public bool left
{
get
@@ -226,6 +245,7 @@ public class Player : MonoBehaviour
}
}
//check colliders for +x
public bool right
{
get
@@ -242,6 +262,7 @@ public class Player : MonoBehaviour
}
}
//stores update data for position and if a player should be removed from the world
public class PlayerPositionUpdateData
{
public PlayerPositionUpdateData(byte id, Vector3 position, bool destroy)
@@ -256,6 +277,7 @@ public class Player : MonoBehaviour
public bool destroy;
}
//stores update data for rotations
public class PlayerRotationsUpdateData
{
public byte id;
+14 -1
View File
@@ -31,12 +31,14 @@ public class World : MonoBehaviour
private void Start()
{
//get default information
name = PlayerPrefs.GetString("userName");
ip = PlayerPrefs.GetString("serverIP");
port = int.Parse(PlayerPrefs.GetString("port"));
client = new Client(ip, port, this, name);
}
//removes a player from the game world
public void RemovePlayer(byte id)
{
playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, Vector3.zero, true));
@@ -44,12 +46,14 @@ public class World : MonoBehaviour
private void Update()
{
//update 1 chunk in list
if (chunksToUpdate.Count > 0)
{
ChunkCoord chunk = chunksToUpdate.Dequeue();
chunks[chunk.x, chunk.z].Update();
}
//create 1 player in list
if (playersToCreate.Count > 0)
{
byte id = playersToCreate.Dequeue();
@@ -61,6 +65,7 @@ public class World : MonoBehaviour
otherPlayers.Add(id, player);
}
//update 1 player position in list
if (playersPositionsToUpdate.Count > 0)
{
Player.PlayerPositionUpdateData data = playersPositionsToUpdate.Dequeue();
@@ -76,6 +81,7 @@ public class World : MonoBehaviour
}
}
//update 1 player rotation in list
if (playersRotationsToUpdate.Count > 0)
{
Player.PlayerRotationsUpdateData data = playersRotationsToUpdate.Dequeue();
@@ -87,32 +93,37 @@ public class World : MonoBehaviour
}
}
//enqueue player position update to list
public void UpdatePlayerPosition(Vector3 position, byte id)
{
playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, position, false));
}
//enqueue player rotation update to list
public void UpdatePlayerRotation(Quaternion rotation, byte id)
{
playersRotationsToUpdate.Enqueue(new Player.PlayerRotationsUpdateData(id, rotation));
}
//enqueue player create data to list
public void AddPlayer(byte id)
{
playersToCreate.Enqueue(id);
}
//creates the array for chunk objects
public void CreateChunkArray()
{
chunks = new Chunk[worldSize, worldSize];
}
//initialized a chunk
public void InitChunk(int x, int z, byte[,,] blocks)
{
chunks[x, z] = new Chunk(material, new ChunkCoord(x, z), this, blocks);
}
//creates an array of chunks
//initialized the chunk objects
public void CreateChunks()
{
for (int x = 0; x < worldSize; x++)
@@ -206,6 +217,7 @@ public class World : MonoBehaviour
return blockData[blockID].textures;
}
//get the perlin noise height
public byte GetHeight(int x, int z)
{
float scale = 0.025f;
@@ -216,6 +228,7 @@ public class World : MonoBehaviour
return height;
}
//edit a block in local chunk storage
public void EditBlock(ChunkCoord chunk, Vector3 positionInChunk, byte blockID)
{
byte xInChunk = (byte)(positionInChunk.x);
+38 -8
View File
@@ -89,12 +89,14 @@ public class Server
}
}
//init message / world transfer
if (data[0] == 0)
{
Thread t = new Thread(TransferWorld);
t.Start(endPoint);
}
//get the world size (first message from client)
if (data[0] == 2)
{
Send(new byte[] { 2, (byte)world.worldSize }, endPoint);
@@ -112,24 +114,43 @@ public class Server
playerPositions.Add(new Vector3(0, 0, 0));
}
//edit a block in world
if (data[0] == 3)
{
ChunkCoord chunk = new ChunkCoord(data[1], data[2]);
Vector3 positionInChunk = new Vector3(data[3], data[4], data[5]);
byte blockID = data[6];
Vector3 positionInWorld = new Vector3(chunk.x * Data.chunkWidth + positionInChunk.x,
positionInChunk.y, chunk.z * Data.chunkWidth + positionInChunk.z);
world.EditBlock(chunk, positionInChunk, blockID);
foreach (IPEndPoint e in players)
//check that there is no player on the block position
bool found = false;
foreach (Vector3 v in playerPositions)
{
Send(new byte[]
if (((int)v.x == (int)positionInWorld.x && (int)v.z == (int)positionInWorld.z) &&
((int)v.y == (int)positionInWorld.y || (int)v.y + 1 == (int)positionInWorld.y))
{
3, (byte)chunk.x, (byte)chunk.z,
(byte)positionInChunk.x, (byte)positionInChunk.y, (byte)positionInChunk.z, blockID
}, e);
found = true;
}
}
if (!found)
{
byte blockID = data[6];
world.EditBlock(chunk, positionInChunk, blockID);
foreach (IPEndPoint e in players)
{
Send(new byte[]
{
3, (byte)chunk.x, (byte)chunk.z,
(byte)positionInChunk.x, (byte)positionInChunk.y, (byte)positionInChunk.z, blockID
}, e);
}
}
}
//a player successfully joined the game...
if (data[0] == 6)
{
int id = GetPlayerID(endPoint);
@@ -139,12 +160,15 @@ public class Server
{
if (id != GetPlayerID(e))
{
//create player objects of that player in every connected client
Send(new byte[]{5, (byte)GetPlayerID(e)}, endPoint);
//create player objects of all connected clients in the currently connected client
Send(new byte[]{5, (byte)GetPlayerID(endPoint)}, e);
}
}
}
//update a players position on the server
if (data[0] == 7)
{
byte[] positionASCII = new byte[data.Length - 1];
@@ -172,6 +196,7 @@ public class Server
send[i] = sendPositionASCII[i - 2];
}
//send update to every other client
foreach (IPEndPoint e in players)
{
if (id != GetPlayerID(e))
@@ -181,6 +206,7 @@ public class Server
}
}
//update a players rotation on server
if (data[0] == 8)
{
byte[] rotationASCII = new byte[data.Length - 1];
@@ -207,6 +233,7 @@ public class Server
send[i] = sendRotationASCII[i - 2];
}
//send update to every other client
foreach (IPEndPoint e in players)
{
if (id != GetPlayerID(e))
@@ -223,6 +250,7 @@ public class Server
}
}
//returns the id of an player
private int GetPlayerID(IPEndPoint endPoint)
{
int i = 0;
@@ -239,6 +267,7 @@ public class Server
return -1;
}
//starts a tcp client for world transfer
private void TransferWorld(object o)
{
try
@@ -290,6 +319,7 @@ public class Server
}
}
//default send function
private void Send(byte[] data, IPEndPoint endPoint)
{
client.Send(data, data.Length, endPoint);