MenuState.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Tutorial013.Controls;
  10. namespace Tutorial013.States
  11. {
  12. public class MenuState : State
  13. {
  14. private List<Component> _components;
  15. public MenuState(Game1 game, GraphicsDevice graphicsDevice, ContentManager content)
  16. : base(game, graphicsDevice, content)
  17. {
  18. var buttonTexture = _content.Load<Texture2D>("Controls/Button");
  19. var buttonFont = _content.Load<SpriteFont>("Fonts/Font");
  20. var newGameButton = new Button(buttonTexture, buttonFont)
  21. {
  22. Position = new Vector2(300, 200),
  23. Text = "New Game",
  24. };
  25. newGameButton.Click += NewGameButton_Click;
  26. var loadGameButton = new Button(buttonTexture, buttonFont)
  27. {
  28. Position = new Vector2(300, 250),
  29. Text = "Load Game",
  30. };
  31. loadGameButton.Click += LoadGameButton_Click;
  32. var quitGameButton = new Button(buttonTexture, buttonFont)
  33. {
  34. Position = new Vector2(300, 300),
  35. Text = "Quit Game",
  36. };
  37. quitGameButton.Click += QuitGameButton_Click;
  38. _components = new List<Component>()
  39. {
  40. newGameButton,
  41. loadGameButton,
  42. quitGameButton,
  43. };
  44. }
  45. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  46. {
  47. spriteBatch.Begin();
  48. foreach (var component in _components)
  49. component.Draw(gameTime, spriteBatch);
  50. spriteBatch.End();
  51. }
  52. private void LoadGameButton_Click(object sender, EventArgs e)
  53. {
  54. Console.WriteLine("Load Game");
  55. }
  56. private void NewGameButton_Click(object sender, EventArgs e)
  57. {
  58. _game.ChangeState(new GameState(_game, _graphicsDevice, _content));
  59. }
  60. public override void PostUpdate(GameTime gameTime)
  61. {
  62. // remove sprites if they're not needed
  63. }
  64. public override void Update(GameTime gameTime)
  65. {
  66. foreach (var component in _components)
  67. component.Update(gameTime);
  68. }
  69. private void QuitGameButton_Click(object sender, EventArgs e)
  70. {
  71. _game.Exit();
  72. }
  73. }
  74. }