-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainMenuScreen.cs
131 lines (117 loc) · 4.83 KB
/
MainMenuScreen.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Newtonsoft.Json;
namespace ShogiClient
{
/// <summary>
/// The screen displayed when the user boots up the game
/// </summary>
public class MainMenuScreen : StatefulScreen<MainMenuScreenState>
{
private UIButton startGameButton;
private UIButton optionsButton;
private UIButton exitGameButton;
private UIButton playPauseButton;
private UIButton startFromSaveButton;
public MainMenuScreen(Game1 game) : base(game)
{
}
public override void Initialize(GameResources resources)
{
base.Initialize(resources);
startGameButton = new UIButton(resources)
{
Position = new Vector2(Game.WindowSize.X / 2, Game.WindowSize.Y * 2 / 5),
Size = new Vector2(200, 100),
Text = "Start Game",
};
startGameButton.OnClick += () =>
{
Game.SetCurrentScreen(new GameplayScreen(Game));
};
startFromSaveButton = new UIButton(resources)
{
Position = new Vector2(Game.WindowSize.X / 4, Game.WindowSize.Y * 2 / 5),
Size = new Vector2(200, 100),
Text = "Start Game From Last Save",
};
startFromSaveButton.OnClick += () =>
{
string json = File.ReadAllText("save.json");
var gameplayState = JsonConvert.DeserializeObject<GameplayScreenState>(json, new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto
});
Game.SetCurrentScreen(new GameplayScreen(Game)
{
State = gameplayState
});
};
optionsButton = new UIButton(resources)
{
Position = new Vector2(Game.WindowSize.X / 2, Game.WindowSize.Y * 3 / 5),
Size = new Vector2(200, 100),
Text = "Options",
};
optionsButton.OnClick += () =>
{
Game.SetCurrentScreen(new OptionsScreen<MainMenuScreenState>(Game, Resources, State, Game.Screenshot()), false);
};
exitGameButton = new UIButton(resources)
{
Position = new Vector2(Game.WindowSize.X / 2, Game.WindowSize.Y * 4 / 5),
Size = new Vector2(200, 100),
Text = "Quit",
};
exitGameButton.OnClick += () =>
{
Game.Exit();
};
playPauseButton = new UIButton(resources)
{
Position = new Vector2(Game.WindowSize.X * 4 / 5, 25),
Size = new Vector2(100, 50),
Text = "Play/Pause",
};
playPauseButton.OnClick += () =>
{
if (MediaPlayer.State == MediaState.Playing)
MediaPlayer.Pause();
else if (MediaPlayer.State == MediaState.Paused)
MediaPlayer.Resume();
};
}
public override void Update(GameTime gameTime, KeyboardState keyboardState, KeyboardState prevKeyboardState, MouseState mouseState, MouseState prevMouseState)
{
if (MediaPlayer.State == MediaState.Stopped)
{
MediaPlayer.Play(Resources.RandomMainMenuSong);
}
startGameButton.Update(gameTime, keyboardState, mouseState, prevMouseState);
optionsButton.Update(gameTime, keyboardState, mouseState, prevMouseState);
exitGameButton.Update(gameTime, keyboardState, mouseState, prevMouseState);
playPauseButton.Update(gameTime, keyboardState, mouseState, prevMouseState);
if (File.Exists("save.json"))
{
startFromSaveButton.Update(gameTime, keyboardState, mouseState, prevMouseState);
}
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Resources.MainMenuBackground, new Rectangle(Point.Zero, Game.WindowSize.ToPoint()), null, Color.White);
var logoPosition = new Vector2(Game.WindowSize.X / 2, Game.WindowSize.Y / 5);
spriteBatch.Draw(Resources.Logo, logoPosition - Resources.Logo.Bounds.Size.ToVector2(), null, Color.White, 0, Vector2.Zero, 2, SpriteEffects.None, 0);
startGameButton.Draw(spriteBatch);
optionsButton.Draw(spriteBatch);
exitGameButton.Draw(spriteBatch);
playPauseButton.Draw(spriteBatch);
if (File.Exists("save.json"))
{
startFromSaveButton.Draw(spriteBatch);
}
}
}
}