Merge pull request #55 from progtime-net/ServerBranchMultiPlayer
Server branch multi player
This commit is contained in:
commit
d111c2f8cd
9 changed files with 115 additions and 43 deletions
|
@ -13,6 +13,8 @@ using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using ZoFo.GameCore.GameObjects.Entities;
|
using ZoFo.GameCore.GameObjects.Entities;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Net;
|
||||||
using ZoFo.GameCore.GameManagers;
|
using ZoFo.GameCore.GameManagers;
|
||||||
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
|
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
|
||||||
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
|
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
|
||||||
|
@ -30,6 +32,8 @@ namespace ZoFo.GameCore
|
||||||
ClientNetworkManager networkManager;
|
ClientNetworkManager networkManager;
|
||||||
|
|
||||||
public bool IsConnected { get { return networkManager.IsConnected; } }
|
public bool IsConnected { get { return networkManager.IsConnected; } }
|
||||||
|
public IPEndPoint InfoConnect => networkManager.InfoConnect;
|
||||||
|
|
||||||
public Client()
|
public Client()
|
||||||
{
|
{
|
||||||
networkManager = new ClientNetworkManager();
|
networkManager = new ClientNetworkManager();
|
||||||
|
|
|
@ -26,25 +26,27 @@ public class SelectingServerGUI : AbstractGUI
|
||||||
Elements.Add(menuBackground);
|
Elements.Add(menuBackground);
|
||||||
menuBackground.LoadTexture(AppManager.Instance.Content);
|
menuBackground.LoadTexture(AppManager.Instance.Content);
|
||||||
|
|
||||||
Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = "Select server", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font"});
|
Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = "Select server", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font" });
|
||||||
|
|
||||||
TextBox ipBox = new TextBox(Manager)
|
TextBox ipBox = new TextBox(Manager)
|
||||||
{
|
{
|
||||||
rectangle = new Rectangle(width / 4 - (width / 4) / 2, height / 4, (int)(width / 4), (int)(height / 20)),
|
rectangle = new Rectangle(width / 4 - (width / 4) / 2, height / 4, (int)(width / 4), (int)(height / 20)),
|
||||||
text = "ip",
|
text = "ip",
|
||||||
scale = 0.2f,
|
scale = 0.5f,
|
||||||
fontColor = Color.White,
|
fontColor = Color.White,
|
||||||
mainColor = Color.Gray,
|
mainColor = Color.Gray,
|
||||||
textAligment = MonogameLibrary.UI.Enums.TextAligment.Left,
|
textAligment = MonogameLibrary.UI.Enums.TextAligment.Left,
|
||||||
fontName = "Fonts/Font"
|
fontName = "Fonts/Font3"
|
||||||
};
|
};
|
||||||
ipBox.TextChanged += input => {
|
ipBox.TextChanged += input =>
|
||||||
|
{
|
||||||
if (input == "ip")
|
if (input == "ip")
|
||||||
{
|
{
|
||||||
ipBox.text = ""; ipBox.fontColor = Color.White;
|
ipBox.text = ""; ipBox.fontColor = Color.White;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ipBox.StopChanging += input => {
|
ipBox.StopChanging += input =>
|
||||||
|
{
|
||||||
if (input.Length == 0)
|
if (input.Length == 0)
|
||||||
{
|
{
|
||||||
ipBox.fontColor = Color.White;
|
ipBox.fontColor = Color.White;
|
||||||
|
@ -66,14 +68,23 @@ public class SelectingServerGUI : AbstractGUI
|
||||||
|
|
||||||
// join
|
// join
|
||||||
Client client = new Client();
|
Client client = new Client();
|
||||||
var endpoint = ipBox.text.Split(':');
|
var endpoint = ipBox.text.Split(':');
|
||||||
int port;
|
int port;
|
||||||
if (int.TryParse(endpoint[1], out port))
|
try
|
||||||
{
|
{
|
||||||
client.JoinRoom(endpoint[0], port);
|
if (int.TryParse(endpoint[1], out port))
|
||||||
AppManager.Instance.SetClient(client);
|
{
|
||||||
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false));
|
client.JoinRoom(endpoint[0], port);
|
||||||
|
AppManager.Instance.SetClient(client);
|
||||||
|
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
|
||||||
|
// throw;
|
||||||
|
}
|
||||||
|
|
||||||
// ваш код здесь
|
// ваш код здесь
|
||||||
};
|
};
|
||||||
Elements.Add(joinButton);
|
Elements.Add(joinButton);
|
||||||
|
@ -91,8 +102,11 @@ public class SelectingServerGUI : AbstractGUI
|
||||||
|
|
||||||
// host
|
// host
|
||||||
Server server = new Server(); //Server Logic MultiPlayer
|
Server server = new Server(); //Server Logic MultiPlayer
|
||||||
server.CreateRoom(5);
|
Client client = new Client();
|
||||||
|
server.CreateRoom(2);
|
||||||
|
client.JoinYourself(server.MyIp.Port);
|
||||||
AppManager.Instance.SetServer(server);
|
AppManager.Instance.SetServer(server);
|
||||||
|
AppManager.Instance.SetClient(client);
|
||||||
string key = server.MyIp.ToString();
|
string key = server.MyIp.ToString();
|
||||||
AppManager.Instance.debugHud.Set(key, "MultiPlayer");
|
AppManager.Instance.debugHud.Set(key, "MultiPlayer");
|
||||||
// ваш код здесь
|
// ваш код здесь
|
||||||
|
@ -101,7 +115,7 @@ public class SelectingServerGUI : AbstractGUI
|
||||||
Elements.Add(hostButton);
|
Elements.Add(hostButton);
|
||||||
|
|
||||||
Button bTExit = new Button(Manager)
|
Button bTExit = new Button(Manager)
|
||||||
{ fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off"};
|
{ fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off" };
|
||||||
Elements.Add(bTExit);
|
Elements.Add(bTExit);
|
||||||
bTExit.LeftButtonPressed += () =>
|
bTExit.LeftButtonPressed += () =>
|
||||||
{
|
{
|
||||||
|
|
|
@ -31,11 +31,15 @@ public class WaitingForPlayersGUI : AbstractGUI
|
||||||
menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures/GUI/background/Waiting" };
|
menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures/GUI/background/Waiting" };
|
||||||
Elements.Add(menuBackground);
|
Elements.Add(menuBackground);
|
||||||
menuBackground.LoadTexture(AppManager.Instance.Content);
|
menuBackground.LoadTexture(AppManager.Instance.Content);
|
||||||
|
// string pcIp =
|
||||||
|
|
||||||
// string pcIp =
|
// string pcIp =
|
||||||
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font3" };
|
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font3" };
|
||||||
Elements.Add(ip);
|
Elements.Add(ip);
|
||||||
if (isHost)
|
if (isHost)
|
||||||
{
|
{
|
||||||
|
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" };
|
||||||
|
Elements.Add(ip);
|
||||||
Button startButton = new Button(Manager)
|
Button startButton = new Button(Manager)
|
||||||
{
|
{
|
||||||
rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)),
|
rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)),
|
||||||
|
@ -48,11 +52,31 @@ public class WaitingForPlayersGUI : AbstractGUI
|
||||||
startButton.LeftButtonPressed += () =>
|
startButton.LeftButtonPressed += () =>
|
||||||
{
|
{
|
||||||
// start
|
// start
|
||||||
|
AppManager.Instance.ChangeState(GameState.HostPlaying);
|
||||||
// ваш код здесь
|
// ваш код здесь
|
||||||
};
|
};
|
||||||
Elements.Add(startButton);
|
Elements.Add(startButton);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.client.InfoConnect.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" };
|
||||||
|
Elements.Add(ip);
|
||||||
|
Button waitButton = new Button(Manager)
|
||||||
|
{
|
||||||
|
rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)),
|
||||||
|
text = "WAITING",
|
||||||
|
scale = 0.3f,
|
||||||
|
fontColor = Color.White,
|
||||||
|
mainColor = Color.Gray,
|
||||||
|
fontName = "Fonts/Font"
|
||||||
|
};
|
||||||
|
waitButton.LeftButtonPressed += () =>
|
||||||
|
{
|
||||||
|
// start
|
||||||
|
AppManager.Instance.ChangeState(GameState.ClientPlaying);
|
||||||
|
// ваш код здесь
|
||||||
|
};
|
||||||
|
Elements.Add(waitButton);
|
||||||
|
}
|
||||||
|
|
||||||
Button bTExit = new Button(Manager)
|
Button bTExit = new Button(Manager)
|
||||||
{ fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off"};
|
{ fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off"};
|
||||||
|
|
|
@ -94,7 +94,7 @@ namespace ZoFo.GameCore.GameManagers
|
||||||
Keyboard.GetState().IsKeyDown(Keys.Escape)) { server.CloseConnection(); Exit(); }
|
Keyboard.GetState().IsKeyDown(Keys.Escape)) { server.CloseConnection(); Exit(); }
|
||||||
|
|
||||||
|
|
||||||
debugHud.Set("key", "value");
|
// debugHud.Set("key", "value");
|
||||||
|
|
||||||
InputManager.Update();
|
InputManager.Update();
|
||||||
currentGUI.Update(gameTime);
|
currentGUI.Update(gameTime);
|
||||||
|
@ -107,7 +107,7 @@ namespace ZoFo.GameCore.GameManagers
|
||||||
client.Update(gameTime);
|
client.Update(gameTime);
|
||||||
break;
|
break;
|
||||||
case GameState.ClientPlaying:
|
case GameState.ClientPlaying:
|
||||||
server.Update(gameTime);
|
client.Update(gameTime);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -108,7 +108,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
while(socket.Connected)
|
while(socket.Connected)
|
||||||
{
|
{
|
||||||
byte[] bytes = new byte[2048];
|
byte[] bytes = new byte[2048];
|
||||||
var countAnsw = socket.Receive(bytes);
|
var countAnsw = socket.Receive(bytes); //Вылетает если кто то закрыл
|
||||||
string update = Encoding.UTF8.GetString(bytes, 0, countAnsw); // обновление отосланные сервером
|
string update = Encoding.UTF8.GetString(bytes, 0, countAnsw); // обновление отосланные сервером
|
||||||
GetDataSent(update);
|
GetDataSent(update);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
public static IPAddress GetIp()
|
public static IPAddress GetIp()
|
||||||
{
|
{
|
||||||
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
|
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
|
||||||
string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP
|
var ipList = Dns.GetHostByName(hostName).AddressList;
|
||||||
|
string myIP = ipList[ipList.Count()-1].ToString();// Get the IP
|
||||||
return IPAddress.Parse(myIP);
|
return IPAddress.Parse(myIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +70,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
}
|
}
|
||||||
updates.Clear();
|
updates.Clear();
|
||||||
return; //TODO TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK
|
return; //TODO TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK
|
||||||
|
//Что это?
|
||||||
//по 10 паков за раз TODO FIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXIT
|
//по 10 паков за раз TODO FIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXIT
|
||||||
List<UpdateData> datasToSend = new List<UpdateData>();
|
List<UpdateData> datasToSend = new List<UpdateData>();
|
||||||
for (int i = 0; i < 5 && i<updates.Count; i++)
|
for (int i = 0; i < 5 && i<updates.Count; i++)
|
||||||
|
@ -141,13 +142,16 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
for (int i = 0; i < playNumber; i++)
|
for (int i = 0; i < playNumber; i++)
|
||||||
{
|
{
|
||||||
Socket client = socket.Accept();
|
Socket client = socket.Accept();
|
||||||
|
AppManager.Instance.debugHud.Log($"Connect {client.LocalEndPoint.ToString()}");
|
||||||
Thread thread = new Thread(StartListening);
|
Thread thread = new Thread(StartListening);
|
||||||
thread.IsBackground = true;
|
thread.IsBackground = true;
|
||||||
thread.Start(client);
|
thread.Start(client);
|
||||||
managerThread.Add(client, thread);
|
managerThread.Add(client, thread);
|
||||||
clients.Add(client); //добавляем клиентов в лист
|
clients.Add(client);
|
||||||
|
//AppManager.Instance.ChangeState(GameState.HostPlaying);
|
||||||
|
//добавляем клиентов в лист
|
||||||
}
|
}
|
||||||
|
AppManager.Instance.ChangeState(GameState.HostPlaying);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -5,6 +5,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class UpdateInteraction : UpdateData
|
public class UpdateInteraction : UpdateData
|
||||||
{
|
{
|
||||||
|
public UpdateInteraction() { UpdateType = "UpdateInteraction"; }
|
||||||
public UpdateInteraction(int id)
|
public UpdateInteraction(int id)
|
||||||
{
|
{
|
||||||
IdEntity = id;
|
IdEntity = id;
|
||||||
|
|
|
@ -9,6 +9,6 @@ public class UpdateInteractionReady(int idEntity, bool isReady)
|
||||||
: UpdateData
|
: UpdateData
|
||||||
{
|
{
|
||||||
public int IdEntity { get; set; } = idEntity;
|
public int IdEntity { get; set; } = idEntity;
|
||||||
public string UpdateType { get; set; }
|
public string UpdateType { get; set; } = "UpdateInteractionReady";
|
||||||
public bool IsReady { get; set; } = isReady;
|
public bool IsReady { get; set; } = isReady;
|
||||||
}
|
}
|
|
@ -28,7 +28,6 @@ namespace ZoFo.GameCore
|
||||||
private ServerNetworkManager networkManager;
|
private ServerNetworkManager networkManager;
|
||||||
private int ticks = 0;
|
private int ticks = 0;
|
||||||
public IPEndPoint MyIp { get { return networkManager.InfoConnect; } }
|
public IPEndPoint MyIp { get { return networkManager.InfoConnect; } }
|
||||||
|
|
||||||
public Server()
|
public Server()
|
||||||
{
|
{
|
||||||
networkManager = new ServerNetworkManager();
|
networkManager = new ServerNetworkManager();
|
||||||
|
@ -56,6 +55,32 @@ namespace ZoFo.GameCore
|
||||||
{
|
{
|
||||||
|
|
||||||
//ТУТ Switch case будет честное слово
|
//ТУТ Switch case будет честное слово
|
||||||
|
switch (updateData.UpdateType)
|
||||||
|
{
|
||||||
|
case "UpdateAnimation":
|
||||||
|
break;
|
||||||
|
case "UpdateEntityHealth":
|
||||||
|
break;
|
||||||
|
case "UpdateGameEnded":
|
||||||
|
break;
|
||||||
|
case "UpdateGameObjectCreated":
|
||||||
|
break;
|
||||||
|
case "UpdateGameObjectDeleted":
|
||||||
|
break;
|
||||||
|
case "UpdateInteraction":
|
||||||
|
break;
|
||||||
|
case "UpdateInteractionReady":
|
||||||
|
break;
|
||||||
|
case "UpdateLoot":
|
||||||
|
break;
|
||||||
|
case "UpdatePlayerParametrs":
|
||||||
|
break;
|
||||||
|
case "UpdatePosition":
|
||||||
|
break;
|
||||||
|
case "UpdateTileCreated":
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CloseConnection()
|
public void CloseConnection()
|
||||||
|
|
Loading…
Add table
Reference in a new issue