diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index d276984..a6f6332 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -13,6 +13,7 @@ namespace DangerousD.GameCore public abstract class GameObject : IDrawableObject { protected Vector2 _pos; + public int id = 0; public Vector2 Pos => _pos; public int Width { get; set; } public int Height { get; set; } @@ -45,10 +46,14 @@ namespace DangerousD.GameCore GraphicsComponent.LoadContent(); } - public virtual void Update(GameTime gameTime) + public void PlayAnimation() { GraphicsComponent.Update(); } + public virtual void Update(GameTime gameTime) + { + PlayAnimation(); + } public static Texture2D debugTexture; public virtual void Draw(SpriteBatch spriteBatch) diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs index d4e616b..6928f40 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using DangerousD.GameCore.GameObjects.PlayerDeath; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Graphics; +using DangerousD.GameCore.Network; namespace DangerousD.GameCore.GameObjects.LivingEntities { @@ -53,7 +54,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities { if (gameObject is Player) { - isVisible = false; + //isVisible = false; } base.OnCollision(gameObject); } @@ -125,6 +126,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities { AnimationJump(); } + if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.SinglePlayer) + { + NetworkTask task = new NetworkTask(id, Pos); + AppManager.Instance.NetworkTasks.Add(task); + } } public void MoveDown() { diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index 3379b3c..cac4d30 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -10,6 +10,8 @@ using DangerousD.GameCore.Graphics; using DangerousD.GameCore.Network; using MonogameLibrary.UI.Base; using DangerousD.GameCore.Managers; +using DangerousD.GameCore.GameObjects.LivingEntities; +using DangerousD.GameCore.GameObjects; namespace DangerousD.GameCore { @@ -19,6 +21,7 @@ namespace DangerousD.GameCore { public static AppManager Instance { get; private set; } public string IpAddress { get; private set; } = "127.0.0.1"; + public List NetworkTasks { get; set; } = new List(); private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; public GameState gameState { get; private set; } @@ -188,12 +191,17 @@ namespace DangerousD.GameCore case NetworkTaskOperationEnum.CreateEntity: break; case NetworkTaskOperationEnum.SendPosition: + LivingEntity entity = GameManager.livingEntities.Find(x => x.id == networkTask.objId); + entity.SetPosition(networkTask.position); break; case NetworkTaskOperationEnum.ChangeState: break; case NetworkTaskOperationEnum.ConnectToHost: + Player connectedPlayer = new Player(Vector2.Zero); + NetworkTasks.Add(new NetworkTask(connectedPlayer.id)); break; case NetworkTaskOperationEnum.GetClientPlayerId: + GameManager.GetPlayer1.id = networkTask.objId; break; default: break; diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index 342813b..914b8e6 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -8,14 +8,16 @@ using System; using System.Collections.Generic; using System.Text; using DangerousD.GameCore.GameObjects.LivingEntities.Monsters; +using System.Linq; namespace DangerousD.GameCore { public class GameManager { public List GetAllGameObjects { get; private set; } - + private int currentEntityId = 0; public List livingEntities; + public List livingEntitiesWithoutPlayers; public List entities; public List mapObjects; public List BackgroundObjects; @@ -31,6 +33,7 @@ namespace DangerousD.GameCore others = new List(); GetAllGameObjects = new List(); livingEntities = new List(); + livingEntitiesWithoutPlayers = new List(); mapObjects = new List(); BackgroundObjects = new List(); entities = new List(); @@ -45,14 +48,23 @@ namespace DangerousD.GameCore internal void Register(GameObject gameObject) { GetAllGameObjects.Add(gameObject); + if (gameObject is Entity) + { + gameObject.id = currentEntityId; + currentEntityId++; + } if (gameObject is Player objPl) { livingEntities.Add(gameObject as LivingEntity); players.Add(objPl); - GetPlayer1 = players[0]; + if (GetPlayer1 is null) + { + GetPlayer1 = players[players.Count - 1]; + } } else if (gameObject is LivingEntity objLE) { + livingEntitiesWithoutPlayers.Add(objLE); livingEntities.Add(objLE); } else if (gameObject is Entity objE) @@ -88,6 +100,11 @@ namespace DangerousD.GameCore public void Update(GameTime gameTime) { + if (AppManager.Instance.NetworkTasks.Count > 0) + { + AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList()); + AppManager.Instance.NetworkTasks.Clear(); + } foreach (var item in BackgroundObjects) item.Update(gameTime); foreach (var item in mapObjects) @@ -95,8 +112,22 @@ namespace DangerousD.GameCore foreach (var item in entities) item.Update(gameTime); - for (int i = 0; i < livingEntities.Count; i++) - livingEntities[i].Update(gameTime); + if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.Client) + { + for (int i = 0; i < livingEntitiesWithoutPlayers.Count; i++) + { + livingEntitiesWithoutPlayers[i].Update(gameTime); + } + GetPlayer1.Update(gameTime); + } + else + { + for (int i = 0; i < livingEntitiesWithoutPlayers.Count; i++) + { + livingEntitiesWithoutPlayers[i].PlayAnimation(); + } + GetPlayer1.Update(gameTime); + } foreach (var item in otherObjects) item.Update(gameTime); diff --git a/DangerousD/GameCore/Managers/SoundManager.cs b/DangerousD/GameCore/Managers/SoundManager.cs index 3a1e03f..ab9a5d8 100644 --- a/DangerousD/GameCore/Managers/SoundManager.cs +++ b/DangerousD/GameCore/Managers/SoundManager.cs @@ -46,7 +46,7 @@ namespace DangerousD.GameCore PlayingSounds.Add(sound); if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host) { - AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(Vector2.Zero, soundName)); + AppManager.Instance.NetworkTasks.Add(new Network.NetworkTask(Vector2.Zero, soundName)); } } public void StartSound(string soundName, Vector2 soundPos, Vector2 playerPos) // запустить звук у которого есть позиция @@ -58,7 +58,7 @@ namespace DangerousD.GameCore PlayingSounds.Add(sound); if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host) { - AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(soundPos, soundName)); + AppManager.Instance.NetworkTasks.Add(new Network.NetworkTask(soundPos, soundName)); } } public void StopAllSounds() // остановка всех звуков diff --git a/DangerousD/GameCore/Network/NetworkManager.cs b/DangerousD/GameCore/Network/NetworkManager.cs index caf16a1..292e210 100644 --- a/DangerousD/GameCore/Network/NetworkManager.cs +++ b/DangerousD/GameCore/Network/NetworkManager.cs @@ -25,7 +25,7 @@ namespace DangerousD.GameCore.Network { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address = IPAddress.Parse(IpAddress); - int port = 8000; + int port = 51873; endPoint = new IPEndPoint(address, port); } catch { } @@ -71,6 +71,7 @@ namespace DangerousD.GameCore.Network Thread acceptThread = new Thread(AcceptSockets); acceptThread.Start(); state = "Host"; + AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host); } catch { } } @@ -84,10 +85,13 @@ namespace DangerousD.GameCore.Network Thread.Sleep(10); Thread ReceivingThread = new Thread(ReceiveMsgFromHost); ReceivingThread.Start(); + NetworkTask connectionTask = new NetworkTask("Player"); + AppManager.Instance.NetworkTasks.Add(connectionTask); + AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client); } catch { } } - public void SendMsg(NetworkTask networkTask) + public void SendMsg(List networkTask) { byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask)); int count = Data.Length; diff --git a/DangerousD/GameCore/Network/NetworkTask.cs b/DangerousD/GameCore/Network/NetworkTask.cs index 1dbb508..2655274 100644 --- a/DangerousD/GameCore/Network/NetworkTask.cs +++ b/DangerousD/GameCore/Network/NetworkTask.cs @@ -19,6 +19,8 @@ namespace DangerousD.GameCore.Network public Vector2 velocity { get; set; } public Type type { get; set; } + + public NetworkTask() { } /// /// Нанести урон сущности ///