-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.cs
388 lines (346 loc) · 12.7 KB
/
GameController.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
namespace KosmiczniNajeźdźcy
{
public class GameController
{
private Bitmap playArea;
private System.Timers.Timer playerFireCooldown;
private System.Timers.Timer enemyTick;
public readonly static int PixelSize = 2;
List<AnimShEntity> enemies = new();
const int startEnemyCount = 11 * 5;
bool areEnemiesMovingRight = true;
List<Bullet> enemyBullets = new();
bool enemiesTickUnHandled = false;
PlayerCannon player;
List<Bullet> playerBullets = new();
public int[] vectToMovePlayerBy = new int[] { 0, 0 };
public bool fireButtonHeld = false;
bool playerFireCooldownElapsed = true;
public bool ogFireMode = true;
public int player1Score = 0;
private int playerLives = 3;
public int HighScore
{
get
{
return Properties.Settings1.Default.highScore;
}
}
public bool GameOverHappened { private set; get; }
bool bulletHasCollided = true; // since last partial redraw
List<Barrier> barriers = new();
public int PlayerLives { get => playerLives; private set => playerLives = value; }
public void Start()
{
playArea = new Bitmap(700, 800);
player = new PlayerCannon(new Point(330, 680));
SpawnEnemies();
for (int b = 0; b < 4; b++) // spawn barriers
{
barriers.Add(new Barrier(new Point(90 + 150 * b, 590), 2));
}
playerFireCooldown = new System.Timers.Timer(100); //450
playerFireCooldown.Elapsed += OnPlayerFireCooldownElapsed;
playerFireCooldown.AutoReset = false;
playerFireCooldown.Enabled = false;
enemyTick = new System.Timers.Timer(1000);
enemyTick.Interval = 1000 - ((startEnemyCount - enemies.Count) * 18);
enemyTick.Elapsed += OnEnemyTick;
enemyTick.AutoReset = true;
enemyTick.Enabled = true;
}
private void SpawnEnemies()
{
enemies.Clear();
for (int j = 0; j < startEnemyCount / 5; j++) // spawn enemies
{
enemies.Add(new EnemySquid((PixelSize * 25) * j + 70 + 2 * PixelSize, 175));
enemies.Add(new EnemyCrab((PixelSize * 25) * j + 70, 175 + 50 * 1));
enemies.Add(new EnemyCrab((PixelSize * 25) * j + 70, 175 + 50 * 2));
enemies.Add(new EnemyEclipse((PixelSize * 25) * j + 70, 175 + 50 * 3));
enemies.Add(new EnemyEclipse((PixelSize * 25) * j + 70, 175 + 50 * 4));
}
}
private void RefreshAll(List<IDrawable> list, Graphics g)
{
foreach (var entity in list)
{
entity.Refresh(g);
}
}
public void Update(PaintEventArgs e) // runs every draw event
{
int speed = 5;
Graphics g = Graphics.FromImage(playArea);
player.MoveBy(vectToMovePlayerBy[0] * speed, vectToMovePlayerBy[1] * speed, true);
if (fireButtonHeld && !player.IsDead)
{
PlayerFire();
}
if (player.IsDead)
{
GameOverHappened = true;
}
player.Refresh(g);
if (enemies.Count == 0)
{
//some delay?
SpawnEnemies();
}
//foreach (var enemy in enemies)
//{
// enemy.Refresh(g);
//}
for (int bullet = 0; bullet < playerBullets.Count(); bullet++) // Update player bullets
{
playerBullets[bullet].Refresh(g);
if (playerBullets[bullet].IsDead) //if it is dead, remove from list
{
playerBullets.RemoveAt(bullet);
}
else
{
int collidedBulletIndex = CheckPlayerBullets(bullet, g);
if (collidedBulletIndex != -1) // -1 means it didnt collide
{
bulletHasCollided = true;
playerBullets[collidedBulletIndex].ReciveDamage();// damage collided bullet
}
}
}
for (int bullet = 0; bullet < enemyBullets.Count(); bullet++) // Update enemy bullets
{
enemyBullets[bullet].Refresh(g);
if (enemyBullets[bullet].IsDead)
{
enemyBullets.RemoveAt(bullet); //if it is dead, remove from list
}
else
{
int collidedBulletIndex = CheckEnemyBullets(bullet, g);
if (collidedBulletIndex != -1) // -1 means it didnt collide
{
bulletHasCollided = true;
enemyBullets[collidedBulletIndex].ReciveDamage();// damage collided bullet
}
}
}
if (bulletHasCollided)
{
RefreshAll(barriers.Cast<IDrawable>().ToList(), g);
RefreshAll(enemies.Cast<IDrawable>().ToList(), g);
bulletHasCollided = false;
}
if (enemiesTickUnHandled)
{
{
int enemyNrToDelete = -1;
do
{
enemyNrToDelete = -1;
for (int i = 0; i < enemies.Count; i++)
{
if (enemies[i].IsDead)
{
enemyNrToDelete = i;
}
}
if (enemyNrToDelete != -1)
{
enemies.RemoveAt(enemyNrToDelete);
}
} while (enemyNrToDelete != -1);
}
MoveEnemies(g);
// enemy shooting
Random r = new Random();
foreach (var enemy in enemies)
{
if (r.Next(0, 100) > 96)
{
enemyBullets.Add(enemies[r.Next(0, enemies.Count())].Fire(Color.White));
}
}
//int howManyShots = r.Next((enemies.Count / 10 + 1) * (-1), enemies.Count / 10 + 2);
//if (howManyShots > 0)
//{
// for (int i = 0; i < howManyShots; i++)
// {
// }
//}
enemiesTickUnHandled = false;
}
e.Graphics.DrawImage(playArea, 0, 0);
}
private int CheckPlayerBullets(int bullet, Graphics g)
{
int ifNotHitValue = -1;
if (playerBullets[bullet].Pos.Y < 70) // check if bullet has left upper bounds
{
return bullet;
}
for (int enemy = 0; enemy < enemies.Count(); enemy++) // chech if bullet is in one of the enemies
{
if (enemies[enemy].IsAt(playerBullets[bullet].Pos.X, playerBullets[bullet].Pos.Y))
{
player1Score += enemies[enemy].PointVal;
enemies[enemy].ReciveDamage();
enemies[enemy].Refresh(g);
return bullet;
}
}
foreach (var barrier in barriers) // check for collisions with barriers
{
if (barrier.IsAt(playerBullets[bullet].Pos.X, playerBullets[bullet].Pos.Y))
{
barrier.ReciveDamage(playerBullets[bullet].Pos.X, playerBullets[bullet].Pos.Y);
return bullet;
}
}
return ifNotHitValue;
}
private int CheckEnemyBullets(int bullet, Graphics g)
{
int ifNotHitValue = -1;
if (enemyBullets[bullet].Pos.Y >= 750) // check if bullet has left lower bounds
{
return bullet;
}
foreach (var barrier in barriers) // check for collisions with barriers
{
if (barrier.IsAt(enemyBullets[bullet].Pos.X, enemyBullets[bullet].Pos.Y))
{
barrier.ReciveDamage(enemyBullets[bullet].Pos.X, enemyBullets[bullet].Pos.Y);
return bullet;
}
}
if (player.IsAt(enemyBullets[bullet].Pos.X, enemyBullets[bullet].Pos.Y))
{
PlayerShot();
return bullet;
}
return ifNotHitValue;
}
private void PlayerShot()
{
PlayerLives--;
if (PlayerLives == 0)
{
player.ReciveDamage();
}
}
private void OnPlayerFireCooldownElapsed(Object? source, System.Timers.ElapsedEventArgs e)
{
playerFireCooldownElapsed = true;
}
private void PlayerFire()
{
if (ogFireMode)
{
if (playerBullets.Count() == 0)
{
playerBullets.Add(player.Fire(PlayerCannon.color));
}
}
else
{
if (playerFireCooldownElapsed)
{
playerFireCooldownElapsed = false;
playerBullets.Add(player.Fire(PlayerCannon.color));
playerFireCooldown.Start();
}
}
}
private void OnEnemyTick(Object? source, System.Timers.ElapsedEventArgs e)
{
enemyTick.Interval = 1000 - ((startEnemyCount - enemies.Count) * 18);
enemiesTickUnHandled = true;
}
private void MoveEnemies(Graphics g)
{
if (enemies.Count != 0)
{
int stepSizeX = 5;
int stepSizeY = 25;
Entity rightmost = GetRightMostEnemy();
Entity leftmost = GetLeftMostEnemy();
int moveByX = 0, moveByY = 0;
if ((rightmost.Pos.X + rightmost.Graphic.Size.Width > 700 - stepSizeX) && areEnemiesMovingRight ||
(leftmost.Pos.X < 0 + stepSizeX) && !areEnemiesMovingRight)
{
moveByY += stepSizeY;
areEnemiesMovingRight = !areEnemiesMovingRight;
}
else
{
if (areEnemiesMovingRight)
{
moveByX += stepSizeX;
}
else
{
moveByX -= stepSizeX;
}
}
foreach (var enemie in enemies)
{
enemie.MoveBy(moveByX, moveByY);
enemie.Refresh(g);
if (enemie.Pos.Y > barriers[0].Pos.Y + barriers[0].Graphic.Size.Height) // if enemies get to low, kill player
{
player.ReciveDamage();
}
}
}
}
private Entity GetRightMostEnemy()
{
int max = int.MinValue;
Entity entity = enemies[0];
foreach (var enemy in enemies)
{
if (enemy.Pos.X + enemy.Graphic.Size.Width > max)
{
max = enemy.Pos.X + enemy.Graphic.Size.Width;
entity = enemy;
}
}
return entity;
}
private Entity GetLeftMostEnemy()
{
int min = int.MaxValue;
Entity entity = enemies[0];
foreach (var enemy in enemies)
{
if (enemy.Pos.X > min)
{
min = enemy.Pos.X;
entity = enemy;
}
}
return entity;
}
public void RestartAfterGameOver()
{
enemies.Clear();
enemyBullets.Clear();
playerBullets.Clear();
SaveHighscore();
player1Score = 0;
PlayerLives = 3;
GameOverHappened = false;
Start(); // Start() clears playArea at the beginning
}
const string highScoreFilePath = "highscore.txt";
public void SaveHighscore()
{
if (player1Score > HighScore)
{
Properties.Settings1.Default.highScore = player1Score;
Properties.Settings1.Default.Save();
}
}
}
}