138 lines
4.7 KiB
C#
138 lines
4.7 KiB
C#
using ProjectAirplaneWithRadar.Drawnings;
|
|
using ProjectAirplaneWithRadar.Exceptions;
|
|
|
|
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
|
{
|
|
/// <summary>
|
|
/// Абстракция компании, хранящий коллекцию автомобилей
|
|
/// </summary>
|
|
public abstract class AbstractCompany
|
|
{
|
|
/// <summary>
|
|
/// Размер места (ширина)
|
|
/// </summary>
|
|
protected readonly int _placeSizeWidth = 210;
|
|
|
|
/// <summary>
|
|
/// Размер места (высота)
|
|
/// </summary>
|
|
protected readonly int _placeSizeHeight = 110;
|
|
|
|
/// <summary>
|
|
/// Ширина окна
|
|
/// </summary>
|
|
protected readonly int _pictureWidth;
|
|
|
|
/// <summary>
|
|
/// Высота окна
|
|
/// </summary>
|
|
protected readonly int _pictureHeight;
|
|
|
|
/// <summary>
|
|
/// Коллекция автомобилей
|
|
/// </summary>
|
|
protected ICollectionGenericObjects<DrawningAirplane>? _collection = null;
|
|
|
|
/// <summary>
|
|
/// Вычисление максимального количества элементов, который можно разместить в окне
|
|
/// </summary>
|
|
private int GetMaxCount => _pictureWidth / _placeSizeWidth * (_pictureHeight / _placeSizeHeight);
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
/// <param name="picWidth">Ширина окна</param>
|
|
/// <param name="picHeight">Высота окна</param>
|
|
/// <param name="collection">Коллекция автомобилей</param>
|
|
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningAirplane> collection)
|
|
{
|
|
_pictureWidth = picWidth;
|
|
_pictureHeight = picHeight;
|
|
_collection = collection;
|
|
_collection.MaxCount = GetMaxCount;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Перегрузка оператора сложения для класса
|
|
/// </summary>
|
|
/// <param name="company">Компания</param>
|
|
/// <param name="airplane">Добавляемый объект</param>
|
|
/// <returns></returns>
|
|
public static int operator +(AbstractCompany company, DrawningAirplane airplane)
|
|
{
|
|
return company._collection.Insert(airplane, new DrawningAirplaneEqutables());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Перегрузка оператора удаления для класса
|
|
/// </summary>
|
|
/// <param name="company">Компания</param>
|
|
/// <param name="position">Номер удаляемого объекта</param>
|
|
/// <returns></returns>
|
|
public static DrawningAirplane operator -(AbstractCompany company, int position)
|
|
{
|
|
return company._collection.Remove(position);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение случайного объекта из коллекции
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public DrawningAirplane? GetRandomObject()
|
|
{
|
|
Random rnd = new();
|
|
try
|
|
{
|
|
return _collection?.Get(rnd.Next(GetMaxCount));
|
|
}
|
|
catch (ObjectNotFoundException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Вывод всей коллекции
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Bitmap? Show()
|
|
{
|
|
Bitmap bitmap = new(_pictureWidth, _pictureHeight);
|
|
Graphics graphics = Graphics.FromImage(bitmap);
|
|
DrawBackgound(graphics);
|
|
|
|
SetObjectsPosition();
|
|
for (int i = 0; i < (_collection?.Count ?? 0); ++i)
|
|
{
|
|
try
|
|
{
|
|
DrawningAirplane? obj = _collection?.Get(i);
|
|
obj?.DrawTransport(graphics);
|
|
}
|
|
catch (ObjectNotFoundException)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return bitmap;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Вывод заднего фона
|
|
/// </summary>
|
|
/// <param name="g"></param>
|
|
protected abstract void DrawBackgound(Graphics g);
|
|
|
|
/// <summary>
|
|
/// Расстановка объектов
|
|
/// </summary>
|
|
protected abstract void SetObjectsPosition();
|
|
|
|
/// <summary>
|
|
/// Сортировка
|
|
/// </summary>
|
|
/// <param name="comparer">Сравнитель объектов</param>
|
|
public void Sort(IComparer<DrawningAirplane?> comparer) => _collection?.CollectionSort(comparer);
|
|
}
|
|
} |