[177586]: src / PathDefense / Game.cs Maximize Restore History

Download this file

Game.cs    270 lines (223 with data), 7.4 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
 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
/*
------------------------------------------------------------------------------
This source file is a part of the Path Defense Framework.
Copyright (c) 2012 Florian Haag
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
------------------------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace PathDefense
{
using Events;
using Navigation;
/// <summary>
/// The central game logic component.
/// </summary>
[Serializable]
public partial class Game
{
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="tileWidth">The width of the tiles in the game.</param>
/// <param name="tileHeight">The height of the tiles in the game.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tileWidth"/> or <paramref name="tileHeight"/> is not a positive number.</exception>
public Game(double tileWidth, double tileHeight)
{
if (tileWidth <= 0) {
throw new ArgumentOutOfRangeException("tileWidth", "The tile width must be a positive number.");
}
if (tileHeight <= 0) {
throw new ArgumentOutOfRangeException("tileHeight", "The tile height must be a positive number.");
}
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.waves.Changed += WavesChanged;
}
/// <summary>
/// The random generator used by the instance.
/// </summary>
/// <seealso cref="RandomGenerator"/>
private readonly Random rdm = new Random();
/// <summary>
/// The random generator used by the instance.
/// </summary>
/// <value>
/// <para>This member is provided as a courtesy to other modules that need an initialized random generator.</para>
/// </value>
public Random RandomGenerator {
get {
return rdm;
}
}
/// <summary>
/// The current leve.
/// </summary>
/// <seealso cref="CurrentLevel"/>
private Level currentLevel;
/// <summary>
/// The current leve.
/// </summary>
/// <exception cref="InvalidOperationException">The property is read while no level is loaded.</exception>
/// <value>
/// <para>Gets or sets the level that is currently executed by the <see cref="Game"/> instance.</para>
/// </value>
/// <seealso cref="IsLevelLoaded"/>
public Level CurrentLevel {
get {
CheckLevelLoaded();
return currentLevel;
}
set {
if (currentLevel != value) {
FinalizeLevel();
currentLevel = value;
InitializeLevel();
}
}
}
/// <summary>
/// Indicates whether there is currently any level loaded in the instance.
/// </summary>
public bool IsLevelLoaded {
get {
return currentLevel != null;
}
}
/// <summary>
/// Ensures that there is an active level, otherwise throws an exception.
/// </summary>
/// <exception cref="InvalidOperationException"><see cref="IsLevelLoaded"/> is <see langword="false"/>.</exception>
protected void CheckLevelLoaded()
{
if (currentLevel == null) {
throw new InvalidOperationException("There is no level loaded in the game instance.");
}
}
/// <summary>
/// Stores whether the current level has been initialized.
/// </summary>
private bool levelInitialized;
/// <summary>
/// Initializes the current level, if any level has been assigned.
/// </summary>
private void InitializeLevel()
{
if ((currentLevel != null) && !levelInitialized) {
foreach (var waveFactory in currentLevel.Waves) {
triggers.Add(waveFactory.CreateTrigger());
}
levelInitialized = true;
currentLevel.NotifyLoaded(this);
OnLevelLoaded(new LevelEventArgs(currentLevel));
levelChanged = true;
}
}
protected virtual void OnLevelLoaded(LevelEventArgs e)
{
if (e == null) {
throw new ArgumentNullException("e");
}
if (LevelLoaded != null) {
LevelLoaded(this, e);
}
}
/// <summary>
/// This event is fired after a level has been initialized.
/// </summary>
/// <seealso cref="LevelUnloaded"/>
public event EventHandler<LevelEventArgs> LevelLoaded;
/// <summary>
/// Finalizes the current level, if any level is active.
/// </summary>
private void FinalizeLevel()
{
if (currentLevel != null) {
shots.Clear();
triggers.Clear();
waves.Clear();
customObjects.Clear();
}
levelInitialized = false;
if (currentLevel != null) {
Level oldLevel = currentLevel;
currentLevel = null;
OnLevelUnloaded(new LevelEventArgs(oldLevel));
}
}
protected virtual void OnLevelUnloaded(LevelEventArgs e)
{
if (e == null) {
throw new ArgumentNullException("e");
}
if (LevelUnloaded != null) {
LevelUnloaded(this, e);
}
}
/// <summary>
/// This event is fired after a level has been finalized.
/// </summary>
/// <seealso cref="LevelLoaded"/>
public event EventHandler<LevelEventArgs> LevelUnloaded;
private bool levelChanged;
/// <summary>
/// Advances the state of the game by the given amount of time.
/// </summary>
/// <param name="deltaTime">The elapsed amount of time since the last step.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="deltaTime"/> is not greater than zero.</exception>
public void Step(double deltaTime)
{
if (deltaTime <= 0) {
throw new ArgumentOutOfRangeException("deltaTime", "The elapsed amount of time must be greater than zero.");
}
levelChanged = false;
if (currentLevel != null) {
UpdateShots(deltaTime);
UpdateCreatures(deltaTime);
UpdateTowers(deltaTime);
UpdateCustomObjects(deltaTime);
for (int i = 0; i < triggers.Count; i++) {
var trigger = triggers[i];
if (trigger.Execute(this)) {
if (levelChanged) {
return;
}
triggers.RemoveAt(i);
i--;
}
}
if (TimeElapsed != null) {
TimeElapsed(this, new TimeElapsedEventArgs(deltaTime));
}
}
}
private readonly ObservableList<Trigger> triggers = new ObservableList<Trigger>(false);
public void AddTrigger(Trigger trigger)
{
if (trigger == null) {
throw new ArgumentNullException("trigger");
}
triggers.Add(trigger);
}
/// <summary>
/// This event is fired when a period of time has elapsed.
/// </summary>
public event EventHandler<TimeElapsedEventArgs> TimeElapsed;
}
}