2017-06-18 79 views
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); 
    } 
} 

我想知道是否有這樣做的更好的方法(也許更簡單,或更快,或兩者)。

非常感謝!

回答

0

沒關係,找到了解決辦法:

static void move() 
    { 
     if (Game1.currentKeyboardState.IsKeyDown(Keys.D)) 
     { 
      bool isColliding = false; 
      foreach (tile Tile in gameWorld.tileArray) 
      { 
       if (currentCharacterRectangle.Right >= Tile.currentRectangle.Left && currentCharacterRectangle.Left < Tile.currentRectangle.Right && currentCharacterRectangle.Top < Tile.currentRectangle.Bottom && currentCharacterRectangle.Bottom > Tile.currentRectangle.Top && Tile.isSolid) 
       { 
        isColliding = true; 
       } 
      } 
      if (!isColliding) 
      { 
       currentCharacterRectangle.X += walkingSpeed; 
      } 
     } 
     if (Game1.currentKeyboardState.IsKeyDown(Keys.D) && Game1.currentKeyboardState.IsKeyDown(Keys.LeftShift)) 
     { 
      currentCharacterRectangle.X += runningSpeed; 
     } 

     if (Game1.currentKeyboardState.IsKeyDown(Keys.Q)) 
     { 
      bool isColliding = false; 
      foreach (tile Tile in gameWorld.tileArray) 
      { 
       if (currentCharacterRectangle.Left <= Tile.currentRectangle.Right && currentCharacterRectangle.Right > Tile.currentRectangle.Left && currentCharacterRectangle.Top < Tile.currentRectangle.Bottom && currentCharacterRectangle.Bottom > Tile.currentRectangle.Top && Tile.isSolid) 
       { 
        isColliding = true; 
       } 
      } 
      if (!isColliding) 
      { 
       currentCharacterRectangle.X -= walkingSpeed; 
      } 
     } 
     if (Game1.currentKeyboardState.IsKeyDown(Keys.Q) && Game1.currentKeyboardState.IsKeyDown(Keys.LeftShift)) 
     { 
      currentCharacterRectangle.X -= runningSpeed; 
     } 

     if (Game1.currentKeyboardState.IsKeyDown(Keys.S)) 
     { 
      bool isColliding = false; 
      foreach (tile Tile in gameWorld.tileArray) 
      { 
       if (currentCharacterRectangle.Bottom >= Tile.currentRectangle.Top && currentCharacterRectangle.Top < Tile.currentRectangle.Bottom && currentCharacterRectangle.Right > Tile.currentRectangle.Left && currentCharacterRectangle.Left < Tile.currentRectangle.Right && Tile.isSolid) 
       { 
        isColliding = true; 
       } 
      } 
      if (!isColliding) 
      { 
       currentCharacterRectangle.Y++; 
      } 
     } 

     if (Game1.currentKeyboardState.IsKeyDown(Keys.Z)) 
     { 
      bool isColliding = false; 
      foreach (tile Tile in gameWorld.tileArray) 
      { 
       if (currentCharacterRectangle.Top <= Tile.currentRectangle.Bottom && currentCharacterRectangle.Top > Tile.currentRectangle.Top && currentCharacterRectangle.Right > Tile.currentRectangle.Left && currentCharacterRectangle.Left < Tile.currentRectangle.Right && Tile.isSolid) 
       { 
        isColliding = true; 
       } 
      } 
      if (!isColliding) 
      { 
       currentCharacterRectangle.Y--; 
      } 
     } 
    }