-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
287 lines (228 loc) · 10.9 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
#endregion
namespace gameProject
{
public class RenderContext
{
public SpriteBatch SpriteBatch { get; set; } //For sprite/Particle rendering
public GraphicsDevice GraphicsDevice { get; set; } //for states setting
public GraphicsDeviceManager Graphics; //For Viewport and stuff
public GameTime GameTime { get; set; }
public BaseCamera Camera; //for rendering 3D
public World World; //for creating rigidBodies
public Game1 Game; //for easy access to restarting
public ContentManager Content; //loading of content
public Vector2 ViewPortSize;
public bool GameActive = false;
public Player Player;
public GamePadState PadState;
public GameComponentCollection Component;
}
public class Game1 : Game
{
// General
GraphicsDeviceManager m_Graphics;
public SpriteBatch SpriteBatch;
RenderContext m_RenderContext;
// Physics
World m_FarseerWorld;
Vector2 m_Gravity = new Vector2(0, -9.8f);
//GameScene
public GameScene MainScene;
// Pause menu && startMenu
public MenuManager m_MenuManager;
MenuManager m_LostMenu;
public bool PausedGame = true;
KeyboardState m_OldState;
//BloomEffect
BloomPostProcessing m_BloomPP;
WindParticleSystem m_Particles;
//Testing of Sprites
Texture2D m_Texture;
GameSprite m_Sprite;
public Game1()
: base()
{
//Set Screen size baed on settings
var screenData = XmlLoader.Load<WorldSettings>("WorldSettings");
m_Graphics = new GraphicsDeviceManager(this);
if (screenData.Fullscreen)
{
m_Graphics.ToggleFullScreen();
m_Graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
m_Graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
}
else
{
m_Graphics.PreferredBackBufferWidth = screenData.ScreenWidth;
m_Graphics.PreferredBackBufferHeight = screenData.ScreenHeight;
}
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
// Create a new SpriteBatch, which can be used to draw textures.
SpriteBatch = new SpriteBatch(GraphicsDevice);
//Create a new FarseerWorld to allow physics calculations
m_FarseerWorld = new World(m_Gravity);
ConvertUnits.SetDisplayUnitToSimUnitRatio(1.0f);
//Create a renderContext that holds important information that we can easily access
m_RenderContext = new RenderContext();
m_RenderContext.World = m_FarseerWorld;
m_RenderContext.SpriteBatch = SpriteBatch;
m_RenderContext.GraphicsDevice = m_Graphics.GraphicsDevice;
m_RenderContext.Content = Content;
m_RenderContext.Game = this;
m_RenderContext.Camera = new BaseCamera(m_RenderContext);
m_RenderContext.ViewPortSize = new Vector2((float)m_Graphics.PreferredBackBufferWidth, (float)m_Graphics.PreferredBackBufferHeight);
m_RenderContext.Graphics = m_Graphics;
m_RenderContext.Component = Components;
//René's Controller problems
if (System.Environment.UserName == "Rene") m_RenderContext.PadState = GamePad.GetState(PlayerIndex.Two, GamePadDeadZone.Circular);
else m_RenderContext.PadState = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular);
base.Initialize();
}
protected override void LoadContent()
{
//MENU
//***************
m_MenuManager = new MenuManager(m_RenderContext.ViewPortSize / 2);
m_MenuManager.AddItem(new GUIButton("Textures/GUI/Buttons/StartNormal", "Textures/GUI/Buttons/StartOnHover", ButtonName.Start, m_RenderContext));
m_MenuManager.AddItem(new GUIButton("Textures/GUI/Buttons/ExitNormal", "Textures/GUI/Buttons/ExitOnHover", ButtonName.Exit, m_RenderContext));
m_MenuManager.AddItem(new GUIButton("Textures/GUI/Buttons/RestartNormal", "Textures/GUI/Buttons/RestartOnHover", ButtonName.Restart, m_RenderContext));
m_MenuManager.AddItem(new GUIButton("Textures/GUI/Buttons/ToggleFullscreenNormal", "Textures/GUI/Buttons/ToggleFullscreenOnHover", ButtonName.Fullscreen, m_RenderContext));
//Set the splash screen and hit screen
m_MenuManager.BackgroundScreen = new GameSprite("Textures/GUI/BackgroundStartScreen", m_RenderContext);
m_MenuManager.SplashScreen = new GameSprite("Textures/GUI/DAEScreen_GUI", m_RenderContext);
m_MenuManager.Initialize(m_RenderContext);
m_LostMenu = new MenuManager(m_RenderContext.ViewPortSize / 2);
m_LostMenu.AddItem(new GUIButton("Textures/GUI/Buttons/ExitNormal", "Textures/GUI/Buttons/ExitOnHover", ButtonName.Exit, m_RenderContext));
m_LostMenu.AddItem(new GUIButton("Textures/GUI/Buttons/RestartNormal", "Textures/GUI/Buttons/RestartOnHover", ButtonName.Restart, m_RenderContext));
m_LostMenu.Initialize(m_RenderContext);
//SOUND
//*****
SoundManager.AddSound("Background", m_RenderContext);
// SoundManager.AddSound("ButtonClick", m_RenderContext);
//POST PROCESSING
//***************
m_BloomPP = new BloomPostProcessing(this);
m_RenderContext.Component.Add(m_BloomPP);
m_Particles = new WindParticleSystem(m_RenderContext, 7);
// m_RenderContext.Component.Add(m_Particles);
//GAMESCENE
//*********
MainScene = new GameScene();
m_Texture = Content.Load<Texture2D>("Textures/Models/D_Default");
m_Sprite = new GameSprite("Textures/Models/D_Default", m_RenderContext);
m_Sprite.Scale(0.5f);
}
protected override void UnloadContent()
{
m_RenderContext.Content.Unload();
}
protected override void Update(GameTime gameTime)
{
//SET GAMEPAD STATE
//*****************
if (System.Environment.UserName == "Rene") m_RenderContext.PadState = GamePad.GetState(PlayerIndex.Two, GamePadDeadZone.Circular);
else m_RenderContext.PadState = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular);
m_RenderContext.GameTime = gameTime;
m_RenderContext.GameActive = MainScene.isActive;
m_RenderContext.ViewPortSize = new Vector2(m_Graphics.GraphicsDevice.Viewport.Width, m_Graphics.GraphicsDevice.Viewport.Height);
//update our scene
PauseGame();
if (!PausedGame && MainScene.isActive)
{
//GAMESCENE UPDATE
//****************
MainScene.Update(m_RenderContext);
//update the farseer world
m_FarseerWorld.Step((float)m_RenderContext.GameTime.ElapsedGameTime.Milliseconds / 100);
Vector2 Where = m_RenderContext.Player.Position;
m_Particles.AddParticles(Where);
//LOSE SCREEN UPDATE
if (MainScene.hasLost)
m_LostMenu.Update(m_RenderContext);
}
else
m_MenuManager.Update(m_RenderContext);
m_Sprite.Update(m_RenderContext);
base.Update(gameTime);
}
public Rectangle? DrawRect;
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
//DRAW MAIN SCENE AND DRAW POSTPROCESSING
//***************************************
if (!PausedGame && MainScene.isActive)
{
m_BloomPP.BeginDraw();
m_BloomPP.Visible = true;
MainScene.Draw(m_RenderContext);
if (MainScene.hasLost)
{
m_LostMenu.Draw(m_RenderContext);
TextRenderer.DrawText("You Died! Press Escape to go back to the menu", m_RenderContext.ViewPortSize.X * 0.30f, m_RenderContext.ViewPortSize.Y * 0.1f, Color.White, m_RenderContext);
}
}
else
{
//No post processing in the menu
m_BloomPP.Visible = false;
m_MenuManager.Draw(m_RenderContext);
}
//if (m_RenderContext.Player != null)
//{
// Vector2 translation = -m_RenderContext.Player.Position + m_RenderContext.ViewPortSize / 2;
// Matrix View = Matrix.CreateTranslation(new Vector3(translation, 0));
// SpriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, View);
// // SpriteBatch.Draw(m_Texture, new Vector2(150, 450), Color.Orange);
// m_Sprite.Color = Color.Green;
// //m_Sprite.Draw(m_RenderContext);
// m_Sprite.Position = new Vector2(150, 150);
// m_Sprite.Color = Color.Red;
// m_RenderContext.SpriteBatch.Draw(m_Sprite.m_Texture, m_Sprite.Position, null, Color.White, MathHelper.ToRadians(0), Vector2.Zero, Vector2.One, SpriteEffects.None, 1);
// m_Sprite.Position = new Vector2(660, 150);
// //m_Sprite.Position = ConvertUnits.ToDisplayUnits(m_Sprite.Position);
// m_Sprite.Draw(m_RenderContext, 1.0f, true);
//}
base.Draw(gameTime);
}
void PauseGame()
{
// Pause menu
var newState = Keyboard.GetState();
if (newState.IsKeyDown(Keys.Escape) && !m_OldState.IsKeyDown(Keys.Escape))
{
if (!PausedGame)
SoundManager.Pause("Background");
else
SoundManager.Play("Background", true);
PausedGame = !PausedGame;
}
m_OldState = newState;
}
public void RestartGame()
{
//Clean up previous level
MainScene.CleanUp();
//Reinitialize scene
MainScene.Initialize(m_RenderContext);
PausedGame = false;
}
}
}