0
我正在開發一款遊戲,並且我想爲它創建一個簡單的碰撞引擎。遊戲以2D爲基礎,基於圖塊,並且只有一個玩家角色。我正在使用Monogame(C#)來創建它。如何改善我的碰撞邏輯
這裏是我的代碼:
static class character
{
public static Rectangle currentCharacterRectangle = new Rectangle(0, 0, Game1.human.Width, Game1.human.Height);
public static Rectangle futureCharacterRectangle = currentCharacterRectangle;
public static Vector2 currentRespawnPos;
public static Vector2 currentCharacterVelocity;
public static Vector2 currentCharacterAcceleration;
public static int walkingSpeed = 1;
public static int runningSpeed = 3;
static int jumpSpeed = -14;
static bool isJumping = false;
static bool willBeColliding = false;
static void getFutureRectangle()
{
if (Game1.currentKeyboardState.IsKeyDown(Keys.D))
{
futureCharacterRectangle.X += walkingSpeed;
}
if (Game1.currentKeyboardState.IsKeyDown(Keys.D) && Game1.currentKeyboardState.IsKeyDown(Keys.LeftShift))
{
futureCharacterRectangle.X += runningSpeed;
}
if (Game1.currentKeyboardState.IsKeyDown(Keys.Q))
{
futureCharacterRectangle.X -= walkingSpeed;
}
if (Game1.currentKeyboardState.IsKeyDown(Keys.Q) && Game1.currentKeyboardState.IsKeyDown(Keys.LeftShift))
{
futureCharacterRectangle.X -= runningSpeed;
}
if(Game1.currentKeyboardState.IsKeyDown(Keys.Space) && Game1.previousKeyboardState.IsKeyUp(Keys.Space))
{
isJumping = true;
}
if (Game1.currentKeyboardState.IsKeyDown(Keys.S))
{
futureCharacterRectangle.Y++;
}
if (Game1.currentKeyboardState.IsKeyDown(Keys.Z))
{
futureCharacterRectangle.Y--;
}
}
public static void jump()
{
if (jumpSpeed <= 14 && isJumping)
{
futureCharacterRectangle.Y += jumpSpeed;
jumpSpeed++;
}
else if (jumpSpeed > 14)
{
isJumping = false;
jumpSpeed = -14;
}
}
public static void Update()
{
jump();
getFutureRectangle();
foreach(earthTile Tile in gameWorld.tileArray)
{
if(futureCharacterRectangle.Intersects(Tile.currentRectangle))
{
willBeColliding = true;
}
}
if(!willBeColliding)
{
currentCharacterRectangle = futureCharacterRectangle;
}
futureCharacterRectangle = currentCharacterRectangle;
willBeColliding = false;
}
public static void Draw()
{
Game1.spriteBatch.Draw(Game1.human, currentCharacterRectangle, Color.White);
}
}
我想知道是否有這樣做的更好的方法(也許更簡單,或更快,或兩者)。
非常感謝!