From 3849435115c3f41cbb2c4e5d24e989384de721e6 Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 11:24:12 +0300 Subject: [PATCH] MbReadyMultiPlayer --- ZoFo/GameCore/Client.cs | 4 ++ ZoFo/GameCore/GUI/SelectingServerGUI.cs | 49 ++++++++++++++--------- ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs | 33 ++++++++++++--- 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index f49020f..64d088c 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -14,6 +14,8 @@ using ZoFo.GameCore.GameObjects.MapObjects.Tiles; using System.Drawing; using System.Reflection; using ZoFo.GameCore.GameObjects.Entities; +using System.Net.Sockets; +using System.Net; namespace ZoFo.GameCore { @@ -22,6 +24,8 @@ namespace ZoFo.GameCore ClientNetworkManager networkManager; public bool IsConnected { get { return networkManager.IsConnected; } } + public IPEndPoint InfoConnect => networkManager.InfoConnect; + public Client() { networkManager = new ClientNetworkManager(); diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index fafc259..f2c911c 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -21,14 +21,14 @@ public class SelectingServerGUI : AbstractGUI { int width = AppManager.Instance.CurentScreenResolution.X; int height = AppManager.Instance.CurentScreenResolution.Y; - + menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures/GUI/background/join" }; Elements.Add(menuBackground); 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"}); - TextBox ipBox = new TextBox(Manager) + 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) { rectangle = new Rectangle(width / 4 - (width / 4) / 2, height / 4, (int)(width / 4), (int)(height / 20)), text = "ip", @@ -38,13 +38,15 @@ public class SelectingServerGUI : AbstractGUI textAligment = MonogameLibrary.UI.Enums.TextAligment.Left, fontName = "Fonts/Font" }; - ipBox.TextChanged += input => { + ipBox.TextChanged += input => + { if (input == "ip") { ipBox.text = ""; ipBox.fontColor = Color.White; } }; - ipBox.StopChanging += input => { + ipBox.StopChanging += input => + { if (input.Length == 0) { ipBox.fontColor = Color.White; @@ -52,7 +54,7 @@ public class SelectingServerGUI : AbstractGUI } }; Elements.Add(ipBox); - Button joinButton = new Button(Manager) + Button joinButton = new Button(Manager) { rectangle = new Rectangle(width / 4 + (width / 4) / 2, height / 4, (int)(width / 15), (int)(height / 20)), text = "Join", @@ -61,23 +63,34 @@ public class SelectingServerGUI : AbstractGUI mainColor = Color.Gray, fontName = "Fonts/Font" }; - joinButton.LeftButtonPressed += () => + joinButton.LeftButtonPressed += () => { // join Client client = new Client(); - var endpoint = ipBox.text.Split(':'); + var endpoint = ipBox.text.Split(':'); int port; - if (int.TryParse(endpoint[1], out port)) + try { - client.JoinRoom(endpoint[0], port); - AppManager.Instance.SetClient(client); - AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); + if (int.TryParse(endpoint[1], out port)) + { + client.JoinRoom(endpoint[0], port); + AppManager.Instance.SetClient(client); + Server server = new Server(); + AppManager.Instance.SetServer(server); + AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); + } } + catch (Exception) + { + + // throw; + } + // ваш код здесь }; Elements.Add(joinButton); - Button hostButton = new Button(Manager) + Button hostButton = new Button(Manager) { rectangle = new Rectangle(width / 4 + (width / 4) / 2 + (width / 15), height / 4, (int)(width / 15), (int)(height / 20)), text = "Host", @@ -86,9 +99,9 @@ public class SelectingServerGUI : AbstractGUI mainColor = Color.Gray, fontName = "Fonts/Font" }; - hostButton.LeftButtonPressed += () => + hostButton.LeftButtonPressed += () => { - + // host Server server = new Server(); //Server Logic MultiPlayer server.CreateRoom(5); @@ -99,9 +112,9 @@ public class SelectingServerGUI : AbstractGUI AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true)); }; Elements.Add(hostButton); - + 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); bTExit.LeftButtonPressed += () => { diff --git a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs index 12a67b9..dd9cd13 100644 --- a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs +++ b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs @@ -31,12 +31,13 @@ public class WaitingForPlayersGUI : AbstractGUI menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures\\GUI\\background\\Waiting" }; Elements.Add(menuBackground); menuBackground.LoadTexture(AppManager.Instance.Content); - // 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" }; - Elements.Add(ip); + // string pcIp = + if (isHost) { - Button startButton = new Button(Manager) + 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) { rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)), text = "Start", @@ -45,14 +46,34 @@ public class WaitingForPlayersGUI : AbstractGUI mainColor = Color.Gray, fontName = "Fonts/Font" }; - startButton.LeftButtonPressed += () => + startButton.LeftButtonPressed += () => { // start - + // ваш код здесь }; 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 + + // ваш код здесь + }; + Elements.Add(waitButton); + } 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"};