From 9e3c0f09adb600de1dc75eb5ca28dc69f56fca92 Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Wed, 16 Aug 2023 15:46:12 +0300 Subject: [PATCH] fixed physics --- DangerousD/GameCore/GameObjects/GameObject.cs | 4 +- .../LivingEntities/Monsters/Zombie.cs | 12 +++--- .../GameCore/GameObjects/LivingEntity.cs | 2 +- DangerousD/GameCore/Levels/Level1.cs | 4 +- DangerousD/GameCore/Managers/AppManager.cs | 3 +- .../GameCore/Managers/PhysicsManager.cs | 40 +++++++++++++------ 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index c4b19cf..a29eb79 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -48,12 +48,12 @@ namespace DangerousD.GameCore GraphicsComponent.Update(); } - //static Texture2D debugTexture; + public static Texture2D debugTexture; public virtual void Draw(SpriteBatch spriteBatch) { GraphicsComponent.DrawAnimation(Rectangle, spriteBatch); //debug - //spriteBatch.Draw(debugTexture, Rectangle, Color.White); + // spriteBatch.Draw(debugTexture, Rectangle, Color.White); } } } \ No newline at end of file diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs index 23e010a..e120977 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs @@ -48,14 +48,14 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters { if (isGoRight) { - GraphicsComponent.StopAnimation(); - GraphicsComponent.StartAnimation("ZombieRightAttack"); + if (GraphicsComponent.GetCurrentAnimation != "ZombieRightAttack") + GraphicsComponent.StartAnimation("ZombieRightAttack"); AppManager.Instance.GameManager.players[0].Death(name); } else if (!isGoRight) { - GraphicsComponent.StopAnimation(); - GraphicsComponent.StartAnimation("ZombieLeftAttack"); + if (GraphicsComponent.GetCurrentAnimation != "ZombieLeftAttack") + GraphicsComponent.StartAnimation("ZombieLeftAttack"); AppManager.Instance.GameManager.players[0].Death(name); } } @@ -72,14 +72,14 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters { if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveRight") GraphicsComponent.StartAnimation("ZombieMoveRight"); - velocity = new Vector2(monster_speed, 0); + velocity.X = monster_speed; } else if (!isGoRight) { if(GraphicsComponent.GetCurrentAnimation != "ZombieMoveLeft") GraphicsComponent.StartAnimation("ZombieMoveLeft"); - velocity = new Vector2(-monster_speed, 0); + velocity.X = -monster_speed; } } diff --git a/DangerousD/GameCore/GameObjects/LivingEntity.cs b/DangerousD/GameCore/GameObjects/LivingEntity.cs index 5352489..75890aa 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntity.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntity.cs @@ -9,7 +9,7 @@ public abstract class LivingEntity : Entity public Vector2 acceleration; public LivingEntity(Vector2 position) : base(position) { - acceleration = new Vector2(0, 10); + acceleration = new Vector2(0, 30); } public void SetPosition(Vector2 position) { targetPosition = position; _pos = position; } //TODO befrend targetpos and physics engine diff --git a/DangerousD/GameCore/Levels/Level1.cs b/DangerousD/GameCore/Levels/Level1.cs index e468dbb..d23c7c6 100644 --- a/DangerousD/GameCore/Levels/Level1.cs +++ b/DangerousD/GameCore/Levels/Level1.cs @@ -12,8 +12,8 @@ namespace DangerousD.GameCore.Levels { new Player(new Vector2(0,0)); - var Zombie = new Zombie(new Vector2(256, 128)); - var Frank = new Frank(new Vector2(384, 128)); + var Zombie = new Zombie(new Vector2(300, 64)); + var Frank = new Frank(new Vector2(100, 64)); new GrassBlock(new Vector2(0, 224)); for (int i = 0; i < 50; i++) diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index e7abf16..04ffef1 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -53,7 +53,8 @@ namespace DangerousD.GameCore _spriteBatch = new SpriteBatch(GraphicsDevice); MenuGUI.LoadContent(); LoginGUI.LoadContent(); - //GameObject.te + GameObject.debugTexture = new Texture2D(GraphicsDevice, 1, 1); + GameObject.debugTexture.SetData(new Color[] { new Color(1, 0,0,0.25f) }); } protected override void Update(GameTime gameTime) diff --git a/DangerousD/GameCore/Managers/PhysicsManager.cs b/DangerousD/GameCore/Managers/PhysicsManager.cs index e847c45..19c84ad 100644 --- a/DangerousD/GameCore/Managers/PhysicsManager.cs +++ b/DangerousD/GameCore/Managers/PhysicsManager.cs @@ -40,26 +40,35 @@ namespace DangerousD.GameCore.Managers { var currentEntity = livingEntities[i]; Rectangle oldRect = currentEntity.Rectangle; - + bool isXNormalise = true; + bool isYNormalise = true; oldRect.Offset((int)currentEntity.velocity.X / 2, 0); for (int j = 0; j < mapObjects.Count; j++) { if (oldRect.Intersects(mapObjects[j].Rectangle)) { + isXNormalise = false; 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)) + if (isXNormalise) + { + oldRect.Offset((int)currentEntity.velocity.X / 2, 0); + for (int j = 0; j < mapObjects.Count; j++) { - oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0); - break; + if (oldRect.Intersects(mapObjects[j].Rectangle)) + { + isXNormalise = false; + oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0); + break; + } } } + if (!isXNormalise) + currentEntity.velocity.X = 0; oldRect.Offset(0, (int)currentEntity.velocity.Y/2); @@ -67,19 +76,26 @@ namespace DangerousD.GameCore.Managers { if (oldRect.Intersects(mapObjects[j].Rectangle)) { + isYNormalise = false; 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 (isYNormalise) { - if (oldRect.Intersects(mapObjects[j].Rectangle)) + oldRect.Offset(0, (int)currentEntity.velocity.Y / 2); + for (int j = 0; j < mapObjects.Count; j++) { - oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2); - break; + if (oldRect.Intersects(mapObjects[j].Rectangle)) + { + isYNormalise = false; + oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2); + break; + } } } + if (!isYNormalise) + currentEntity.velocity.Y = 0; currentEntity.SetPosition(new Vector2(oldRect.X, oldRect.Y)); }