diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 25ae68c..973097d 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -50,8 +50,17 @@ namespace ZoFo.GameCore networkManager.AddData(new UpdateInput() { InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection, - InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection - }); + InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection + }); + + }; + AppManager.Instance.InputManager.OnInteract += () => + { + networkManager.AddData(new UpdateInputInteraction() { }); + }; + AppManager.Instance.InputManager.ShootEvent += () => + { + networkManager.AddData(new UpdateInputShoot() { }); }; } @@ -88,6 +97,7 @@ namespace ZoFo.GameCore List gameObjects = new List(); List players = new List(); List stopObjects = new List(); + /// /// Клиент должен обнговлять игру анимаций /// @@ -99,6 +109,8 @@ namespace ZoFo.GameCore AppManager.Instance.debugHud.Set("GameTime", gameTime.TotalGameTime.ToString()); gameObjects[i].UpdateAnimations(); } + + networkManager.SendData();//set to ticks } internal void Draw(SpriteBatch spriteBatch) { diff --git a/ZoFo/GameCore/GameManagers/InputManager.cs b/ZoFo/GameCore/GameManagers/InputManager.cs index dd13535..c390273 100644 --- a/ZoFo/GameCore/GameManagers/InputManager.cs +++ b/ZoFo/GameCore/GameManagers/InputManager.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading.Tasks; +using ZoFo.GameCore.GUI; namespace ZoFo.GameCore.GameManagers { @@ -27,9 +28,6 @@ namespace ZoFo.GameCore.GameManagers private Vector2 prevInputMovementDirection; public Vector2 InputAttackDirection; private Vector2 prevInputAttackDirection; - - public event Action TalkEvent; - public ScopeState currentScopeState; // Положение оружия. Left, Right, Straight, Back, StraightLeft, StraightRight, BackLeft, BackRight. private ScopeState prevCurrentScopeState; private bool _cheatsEnabled = false; @@ -48,6 +46,7 @@ namespace ZoFo.GameCore.GameManagers public InputManager() { + isInteract = true; InputMovementDirection = new Vector2(0, 0); InputAttackDirection = new Vector2(0, 0); this.isShoot = false; @@ -173,38 +172,46 @@ namespace ZoFo.GameCore.GameManagers if (keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.W)) { currentScopeState = ScopeState.Straight; + InputMovementDirection = new Vector2(0, -1); } else if (keyBoardState.IsKeyDown(Keys.Down) || keyBoardState.IsKeyDown(Keys.S)) { currentScopeState = ScopeState.Back; + InputMovementDirection = new Vector2(0, 1); } else if(keyBoardState.IsKeyDown(Keys.Left) || keyBoardState.IsKeyDown(Keys.A)) { currentScopeState = ScopeState.Left; + InputMovementDirection = new Vector2(-1, 0); } else if(keyBoardState.IsKeyDown(Keys.Right) || keyBoardState.IsKeyDown(Keys.D)) { currentScopeState = ScopeState.Right; + InputMovementDirection = new Vector2(1, 0); } else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.W)) { currentScopeState = ScopeState.StraightRight; + InputMovementDirection = new Vector2(1, 1); } else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.W)) { currentScopeState = ScopeState.StraightLeft; + InputMovementDirection = new Vector2(-1, 1); } else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Down) || keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.S)) { currentScopeState = ScopeState.BackRight; + InputMovementDirection = new Vector2(1, -1); } else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Down) || keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.S)) { currentScopeState = ScopeState.BackLeft; + InputMovementDirection = new Vector2(-1, -1); } #endregion @@ -247,6 +254,8 @@ namespace ZoFo.GameCore.GameManagers prevInputAttackDirection = InputAttackDirection; prevCurrentScopeState = currentScopeState; #endregion + + DebugHUD.Instance.Set("controls", currentScopeState.ToString()); } } } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index 6e1e6b4..b8e3737 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -42,6 +42,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public void SendData() { + for (int i = 0; i < updates.Count; i++) + { + + AppManager.Instance.server.ProcessIUpdateData(updates[i]); + } + updates.Clear(); + return;// TODO remove byte[] bytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(updates)); //нужно сериализовать socket.Send(bytes); } @@ -98,7 +105,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public static IPAddress GetIp() { - string hostName = Dns.GetHostName(); // Retrive the Name of HOST + /*string hostName = Dns.GetHostName(); // Retrive the Name of HOST var ipList = Dns.GetHostByName(hostName).AddressList; foreach (var ip in ipList) @@ -107,8 +114,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager { return ip; } - } - return IPAddress.Loopback; + }*/ + return IPAddress.Parse("127.0.0.1"); } //поток 2 diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index 697c61d..e8be1b5 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -51,18 +51,17 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// /// public static IPAddress GetIp() - { - string hostName = Dns.GetHostName(); // Retrive the Name of HOST - var ipList = Dns.GetHostEntry(hostName).AddressList; - + { + /*string hostName = Dns.GetHostName(); // Retrive the Name of HOST + var ipList = Dns.GetHostByName(hostName).AddressList; foreach (var ip in ipList) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { return ip; - } - } - return IPAddress.Loopback; + } + }*/ + return IPAddress.Parse("127.0.0.1"); } /// diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ClientToServer/UpdateInputInteraction.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ClientToServer/UpdateInputInteraction.cs new file mode 100644 index 0000000..243fce0 --- /dev/null +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ClientToServer/UpdateInputInteraction.cs @@ -0,0 +1,12 @@ +using System; +using ZoFo.GameCore.GameManagers.NetworkManager; + +namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer; + +/// +/// уведомляет сервер о том, что игрок взаимодействует +/// +public class UpdateInputInteraction : UpdateData +{ + public UpdateInputInteraction() { UpdateType = "UpdateInputInteraction"; } +} diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ClientToServer/UpdateShootInteraction.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ClientToServer/UpdateShootInteraction.cs new file mode 100644 index 0000000..d5ef20c --- /dev/null +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ClientToServer/UpdateShootInteraction.cs @@ -0,0 +1,9 @@ +using System; +using ZoFo.GameCore.GameManagers.NetworkManager.Updates; + +namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer; + +public class UpdateInputShoot : UpdateData +{ + public UpdateInputShoot() { UpdateType = "UpdateInputShoot"; } +} diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs index 6f4711d..25848ca 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs @@ -2,6 +2,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; /// /// При попытке взаимодействия с объектом +/// отправляет пользователю разрешение на взаимодействие +/// TODO: Вероятно убрать(обсудить) /// public class UpdateInteraction : UpdateData { diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs index 7741cee..4f3d21c 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs @@ -14,9 +14,9 @@ namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "zombie_damaged","zombie_walk","zombie_idle","zombie_attack","zombie_death" }, "zombie_walk"); public Zombie(Vector2 position) : base(position) { - health = 5; + health = 5; speed =2; - collisionComponent.stopRectangle = new Rectangle(0, 0, 52, 100); + collisionComponent.stopRectangle = new Rectangle(0, 0, 52, 100); graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100); } diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 0041c90..a03eb95 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -18,19 +18,21 @@ public class Player : LivingEntity /// /// Факт того, что плеер в этом апдейте пытается стрелять /// - public bool IsTryingToShoot { get; set; } + //public bool IsTryingToShoot { get; set; } private float speed; private int health; public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_look_down" }, "player_look_down"); private LootData lootData; + //public bool isTryingToInteract { get; set; } public Player(Vector2 position) : base(position) { - //InputWeaponRotation = new Vector2(0, 0); - //InputPlayerRotation = new Vector2(0, 0); graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100); - collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100); + collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100); + speed = 10; + //isTryingToInteract = false; + //IsTryingToShoot = false; - StartAnimation("player_look_down"); + StartAnimation("player_look_down"); } @@ -39,21 +41,21 @@ public class Player : LivingEntity MovementLogic(); } - float t; - public void MovementLogic() - { - IsTryingToShoot = true; //gslkjfsnblkjsdfnnlkjbn;zkcjnb;kkjnzx;cjkb;kzjxb;kSErgo - //velocity.X = 3+(float)Math.Sin(t); - t++; - if (InputPlayerRotation.X > 0.9) - { - } - if (Keyboard.GetState().IsKeyDown(Keys.D)) velocity.X += 5; - if (Keyboard.GetState().IsKeyDown(Keys.A)) velocity.X += -5; - if (Keyboard.GetState().IsKeyDown(Keys.S)) velocity.Y += 5; - if (Keyboard.GetState().IsKeyDown(Keys.W)) velocity.Y += -5; + public void MovementLogic() + { + velocity = InputPlayerRotation * speed; } public void HandleNewInput(UpdateInput updateInput) + { + InputPlayerRotation = updateInput.InputMovementDirection; + InputWeaponRotation = updateInput.InputAttackDirection; + + } + public void HandleInteract(UpdateInputInteraction updateInputInteraction) + { + //isTryingToInteract = true; + } + public void HandleShoot(UpdateInputShoot updateInputShoot) { } diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index 944b098..f3433d0 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -12,6 +12,7 @@ using ZoFo.GameCore.GameManagers.CollisionManager; using ZoFo.GameCore.GameManagers.MapManager; using ZoFo.GameCore.GameManagers.NetworkManager; using ZoFo.GameCore.GameManagers.NetworkManager.Updates; +using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameObjects; using ZoFo.GameCore.GameObjects.Entities; @@ -78,11 +79,17 @@ namespace ZoFo.GameCore break; case "UpdatePlayerParametrs": break; - case "UpdatePosition": + case "UpdateInput": + players[0].HandleNewInput(updateData as UpdateInput); break; case "UpdateTileCreated": break; - + case "UpdateInputInteraction": + players[0].HandleInteract(updateData as UpdateInputInteraction); + break; + case "UpdateInputShoot": + players[0].HandleShoot(updateData as UpdateInputShoot); + break; } } @@ -131,6 +138,9 @@ namespace ZoFo.GameCore //AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(0, 0))); AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(740, 140))); AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1000, 1000))); + AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1300, 1000))); + AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1500, 1000))); + AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1700, 1000))); AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(140, 440))); AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(240, 440))); }