From ec275c7584b68ba6c964c4f4319eec7c000efd94 Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Mon, 19 Aug 2024 01:20:28 +0300 Subject: [PATCH] hotfixes camera scaling --- ZoFo/GameCore/Client.cs | 25 ++++++---- .../CollisionManager/CollisionManager.cs | 4 +- ZoFo/GameCore/GameManagers/InputManager.cs | 50 +++++++++---------- .../ServerToClient/UpdateStopObjectCreated.cs | 22 ++++++++ .../Entities/LivingEntities/Enemies/Zombie.cs | 14 +++--- .../Entities/LivingEntities/LivingEntity.cs | 6 +-- .../Entities/LivingEntities/Player/Player.cs | 4 +- ZoFo/GameCore/Graphics/GraphicsComponent.cs | 2 +- ZoFo/GameCore/Server.cs | 22 ++++++-- 9 files changed, 95 insertions(+), 54 deletions(-) create mode 100644 ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateStopObjectCreated.cs diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 973097d..82e5f20 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -93,6 +93,7 @@ namespace ZoFo.GameCore #endregion + Player myPlayer; List mapObjects = new List(); List gameObjects = new List(); List players = new List(); @@ -111,6 +112,8 @@ namespace ZoFo.GameCore } networkManager.SendData();//set to ticks + if (myPlayer!=null) + GraphicsComponent.CameraPosition = (myPlayer.position + myPlayer.graphicsComponent.ObjectDrawRectangle.Size.ToVector2()/2 - AppManager.Instance.CurentScreenResolution.ToVector2()/(2*GraphicsComponent.scaling)).ToPoint(); } internal void Draw(SpriteBatch spriteBatch) { @@ -140,16 +143,17 @@ namespace ZoFo.GameCore (update as UpdateTileCreated).tileSetName )); } - //else if (update is UpdateStopObjectCreated) - //{ - // stopObjects.Add( - // new StopObject( - // (update as UpdateStopObjectCreated).Position, - // (update as UpdateStopObjectCreated).Size.ToVector2(), - // (update as UpdateStopObjectCreated).sourceRectangle, - // (update as UpdateStopObjectCreated).tileSetName - // )); - //} + else if (update is UpdateStopObjectCreated) + { + stopObjects.Add( + new StopObject( + (update as UpdateStopObjectCreated).Position, + (update as UpdateStopObjectCreated).Size.GetPoint().ToVector2(), + (update as UpdateStopObjectCreated).sourceRectangle.GetRectangle(), + (update as UpdateStopObjectCreated).tileSetName, + (update as UpdateStopObjectCreated).collisions.Select(x =>x.GetRectangle()).ToArray() + )); + } else if (update is UpdateGameObjectCreated) { GameObject created_gameObject; @@ -159,6 +163,7 @@ namespace ZoFo.GameCore { created_gameObject = new Player((update as UpdateGameObjectCreated).position); players.Add(created_gameObject as Player); + myPlayer = players[0]; gameObjects.Add(created_gameObject); } if ((update as UpdateGameObjectCreated).GameObjectType == "Ammo") diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs index fc9ee75..84d4f10 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs @@ -211,8 +211,8 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager { public static Rectangle SetOrigin(this Rectangle rectangle, Vector2 origin) { - rectangle.X = (int)origin.X; - rectangle.Y = (int)origin.Y; + rectangle.X += (int)origin.X; + rectangle.Y += (int)origin.Y; return rectangle; } } diff --git a/ZoFo/GameCore/GameManagers/InputManager.cs b/ZoFo/GameCore/GameManagers/InputManager.cs index c390273..552552f 100644 --- a/ZoFo/GameCore/GameManagers/InputManager.cs +++ b/ZoFo/GameCore/GameManagers/InputManager.cs @@ -169,7 +169,31 @@ namespace ZoFo.GameCore.GameManagers #endregion // Cheats #region Обработка состояния объекта. Задает значение полю scopeState. - if (keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.W)) + 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); + } + else if (keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.W)) { currentScopeState = ScopeState.Straight; InputMovementDirection = new Vector2(0, -1); @@ -189,30 +213,6 @@ namespace ZoFo.GameCore.GameManagers 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 #region Обработка нажатия выстрела. Вызывает событие ShootEvent diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateStopObjectCreated.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateStopObjectCreated.cs new file mode 100644 index 0000000..a8389e3 --- /dev/null +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateStopObjectCreated.cs @@ -0,0 +1,22 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; + +namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient +{ + internal class UpdateStopObjectCreated : UpdateData + { + public UpdateStopObjectCreated() { UpdateType = "UpdateStopObjectCreated"; } + public Texture2D TextureTile { get; set; } + public Vector2 Position { get; set; } + public SerializablePoint Size { get; set; } + public SerializableRectangle sourceRectangle { get; set; } + public string tileSetName { get; set; } + public SerializableRectangle[] collisions { get; set; } + } +} diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs index 4f3d21c..e37f822 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Enemies/Zombie.cs @@ -11,21 +11,21 @@ namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies { class Zombie : Enemy { - public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "zombie_damaged","zombie_walk","zombie_idle","zombie_attack","zombie_death" }, "zombie_walk"); + 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; - speed =2; - collisionComponent.stopRectangle = new Rectangle(0, 0, 52, 100); - graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100); + health = 5; + speed = 2; + graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 30, 30); + collisionComponent.stopRectangle = new Rectangle(0, 10, 30, 20); } - + public override void Update() { Vector2 duration = Vector2.Normalize( AppManager.Instance.server.players[0].position - position ); - velocity+=new Vector2(duration.X * speed, duration.Y*speed); + velocity += new Vector2(duration.X * speed, duration.Y * speed); if (Random.Shared.NextDouble() > 0.9) { diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs index c1f95ac..36523a8 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs @@ -33,7 +33,7 @@ public class LivingEntity : Entity public void OnCollision(CollisionComponent component) { - + } public override void UpdateAnimations() @@ -43,9 +43,9 @@ public class LivingEntity : Entity Vector2 prevPosition_forClient; public override void Draw(SpriteBatch spriteBatch) { - if ((position - prevPosition_forClient).X< 0) + if ((position - prevPosition_forClient).X < 0) graphicsComponent.Flip = SpriteEffects.FlipHorizontally; - else if((position - prevPosition_forClient).X > 0) + else if ((position - prevPosition_forClient).X > 0) graphicsComponent.Flip = SpriteEffects.None; base.Draw(spriteBatch); prevPosition_forClient = position; diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index a03eb95..04143f1 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -26,8 +26,8 @@ public class Player : LivingEntity //public bool isTryingToInteract { get; set; } public Player(Vector2 position) : base(position) { - graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100); - collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100); + graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 30, 30); + collisionComponent.stopRectangle = new Rectangle(0, 20, 30, 10); speed = 10; //isTryingToInteract = false; //IsTryingToShoot = false; diff --git a/ZoFo/GameCore/Graphics/GraphicsComponent.cs b/ZoFo/GameCore/Graphics/GraphicsComponent.cs index 3fa8fa4..b4580ab 100644 --- a/ZoFo/GameCore/Graphics/GraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/GraphicsComponent.cs @@ -6,7 +6,7 @@ namespace ZoFo.GameCore.Graphics; public abstract class GraphicsComponent { public Rectangle ObjectDrawRectangle; - public static int scaling = 1; + public static int scaling = 3; public string mainTextureName;//TODO костыль - пофиксить public SpriteEffects Flip = SpriteEffects.None; diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index f3433d0..e604cb1 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -183,6 +183,22 @@ namespace ZoFo.GameCore { gameObjects.Add(gameObject); + if (gameObject is StopObject) + { + AddData(new UpdateStopObjectCreated() + { + Position = (gameObject as StopObject).position, + sourceRectangle = new SerializableRectangle((gameObject as StopObject).sourceRectangle), + Size = new SerializablePoint((gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size), + tileSetName = ((gameObject as StopObject).graphicsComponent as StaticGraphicsComponent)._textureName, + collisions = (gameObject as StopObject).collisionComponents.Select(x=>new SerializableRectangle(x.stopRectangle)).ToArray() + });//TODO + foreach (var col in (gameObject as StopObject).collisionComponents) + { + collisionManager.Register(col); + } + return; + } if (gameObject is MapObject) { AddData(new UpdateTileCreated() @@ -191,7 +207,7 @@ namespace ZoFo.GameCore sourceRectangle = new SerializableRectangle((gameObject as MapObject).sourceRectangle), Size = new SerializablePoint((gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size), tileSetName = ((gameObject as MapObject).graphicsComponent as StaticGraphicsComponent)._textureName - });//TODO + }); return; } if (gameObject is Entity entity) @@ -207,9 +223,7 @@ namespace ZoFo.GameCore if (gameObject is Player) - { - players.Add(gameObject as Player); - } + players.Add(gameObject as Player); ////var elems = gameObject.GetType().GetProperties(System.Reflection.BindingFlags.Public); ////if (elems.Count()>0) TODO ////{