Players disconnect

This commit is contained in:
brausjonas
2022-05-13 16:51:55 +02:00
parent fb04675716
commit a36cf65f6d
4 changed files with 44 additions and 15 deletions
@@ -187,6 +187,11 @@ public class Client
world.UpdatePlayerPosition(new Vector3(x, y, z), id);
}
if (data[0] == 4)
{
world.RemovePlayer(data[1]);
}
}
}
@@ -235,14 +235,16 @@ public class Player : MonoBehaviour
public class PlayerUpdateData
{
public PlayerUpdateData(byte id, Vector3 position)
public PlayerUpdateData(byte id, Vector3 position, bool destroy)
{
this.id = id;
this.position = position;
this.destroy = destroy;
}
public byte id;
public Vector3 position;
public bool destroy;
}
}
+18 -4
View File
@@ -34,6 +34,11 @@ public class World : MonoBehaviour
client = new Client(ip, port, this, name);
}
public void RemovePlayer(byte id)
{
playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, Vector3.zero, true));
}
private void Update()
{
if (chunksToUpdate.Count > 0)
@@ -47,7 +52,8 @@ public class World : MonoBehaviour
byte id = playersToCreate.Dequeue();
GameObject player = Instantiate(playerPrefab);
player.transform.position = new Vector3((byte)player.transform.position.x,
GetHeight((byte)player.transform.position.x, (byte)player.transform.position.z) + 2, (byte)player.transform.position.z);
GetHeight((byte)player.transform.position.x, (byte)player.transform.position.z) + 2,
(byte)player.transform.position.z);
player.transform.SetParent(gameObject.transform);
otherPlayers.Add(id, player);
}
@@ -56,13 +62,21 @@ public class World : MonoBehaviour
{
Player.PlayerUpdateData data = playersToUpdate.Dequeue();
otherPlayers.TryGetValue(data.id, out GameObject g);
g.transform.position = data.position;
if (g != null)
{
g.transform.position = data.position;
if (data.destroy)
{
Destroy(g);
otherPlayers.Remove(data.id);
}
}
}
}
public void UpdatePlayerPosition(Vector3 position, byte id)
{
playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, position));
playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, position, false));
}
public void AddPlayer(byte id)
@@ -191,7 +205,7 @@ public class World : MonoBehaviour
byte zInChunk = (byte)(positionInChunk.z);
//edit block in chunk
if(chunks[chunk.x, chunk.z].blocks[xInChunk, yInChunk, zInChunk] != 1)
if (chunks[chunk.x, chunk.z].blocks[xInChunk, yInChunk, zInChunk] != 1)
{
chunks[chunk.x, chunk.z].Edit(xInChunk, yInChunk, zInChunk, blockID);
chunksToUpdate.Enqueue(new ChunkCoord(chunk.x, chunk.z));
+8
View File
@@ -73,6 +73,14 @@ public class Server
if (tempEndPoint != null)
{
foreach (IPEndPoint e in players)
{
if (i != GetPlayerID(e))
{
Send(new byte[]{4, (byte)i}, e);
}
}
players.Remove(tempEndPoint);
Console.WriteLine(playerNames[i] + " left the game" + "\n");
playerNames.RemoveAt(i);