开发者

XNA how to organize code with game components (managers)

XNA If I have a button class, and I have a buttonManager class that manages what buttons to be drawn to the screen depending on what the current game state开发者_如何学编程 is (main menu, in game, etc), how do I organize my code so that when I click on a button, it takes the user to a different screen. (ex. options menu, ingame). To be more specific, if I were to put the check to see if the user clicks on the button within the buttonManager how would I have the game switch and run the actual game (which is in an entire different class)?

  • Main gameclass
  • buttonManager game component (adds buttons)
  • Button class


One way of doing that is by using delegates. Another way would be to have a global class that all classes use for determining state.

If you're unfamiliar with the concept of game state I have a tutorial on my site easing you into the basics (and the last sample in that tutorial uses delegates as well!)

http://www.xnadevelopment.com/tutorials/thestateofthings/thestateofthings.shtml


Your screen class (menuScreen, optionsScreen, gameplayScreen, etc.) will need to hold the functionality for each button it displays. For instance:

//in optionsScreen class' update method:
if(input.gamepad.Buttons.A == ButtonState.Pressed && cursorSpriteRect.Intersects(button[0].Rect)
{
  //begin transition into gameplay screen... or whatever
} 


Your game must have or be able to obtain a reference to the button manager. Usually your game will create and own the buttonManager.

class Game
{
   ButtonManager m_buttonManager;

   ...
}  

Your button manager can expose an event like OnButtonClicked.

class ButtonManager
{
   private Button m_playGameButton;

   // delegate to define the type of the event handler
   public delegate void ButtonClickedEventHandler(ButtonId buttonId);

   // event object matching the type of the event handler
   public event ButtonClickedEventHandler OnButtonClicked;

   void Update()
   {
      ...

      if (m_playGameButton.Clicked)
      {
         // Launch the event when appropriate if there are any subscribers
         if (OnButtonClicked != null)
         {
            OnButtonClicked(ButtonId.PlayGame)
         }
      }
   }
}

Your game class can then subscribe to the event and provide a handler method.

class Game
{
    ...

    void Initialise()
    {
        m_buttonManager += ButtonClicked;
    }

    void ButtonClicked(ButtonId buttonId)
    {
        switch (buttonId)
        {
            case ButtonId.PlayGame:
                PlayGame();
                break;

            case ButtonId.OptionsMenu:
                OptionsMenu();
                break;
        }
    }

    ...
}

Alternatively the game class can poll the button manager.

class Game
{
   ...

   void Update()
   {
      if (m_buttonManager.IsPlayGameButtonHit)
      {
         PlayGame();
      }
      else if (m_buttonManager.IsOptionsMenuButtonHit)
      {
         OptionsMenu();
      }
   }

   ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜