diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs index 880e159..146d173 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs @@ -60,6 +60,26 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager } + public CollisionComponent(GameObject gameObject) + { + this.gameObject = gameObject; + doesStop = false; + this.isTrigger = false; + AppManager.Instance.server.collisionManager.Register(this); + } + public CollisionComponent(GameObject gameObject, bool hasCollision = false, Rectangle? collisionRectangle = null, bool isTrigger = false, Rectangle? triggerRectangle = null) + { + this.gameObject = gameObject; + + doesStop = hasCollision; + this.isTrigger = isTrigger; + if (hasCollision) + this.stopRectangle = collisionRectangle.Value; + if (isTrigger) + this.triggerRectanglee = triggerRectangle.Value; + + AppManager.Instance.server.collisionManager.Register(this); + } } } diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs index 96c9cbc..a18b9b8 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs @@ -10,6 +10,8 @@ using Microsoft.Xna.Framework; using ZoFo.GameCore.GameManagers.MapManager.MapElements; using ZoFo.GameCore.GameObjects.Entities; using ZoFo.GameCore.GameObjects.Entities.LivingEntities; +using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; +using ZoFo.GameCore.ZoFo_graphics; namespace ZoFo.GameCore.GameManagers.CollisionManager { @@ -21,46 +23,48 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager public List EntitiesWithMovements; public List ObjectsWithTriggers; - + //чекаем коллизии в листе - public void CheckComponentCollision(LivingEntity entity) + public void CheckComponentCollision(CollisionComponent componentOfEntity) { + var entity = componentOfEntity.gameObject as LivingEntity; //for (int i = 0; i < ObjectsWithCollisions.Count; i++) //{ - var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК - var newRect = currentRect; // задаём значение старого РЕК новому РЕК - bool flagRemovedObject = false; //флаг удаления + var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК + var newRect = currentRect; // задаём значение старого РЕК новому РЕК + bool flagRemovedObject = false; //флаг удаления - var collidedX = false; // соприкосновение - var tryingRectX = currentRect;//переменная для попытки перемещения по X + var collidedX = false; // соприкосновение + var tryingRectX = currentRect;//переменная для попытки перемещения по X - tryingRectX.Offset((int)(entity.velocity.X), 0);//задаём значения для tryingRectX по X и по Y + tryingRectX.Offset((int)(entity.velocity.X), 0);//задаём значения для tryingRectX по X и по Y + + foreach (var item in ObjectsWithCollisions)//фильтрация + { + if (Math.Abs(item.stopRectangle.X - entity.collisionComponent.stopRectangle.X) < 550 + && Math.Abs(item.stopRectangle.Y - entity.collisionComponent.stopRectangle.Y) < 550 + && tryingRectX.Intersects(item.stopRectangle)) - foreach (var item in ObjectsWithCollisions)//фильтрация { - if (Math.Abs(item.stopRectangle.X - entity.collisionComponent.stopRectangle.X) < 550 - && Math.Abs(item.stopRectangle.Y - entity.collisionComponent.stopRectangle.Y) < 550 - && tryingRectX.Intersects(item.stopRectangle)) + collidedX = true;// меняем значение соприкосновения на true + entity.OnCollision(item);//подписываем entity на ивент коллизии - { - collidedX = true;// меняем значение соприкосновения на true - entity.OnCollision(item);//подписываем entity на ивент коллизии - - break;// выход - } - } - - if (collidedX)// срабатывает, если перемещение блокируется - { - entity.velocity.X = 0;// задаём значение смещения entity на 0 - } - else - { - newRect.X = tryingRectX.X;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК + break;// выход } + } - //==ПОВТОРЯЕМ ТОЖЕ САМОЕ ДЛЯ Y== + if (collidedX)// срабатывает, если перемещение блокируется + { + entity.velocity.X = 0;// задаём значение смещения entity на 0 + } + else + { + entity.position.X += entity.velocity.X; //update player position + newRect.X = tryingRectX.X;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК + } + + //==ПОВТОРЯЕМ ТОЖЕ САМОЕ ДЛЯ Y== var collidedY = false; // соприкосновение var tryingRectY = currentRect;//переменная для попытки перемещения по X @@ -78,7 +82,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager entity.OnCollision(item);//подписываем entity на ивент коллизии break;// выход - } + } } if (collidedY)// срабатывает, если перемещение блокируется @@ -87,8 +91,13 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager } else { + entity.position.X += entity.velocity.Y; newRect.Y = tryingRectY.Y;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК } + + entity.collisionComponent.stopRectangle = newRect; + entity.graphicsComponent.ObjectDrawRectangle = newRect; + entity.velocity = Vector2.Zero; } //обновление позиции объекта @@ -97,17 +106,32 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager } + public void UpdatePositions() + { + foreach (var item in EntitiesWithMovements) + { + CheckComponentCollision(item); + } + } + + public CollisionManager() + { + //graphicsComponent + //.ObjectDrawRectangle = new Rectangle(0, 0, 16 * 12, 16 * 16); + EntitiesWithMovements = new List(); + ObjectsWithCollisions = new List(); + } //регистрация компонента(его коллизии) public void Register(CollisionComponent component) { ObjectsWithCollisions.Add(component); - if (component.gameObject is Entity) + if (component.gameObject is LivingEntity) { EntitiesWithMovements.Add(component); } } - + } diff --git a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs index adafb5e..00cde0f 100644 --- a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs +++ b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs @@ -20,5 +20,7 @@ namespace ZoFo.GameCore.GameObjects.Entities } + + } } diff --git a/ZoFo/GameCore/GameObjects/Entities/Entity.cs b/ZoFo/GameCore/GameObjects/Entities/Entity.cs index 0c7d453..c0a192d 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Entity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Entity.cs @@ -15,10 +15,15 @@ namespace ZoFo.GameCore.GameObjects.Entities public int Id { get; set; } protected Entity(Vector2 position) : base(position) { + } public virtual void Update() { - + } + public override void UpdateLogic() + { + Update(); + base.UpdateLogic(); } } } diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs index 84e7d45..02cc503 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs @@ -9,6 +9,9 @@ using ZoFo.GameCore.GameManagers.CollisionManager; namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities; public class LivingEntity : Entity { + /// + /// Переменная для заявки на передвижения, т.е. то, на сколько вы хотите, чтобы в этом кадре переместился объект + /// public Vector2 velocity; private InputManager inputManager; diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 20bbe18..cc56edd 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -1,27 +1,49 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; +using System.Collections.Generic; using ZoFo.GameCore.GameManagers; +using ZoFo.GameCore.GameManagers.CollisionManager; +using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; +using ZoFo.GameCore.ZoFo_graphics; namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; public class Player : LivingEntity { - public Vector2 InputWeaponRotation{ get; set; } - public Vector2 InputPlayerRotation{ get; set;} - public bool IsTryingToShoot{get;set;} - Texture2D texture; + public Vector2 InputWeaponRotation { get; set; } + public Vector2 InputPlayerRotation { get; set; } + /// + /// Факт того, что плеер в этом апдейте пытается стрелять + /// + public bool IsTryingToShoot { get; set; } private float speed; private int health; + public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); public Player(Vector2 position) : base(position) { //InputWeaponRotation = new Vector2(0, 0); //InputPlayerRotation = new Vector2(0, 0); + + collisionComponent = new CollisionComponent(this, true, new Rectangle(0, 0, 10, 10)); } + public void Update(GameTime gameTime) - { - + { + + MovementLogic(); + } + public void MovementLogic() + { + if (InputPlayerRotation.X > 0.9) + { + velocity.X = 5; + } + } + public void HandleNewInput(UpdateInput updateInput) + { + } } diff --git a/ZoFo/GameCore/GameObjects/GameObject.cs b/ZoFo/GameCore/GameObjects/GameObject.cs index 05edc34..d0fa9bb 100644 --- a/ZoFo/GameCore/GameObjects/GameObject.cs +++ b/ZoFo/GameCore/GameObjects/GameObject.cs @@ -20,10 +20,16 @@ public abstract class GameObject { this.position = position; graphicsComponent.LoadContent(); + + graphicsComponent.ObjectDrawRectangle.X = (int)position.X; + graphicsComponent.ObjectDrawRectangle.Y = (int)position.Y; + } - public virtual void UpdateLogic(GameTime gameTime) + public virtual void UpdateLogic() { PlayAnimation_OnServer(); + + } diff --git a/ZoFo/GameCore/GameObjects/MapObjects/StopObjects/StopObject.cs b/ZoFo/GameCore/GameObjects/MapObjects/StopObjects/StopObject.cs index 02f828e..56429c3 100644 --- a/ZoFo/GameCore/GameObjects/MapObjects/StopObjects/StopObject.cs +++ b/ZoFo/GameCore/GameObjects/MapObjects/StopObjects/StopObject.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using System; +using System.Diagnostics; using ZoFo.GameCore.GameManagers.CollisionManager; using ZoFo.GameCore.ZoFo_graphics; @@ -7,10 +8,15 @@ namespace ZoFo.GameCore.GameObjects.MapObjects.StopObjects; public class StopObject : MapObject { - CollisionComponent collisionComponent; + CollisionComponent[] collisionComponent; - protected StopObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName) : base(position, size, sourceRectangle, textureName) + protected StopObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName, Rectangle[] collisions) : base(position, size, sourceRectangle, textureName) { + collisionComponent = new CollisionComponent[collisions.Length]; + for (int i = 0; i < collisionComponent.Length; i++) + { + collisionComponent[i] = new CollisionComponent(this, true, collisions[i]); + } // TODO: Написать коллизию, пусть тразмер будет чисто таким же как и текстурка. // Поменяйте уровень защиты конструктора, после снимите в MapManager комментарий в методе LoadMap с создания StopObject-а } diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index 93c7018..ae53f38 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -8,12 +8,14 @@ using System.Text; using System.Text.Json; using System.Threading.Tasks; using ZoFo.GameCore.GameManagers; +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.ServerToClient; using ZoFo.GameCore.GameObjects; using ZoFo.GameCore.GameObjects.Entities; +using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; using ZoFo.GameCore.GameObjects.MapObjects; namespace ZoFo.GameCore @@ -30,6 +32,8 @@ namespace ZoFo.GameCore } #region server logic as App + + #region Net Methods //TODO Comment pls public void OnDataSend(string data) { @@ -73,6 +77,10 @@ namespace ZoFo.GameCore networkManager.Start(players); } + #endregion + + #region Game Methods + /// /// Запуск игры в комнате /// @@ -81,12 +89,13 @@ namespace ZoFo.GameCore //TODO начинает рассылку и обмен пакетами игры //Грузит карту - + collisionManager = new CollisionManager(); gameObjects = new List(); entities = new List(); new MapManager().LoadMap(); AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(40, 40))); + AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(140, 140))); } /// @@ -98,6 +107,8 @@ namespace ZoFo.GameCore networkManager.AddData(gameEnded); networkManager.CloseConnection(); } + + public CollisionManager collisionManager; private List gameObjects = new List(); private List entities; //entity public void Update(GameTime gameTime) @@ -106,8 +117,9 @@ namespace ZoFo.GameCore { foreach (var go in gameObjects) { - go.UpdateLogic(gameTime); + go.UpdateLogic(); } + collisionManager.UpdatePositions(); ticks = 0; networkManager.SendData(); } @@ -143,4 +155,6 @@ namespace ZoFo.GameCore } } #endregion + + #endregion }