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