generic classes

This commit is contained in:
Denis 2022-10-07 19:56:39 +04:00
parent a9903ae47b
commit f8b95a402a
2 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,142 @@
namespace AirplaneWithRadar
{
// Карта с набром объектов под нее
internal class MapWithSetAirplaneGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
// Ширина окна отрисовки
private readonly int _pictureWidth;
// Высота окна отрисовки
private readonly int _pictureHeight;
// Размер занимаемого объектом места (ширина)
private readonly int _placeSizeWidth = 180;
// Размер занимаемого объектом места (высота)
private readonly int _placeSizeHeight = 90;
// Набор объектов
private readonly SetAirplaneGeneric<T> _setAirplane;
// Карта
private readonly U _map;
// Конструктор
public MapWithSetAirplaneGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setAirplane = new SetAirplaneGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
// Перегрузка оператора сложения
public static int operator +(MapWithSetAirplaneGeneric<T, U> map, T tractor)
{
return map._setAirplane.Insert(tractor);
}
// Перегрузка оператора вычитания
public static T operator -(MapWithSetAirplaneGeneric<T, U> map, int position)
{
return map._setAirplane.Remove(position);
}
// Вывод всего набора объектов
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawAirplane(gr);
return bmp;
}
// Просмотр объекта на карте
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setAirplane.Count; i++)
{
var airplane = _setAirplane.Get(i);
if (airplane != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, airplane);
}
}
return new(_pictureWidth, _pictureHeight);
}
// Перемещение объекта по карте
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
// "Взбалтываем" набор, чтобы все элементы оказались в начале
private void Shaking()
{
int j = _setAirplane.Count - 1;
for (int i = 0; i < _setAirplane.Count; i++)
{
if (_setAirplane.Get(i) == null)
{
for (; j > i; j--)
{
var airplane = _setAirplane.Get(j);
if (airplane != null)
{
_setAirplane.Insert(airplane, i);
_setAirplane.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
// Метод отрисовки фона
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
Brush brush = new SolidBrush(Color.LightSlateGray);
g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight);
for (int i = 0; i <= _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j <= _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия разметки места
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight);
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
// Метод прорисовки объектов
private void DrawAirplane(Graphics g)
{
int widthEl = _pictureWidth / _placeSizeWidth;
int heightEl = _pictureHeight / _placeSizeHeight;
int curWidth = 0;
int curHeight = 0;
for (int i = _setAirplane.Count; i >= 0; i--)
{
_setAirplane.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 10,
curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setAirplane.Get(i)?.DrawningObject(g);
if (curWidth < widthEl)
curWidth++;
else
{
curWidth = 1;
curHeight++;
}
if (curHeight > heightEl)
{
return;
}
}
}
}
}

View File

@ -0,0 +1,75 @@
namespace AirplaneWithRadar
{
// Параметризованный набор объектов
internal class SetAirplaneGeneric<T>
where T : class
{
// Массив объектов, которые храним
private readonly T[] _places;
// Количество объектов в массиве
public int Count => _places.Length;
// Конструктор
public SetAirplaneGeneric(int count)
{
_places = new T[count];
}
// Добавление объекта в набор
public int Insert(T airplane)
{
// вставка в начало набора
return Insert(airplane, 0);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T airplane, int position)
{
// проверка позиции
if (position >= _places.Length || position < 0)
return -1;
//проверка, что элемент массива по этой позиции пустой, если нет, то
if (_places[position] == null)
{
_places[position] = airplane;
return position;
}
//проверка, что после вставляемого элемента в массиве есть пустой элемент
int findEmptyPos = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
findEmptyPos = i;
break;
}
}
if (findEmptyPos < 0) return -1;
//сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
for (int i = findEmptyPos; i > position; i--)
{
_places[i] = _places[i - 1];
}
// вставка по позиции
_places[position] = airplane;
return position;
}
// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
// проверка позиции
if (position >= _places.Length || position < 0) return null;
// удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position];
_places[position] = null;
return temp;
}
// Получение объекта из набора по позиции
public T Get(int position)
{
// проверка позиции
if (position >= _places.Length || position < 0)
return null;
return _places[position];
}
}
}