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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class World : MonoBehaviour
|
|||||||
|
|
||||||
//blocks
|
//blocks
|
||||||
[SerializeField] private BlockData[] blockData;
|
[SerializeField] private BlockData[] blockData;
|
||||||
|
|
||||||
//network
|
//network
|
||||||
private Client client;
|
private Client client;
|
||||||
private string ip;
|
private string ip;
|
||||||
@@ -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)
|
||||||
@@ -79,7 +93,7 @@ public class World : MonoBehaviour
|
|||||||
{
|
{
|
||||||
chunks[x, z] = new Chunk(material, new ChunkCoord(x, z), this, blocks);
|
chunks[x, z] = new Chunk(material, new ChunkCoord(x, z), this, blocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
//creates an array of chunks
|
//creates an array of chunks
|
||||||
public void CreateChunks()
|
public void CreateChunks()
|
||||||
{
|
{
|
||||||
@@ -99,7 +113,7 @@ public class World : MonoBehaviour
|
|||||||
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, (int)positionInWorld.z);
|
||||||
|
|
||||||
//world pass
|
//world pass
|
||||||
if (positionInWorld.x < 0 || positionInWorld.y < 0 || positionInWorld.z < 0 ||
|
if (positionInWorld.x < 0 || positionInWorld.y < 0 || positionInWorld.z < 0 ||
|
||||||
positionInWorld.x >= worldSize * Data.chunkWidth || positionInWorld.y > height ||
|
positionInWorld.x >= worldSize * Data.chunkWidth || positionInWorld.y > height ||
|
||||||
@@ -123,7 +137,7 @@ public class World : MonoBehaviour
|
|||||||
{
|
{
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((int)positionInWorld.y < height)
|
if ((int)positionInWorld.y < height)
|
||||||
{
|
{
|
||||||
return 4;
|
return 4;
|
||||||
@@ -131,7 +145,7 @@ public class World : MonoBehaviour
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//returns if a block is on the position
|
//returns if a block is on the position
|
||||||
public bool CheckBlock(Vector3 positionInWorld)
|
public bool CheckBlock(Vector3 positionInWorld)
|
||||||
{
|
{
|
||||||
@@ -142,7 +156,7 @@ public class World : MonoBehaviour
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (positionInWorld.x < 0 || positionInWorld.y < 0 || positionInWorld.z < 0 ||
|
if (positionInWorld.x < 0 || positionInWorld.y < 0 || positionInWorld.z < 0 ||
|
||||||
positionInWorld.x >= worldSize * Data.chunkWidth || positionInWorld.y >= Data.chunkHeight ||
|
positionInWorld.x >= worldSize * Data.chunkWidth || positionInWorld.y >= Data.chunkHeight ||
|
||||||
positionInWorld.z >= worldSize * Data.chunkWidth)
|
positionInWorld.z >= worldSize * Data.chunkWidth)
|
||||||
@@ -167,13 +181,13 @@ public class World : MonoBehaviour
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return textures array of a block
|
//return textures array of a block
|
||||||
public byte[] GetTextures(byte blockID)
|
public byte[] GetTextures(byte blockID)
|
||||||
{
|
{
|
||||||
return blockData[blockID].textures;
|
return blockData[blockID].textures;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte GetHeight(int x, int z)
|
public byte GetHeight(int x, int z)
|
||||||
{
|
{
|
||||||
float scale = 0.025f;
|
float scale = 0.025f;
|
||||||
@@ -189,9 +203,9 @@ public class World : MonoBehaviour
|
|||||||
byte xInChunk = (byte)(positionInChunk.x);
|
byte xInChunk = (byte)(positionInChunk.x);
|
||||||
byte yInChunk = (byte)positionInChunk.y;
|
byte yInChunk = (byte)positionInChunk.y;
|
||||||
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));
|
||||||
@@ -237,4 +251,4 @@ public class World : MonoBehaviour
|
|||||||
public string name;
|
public string name;
|
||||||
public byte[] textures = new byte[6];
|
public byte[] textures = new byte[6];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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