-
Notifications
You must be signed in to change notification settings - Fork 0
/
RailsrogueGame.cs
212 lines (174 loc) · 4.67 KB
/
RailsrogueGame.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
#region File Description
//-----------------------------------------------------------------------------
// RailsrogueGame.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using Railsrogue.Entities;
#endregion
namespace Railsrogue
{
/// <summary>
/// Default Project Template
/// </summary>
public class RailsrogueGame : Game
{
#region Fields
private static RailsrogueGame s_game = null;
public static RailsrogueGame Game
{
get { return s_game; }
}
GraphicsDeviceManager m_graphics;
SpriteBatch m_spriteBatch;
bool m_enumeratingActiveSet;
List<Entity> m_entities, m_addedEntities, m_removedEntities;
public Terrain Terrain {
get;
set;
}
public WizardHero Hero {
get;
set;
}
#endregion
#region Initialization
public RailsrogueGame ()
{
s_game = this;
m_entities = new List<Entity>();
m_addedEntities = new List<Entity>();
m_removedEntities = new List<Entity>();
m_enumeratingActiveSet = false;
IsMouseVisible = true;
Content.RootDirectory = "Content";
m_graphics = new GraphicsDeviceManager (this);
m_graphics.IsFullScreen = false;
}
public void Add (Entity e)
{
if (m_enumeratingActiveSet) {
m_addedEntities.Add (e);
} else {
e.LoadContent (Content);
m_entities.Add (e);
e.Initialize ();
}
}
private void AddPendingEntities ()
{
foreach (Entity entity in m_addedEntities) {
entity.LoadContent (Content);
}
m_entities.AddRange (m_addedEntities);
foreach (Entity entity in m_addedEntities) {
entity.Initialize ();
}
m_addedEntities.Clear ();
}
public void Remove (Entity e)
{
if (m_enumeratingActiveSet) {
m_removedEntities.Add (e);
} else {
m_entities.Remove (e);
e.OnDestroy ();
}
}
private void RemovePendingEntities ()
{
foreach (Entity entity in m_removedEntities) {
m_entities.Remove (entity);
entity.OnDestroy ();
}
m_removedEntities.Clear ();
}
/// <summary>
/// Overridden from the base Game.Initialize. Once the GraphicsDevice is setup,
/// we'll use the viewport to initialize some values.
/// </summary>
protected override void Initialize ()
{
m_entities.Add (Terrain = new Terrain (GraphicsDevice.Viewport.Width));
m_entities.Add (Hero = new WizardHero (new Vector2 (37, 398 - 64)));
m_enumeratingActiveSet = true;
// Initialize calls LoadContent.
base.Initialize ();
foreach (Entity entity in m_entities) {
entity.Initialize ();
}
m_enumeratingActiveSet = false;
}
/// <summary>
/// Load your graphics content.
/// </summary>
protected override void LoadContent ()
{
// Create a new SpriteBatch, which can be use to draw textures.
m_spriteBatch = new SpriteBatch (m_graphics.GraphicsDevice);
foreach (Entity entity in m_entities) {
entity.LoadContent (Content);
}
}
#endregion
#region Update and Draw
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update (GameTime gameTime)
{
KeyboardState state = Keyboard.GetState ();
Keys[] keys = state.GetPressedKeys ();
foreach (Keys key in keys) {
switch (key) {
case Keys.Escape:
Exit ();
break;
}
}
m_enumeratingActiveSet = true;
foreach (Entity entity in m_entities) {
entity.Update (gameTime);
}
m_enumeratingActiveSet = false;
RemovePendingEntities ();
AddPendingEntities ();
base.Update (gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw (GameTime gameTime)
{
// Clear the backbuffer
m_graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
m_spriteBatch.Begin ();
// TODO: Layers / ordering.
m_enumeratingActiveSet = true;
foreach (Entity entity in m_entities) {
entity.Draw (m_spriteBatch);
}
m_enumeratingActiveSet = false;
m_spriteBatch.End ();
base.Draw (gameTime);
}
#endregion
}
}