fix jump
This commit is contained in:
parent
12d44ff9f1
commit
b48bec2d6d
5 changed files with 52 additions and 33 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -80,9 +80,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
|
|||
isAlive = false;*/
|
||||
}
|
||||
public void AnimationJump()
|
||||
{
|
||||
if (isOnGround)
|
||||
{
|
||||
velocity.Y = -11;
|
||||
isJump = true;
|
||||
}
|
||||
// здесь будет анимация
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
else
|
||||
{
|
||||
newRect.X = tryingRectX.X;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
oldRect.Offset(0, (int)currentEntity.velocity.Y);
|
||||
#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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue