PIbd_23_Kislitsa_E.D_AirFig.../AirFighter/AirplaneGenericCollection.cs

99 lines
3.0 KiB
C#
Raw Normal View History

2023-10-15 17:11:27 +04:00
using ProjectAirFighter.MovementStrategy;
using ProjectAirFighter.DrawningObjects;
using System.Drawing;
namespace ProjectAirFighter.Generics
{
internal class AirplaneslGenericCollection<T,U>
where T :DrawningAirplane
where U: IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
2023-10-15 21:41:44 +04:00
private readonly int _placeSizeWidth = 166;
2023-10-15 17:11:27 +04:00
2023-10-15 21:41:44 +04:00
private readonly int _placeSizeHeight = 160;
2023-10-15 17:11:27 +04:00
private readonly SetGeneric<T> _collection;
public AirplaneslGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
2023-10-27 00:17:04 +04:00
2023-10-15 17:11:27 +04:00
}
2023-10-27 00:17:04 +04:00
public static bool operator +(AirplaneslGenericCollection<T,U> collect, T? obj)
2023-10-15 17:11:27 +04:00
{
if (obj == null)
2023-10-27 00:17:04 +04:00
return false;
return collect?._collection.Insert(obj) ?? false;
2023-10-15 17:11:27 +04:00
}
2023-10-27 00:17:04 +04:00
public static T? operator -(AirplaneslGenericCollection<T, U> collect, int
2023-10-15 17:11:27 +04:00
pos)
{
2023-10-27 00:17:04 +04:00
T? obj = collect._collection[pos];
2023-10-15 17:11:27 +04:00
if (obj != null)
2023-10-27 00:17:04 +04:00
collect._collection.Remove(pos);
return obj;
2023-10-15 17:11:27 +04:00
}
public U? GetU(int pos)
{
2023-10-27 00:17:04 +04:00
return (U?)_collection[pos]?.GetMoveableObject;
2023-10-15 17:11:27 +04:00
}
public Bitmap ShowAirplanes()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
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 +
1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
2023-10-27 00:17:04 +04:00
int i = 0;
foreach (var airplane in _collection.GetAirplanes())
2023-10-15 17:11:27 +04:00
{
2023-10-27 00:17:04 +04:00
2023-10-15 17:11:27 +04:00
if (airplane != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
2023-11-19 00:11:44 +04:00
airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeWidth / 24, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight + (_placeSizeHeight - _placeSizeHeight * 170 / 1000) / 2);
2023-10-15 17:11:27 +04:00
airplane.DrawTransport(g);
2023-10-15 21:41:44 +04:00
2023-10-27 00:17:04 +04:00
2023-10-15 17:11:27 +04:00
}
2023-10-27 00:17:04 +04:00
i++;
2023-10-15 17:11:27 +04:00
}
}
}
}