-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
141 lines (119 loc) · 4.33 KB
/
Program.cs
File metadata and controls
141 lines (119 loc) · 4.33 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
using System;
using System.Collections.Generic;
namespace Generics
{
internal class Program
{
static void Main(string[] args)
{
const int MIN_DAMAGE = 5;
const int MAX_DAMAGE = 15;
const int MAX_X = 80;
const int MAX_Y = 20;
ObjectPool<Bullet> bulletPool = new ObjectPool<Bullet>(
createFunc: () => new Bullet(),
resetAction: (bullet) => bullet.Reset(),
initialPoolSize: 5
);
Console.WriteLine("Пул создан. Начинаем тест:\n");
Console.WriteLine("Получаем 3 пули из пула:");
Bullet bullet1 = bulletPool.Get();
Bullet bullet2 = bulletPool.Get();
Bullet bullet3 = bulletPool.Get();
Random rand = new Random();
bullet1.Activate(rand.Next(MIN_DAMAGE, MAX_DAMAGE + 1), rand.Next(0, MAX_X + 1), rand.Next(0, MAX_Y + 1));
bullet2.Activate(rand.Next(MIN_DAMAGE, MAX_DAMAGE + 1), rand.Next(0, MAX_X + 1), rand.Next(0, MAX_Y + 1));
bullet3.Activate(rand.Next(MIN_DAMAGE, MAX_DAMAGE + 1), rand.Next(0, MAX_X + 1), rand.Next(0, MAX_Y + 1));
bullet1.Show();
bullet2.Show();
bullet3.Show();
Console.WriteLine("\nВозвращаем 2 пули в пул:");
bulletPool.Return(bullet1);
bulletPool.Return(bullet2);
Console.WriteLine("\nПолучаем еще 6 пуль (4 из пула, 2 новых):");
Bullet bullet4 = bulletPool.Get();
Bullet bullet5 = bulletPool.Get();
Bullet bullet6 = bulletPool.Get();
Bullet bullet7 = bulletPool.Get();
Bullet bullet8 = bulletPool.Get();
Bullet bullet9 = bulletPool.Get();
bullet4.Show();
bullet5.Show();
bullet6.Show();
bullet7.Show();
bullet8.Show();
bullet9.Show();
Console.WriteLine("\nВозвращаем 2 пули в пул:");
bulletPool.Return(bullet4);
bulletPool.Return(bullet5);
bulletPool.ShowPoolInfo();
}
}
public class ObjectPool<T>
{
private Queue<T> _objectPool = new Queue<T>();
public int TotalCreated { get; private set; }
private Func<T> _createFunc { get; }
private Action<T> _resetAction { get; }
public ObjectPool(Func<T> createFunc, Action<T> resetAction, int initialPoolSize)
{
_createFunc = createFunc;
_resetAction = resetAction;
for (int i = 0; i < initialPoolSize; i++)
{
T newItem = createFunc();
_resetAction(newItem);
_objectPool.Enqueue(newItem);
TotalCreated++;
}
}
public T Get()
{
if (_objectPool.Count > 0)
{
T item = _objectPool.Dequeue();
_resetAction(item);
return item;
}
else
{
T newItem = _createFunc();
_resetAction(newItem);
TotalCreated++;
return newItem;
}
}
public void Return(T item)
{
_resetAction(item);
_objectPool.Enqueue(item);
}
public void ShowPoolInfo()
{
Console.WriteLine($"\nОбъектов в пуле: {_objectPool.Count}.\nВсего новых объектов создано: {TotalCreated}");
}
}
public class Bullet
{
public int Damage;
public int X, Y;
public bool IsActive;
public void Reset()
{
X = 0; Y = 0;
Damage = 1;
IsActive = false;
}
public void Activate(int damage, int x, int y)
{
Damage = damage;
X = x;
Y = y;
IsActive = true;
}
public void Show()
{
Console.WriteLine($"Пуля: урон {Damage}, позиция ({X},{Y}), активна: {IsActive.ToString()}");
}
}
}