-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame1.cs
More file actions
81 lines (66 loc) · 2.49 KB
/
Game1.cs
File metadata and controls
81 lines (66 loc) · 2.49 KB
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
using System; //need to add this for exception handling
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MyIslandGame.Core;
using MyIslandGame.States;
namespace MyIslandGame
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private GameStateManager _stateManager;
private TimeManager _timeManager; // Add TimeManager field
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
// Set window size (for development)
_graphics.PreferredBackBufferWidth = 1280;
_graphics.PreferredBackBufferHeight = 720;
}
protected override void Initialize()
{
// Create the state manager
_stateManager = new GameStateManager(this);
// Initialize TimeManager
_timeManager = new TimeManager();
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
// Create and add game states
var playingState = new PlayingState(this, _stateManager);
_stateManager.AddState<PlayingState>(playingState);
// Set the initial state
_stateManager.ChangeState<PlayingState>();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Update the current state
_stateManager.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
try
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Draw the current state
_stateManager.Draw(gameTime, _spriteBatch);
base.Draw(gameTime);
}
catch (Exception ex)
{
// Log the exception
System.Diagnostics.Debug.WriteLine($"Draw error: {ex.Message}\n{ex.StackTrace}");
// Optionally reset states or take recovery actions
}
}
}
}