-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsteroidsGame.cs
333 lines (277 loc) · 10.1 KB
/
AsteroidsGame.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System.Drawing;
using System.Media;
using System.Collections.Generic;
using AsteroidsGdiApp.GameObjects;
using AsteroidsGdiApp.GameObjects.Meteors;
using AsteroidsGdiApp.Core;
using System;
using System.IO;
namespace AsteroidsGdiApp.Core
{
public class AsteroidsGame : Game
{
EnemyShipShooterService _enemyShipShooterService;
PlayerShip _ship = new PlayerShip();
private Photon[] _photons;
private IGameController _gameController = new RunAloneGameController();
private List<Meteor> _meteors;
GameManager _gameManager;
private GameScoreKeeper _gameScoreKeeper = new GameScoreKeeper();
EnemyShip _enemyShip = new EnemyShip();
PhotonTimeManager _shipPhotonTimeManager = new PhotonTimeManager(150);
ScoredPointsDisplay _scorePointsDisplay = new ScoredPointsDisplay();
DateTime _enemyShipLastShown;
public AsteroidsGame(IGameForm parent, IGameController gameController) : base(parent)
{
StartNewRound();
_gameController = gameController;
}
private void StartNewRound()
{
_enemyShipLastShown = DateTime.Now;
_gameScoreKeeper.IncrementLevel();
_photons = new Photon[20];
_meteors = new List<Meteor>();
_enemyShipShooterService = new EnemyShipShooterService(_enemyShip);
for(int i = 0; i < _photons.Length; i++)
{
_photons[i] = new Photon();
}
_photons[0] = new TracerPhoton();
for (int i = 0; i < 4+ _gameScoreKeeper.Level; i++)
{
_meteors.Add( new Meteor(new LargeMeteorType(), new Point(0, 0)));
}
}
//TODO Get rid of event driven design
protected void GameManager_MeteorWasHit(object sender, MeteorHitEventArgs e)
{
_gameScoreKeeper.UpdateScore(e.Meteor.Score);
_scorePointsDisplay.Display(e.Meteor.Location, e.Meteor.Score);
if (!(e.Meteor.GetNextSmallerMeteor() is NullMeteorType))
{
_meteors.Add(new Meteor(e.Meteor.GetNextSmallerMeteor(), e.Meteor.Location));
_meteors.Add(new Meteor(e.Meteor.GetNextSmallerMeteor(), e.Meteor.Location));
}
}
protected override void Update()
{
InitializeTheGameManager();
HandleControllerInputs();
UpdateTheShip();
UpdatePhotons();
UpdateMeteors();
DetectBulletCollisions();
DetectShipToMeteorCollisions();
StartRoundOverIfNecessary();
ReactivateAShipIfPlayerHasLivesLeft();
SetGameOverIfNecessary();
SendOffEnemyShip();
_enemyShip.Update();
_enemyShipShooterService.Update();
_enemyShipShooterService.FireRound();
_scorePointsDisplay.Update();
}
private void SendOffEnemyShip()
{
if (!_enemyShip.IsActive && DateTime.Now.Subtract(_enemyShipLastShown).TotalSeconds > Constants.EnemyShipInterval)
{
_enemyShip.Activate();
_enemyShipLastShown = DateTime.Now;
}
}
private void SetGameOverIfNecessary()
{
if (_gameScoreKeeper.Lives == 0)
{
_gameScoreKeeper.SetStandaloneMode();
}
}
private void InitializeTheGameManager()
{
if (_gameManager == null)
{
_gameManager = GameManager.TheGameManager;
_gameManager.MeteorWasHit += new System.EventHandler<MeteorHitEventArgs>(GameManager_MeteorWasHit);
}
}
private void UpdateTheShip()
{
_ship.Update();
}
private void HandleControllerInputs()
{
if(_gameController.KeyboardState.IsLeftKeyDown)
_ship.RotateLeft();
if(_gameController.KeyboardState.IsRightKeyDown)
_ship.RotateRight();
if(_gameController.KeyboardState.IsUpKeyDown)
_ship.Thrust();
else if(_gameController.KeyboardState.IsDownKeyDown)
_ship.SlowDown();
if (_gameController.KeyboardState.IsFireKeyDown)
Fire();
if (_gameController.KeyboardState.IsShieldKeyDown)
_ship.TurnOnShield();
if (_gameController.KeyboardState.IsUpKeyDown)
_ship.TurnOnThruster();
else
_ship.TurnOffThruster();
}
private void ReactivateAShipIfPlayerHasLivesLeft()
{
if (!_ship.IsActive && _gameScoreKeeper.Lives > 0)
{
_ship.Activate();
}
}
private void StartRoundOverIfNecessary()
{
if(RoundIsComplete())
StartNewRound();
}
private bool RoundIsComplete()
{
bool thereAreNoMoreActiveMeteors = true;
foreach (Meteor meteor in _meteors)
{
if (meteor.IsActive)
thereAreNoMoreActiveMeteors = false;
}
return thereAreNoMoreActiveMeteors;
}
private void DetectBulletCollisions()
{
for(int meteorIndex = 0; meteorIndex < _meteors.Count; meteorIndex++)
{
for (int bulletIndex = 0; bulletIndex < _photons.Length; bulletIndex++)
{
//TODO get rid of event drive desing here
_meteors[meteorIndex].TestIfShot(_photons[bulletIndex]);
}
}
//Test if player ship has been hit by the enemy ships bullet
if (_enemyShipShooterService.PhotonCollidesWithShip(_ship))
{
BlowUpShip();
}
//Test to see if the players bullet has hit the enemy ship
foreach (Photon photon in _photons)
{
if (photon.IsActive && _enemyShip.IsActive)
{
if (_enemyShip.IsPointWithin(photon.Location))
{
_enemyShip.Inactivate();
_gameScoreKeeper.UpdateScore(2000);
}
}
}
}
private void DetectShipToMeteorCollisions()
{
if (!_ship.IsActive)
return;
for (int meteorIndex = 0; meteorIndex < _meteors.Count; meteorIndex++)
{
if (_meteors[meteorIndex].CollidesWithShip(_ship))
{
_meteors[meteorIndex].IsActive = false;
GameManager.RaiseMeteorHit(_meteors[meteorIndex]);
BlowUpShip();
System.IO.Stream str = Properties.Resources.mySoundFile;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer("bang_6.wav");
snd.Play();
}
}
}
private void DetectShipToShipCollision()
{
if (_enemyShip.IsPointWithin(_ship.Location))
{
}
}
private void BlowUpShip()
{
_ship.SetInactive();
_gameScoreKeeper.DecrementNumberOfLives();
_ship.BlowUpShip();
}
private void UpdatePhotons()
{
_photons.Update();
}
private void
UpdateMeteors()
{
_meteors.Update();
}
private void DrawPhotons(Graphics graphics)
{
_photons.Draw(graphics);
}
private void DrawMeteors(Graphics graphics)
{
_meteors.Draw(graphics);
}
private void Fire()
{
if (!_ship.IsActive)
return;
foreach (var photon in _photons)
{
if (!photon.IsActive)
{
photon.Fire(_ship.Location, _ship.ShipDirection, Constants.MaxPlayerBulletDistance, _shipPhotonTimeManager);
}
}
}
public override void Draw()
{
Bitmap backbuffer = new Bitmap(Constants.CanvasWidth, Constants.CanvasWidth);
using (Graphics graphics = Graphics.FromImage(backbuffer))
{
ClearTheCanvas(graphics);
DrawShips(graphics);
DrawPhotons(graphics);
DrawMeteors(graphics);
DrawHeadsUpDisplay(graphics);
_enemyShipShooterService.Draw(graphics);
_scorePointsDisplay.Draw(graphics);
}
//Draw onto the window
using (Graphics windowGraphics = ParentForm.CreateGraphics())
{
windowGraphics.DrawImageUnscaled(backbuffer, 0, 0);
}
}
private void ClearTheCanvas(Graphics graphics)
{
graphics.FillRectangle(Brushes.Black, 0, 0, Constants.CanvasWidth, Constants.CanvasWidth);
}
private void DrawHeadsUpDisplay(Graphics graphics)
{
_gameScoreKeeper.Draw(graphics);
}
private void DrawShips(Graphics graphics)
{
_ship.Draw(graphics);
_enemyShip.Draw(graphics);
}
public void Init(IGameController controller)
{
_gameController = controller;
_gameScoreKeeper.StartGame();
_ship.Activate();
StartNewRound();
}
}
internal class WaveReader
{
private Stream str;
public WaveReader(Stream str)
{
this.str = str;
}
}
}