-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame1.cs
277 lines (269 loc) · 12.3 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Diagnostics;
using System.Linq;
namespace Conways_Game_of_Life
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private SpriteBatch _spriteBatchUI;
private Grid _grid;
public View CurrentView;
public string fileString { get; set; } = "";
public Rectangle LogoRectangle;
public bool Logohovered { get; set; } = false;
public bool UIActive { get; set; } = false;
public bool FileInputInProgress { get; set; } = false;
public bool CapsLockEnabled { get; set; } = false;
public bool ShiftEnabled { get; set; } = false;
public string FramesPerSecondCounter { get; set; }
public SpriteFont font;
public Texture2D Logo;
public KeyboardState PreviousKeyboardstate;
public Keys[] PreviousKeysPressed = new Keys[10];
public int updateValue { get; set; } = 1;
/// <summary>
/// Main method for the game class.
/// </summary>
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
this.Window.AllowUserResizing = true;
IsFixedTimeStep = true;
}
/// <summary>
/// Initialize.
/// </summary>
protected override void Initialize()
{
// Generate new Grid.
_grid = new Grid();
_graphics.PreferredBackBufferWidth = 1280;
_graphics.PreferredBackBufferHeight = 720;
_graphics.ApplyChanges();
Window.Title = "Conway's Game of Life";
//Generate a view for the game
CurrentView = new View();
_grid.Main(this, _graphics, CurrentView);
_grid.CreateGrid();
base.Initialize();
}
/// <summary>
/// Load content of the game.
/// </summary>
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_spriteBatchUI = new SpriteBatch(GraphicsDevice);
Logo = this.Content.Load<Texture2D>("GOLLogo");
font = this.Content.Load<SpriteFont>("ArialFont");
}
/// <summary>
/// Update loop section for main game class.
/// </summary>
/// <param name="gameTime"></param>
protected override void Update(GameTime gameTime)
{
var mouseState = Mouse.GetState();
// Gets Location of mouse
var mouseLocation = new Point(mouseState.X, mouseState.Y);
var keyboardState = Keyboard.GetState();
// Update the View
CurrentView.Update(gameTime);
// Have the rectangle dimensions follow the width of the window, even on resize
LogoRectangle = new Rectangle(_graphics.GraphicsDevice.Viewport.Width / 2 - 150, 0, 300, 125);
// If the Escape key is pressed the program will exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Checks to see if the update value is decreased
if (keyboardState.IsKeyDown(Keys.Up) && PreviousKeyboardstate.IsKeyUp(Keys.Up) && FileInputInProgress == false)
{
// 1 is the min update value
if (updateValue != 1 || updateValue !< 1)
{
updateValue -= 1;
}
}
// Checks to see if the update value is increased
if (keyboardState.IsKeyDown(Keys.Down) && PreviousKeyboardstate.IsKeyUp(Keys.Down) && FileInputInProgress == false)
{
// 20 is the max update value
if (updateValue != 20 || updateValue !> 20)
{
updateValue += 1;
}
}
// If "T" is pressed the UI is either toggled on/off
if (keyboardState.IsKeyDown(Keys.T) && PreviousKeyboardstate.IsKeyUp(Keys.T) && FileInputInProgress == false)
{
UIActive = !UIActive;
}
// If "`" is pressed the file input state of the game is either on/off
if (keyboardState.IsKeyDown(Keys.OemTilde) && PreviousKeyboardstate.IsKeyUp(Keys.OemTilde) && _grid.GameInProgress == false )
{
FileInputInProgress = !FileInputInProgress;
}
// General Caps Lock checker for the file input
if (keyboardState.IsKeyDown(Keys.CapsLock) && PreviousKeyboardstate.IsKeyUp(Keys.CapsLock))
{
CapsLockEnabled = !CapsLockEnabled;
}
// Checks to see if either left or right shift is held down
if (keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift) )
{
ShiftEnabled = true;
}
else
{
ShiftEnabled = false;
}
// Checks to see if the file input state is enabled
if (FileInputInProgress == true )
{
Keys[] CurrentPressedKeys = keyboardState.GetPressedKeys();
// Array of exempt keys
Keys[] ExemptKeys = { Keys.Back, Keys.LeftShift, Keys.RightShift, Keys.LeftControl, Keys.RightControl, Keys.CapsLock, Keys.OemTilde };
Keys[] Numbers = { Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9};
// Loops through each key pressed in the current pressed keys array
foreach (Keys key in CurrentPressedKeys)
{
// Makes sure one key stroke input is not mistaken for several key storkes.
if ( !PreviousKeysPressed.Contains(key) || PreviousKeysPressed.Contains(Keys.Back))
{
// When the back key is preseed remove one character off the file string
if (key == Keys.Back && fileString.Length > 0)
{
fileString = fileString.Remove(fileString.Length - 1);
}
// Checks to see if the keys are not in the exempt array and proceeds to add to the file string respective of the key pressed
if (!ExemptKeys.Contains(key))
{
if (key == Keys.OemPeriod)
{
fileString += ".";
}
else if (key == Keys.Space)
{
fileString += " ";
}
else if (Numbers.Contains(key))
{
// Takes the number input e.g "D0-9" and turns the second character into a string to represent the actual number
fileString += key.ToString()[^1];
}
else if (key == Keys.OemMinus)
{
if (CapsLockEnabled == true || ShiftEnabled == true)
{
fileString += "_";
}
else
{
fileString += "-";
}
}
else if (key == Keys.OemQuestion)
{
if (ShiftEnabled == true)
{
fileString += "?";
}
else
{
fileString += "/";
}
}
else if (key == Keys.OemPipe)
{
if (ShiftEnabled == true)
{
fileString += "|";
}
else
{
fileString += '\\';
}
}
// Basic uppercase and lowercase to check if caps or shift is enabled
else if (CapsLockEnabled == true || ShiftEnabled == true)
{
fileString += key.ToString().ToUpper();
}
else
{
fileString += key.ToString().ToLower();
}
}
}
}
// Sets the current array of keys to the previous keys for the next update loop
PreviousKeysPressed = CurrentPressedKeys;
}
// Checks to see if the mouse is in the rectangle of the logo
if (LogoRectangle.Contains(mouseLocation))
{
// Sets Logohovered
Logohovered = true;
}
else
{
Logohovered = false;
}
// Sets the previous keyboard state to the current keyboard state
PreviousKeyboardstate = keyboardState;
// Calls the grid update function
_grid.Update(updateValue, FileInputInProgress, fileString);
base.Update(gameTime);
}
/// <summary>
/// Draw loop for the main game class.
/// </summary>
/// <param name="gameTime"></param>
protected override void Draw(GameTime gameTime)
{
// Sets the background to White
GraphicsDevice.Clear(Color.White);
// Calls the grid draw function
_grid.Draw(_spriteBatch);
_spriteBatchUI.Begin();
// Only draws the Items and follow logic if the UI is active
if (UIActive == false)
{
// Set to Running or Not Running if the game is active
if (_grid.GameInProgress == true)
{
_spriteBatchUI.DrawString(font,"Running", new Vector2(0,0), Color.Green);
}
else
{
_spriteBatchUI.DrawString(font, "Not Running", new Vector2(0, 0), Color.Red);
}
// Checks to see if the logo is hovered and sets the opacity depending if the logo is being hovered over
if (Logohovered == true)
{
_spriteBatchUI.Draw(Logo, LogoRectangle, new Color(Color.White, 0.9f));
}
else
{
_spriteBatchUI.Draw(Logo, LogoRectangle, new Color(Color.White, 0.7f));
}
// Draws the different parameters for the game
_spriteBatchUI.DrawString(font, "X:" + _grid.mouseLocationSimplified.X.ToString() + "Y:" + _grid.mouseLocationSimplified.Y.ToString(), new Vector2(0, 15), Color.Black);
_spriteBatchUI.DrawString(font,"Slow: x" + updateValue.ToString(), new Vector2(0, 30), Color.Black);
_spriteBatchUI.DrawString(font, "File: " + fileString, new Vector2(0, 45), Color.Black);
_spriteBatchUI.DrawString(font,"Gen: " + _grid.GenerationNumber.ToString(), new Vector2(0, 60), Color.Black);
_spriteBatchUI.DrawString(font, "Pop: " + _grid.CellPopulation.ToString(), new Vector2(0, 75), Color.Black);
// Draws the FPS
_spriteBatchUI.DrawString(font, "FPS: " + Math.Round(1 / gameTime.ElapsedGameTime.TotalSeconds).ToString(), new Vector2(0, 90), Color.Black);
}
_spriteBatchUI.End();
base.Draw(gameTime);
}
}
}