Я не знаю почему, но мой метод прыжка не работает. Мой персонаж может прыгать только на одном объекте. Другое препятствие не имеет никакого влияния на игрока. Я действительно не вижу своей ошибки
foreach (RectangleShape obstacles in MoveAtObjectList)
{
MainPlayer.Move(deltaTime, obstacles);
}
foreach (RectangleShape obstacles in JumpAtObjectList)
{
MainPlayer.Jump(deltaTime, obstacles);
break;
}
Draw();
Window.Display();
}
}
}
}
Это было частью моего основного класса, где я вызываю метод jump внутри foreach l oop. Здесь, в своем классе Payer, я попытался написать функцию перехода.
using SFML.Graphics;
using SFML.System;
using SFML.Window;
namespace Pong3final
{
class Player : RectangleShape
{
float CollisionDistance = 0.5f;
float PlayerSpeed = 50;
float JumpSpeed = 100;
float FallSpeed = 100;
float JumpTimer;
bool JumpBool = false;
bool Fall = true;
public void Jump(float deltaTime, RectangleShape collisionObject)
{
if (Keyboard.IsKeyPressed(Keyboard.Key.Space) && !Fall && CheckCollision(this, collisionObject))
{
JumpBool = true;
}
//Player is jumping
if (JumpBool)
{
JumpTimer += deltaTime;
this.Position -= new Vector2f(0, JumpSpeed) * deltaTime;
if (JumpTimer > 0.5f)
{
JumpBool = false;
Fall = true;
JumpTimer = 0;
}
}
//Player is falling when he doesnt touch anything and he isnt jumping
else if (!CheckCollision(this, collisionObject))
{
Fall = true;
}
//Player is falling
if (!JumpBool && !CheckCollision(this, collisionObject) && Fall)
{
this.Position += new Vector2f(0, FallSpeed) * deltaTime;
if (CheckCollision(this, collisionObject)) //Player stops falling because of a collision with an object
{
Fall = false;
}
}
}
public void Move(float deltaTime, RectangleShape collisionObject)
{
{
Vector2f MovePlayerPosition = Position;
if ((Keyboard.IsKeyPressed(Keyboard.Key.Left) || Keyboard.IsKeyPressed(Keyboard.Key.A)))
{
MovePlayerPosition -= new Vector2f(PlayerSpeed, 0) * deltaTime;
FloatRect collisionOverlap;
if (CheckCollision(this, collisionObject, out collisionOverlap))
{
MovePlayerPosition = MovePlayerPosition + new Vector2f(collisionOverlap.Width + CollisionDistance, 0);
}
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Right) || Keyboard.IsKeyPressed(Keyboard.Key.D))
{
MovePlayerPosition += new Vector2f(PlayerSpeed, 0) * deltaTime;
FloatRect collisionOverlap;
if (CheckCollision(this, collisionObject, out collisionOverlap))
{
MovePlayerPosition = MovePlayerPosition - new Vector2f(collisionOverlap.Width + CollisionDistance, 0);
}
}
Position = MovePlayerPosition;
}
}
public static bool CheckCollision(RectangleShape obj1, RectangleShape obj2, out FloatRect overlap)
{
FloatRect obj1Collider = obj1.GetGlobalBounds();
FloatRect obj2Collider = obj2.GetGlobalBounds();
//Nur die rechte, linke und obere Seite kollidiert
if (obj1Collider.Intersects(obj2Collider, out overlap))
{
return true;
}
overlap = new FloatRect();
return false;
}
public static bool CheckCollision(RectangleShape obj1, RectangleShape obj2)
{
FloatRect obj1Collider = obj1.GetGlobalBounds();
FloatRect obj2Collider = obj2.GetGlobalBounds();
return obj1Collider.Intersects(obj2Collider);
}
}
}