diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index d276984..96ff4ad 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -17,8 +17,6 @@ namespace DangerousD.GameCore public int Width { get; set; } public int Height { get; set; } public Rectangle Rectangle => new Rectangle((int)Pos.X, (int)Pos.Y, Width, Height); - public Vector2 velocity; - public Vector2 acceleration; protected abstract GraphicsComponent GraphicsComponent { get; } public GameObject(Vector2 pos) { diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs index 9a9cc5e..122f360 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs @@ -81,8 +81,10 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities } public void AnimationJump() { - velocity.Y = -11; - isJump = true; + if (isOnGround) + { + velocity.Y = -11; + } // здесь будет анимация } public void Shoot() @@ -93,11 +95,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities public override void Update(GameTime gameTime) { GraphicsComponent.CameraPosition = (_pos-new Vector2(200, 350)).ToPoint(); - velocity.X = 0.5f; - if (velocity.Y == 0) - { - isJump = false; - } base.Update(gameTime); Move(gameTime); } @@ -105,21 +102,20 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities public void Move(GameTime gameTime) { float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; - if (AppManager.Instance.InputManager.VectorMovementDirection.X==1) + velocity.X = 5 * AppManager.Instance.InputManager.VectorMovementDirection.X; + if (AppManager.Instance.InputManager.VectorMovementDirection.X > 0) { if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveRight")//идёт направо { GraphicsComponent.StartAnimation("ZombieMoveRight"); } - velocity.X = 5; } - else if (AppManager.Instance.InputManager.VectorMovementDirection.X == -1)//идёт налево + else if (AppManager.Instance.InputManager.VectorMovementDirection.X < 0)//идёт налево { if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveLeft") { GraphicsComponent.StartAnimation("ZombieMoveLeft"); } - velocity.X = -5; } else if(AppManager.Instance.InputManager.VectorMovementDirection.X == 0)//стоит { @@ -127,13 +123,12 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities { GraphicsComponent.StartAnimation("ZombieMoveLeft"); } - velocity.X = 0; } } public void MoveDown() { + // ПОЧЕМУ velocity.Y = -11; - isJump = true; } } diff --git a/DangerousD/GameCore/GameObjects/LivingEntity.cs b/DangerousD/GameCore/GameObjects/LivingEntity.cs index 5620568..90a2170 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntity.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntity.cs @@ -4,7 +4,7 @@ namespace DangerousD.GameCore.GameObjects; public abstract class LivingEntity : Entity { - private Vector2 targetPosition; + public bool isOnGround = true; public Vector2 velocity; public Vector2 acceleration; public LivingEntity(Vector2 position) : base(position) @@ -13,7 +13,7 @@ public abstract class LivingEntity : Entity } public override void SetPosition(Vector2 position) { - targetPosition = position; _pos = position; + _pos = position; } //TODO befrend targetpos and physics engine diff --git a/DangerousD/GameCore/Managers/PhysicsManager.cs b/DangerousD/GameCore/Managers/PhysicsManager.cs index 7e257e0..7899c98 100644 --- a/DangerousD/GameCore/Managers/PhysicsManager.cs +++ b/DangerousD/GameCore/Managers/PhysicsManager.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using DangerousD.GameCore.GameObjects.LivingEntities; using Microsoft.Xna.Framework; namespace DangerousD.GameCore.Managers @@ -38,41 +39,65 @@ namespace DangerousD.GameCore.Managers { foreach (var currentEntity in livingEntities) { - Rectangle oldRect = currentEntity.Rectangle; - bool isXNormalise = true; - bool isYNormalise = true; + var currentRect = currentEntity.Rectangle; + var newRect = currentRect; - oldRect.Offset((int)currentEntity.velocity.X, 0); + #region x collision + var collidedX = false; + var tryingRectX = currentRect; + tryingRectX.Offset((int)Math.Ceiling(currentEntity.velocity.X), 0); foreach (var mapObject in mapObjects) { if ( Math.Abs(mapObject.Pos.X - currentEntity.Pos.X) < 550 && Math.Abs(mapObject.Pos.Y - currentEntity.Pos.Y) < 550 - && oldRect.Intersects(mapObject.Rectangle) + && tryingRectX.Intersects(mapObject.Rectangle) ) { - isXNormalise = false; - oldRect.Offset(-(int)currentEntity.velocity.X, 0); + collidedX = true; break; } } - if (!isXNormalise) + if (collidedX) + { currentEntity.velocity.X = 0; - - - oldRect.Offset(0, (int)currentEntity.velocity.Y); + } + else + { + newRect.X = tryingRectX.X; + } + #endregion + + #region y collision + var collidedY = false; + var tryingRectY = currentRect; + tryingRectY.Offset(0, (int)Math.Ceiling(currentEntity.velocity.Y)); + if (currentEntity is Player) + { + AppManager.Instance.DebugHUD.Set("velocity", currentEntity.velocity.ToString()); + AppManager.Instance.DebugHUD.Set("intersects y", ""); + } foreach (var mapObject in mapObjects) { - if (oldRect.Intersects(mapObject.Rectangle)) + if (tryingRectY.Intersects(mapObject.Rectangle)) { - isYNormalise = false; - oldRect.Offset(0, -(int)currentEntity.velocity.Y); + if (currentEntity is Player) AppManager.Instance.DebugHUD.Set("intersects y", mapObject.GetType().ToString()); + collidedY = true; break; } } - if (!isYNormalise) + currentEntity.isOnGround = collidedY && currentEntity.velocity.Y > 0; + if (collidedY) + { currentEntity.velocity.Y = 0; - currentEntity.SetPosition(new Vector2(oldRect.X, oldRect.Y)); + } + else + { + newRect.Y = tryingRectY.Y; + } + #endregion + + currentEntity.SetPosition(new Vector2(newRect.X, newRect.Y)); } } diff --git a/DangerousD/GameCore/Managers/SoundManager.cs b/DangerousD/GameCore/Managers/SoundManager.cs index 3a1e03f..7da1b17 100644 --- a/DangerousD/GameCore/Managers/SoundManager.cs +++ b/DangerousD/GameCore/Managers/SoundManager.cs @@ -40,6 +40,7 @@ namespace DangerousD.GameCore public void StartAmbientSound(string soundName) // запустить звук у которого нет позиции { + return; var sound = new Sound(Sounds[soundName]); sound.SoundEffect.IsLooped = false; sound.SoundEffect.Play();