diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index f36c454..00c2a91 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -15,11 +15,16 @@ using System.Reflection; using ZoFo.GameCore.GameObjects.Entities; using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer; - +using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; +using System.Linq; +using System.Web; +using ZoFo.GameCore.GUI; namespace ZoFo.GameCore { public class Client { + #region Network part + ClientNetworkManager networkManager; public bool IsConnected { get { return networkManager.IsConnected; } } @@ -48,12 +53,13 @@ namespace ZoFo.GameCore } public void GameEndedUnexpectedly() { } - public void JoinRoom(string ip,int port) + public void JoinRoom(string ip, int port) { - networkManager.JoinRoom(ip,port); + networkManager.JoinRoom(ip, port); } public void JoinYourself(int port) { networkManager.JoinYourself(port); } + #endregion List mapObjects = new List(); List gameObjects = new List(); @@ -94,20 +100,43 @@ namespace ZoFo.GameCore } else if (update is UpdateGameObjectCreated) { - var a = Assembly.GetAssembly(typeof(GameObject)); if ((update as UpdateGameObjectCreated).GameObjectType == "EntittyForAnimationTests") - { + gameObjects.Add(new EntittyForAnimationTests((update as UpdateGameObjectCreated).position)); + if ((update as UpdateGameObjectCreated).GameObjectType == "Player") + gameObjects.Add(new Player((update as UpdateGameObjectCreated).position)); - gameObjects.Add( - new EntittyForAnimationTests(new Vector2(100,100)) - ); - } + (gameObjects.Last() as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity); + //var a = Assembly.GetAssembly(typeof(GameObject)); //gameObjects.Add( TODO reflection //Activator.CreateInstance(Type.GetType("ZoFo.GameCore.GameObjects.Entities.EntittyForAnimationTests") ///*(update as UpdateGameObjectCreated).GameObjectType*/, new []{ new Vector2(100, 100) }) //as GameObject //); } + else if (update is UpdatePosition) + { + var ent = FindEntityById(update.IdEntity); + + ent.position = (update as UpdatePosition).NewPosition; + DebugHUD.Instance.Log("newPosition " + ent.position); + } } + + + public Entity FindEntityById(int id) + { + for (int i = 0; i < gameObjects.Count; i++) + { + if (gameObjects[i] is Entity) + { + if ((gameObjects[i] as Entity).Id == id) + { + return gameObjects[i] as Entity; + } + } + } + return null; + } + } } \ No newline at end of file diff --git a/ZoFo/GameCore/GUI/DebugHUD.cs b/ZoFo/GameCore/GUI/DebugHUD.cs index fdc5371..294530a 100644 --- a/ZoFo/GameCore/GUI/DebugHUD.cs +++ b/ZoFo/GameCore/GUI/DebugHUD.cs @@ -13,9 +13,11 @@ public class DebugHUD private SpriteFont _spriteFont; private Dictionary _text = new(); private List _log = new(); + public static DebugHUD Instance { get; private set; } public void Initialize() { + Instance = this; } public void LoadContent() diff --git a/ZoFo/GameCore/GameManagers/AppManager.cs b/ZoFo/GameCore/GameManagers/AppManager.cs index 224dba2..42e886a 100644 --- a/ZoFo/GameCore/GameManagers/AppManager.cs +++ b/ZoFo/GameCore/GameManagers/AppManager.cs @@ -12,6 +12,7 @@ using ZoFo.GameCore.GameManagers.ItemManager; using ZoFo.GameCore.GUI; using static System.Collections.Specialized.BitVector32; using MonogameLibrary.UI.Base; +using ZoFo.GameCore.GameObjects; namespace ZoFo.GameCore.GameManagers { @@ -83,6 +84,8 @@ namespace ZoFo.GameCore.GameManagers currentGUI.LoadContent(); animationBuilder = new AnimationBuilder(); animationBuilder.LoadAnimations(); + GameObject.debugTexture = new Texture2D(GraphicsDevice, 1, 1); + GameObject.debugTexture.SetData(new Color[] { Color.White }); } protected override void Update(GameTime gameTime) diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollectionComponent.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollectionComponent.cs deleted file mode 100644 index 7143312..0000000 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollectionComponent.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ZoFo.GameCore.GameManagers.CollisionManager -{ - public class CollectionComponent - { - } -} \ No newline at end of file diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs index 0132188..3ad2811 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs @@ -12,20 +12,38 @@ using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; namespace ZoFo.GameCore.GameManagers.CollisionManager { public class CollisionComponent - { - //==КОНСТРУКТОР== + { + //==КОНСТРУКТОР== public CollisionComponent(GameObject gameObject) { - this.gameObject = gameObject; - AppManager.Instance.server.collisionManager.Register(this); - } + this.gameObject = gameObject; + hasCollision = false; + this.isTrigger = false; + } + + public CollisionComponent(GameObject gameObject, bool hasCollision = false, Rectangle? collisionRectangle = null, bool isTrigger = false, Rectangle? triggerRectangle = null) + { + this.gameObject = gameObject; + + hasCollision = hasCollision; + this.isTrigger = isTrigger; + if (hasCollision) + this.stopRectangle = collisionRectangle.Value; + if (isTrigger) + this.triggerRectangle = triggerRectangle.Value; + + + + + } //==ПОЛЯ== public GameObject gameObject { get; set; } + bool doesStop; bool hasCollision; public Rectangle stopRectangle; @@ -46,30 +64,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager public event EventHandler OnCollision; - - // methods-event - - - 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 903c056..6bb9698 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs @@ -31,8 +31,10 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager //for (int i = 0; i < ObjectsWithCollisions.Count; i++) //{ var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК - var newRect = currentRect; // задаём значение старого РЕК новому РЕК - bool flagRemovedObject = false; //флаг удаления + currentRect.X+=(int)entity.position.X; + currentRect.Y+=(int)entity.position.Y; + + var newRect = currentRect; // задаём значение старого РЕК новому РЕК var collidedX = false; // соприкосновение @@ -94,17 +96,15 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager entity.position.X += entity.velocity.Y; newRect.Y = tryingRectY.Y;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК } - - entity.collisionComponent.stopRectangle = newRect; - entity.graphicsComponent.ObjectDrawRectangle = newRect; + + entity.graphicsComponent.ObjectDrawRectangle.X = (int)entity.position.X; + entity.graphicsComponent.ObjectDrawRectangle.Y = (int)entity.position.Y; + AppManager.Instance.server.AddData(new UpdatePosition() { NewPosition = entity.position, IdEntity = entity.Id }); + AppManager.Instance.debugHud.Set("testPos", entity.position.ToString()); //TODO remove entity.velocity = Vector2.Zero; } - //обновление позиции объекта - public void UpdateObjectsPositions() - { - - } + //обновление позиции объекта public void UpdatePositions() { diff --git a/ZoFo/GameCore/GameManagers/ItemManager/ItemInfo.cs b/ZoFo/GameCore/GameManagers/ItemManager/ItemInfo.cs index 375d745..cfd5a4f 100644 --- a/ZoFo/GameCore/GameManagers/ItemManager/ItemInfo.cs +++ b/ZoFo/GameCore/GameManagers/ItemManager/ItemInfo.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ZoFo.GameCore.GameManagers.ItemManager { - class ItemInfo + public class ItemInfo { //поля string tag; diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs index 0ca86a4..8e1b5b6 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Xna.Framework; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -14,5 +15,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient public UpdateGameObjectCreated() { UpdateType = "UpdateGameObjectCreated"; } public string GameObjectType; public string GameObjectId; + public Vector2 position; } } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs index df1cba3..189a299 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs @@ -1,6 +1,7 @@ -using System; +using Microsoft.Xna.Framework; +using System; using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Text; using System.Threading.Tasks; using ZoFo.GameCore.GameObjects.Entities.LivingEntities; @@ -10,8 +11,10 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient /// /// Хранит новую позицию /// - public class UpdatePosition : UpdateData + public class UpdatePosition : UpdateData { public UpdatePosition() { UpdateType = "UpdatePosition"; } + + public Vector2 NewPosition { get; set; } } } diff --git a/ZoFo/GameCore/GameObjects/Entities/Entity.cs b/ZoFo/GameCore/GameObjects/Entities/Entity.cs index db68013..456d8da 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Entity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Entity.cs @@ -12,10 +12,22 @@ namespace ZoFo.GameCore.GameObjects.Entities //public override GraphicsComponent graphicsComponent => null; public CollisionComponent collisionComponent { get; protected set; } public int Id { get; set; } + static int totalEntitiesCreated = 0; protected Entity(Vector2 position) : base(position) { - + Id = totalEntitiesCreated; + totalEntitiesCreated++; + collisionComponent = new CollisionComponent(this); } + /// + /// For initialisation on Client + /// + /// + public void SetIdByClient(int newId) + { + Id = newId; + } + public virtual void Update() { } diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs index bfe1b42..2701d49 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs @@ -15,7 +15,7 @@ public class Interactable : Entity { collisionComponent.OnTriggerEnter += (sender, e) => ChangeInteraction(sender, e, true); collisionComponent.OnTriggerExit += (sender, e) => ChangeInteraction(sender, e, false); - collisionComponent.OnTriggerInteract += OnInteraction; + collisionComponent.OnTriggerZone += OnInteraction; } private void ChangeInteraction(object sender, CollisionComponent e, bool isReady) diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs index e7e15f1..cc6cb9c 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs @@ -34,6 +34,7 @@ public class LivingEntity : Entity { } + } diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 026a746..c5fccc3 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -26,20 +26,23 @@ public class Player : LivingEntity //InputWeaponRotation = new Vector2(0, 0); //InputPlayerRotation = new Vector2(0, 0); - collisionComponent = new CollisionComponent(this, true, new Rectangle(0, 0, 10, 10)); + collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100); + graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,100,100); } - public void Update(GameTime gameTime) + public override void Update() { MovementLogic(); } + float t; public void MovementLogic() { + velocity.X = (float)Math.Sin(t); + t++; 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 66cb30d..7a4eb43 100644 --- a/ZoFo/GameCore/GameObjects/GameObject.cs +++ b/ZoFo/GameCore/GameObjects/GameObject.cs @@ -46,6 +46,7 @@ public abstract class GameObject #region Client Side + public static Texture2D debugTexture; /// /// Для клиента /// Это вызывается в клиентской части игры @@ -82,19 +83,21 @@ public abstract class GameObject { graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch); //debug + DrawDebugRectangle(spriteBatch, graphicsComponent.ObjectDrawRectangle); + if (AppManager.Instance.InputManager.CollisionsCheat) DrawDebugRectangle(spriteBatch, graphicsComponent.ObjectDrawRectangle); } public void DrawDebugRectangle(SpriteBatch spriteBatch, Rectangle _rectangle, Nullable color = null) { - if (color is null) color = new Color(1, 0, 0, 0.25f); + if (color is null) color = new Color(1, 0, 0, 0.1f); if (color.Value.A == 255) color = new Color(color.Value, 0.25f); - //spriteBatch.Draw(debugTexture, - // new Rectangle((_rectangle.X - graphicsComponent.CameraPosition.X) * graphicsComponent.scaling, - // (_rectangle.Y - graphicsComponent.CameraPosition.Y) * graphicsComponent.scaling, - // _rectangle.Width * graphicsComponent.scaling, - // _rectangle.Height * graphicsComponent.scaling), color.Value); + spriteBatch.Draw(debugTexture, + new Rectangle((_rectangle.X - GraphicsComponent.CameraPosition.X) * GraphicsComponent.scaling, + (_rectangle.Y - GraphicsComponent.CameraPosition.Y) * GraphicsComponent.scaling, + _rectangle.Width * GraphicsComponent.scaling, + _rectangle.Height * GraphicsComponent.scaling), color.Value); //TODO: debugTexture } diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index 045f399..de52802 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -15,6 +15,7 @@ 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.Enemies; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; using ZoFo.GameCore.GameObjects.MapObjects; @@ -25,9 +26,7 @@ namespace ZoFo.GameCore private ServerNetworkManager networkManager; private int ticks = 0; public IPEndPoint MyIp { get { return networkManager.InfoConnect; } } - - public CollisionManager collisionManager; - + public Server() { networkManager = new ServerNetworkManager(); @@ -84,7 +83,7 @@ namespace ZoFo.GameCore #endregion #region Game Methods - + public CollisionManager collisionManager; /// /// Запуск игры в комнате /// @@ -112,7 +111,6 @@ namespace ZoFo.GameCore networkManager.CloseConnection(); } - public CollisionManager collisionManager; private List gameObjects = new List(); private List entities; //entity public void Update(GameTime gameTime) @@ -151,10 +149,20 @@ namespace ZoFo.GameCore });//TODO return; } + if (gameObject is Entity) + { + AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name, IdEntity = (gameObject as Entity).Id }); + collisionManager.Register((gameObject as Entity).collisionComponent); + } + else + AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name }); - AddData(new UpdateGameObjectCreated() - { GameObjectType = gameObject.GetType().Name } - ); + + ////var elems = gameObject.GetType().GetProperties(System.Reflection.BindingFlags.Public); + ////if (elems.Count()>0) TODO + ////{ + //// AppManager.Instance.server.collisionManager.Register((elems.First().GetValue(gameObject) as CollisionComponent)); + ////} }