PIbd-21_MalafeevL.S._Cruise.../Cruiser/Generics/CruisersGenericCollection.cs

152 lines
5.2 KiB
C#
Raw Normal View History

2023-10-21 14:10:32 +04:00
using Cruiser.MovementStrategy;
2023-12-18 16:26:08 +04:00
using Cruiser.Generics;
2023-10-21 14:10:32 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Drawing;
namespace Cruiser.Generics
{
/// <summary>
/// Параметризованный класс для набора объектов DrawingCruiser
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class CarsGenericCollection<T, U>
where T : DrawingCruiser
where U : IMoveableObject
{
2023-12-03 15:02:10 +04:00
/// <summary>
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetCruisers => _collection.GetCruisers();
2023-10-21 14:10:32 +04:00
/// <summary>
2023-12-18 16:26:08 +04:00
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
2023-10-21 14:10:32 +04:00
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 160;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 60;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public CarsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
2023-11-24 11:41:50 +04:00
public static bool operator +(CarsGenericCollection<T, U> collect, T? obj)
2023-10-21 14:10:32 +04:00
{
if (obj == null)
{
2023-11-24 11:41:50 +04:00
return false;
2023-10-21 14:10:32 +04:00
}
2023-12-18 16:26:08 +04:00
return collect._collection.Insert(obj, new DrawiningCruiserEqutables());
2023-10-21 14:10:32 +04:00
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
2023-11-24 11:41:50 +04:00
public static T? operator -(CarsGenericCollection<T, U> collect, int pos)
2023-10-21 14:10:32 +04:00
{
2023-11-24 11:41:50 +04:00
T? obj = collect._collection[pos];
2023-10-21 14:10:32 +04:00
if (obj != null)
{
collect._collection.Remove(pos);
}
2023-11-24 11:41:50 +04:00
return obj;
2023-10-21 14:10:32 +04:00
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
2023-11-24 11:41:50 +04:00
return (U?)_collection[pos]?.GetMoveableObject;
2023-10-21 14:10:32 +04:00
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowCruiser()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawObjects(gr);
DrawBackground(gr);
return bmp;
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
{
g.DrawRectangle(pen, i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
}
}
}
/// <summary>
/// /// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
int Ix = 0;
int Iy = 0;
2023-11-24 11:41:50 +04:00
foreach (var cruiser in _collection.GetCruisers())
2023-10-21 14:10:32 +04:00
{
2023-12-18 15:13:53 +04:00
cruiser?.SetPosition(Ix, Iy); // починил, для починки удаления
cruiser?.DrawTransport(g);
2023-10-21 14:10:32 +04:00
Ix += _placeSizeWidth;
if (Ix + _placeSizeHeight > _pictureWidth)
{
Ix = 0;
2023-12-18 15:13:53 +04:00
Iy += _placeSizeHeight; // починил, т.к. отрисовывалось максимум 8 кораблей
2023-11-24 11:41:50 +04:00
}
2023-10-21 14:10:32 +04:00
}
}
}
}