-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
212 lines (184 loc) · 6 KB
/
Game1.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Game1.cs
* Final project
* Revision History
* Zhiyuan Wu, 2023.12.03: Created
* Zhiyuan Wu, 2023.12.04: Added code
* Zhiyuan Wu, 2023.12.10: Debugging complete
* Zhiyuan Wu, 2023.12.10: Comments added
*
*/
using CoolGame.Controls;
using CoolGame.Scene;
using CoolGame.States;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SharpDX.Direct2D1;
using System;
namespace CoolGame
{
/// <summary>
/// The main class for the game, responsible for initializing, updating, and drawing the game scenes.
/// </summary>
public class Game1 : Game
{
// Fields declarations...
public GraphicsDeviceManager _graphics;
public Microsoft.Xna.Framework.Graphics.SpriteBatch _spriteBatch;
public StartScene startScene;
public HelpScene helpScene;
public ActionScene actionScene;
private HighScoreScene highScoreScene;
private AboutScene aboutScene;
private EnterPlayerNameScene enterPlayerNameScene;
public GameOverScene overScene;
public Level2ActionScene level2ActionScene;
private KeyboardState oldState;
public static float ScreenHeight { get; set; }
public static float ScreenWidth { get; set; }
/// <summary>
/// Constructor for the Game1 class.
/// </summary>
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
/// <summary>
/// Initializes the game settings and scenes.
/// </summary>
protected override void Initialize()
{
Shared.stage = new Vector2(_graphics.PreferredBackBufferWidth,
_graphics.PreferredBackBufferHeight);
ScreenHeight = _graphics.PreferredBackBufferHeight;
ScreenWidth = _graphics.PreferredBackBufferWidth;
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// Handles scene change requests based on button clicks.
/// </summary>
/// <param name="sender">The source of the event, typically a button.</param>
/// <param name="e">Event arguments (not used).</param>
private void HandleSceneChangeRequested_Click(object sender, EventArgs e)
{
// Cast the sender back to a Button to access its properties
Button clickedButton = sender as Button;
// Check if the cast was successful
if (clickedButton != null)
{
// Access the Text property of the clicked button
string buttonText = clickedButton.Text;
// Implement logic to change the scene based on the button click
switch (buttonText)
{
case "Start Game":
hideAllScenes();
enterPlayerNameScene.show();
break;
case "Help":
hideAllScenes();
helpScene.show();
break;
case "High Score":
hideAllScenes();
highScoreScene.show();
break;
case "About":
hideAllScenes();
aboutScene.show();
break;
case "Quit":
Exit();
break;
// Add cases for other buttons
}
}
}
/// <summary>
/// Loads game content, sets up scenes, and initializes UI components.
/// </summary>
protected override void LoadContent()
{
_spriteBatch = new Microsoft.Xna.Framework.Graphics.SpriteBatch(GraphicsDevice);
level2ActionScene = new Level2ActionScene(this,"");
this.Components.Add(level2ActionScene);
enterPlayerNameScene = new EnterPlayerNameScene(this);
this.Components.Add(enterPlayerNameScene);
actionScene = new ActionScene(this, "");
this.Components.Add(actionScene);
startScene = new StartScene(this);
this.Components.Add(startScene);
helpScene = new HelpScene(this);
this.Components.Add(helpScene);
highScoreScene = new HighScoreScene(this);
this.Components.Add(highScoreScene);
aboutScene = new AboutScene(this);
this.Components.Add(aboutScene);
foreach (Button button in startScene.Menu.buttons)
{
button.Click += HandleSceneChangeRequested_Click;
}
startScene.show();
// TODO: use this.Content to load your game content here
//--background music ----
Song backgroundMusic = this.Content.Load<Song>("Sounds/BackgroundMusic");
MediaPlayer.IsRepeating = true;
MediaPlayer.Play(backgroundMusic);
//--background music ----
}
/// <summary>
/// Hides all game scenes.
/// </summary>
public void hideAllScenes()
{
//conditionally work as long as we don't add
//anything OTHER THAN game scenes to the components
//foreach (GameScene item in Components)
//{
// item.hide();
//}
//always work
foreach (GameComponent item in Components)
{
if (item is GameScene)
{
//will work
//GameScene gs = item as GameScene;
GameScene gs = (GameScene)item;
gs.hide();
}
}
}
/// <summary>
/// Updates the game state, handling scene transitions and input.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
KeyboardState ks = Keyboard.GetState();
if (helpScene.Enabled||enterPlayerNameScene.Enabled||highScoreScene.Enabled||aboutScene.Enabled)
{
if (oldState.IsKeyUp(Keys.Escape) && ks.IsKeyDown(Keys.Escape))
{
hideAllScenes();
startScene.show();
}
}
oldState = ks;
base.Update(gameTime);
}
/// <summary>
/// Draws the current game scene.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values, used for frame-based animation.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}