PIbd-22_Isaeva_A.I._Airbus_.../Airbus/Generics/AirbusGenericCollection.cs
2023-12-10 12:27:15 +04:00

108 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirbus.Drawnings;
using ProjectAirbus.MovementStrategy;
namespace ProjectAirbus.Generics
{
internal class AirbusGenericCollection<T, U>
where T : DrawningAirbus
where U : IMoveableObject
{
public int count => _collection.Count;
private readonly int _pictureWidth;
private readonly int _pictureHeight;
// Размер занимаемого места 89х34
private readonly int _placeSizeWidth = 189;
private readonly int _placeSizeHeight = 134;
// коллекция
private readonly SetGeneric<T> _collection;
public AirbusGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(AirbusGenericCollection<T, U> collect, T? obj)
{
if (obj != null)
{
return collect._collection.Insert(obj);
}
return -1;
}
public static bool operator -(AirbusGenericCollection<T, U> collect, int pos)
{
if (collect._collection.GetAirbus(pos) == null)
{
return false;
}
return collect?._collection.Remove(pos) ?? false;
}
// получение объектов коллекции
public IEnumerable<T?> GetAirbus => _collection.GetAirbus();
// получение объекта IMoveableObjecr
public U? GetU(int pos)
{
return (U?)_collection[pos]?.GetMoveableObject;
}
// вывод всего набора
public Bitmap ShowAirbus()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
// прорисовка фона
private void DrawBackground(Graphics gr)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
// линия разметки
gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
}
// прорисовка объекта
private void DrawObjects(Graphics g)
{
// координаты
int x = _pictureWidth / _placeSizeWidth - 1;
int y = _pictureHeight / _placeSizeHeight - 1;
foreach (var airbus in _collection.GetAirbus())
{
if (airbus != null)
{
if (x < 0)
{
x = _pictureWidth / _placeSizeWidth - 1;
--y;
}
airbus.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
airbus.DrawTransport(g);
--x;
}
}
}
}
}