Players disconnect
This commit is contained in:
@@ -187,6 +187,11 @@ public class Client
|
|||||||
|
|
||||||
world.UpdatePlayerPosition(new Vector3(x, y, z), id);
|
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 class PlayerUpdateData
|
||||||
{
|
{
|
||||||
public PlayerUpdateData(byte id, Vector3 position)
|
public PlayerUpdateData(byte id, Vector3 position, bool destroy)
|
||||||
{
|
{
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.position = position;
|
this.position = position;
|
||||||
|
this.destroy = destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte id;
|
public byte id;
|
||||||
public Vector3 position;
|
public Vector3 position;
|
||||||
|
public bool destroy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,11 @@ public class World : MonoBehaviour
|
|||||||
client = new Client(ip, port, this, name);
|
client = new Client(ip, port, this, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RemovePlayer(byte id)
|
||||||
|
{
|
||||||
|
playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, Vector3.zero, true));
|
||||||
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (chunksToUpdate.Count > 0)
|
if (chunksToUpdate.Count > 0)
|
||||||
@@ -47,7 +52,8 @@ public class World : MonoBehaviour
|
|||||||
byte id = playersToCreate.Dequeue();
|
byte id = playersToCreate.Dequeue();
|
||||||
GameObject player = Instantiate(playerPrefab);
|
GameObject player = Instantiate(playerPrefab);
|
||||||
player.transform.position = new Vector3((byte)player.transform.position.x,
|
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);
|
player.transform.SetParent(gameObject.transform);
|
||||||
otherPlayers.Add(id, player);
|
otherPlayers.Add(id, player);
|
||||||
}
|
}
|
||||||
@@ -56,13 +62,21 @@ public class World : MonoBehaviour
|
|||||||
{
|
{
|
||||||
Player.PlayerUpdateData data = playersToUpdate.Dequeue();
|
Player.PlayerUpdateData data = playersToUpdate.Dequeue();
|
||||||
otherPlayers.TryGetValue(data.id, out GameObject g);
|
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)
|
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)
|
public void AddPlayer(byte id)
|
||||||
@@ -191,7 +205,7 @@ public class World : MonoBehaviour
|
|||||||
byte zInChunk = (byte)(positionInChunk.z);
|
byte zInChunk = (byte)(positionInChunk.z);
|
||||||
|
|
||||||
//edit block in chunk
|
//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);
|
chunks[chunk.x, chunk.z].Edit(xInChunk, yInChunk, zInChunk, blockID);
|
||||||
chunksToUpdate.Enqueue(new ChunkCoord(chunk.x, chunk.z));
|
chunksToUpdate.Enqueue(new ChunkCoord(chunk.x, chunk.z));
|
||||||
|
|||||||
@@ -73,6 +73,14 @@ public class Server
|
|||||||
|
|
||||||
if (tempEndPoint != null)
|
if (tempEndPoint != null)
|
||||||
{
|
{
|
||||||
|
foreach (IPEndPoint e in players)
|
||||||
|
{
|
||||||
|
if (i != GetPlayerID(e))
|
||||||
|
{
|
||||||
|
Send(new byte[]{4, (byte)i}, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
players.Remove(tempEndPoint);
|
players.Remove(tempEndPoint);
|
||||||
Console.WriteLine(playerNames[i] + " left the game" + "\n");
|
Console.WriteLine(playerNames[i] + " left the game" + "\n");
|
||||||
playerNames.RemoveAt(i);
|
playerNames.RemoveAt(i);
|
||||||
|
|||||||
Reference in New Issue
Block a user