-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson48.cs
More file actions
556 lines (441 loc) · 13 KB
/
Lesson48.cs
File metadata and controls
556 lines (441 loc) · 13 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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
using System;
using System.Collections;
using System.Collections.Generic;
namespace Tired
{
public delegate void IUpdatableHandler(IUpdatable updatable);
public class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.Update();
}
}
public class Game
{
private readonly int _updatePerSecond = 25;
private UpdatablePool<IUpdatable> _objectPool = new UpdatablePool<IUpdatable>();
private int _blueSoildersCount = 0;
private int _redSoildersCount = 0;
public bool IsOver
{
get
{
return (_blueSoildersCount==0 || _redSoildersCount==0) ? true : false;
}
}
public Game()
{
CreateSoilders();
_objectPool.AddListenerOnDestroyEvent(ListenDestroyedObjects);
}
public void CreateSoilders()
{
int soilderCount = 10;
float offsetX = soilderCount / 2.0f;
float offsetY = 30.0f;
float distanceBeetwen = 4.0f;
for (int i = 0; i < soilderCount; i++)
{
Damager damager = new Damager();
if (i < offsetX)
{
damager.CurrentPosition = new Vector(offsetX+i * distanceBeetwen, 1+i);
damager.SetSkin(new RenderSkin('#', ConsoleColor.Red));
_redSoildersCount++;
}
else
{
damager.CurrentPosition = new Vector(i * distanceBeetwen + offsetX, offsetY - i);
damager.SetSkin(new RenderSkin('#', ConsoleColor.Blue));
_blueSoildersCount++;
}
_objectPool.Create(damager);
}
}
public void ListenDestroyedObjects(IUpdatable updatable)
{
Soilder soilder = updatable as Soilder;
if (soilder != null)
{
if(soilder.CurrentSkin.Color == ConsoleColor.Blue)
{
_blueSoildersCount--;
}
else if(soilder.CurrentSkin.Color == ConsoleColor.Red)
{
_redSoildersCount--;
}
}
}
public void DrawInformation()
{
Console.SetCursorPosition(0,0);
Console.WriteLine("Количество солдат синей армии:" + _blueSoildersCount);
Console.WriteLine("Количество солдат красной армии:" + _redSoildersCount);
}
public void Update()
{
while (_objectPool != null && IsOver == false)
{
_objectPool.Update();
DrawInformation();
System.Threading.Thread.Sleep(_updatePerSecond);
Console.Clear();
}
Console.WriteLine("Игра закончена!");
if (_blueSoildersCount == 0)
{
Console.WriteLine("Выиграли красные!");
}
else
{
Console.WriteLine("Выиграли синие!");
}
}
}
public class Bullet : IUpdatable
{
private EnumeratedPool<IUpdatable> _neighbors;
private RenderSkin _skin = new RenderSkin('o',ConsoleColor.DarkGreen);
private bool _isDestroyed = false;
private Vector _position;
public Soilder SoilderTarget { get; private set; } = null;
public Vector Target { get; private set; }
public int ShooterFractionColor { get; private set; } = (int)ConsoleColor.White;
public int Damage { get; private set; } = 0;
public int Speed { get; private set; } = 3;
public bool IsDestroyed => _isDestroyed;
public Bullet(Vector from,Vector to,int damage, ConsoleColor shooterFractionColor)
{
_position = from;
Target = to;
Damage = damage;
ShooterFractionColor = (int)shooterFractionColor;
}
public void Destroy() => _isDestroyed = true;
public void TakeEnumaratedPool<T>(EnumeratedPool<T> enumeratedPool) where T : IUpdatable
=> _neighbors = enumeratedPool as EnumeratedPool<IUpdatable>;
public void SetSoilderTarget(Soilder soilder) => SoilderTarget = soilder;
public void Update()
{
float minDistanceToCollision = 2.0f;
_position.MoveTo(Target, Speed);
if ( _position.GetDistanceTo(Target) < minDistanceToCollision)
{
Destroy();
if(SoilderTarget != null && _position.GetDistanceTo(SoilderTarget.CurrentPosition) < minDistanceToCollision)
{
SoilderTarget.TakeDamage(Damage);
}
}
}
public void Draw()
{
_skin.Draw(_position);
}
}
public class Damager : Soilder
{
private int _strength = 5;
private int _reloadTime = 0;
private int _maxReloadTime = 20;
private float _speed = 0.5f;
private float _currentTimeToAvoid = 0.0f;
private Vector _safePosition;
public Damager()
{
int minStrenght = 5;
int MaxStrenght = 15;
SetSkin(new RenderSkin('#',ConsoleColor.Red));
_strength = Helper.GetRandomValue(minStrenght,MaxStrenght);
_reloadTime = _maxReloadTime;
}
public void Shoot()
{
if(Target != null && _reloadTime <=0)
{
Bullet bullet = new Bullet(Position, Target.CurrentPosition, _strength, Skin.Color);
bullet.SetSoilderTarget(Target);
Neighbors.CreateInstance(bullet);
_reloadTime = _maxReloadTime;
}
}
public void Reload()
{
if (_reloadTime > 0)
{
_reloadTime--;
}
}
public void Avoid()
{
int maxBound = 100;
_currentTimeToAvoid--;
if (_currentTimeToAvoid <= 0.0f)
{
_safePosition.Set( new Vector(Helper.GetRandomValue(0, Console.WindowWidth), Helper.GetRandomValue(0, Console.WindowHeight) ) );
_currentTimeToAvoid = Helper.GetRandomValue(0, maxBound);
}
Position.MoveTo(_safePosition, _speed);
}
public override void Update()
{
SetTarget( SearchNearestSoilder(soilder => soilder.CurrentSkin.Color != Skin.Color) );
Reload();
Shoot();
Avoid();
}
public override void Draw()
{
Skin.Draw(CurrentPosition);
}
}
public abstract class Soilder : IUpdatable
{
protected EnumeratedPool<IUpdatable> Neighbors;
protected int Health = 100;
protected int MaxHealth = 100;
protected Soilder Target = null;
protected Vector Position;
protected RenderSkin Skin;
public int Fraction
{
get
{
return (int)Skin.Color;
}
}
public bool IsDead
{
get
{
return CurrentHealth <= 0;
}
}
public bool IsDestroyed => IsDead;
public int CurrentMaxHealth
{
get
{
return MaxHealth;
}
private set
{
MaxHealth = value;
}
}
public int CurrentHealth
{
get
{
return Health;
}
private set
{
int overHealth = (Health + value);
if (overHealth > CurrentMaxHealth)
{
value -= (overHealth - CurrentMaxHealth);
}
else if (value < 0)
{
value = 0;
}
Health = value;
}
}
public int Level { get; private set; } = 1;
public Vector CurrentPosition
{
get
{
return Position;
}
set
{
Position = value;
}
}
public Soilder CurrentTarget
{
get
{
return Target;
}
}
public RenderSkin CurrentSkin
{
get
{
return Skin;
}
}
public abstract void Update();
public abstract void Draw();
public void Destroy() => Kill();
public void TakeEnumaratedPool<T>(EnumeratedPool<T> enumeratedPool) where T : IUpdatable
=> Neighbors = enumeratedPool as EnumeratedPool<IUpdatable>;
public virtual void LevelUp(int levelCount)
{
int healthFactorPerLevel = 25;
int defaultHealth = 100;
SetMaxHealth( (Level - 1) * healthFactorPerLevel + defaultHealth);
SetHealth(CurrentMaxHealth);
}
public void SetSkin(RenderSkin skin) => Skin = skin;
public void SetTarget(Soilder soilder) => Target = soilder;
public void SetPosition(Vector position) => Position = position;
public void AddPosition(Vector position) => Position += position;
public bool IsValidTarget() => (Target != null && Target.IsDestroyed == false);
public void TakeDamage(int damageCount) => CurrentHealth -= damageCount;
public void Kill() => Health = 0;
public void RestoreHealth(int restoreCount) => CurrentHealth += restoreCount;
public int SetHealth(int health) => CurrentHealth = health;
public int SetMaxHealth(int maxHealth) => CurrentMaxHealth = maxHealth;
public Soilder SearchNearestSoilder(Predicate<Soilder> additinalPredicate)
{
float distance = 1000.0f;
Soilder foundSoilder = null;
foreach(IUpdatable iupdatable in Neighbors)
{
Soilder soilder = iupdatable as Soilder;
if(soilder == null)
{
continue;
}
if (soilder != this && Position.GetDistanceTo(soilder.CurrentPosition) < distance && additinalPredicate(soilder) == true)
{
distance = Position.GetDistanceTo(soilder.CurrentPosition);
foundSoilder = soilder;
}
}
return foundSoilder;
}
}
public class EnumeratedPool<T> : IEnumerable where T : IUpdatable
{
private readonly List<T> _updateablesList = null;
public EnumeratedPool(List<T> updatables) => _updateablesList = updatables;
public void CreateInstance(T instance) => _updateablesList.Add(instance);
public List<T> FindAllUpdatableObject(Predicate<T> predicate) => _updateablesList.FindAll(predicate);
public T FindUpdatableObject(Predicate<T> predicate) => _updateablesList.Find(predicate);
public T GetUpdatableObjectByIndex(int index)
{
if (_updateablesList == null || _updateablesList.Count == 0
|| (index<0 && index>=_updateablesList.Count) ) return default(T);
return _updateablesList[index];
}
public IEnumerator GetEnumerator()=> _updateablesList.GetEnumerator();
}
public class UpdatablePool<T> where T : IUpdatable
{
private readonly EnumeratedPool<T> _protectedPool = null;
private readonly List<T> _updatablesList = new List<T>();
private readonly List<int> _garbageIndexList = new List<int>();
private event IUpdatableHandler _onDestroyEvent;
public UpdatablePool()
{
_protectedPool = new EnumeratedPool<T>(_updatablesList);
}
public EnumeratedPool<T> GetEnumeratedPool() => _protectedPool;
private void CollectGarbage()
{
for(int i = _garbageIndexList.Count-1; i >=0; i--)
{
_updatablesList.RemoveAt(_garbageIndexList[i]);
}
_garbageIndexList.Clear();
}
public void Update()
{
for (int i = 0; i < _updatablesList.Count; i++)
{
T updatable = _updatablesList[i];
if(updatable.IsDestroyed == false)
{
updatable.TakeEnumaratedPool(_protectedPool);
updatable.Update();
updatable.Draw();
}
else
{
_onDestroyEvent?.Invoke(updatable);
_garbageIndexList.Add(i);
}
}
CollectGarbage();
}
public void Create(T entity) => _updatablesList.Add(entity);
public void AddListenerOnDestroyEvent(IUpdatableHandler updatableHandler) => _onDestroyEvent += updatableHandler;
public void RemoveListenerOnDestroyEvent(IUpdatableHandler updatableHandler) => _onDestroyEvent -= updatableHandler;
}
public struct Vector
{
public float X { get; private set; }
public float Y { get; private set; }
public Vector(float x, float y)
{
X = x;
Y = y;
}
public void Set(Vector vector)
{
X = vector.X;
Y = vector.Y;
}
public static Vector operator +(Vector a, Vector b) => new Vector { X = a.X + b.X, Y = a.Y + b.Y };
public static Vector operator -(Vector a, Vector b) => new Vector { X = a.X - b.X, Y = a.Y - b.Y };
public static Vector operator /(Vector a, Vector b) => new Vector { X = a.X / b.X, Y = a.Y / b.Y };
public static Vector operator /(Vector a, float b) => new Vector { X = a.X / b, Y = a.Y / b };
public static Vector operator *(Vector a, float b) => new Vector { X = a.X * b, Y = a.Y * b };
public float GetLengthSqrt() => MathF.Sqrt( (X * X + Y * Y) );
public float GetDistanceTo(Vector other) => (other - this).GetLengthSqrt();
public void MoveTo(Vector other, float speed)
{
Vector differenceVec = (other - this);
float sqrtLength = differenceVec.GetLengthSqrt();
if(sqrtLength <= 0.5f)
{
sqrtLength = 0.5f;
}
Vector normalizedVector = differenceVec / sqrtLength;
this += normalizedVector * speed;
}
}
public struct RenderSkin
{
public char Symbol { get; private set; }
public ConsoleColor Color { get; private set; }
public RenderSkin(char symbol, ConsoleColor color)
{
Color = color;
Symbol = symbol;
}
public void SetSymbol(char symbol) => Symbol = symbol;
public void SetColor(ConsoleColor color) => Color = color;
public void Draw(Vector position)
{
if(position.X>=0 && position.Y>= 0)
{
Console.ForegroundColor = Color;
Console.SetCursorPosition((int)position.X, (int)position.Y);
Console.Write(Symbol);
Console.ResetColor();
}
}
}
public interface IUpdatable
{
public bool IsDestroyed { get; }
public void Destroy();
public void Update();
public void Draw();
public void TakeEnumaratedPool<T>(EnumeratedPool<T> enumeratedPool) where T : IUpdatable;
}
public static class Helper
{
public static int GetRandomValue(int minValue = 0, int maxValue = 1) => new Random().Next(minValue,maxValue);
}
}