MenuState.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Tutorial020.Sprites;
  10. using Tutorial020.Controls;
  11. namespace Tutorial020.States
  12. {
  13. public class MenuState : State
  14. {
  15. private List<Component> _components;
  16. public MenuState(Game1 game, ContentManager content)
  17. : base(game, content)
  18. {
  19. }
  20. public override void LoadContent()
  21. {
  22. var buttonTexture = _content.Load<Texture2D>("Button");
  23. var buttonFont = _content.Load<SpriteFont>("Font");
  24. _components = new List<Component>()
  25. {
  26. new Sprite(_content.Load<Texture2D>("Background/MainMenu"))
  27. {
  28. Layer = 0f,
  29. Position = new Vector2(Game1.ScreenWidth / 2, Game1.ScreenHeight / 2),
  30. },
  31. new Button(buttonTexture, buttonFont)
  32. {
  33. Text = "1 Player",
  34. Position = new Vector2(Game1.ScreenWidth / 2, 400),
  35. Click = new EventHandler(Button_1Player_Clicked),
  36. Layer = 0.1f
  37. },
  38. new Button(buttonTexture, buttonFont)
  39. {
  40. Text = "2 Players",
  41. Position = new Vector2(Game1.ScreenWidth / 2, 440),
  42. Click = new EventHandler(Button_2Player_Clicked),
  43. Layer = 0.1f
  44. },
  45. new Button(buttonTexture, buttonFont)
  46. {
  47. Text = "Highscores",
  48. Position = new Vector2(Game1.ScreenWidth / 2, 480),
  49. Click = new EventHandler(Button_Highscores_Clicked),
  50. Layer = 0.1f
  51. },
  52. new Button(buttonTexture, buttonFont)
  53. {
  54. Text = "Quit",
  55. Position = new Vector2(Game1.ScreenWidth / 2, 520),
  56. Click = new EventHandler(Button_Quit_Clicked),
  57. Layer = 0.1f
  58. },
  59. };
  60. }
  61. private void Button_1Player_Clicked(object sender, EventArgs args)
  62. {
  63. _game.ChangeState(new GameState(_game, _content)
  64. {
  65. PlayerCount = 1,
  66. });
  67. }
  68. private void Button_2Player_Clicked(object sender, EventArgs args)
  69. {
  70. _game.ChangeState(new GameState(_game, _content)
  71. {
  72. PlayerCount = 2,
  73. });
  74. }
  75. private void Button_Highscores_Clicked(object sender, EventArgs args)
  76. {
  77. _game.ChangeState(new HighscoresState(_game, _content));
  78. }
  79. private void Button_Quit_Clicked(object sender, EventArgs args)
  80. {
  81. _game.Exit();
  82. }
  83. public override void Update(GameTime gameTime)
  84. {
  85. foreach (var component in _components)
  86. component.Update(gameTime);
  87. }
  88. public override void PostUpdate(GameTime gameTime)
  89. {
  90. }
  91. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  92. {
  93. spriteBatch.Begin(SpriteSortMode.FrontToBack);
  94. foreach (var component in _components)
  95. component.Draw(gameTime, spriteBatch);
  96. spriteBatch.End();
  97. }
  98. }
  99. }