From 5a5a24dffdb4744829297923ae9a4253ce51c65e Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Wed, 16 Aug 2023 15:08:55 +0300 Subject: [PATCH] minor changes and fixes, waiting for physics fix --- DangerousD/GameCore/GameObjects/GameObject.cs | 3 + DangerousD/GameCore/Managers/AppManager.cs | 1 + .../GameCore/Managers/PhysicsManager.cs | 72 ++++++++++--------- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index 52c96c1..c4b19cf 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -48,9 +48,12 @@ namespace DangerousD.GameCore GraphicsComponent.Update(); } + //static Texture2D debugTexture; public virtual void Draw(SpriteBatch spriteBatch) { GraphicsComponent.DrawAnimation(Rectangle, spriteBatch); + //debug + //spriteBatch.Draw(debugTexture, Rectangle, Color.White); } } } \ No newline at end of file diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index 67a65cf..e7abf16 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -53,6 +53,7 @@ namespace DangerousD.GameCore _spriteBatch = new SpriteBatch(GraphicsDevice); MenuGUI.LoadContent(); LoginGUI.LoadContent(); + //GameObject.te } protected override void Update(GameTime gameTime) diff --git a/DangerousD/GameCore/Managers/PhysicsManager.cs b/DangerousD/GameCore/Managers/PhysicsManager.cs index 268fc75..e847c45 100644 --- a/DangerousD/GameCore/Managers/PhysicsManager.cs +++ b/DangerousD/GameCore/Managers/PhysicsManager.cs @@ -20,12 +20,6 @@ namespace DangerousD.GameCore.Managers item.velocity = item.velocity + item.acceleration * delta; } - //SD setting positions before check - foreach (var item in livingEntities) - { - item.SetPosition(item.Pos + item.velocity); - } - CheckCollisions(livingEntities, mapObjects); OnCollision(entities, livingEntities); OnCollision(livingEntities); @@ -44,39 +38,49 @@ namespace DangerousD.GameCore.Managers { for (int i = 0; i < livingEntities.Count; i++) { - Rectangle oldRect = new Rectangle(); + var currentEntity = livingEntities[i]; + Rectangle oldRect = currentEntity.Rectangle; + + + oldRect.Offset((int)currentEntity.velocity.X / 2, 0); for (int j = 0; j < mapObjects.Count; j++) { - if (livingEntities[i].Rectangle.Intersects(mapObjects[j].Rectangle)) + if (oldRect.Intersects(mapObjects[j].Rectangle)) { - if (livingEntities[i].Rectangle.Right > mapObjects[j].Rectangle.Left) - { - - //livingEntities[i].SetPosition(livingEntities[i].Pos- new Vector2(livingEntities[i].velocity.X)); - livingEntities[i].velocity.X = 0; - livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X - (livingEntities[i].Rectangle.Right - mapObjects[j].Rectangle.Left), - livingEntities[i].Pos.Y)); - } - if (livingEntities[i].Rectangle.Left < mapObjects[j].Rectangle.Right) - { - livingEntities[i].velocity.X = 0; - livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X + mapObjects[j].Rectangle.Right - livingEntities[i].Rectangle.Left, - livingEntities[i].Pos.Y)); - } - if (livingEntities[i].Rectangle.Bottom > mapObjects[j].Rectangle.Top) - { - livingEntities[i].velocity.Y = 0; - livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X, - livingEntities[i].Pos.Y - (livingEntities[i].Rectangle.Bottom - mapObjects[j].Rectangle.Top))); - } - if (livingEntities[i].Rectangle.Top > mapObjects[j].Rectangle.Bottom) - { - livingEntities[i].velocity.Y = 0; - livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X, - livingEntities[i].Pos.Y + (mapObjects[j].Rectangle.Bottom - livingEntities[i].Rectangle.Top))); - } + oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0); + break; } } + oldRect.Offset((int)currentEntity.velocity.X / 2, 0); + for (int j = 0; j < mapObjects.Count; j++) + { + if (oldRect.Intersects(mapObjects[j].Rectangle)) + { + oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0); + break; + } + } + + + oldRect.Offset(0, (int)currentEntity.velocity.Y/2); + for (int j = 0; j < mapObjects.Count; j++) + { + if (oldRect.Intersects(mapObjects[j].Rectangle)) + { + oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2); + break; + } + } + oldRect.Offset(0, (int)currentEntity.velocity.Y / 2); + for (int j = 0; j < mapObjects.Count; j++) + { + if (oldRect.Intersects(mapObjects[j].Rectangle)) + { + oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2); + break; + } + } + currentEntity.SetPosition(new Vector2(oldRect.X, oldRect.Y)); } }