PIbd_23_Kislitsa_E.D_AirFig.../AirFighter/AirplaneGenericCollection.cs
2023-10-19 21:57:34 +03:00

104 lines
3.2 KiB
C#

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;
private readonly int _placeSizeWidth = 166;
private readonly int _placeSizeHeight = 160;
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);
}
public static int operator +(AirplaneslGenericCollection<T,U> collect, T? obj)
{
if (obj == null)
return -1;
return collect?._collection.Insert(obj) ?? -1;
}
public static bool operator -(AirplaneslGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
return collect._collection.Remove(pos);
return false;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
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)
{
int c = 0,r = _pictureHeight/_placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
DrawningAirplane airplane = _collection.Get(i);
if (airplane != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeWidth /10 * 2 , (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + (_placeSizeHeight - _placeSizeHeight * 170 / 1000) / 2);
airplane.DrawTransport(g);
c++;
if (c - 1 == inRow)
{
c = 0;
r--;
}
}
}
}
}
}