player sync

This commit is contained in:
brausjonas
2022-05-13 14:21:30 +02:00
parent 4e625ea185
commit 1fe20dbb5d
4 changed files with 79 additions and 2 deletions
+27 -2
View File
@@ -12,6 +12,7 @@ public class Server
{
//game
private World world;
private List<Vector3> playerPositions = new List<Vector3>();
//network
private UdpClient client;
@@ -43,7 +44,7 @@ public class Server
//[0] = 4: disconnect player
//[0] = 5: create new player in client
//[0] = 6: player ready
//[0] = 7:
//[0] = 7: player position update
private void Receive()
{
@@ -75,7 +76,7 @@ public class Server
players.Remove(tempEndPoint);
Console.WriteLine(playerNames[i] + " left the game" + "\n");
playerNames.RemoveAt(i);
playerPositions.RemoveAt(i);
}
}
@@ -99,6 +100,7 @@ public class Server
//add player
players.Add(endPoint);
playerNames.Add(name);
playerPositions.Add(new Vector3(0, 0, 0));
}
if (data[0] == 3)
@@ -133,6 +135,29 @@ public class Server
}
}
}
if (data[0] == 7)
{
string xS = data[1] + "," + data[2];
string yS = data[3] + "," + data[4];
string zS = data[5] + "," + data[6];
float x = float.Parse(xS);
float y = float.Parse(yS);
float z = float.Parse(zS);
int id = GetPlayerID(endPoint);
playerPositions[id] = new Vector3(x, y, z);
foreach (IPEndPoint e in players)
{
if (id != GetPlayerID(e))
{
Send(new byte[]{7, data[1], data[2], data[3], data[4], data[5], data[6], (byte)id}, e);
}
}
}
}
catch (Exception e)
{