From ea4a2f31fc933db131b7fb2254780d39b3ebcb9d Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Thu, 17 Aug 2023 11:49:12 +0300 Subject: [PATCH 1/2] first steps --- DangerousD/GameCore/GUI/LobbyGUI.cs | 6 +++++- DangerousD/GameCore/Managers/AppManager.cs | 16 ++++++++++++++-- DangerousD/GameCore/Managers/GameManager.cs | 4 +++- DangerousD/GameCore/Managers/PhysicsManager.cs | 17 +++++++++++++++++ DangerousD/GameCore/Managers/SoundManager.cs | 1 + 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/DangerousD/GameCore/GUI/LobbyGUI.cs b/DangerousD/GameCore/GUI/LobbyGUI.cs index f782750..7ebc2db 100644 --- a/DangerousD/GameCore/GUI/LobbyGUI.cs +++ b/DangerousD/GameCore/GUI/LobbyGUI.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using DangerousD.GameCore.Managers; using MonogameLibrary.UI.Base; using System.Diagnostics; +using DangerousD.GameCore.Network; namespace DangerousD.GameCore.GUI { @@ -55,7 +56,10 @@ namespace DangerousD.GameCore.GUI fontName = "font2" }; hostButton.LeftButtonPressed += () => { - + + AppManager.Instance.NetworkManager.HostInit(""); + + }; Button refreshButton = new ButtonText(Manager) diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index dc44ba9..f4c7a78 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -14,12 +14,14 @@ using DangerousD.GameCore.Managers; namespace DangerousD.GameCore { public enum GameState { Menu, Options, Lobby, Game, Login } + public enum MultiPlayerStatus { SinglePlayer, Host, Client } public class AppManager : Game { public static AppManager Instance { get; private set; } private GraphicsDeviceManager _graphics; - private SpriteBatch _spriteBatch; - GameState gameState; + private SpriteBatch _spriteBatch; + public GameState gameState { get; private set; } + public MultiPlayerStatus multiPlayerStatus { get; private set; } IDrawableObject MenuGUI; IDrawableObject OptionsGUI; IDrawableObject LoginGUI; @@ -45,6 +47,8 @@ namespace DangerousD.GameCore SettingsManager = new SettingsManager(); SettingsManager.LoadSettings(); + NetworkManager.GetReceivingMessages += NetworkSync; + resolution = SettingsManager.Resolution; _graphics.PreferredBackBufferWidth = resolution.X; _graphics.PreferredBackBufferHeight = resolution.Y; @@ -167,5 +171,13 @@ namespace DangerousD.GameCore } } + public void NetworkSync(NetworkTask networkTask) + { + + } + public void SetMultiplayerState(MultiPlayerStatus multiPlayerStatus) + { + this.multiPlayerStatus = multiPlayerStatus; + } } } diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index f017120..65d9d06 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -12,7 +12,7 @@ namespace DangerousD.GameCore { public class GameManager { - + public List GetAllGameObjects { get; private set; } public List livingEntities; public List entities; public List mapObjects; @@ -22,6 +22,7 @@ namespace DangerousD.GameCore public Player GetPlayer1 { get; private set; } public GameManager() { + GetAllGameObjects = new List(); livingEntities = new List(); mapObjects = new List(); entities = new List(); @@ -33,6 +34,7 @@ namespace DangerousD.GameCore internal void Register(GameObject gameObject) { + GetAllGameObjects.Add(gameObject); if (gameObject is LivingEntity) livingEntities.Add(gameObject as LivingEntity); if (gameObject is Entity) diff --git a/DangerousD/GameCore/Managers/PhysicsManager.cs b/DangerousD/GameCore/Managers/PhysicsManager.cs index 7ae99f9..f376c3c 100644 --- a/DangerousD/GameCore/Managers/PhysicsManager.cs +++ b/DangerousD/GameCore/Managers/PhysicsManager.cs @@ -206,5 +206,22 @@ namespace DangerousD.GameCore.Managers } return null; } + + public List CheckRectangle(Rectangle rectangle, Type type) + { + var gameObjects = AppManager.Instance.GameManager.GetAllGameObjects; + List intersected = new List(); + for (int i = 0; i < gameObjects.Count; i++) + { + if (gameObjects[i].GetType() == type) + { + if (gameObjects[i].Rectangle.Intersects(rectangle)) + { + intersected.Add(gameObjects[i]); + } + } + } + return intersected; + } } } \ No newline at end of file diff --git a/DangerousD/GameCore/Managers/SoundManager.cs b/DangerousD/GameCore/Managers/SoundManager.cs index 9021fa8..b32ab35 100644 --- a/DangerousD/GameCore/Managers/SoundManager.cs +++ b/DangerousD/GameCore/Managers/SoundManager.cs @@ -42,6 +42,7 @@ namespace DangerousD.GameCore sound.SoundEffect.Volume = (float)sound.GetDistance(playerPos) / MaxSoundDistance; sound.SoundEffect.Play(); PlayingSounds.Add(sound); + AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(sound.Position, soundName)); } public void StopAllSounds() // остановка всех звуков { From 1cfebd30bbbb7a844a41d0e2282a7739301bf350 Mon Sep 17 00:00:00 2001 From: AnloGames <7383an@gmail.com> Date: Thu, 17 Aug 2023 12:35:26 +0300 Subject: [PATCH 2/2] Login And Sound Network Pripilivanie --- DangerousD/GameCore/GUI/LobbyGUI.cs | 60 +++++++++----------- DangerousD/GameCore/GUI/MenuGUI.cs | 1 + DangerousD/GameCore/Managers/AppManager.cs | 24 +++++++- DangerousD/GameCore/Managers/SoundManager.cs | 10 +++- DangerousD/GameCore/Network/NetworkTask.cs | 24 ++++++++ 5 files changed, 83 insertions(+), 36 deletions(-) diff --git a/DangerousD/GameCore/GUI/LobbyGUI.cs b/DangerousD/GameCore/GUI/LobbyGUI.cs index 7ebc2db..2927758 100644 --- a/DangerousD/GameCore/GUI/LobbyGUI.cs +++ b/DangerousD/GameCore/GUI/LobbyGUI.cs @@ -33,8 +33,32 @@ namespace DangerousD.GameCore.GUI Elements.Add(new Label(Manager) { rectangle = new Rectangle(screenWidth / 30 * 2, screenHeight / 30 * 5, screenWidth / 30 * 26, screenHeight / 15 * 10) }); - // Buttons + // Buttons and ip textbox { + TextBox searchBarTextBox = new TextBox(Manager) + { + rectangle = new Rectangle(screenWidth / 30 * 14, screenHeight / 30, + screenWidth / 30 * 10, screenHeight / 30 * 3), + text = "ip", + scale = 0.16f, + fontColor = Color.Gray, + fontName = "font2", + textAligment = TextAligment.Left + + }; + searchBarTextBox.TextChanged += input => { + if (searchBarTextBox.fontColor == Color.Gray) + { + searchBarTextBox.text = ""; searchBarTextBox.fontColor = Color.Black; + } + }; + searchBarTextBox.StopChanging += input => { + if (input.Length == 0) + { + searchBarTextBox.fontColor = Color.Gray; + searchBarTextBox.text = "ip"; + } + }; Button backButton = new ButtonText(Manager) { rectangle = new Rectangle(screenWidth / 30, screenHeight / 30, 60, 50), @@ -57,8 +81,7 @@ namespace DangerousD.GameCore.GUI }; hostButton.LeftButtonPressed += () => { - AppManager.Instance.NetworkManager.HostInit(""); - + AppManager.Instance.NetworkManager.HostInit(AppManager.Instance.IpAddress); }; @@ -83,7 +106,7 @@ namespace DangerousD.GameCore.GUI fontName = "font2" }; joinSelectedButton.LeftButtonPressed += () => { - + AppManager.Instance.NetworkManager.ClientInit(AppManager.Instance.IpAddress); }; Button joinByIpButton = new ButtonText(Manager) { @@ -94,34 +117,7 @@ namespace DangerousD.GameCore.GUI fontName = "font2" }; joinByIpButton.LeftButtonPressed += () => { - - }; - } - - // SearchBar - { - TextBox searchBarTextBox = new TextBox(Manager) { - rectangle = new Rectangle(screenWidth / 30 * 14, screenHeight / 30, - screenWidth / 30 * 10, screenHeight / 30 * 3), - text = "ip", - scale = 0.16f, - fontColor = Color.Gray, - fontName = "font2", - textAligment = TextAligment.Left - - }; - searchBarTextBox.TextChanged += input => { - if (searchBarTextBox.fontColor == Color.Gray) - { - searchBarTextBox.text = ""; searchBarTextBox.fontColor = Color.Black; - } - }; - searchBarTextBox.StopChanging += input => { - if (input.Length == 0) - { - searchBarTextBox.fontColor = Color.Gray; - searchBarTextBox.text = "ip"; - } + AppManager.Instance.NetworkManager.ClientInit(searchBarTextBox.text); }; } } diff --git a/DangerousD/GameCore/GUI/MenuGUI.cs b/DangerousD/GameCore/GUI/MenuGUI.cs index f619d5f..b1ac08d 100644 --- a/DangerousD/GameCore/GUI/MenuGUI.cs +++ b/DangerousD/GameCore/GUI/MenuGUI.cs @@ -35,6 +35,7 @@ internal class MenuGUI : AbstractGui butSingle.LeftButtonPressed += () => { AppManager.Instance.ChangeGameState(GameState.Game); + AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.SinglePlayer); }; var butMulti = new ButtonText(Manager) { rectangle = new Rectangle((wigth - 300) / 2, 190, 300, 50), text = "Multiplayer", fontName = "ButtonFont" }; Elements.Add(butMulti); diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index f4c7a78..d0bf9b9 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -17,7 +17,8 @@ namespace DangerousD.GameCore public enum MultiPlayerStatus { SinglePlayer, Host, Client } public class AppManager : Game { - public static AppManager Instance { get; private set; } + public static AppManager Instance { get; private set; } + public string IpAddress { get; private set; } = "127.0.0.1"; private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; public GameState gameState { get; private set; } @@ -173,7 +174,26 @@ namespace DangerousD.GameCore public void NetworkSync(NetworkTask networkTask) { - + switch (networkTask.operation) + { + case NetworkTaskOperationEnum.TakeDamage: + break; + case NetworkTaskOperationEnum.SendSound: + SoundManager.StartSound(networkTask.name, networkTask.position, GameManager.GetPlayer1.Pos); + break; + case NetworkTaskOperationEnum.CreateEntity: + break; + case NetworkTaskOperationEnum.SendPosition: + break; + case NetworkTaskOperationEnum.ChangeState: + break; + case NetworkTaskOperationEnum.ConnectToHost: + break; + case NetworkTaskOperationEnum.GetClientPlayerId: + break; + default: + break; + } } public void SetMultiplayerState(MultiPlayerStatus multiPlayerStatus) { diff --git a/DangerousD/GameCore/Managers/SoundManager.cs b/DangerousD/GameCore/Managers/SoundManager.cs index b32ab35..382541e 100644 --- a/DangerousD/GameCore/Managers/SoundManager.cs +++ b/DangerousD/GameCore/Managers/SoundManager.cs @@ -33,8 +33,11 @@ namespace DangerousD.GameCore sound.SoundEffect.IsLooped = false; sound.SoundEffect.Play(); PlayingSounds.Add(sound); + if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.Host) + { + AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(Vector2.Zero, soundName)); + } } - public void StartSound(string soundName, Vector2 soundPos, Vector2 playerPos) // запустить звук у которого есть позиция { var sound = new Sound(Sounds[soundName], soundPos); @@ -42,7 +45,10 @@ namespace DangerousD.GameCore sound.SoundEffect.Volume = (float)sound.GetDistance(playerPos) / MaxSoundDistance; sound.SoundEffect.Play(); PlayingSounds.Add(sound); - AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(sound.Position, soundName)); + if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.Host) + { + AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(soundPos, soundName)); + } } public void StopAllSounds() // остановка всех звуков { diff --git a/DangerousD/GameCore/Network/NetworkTask.cs b/DangerousD/GameCore/Network/NetworkTask.cs index a1fb999..1dbb508 100644 --- a/DangerousD/GameCore/Network/NetworkTask.cs +++ b/DangerousD/GameCore/Network/NetworkTask.cs @@ -13,6 +13,7 @@ namespace DangerousD.GameCore.Network public NetworkTaskOperationEnum operation { get; set; } public string name { get; set; } public int value { get; set; } + public bool isParam { get; set; } public int objId { get; set; } public Vector2 position { get; set; } public Vector2 velocity { get; set; } @@ -102,5 +103,28 @@ namespace DangerousD.GameCore.Network operation = NetworkTaskOperationEnum.GetClientPlayerId; objId = PlayerId; } + + /// + /// Универсальный конструктор для нестандартных операций. То, что не нужно(кроме операции) делать null. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public NetworkTask(NetworkTaskOperationEnum operation, string name, int value, bool isParam, int objId, Vector2 position, Vector2 velocity, Type type) + { + this.operation = operation; + this.name = name; + this.value = value; + this.isParam = isParam; + this.objId = objId; + this.position = position; + this.velocity = velocity; + this.type = type; + } } }