PIbd-22_Shabunov_O.A._AirBo.../AirBomber/Generics/EntitiesGenericCollection.cs

105 lines
3.0 KiB
C#
Raw Normal View History

2023-11-19 17:11:10 +04:00
using AirBomber.MovementStrategy;
using AirBomber.Rendering;
namespace AirBomber.Generics
{
internal class EntitiesGenericCollection<T, U>
where T : BomberRendererBase
where U : IMovableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 200;
private readonly SetGeneric<T> _collection;
public IEnumerable<T?> Entities => _collection.GetEntities();
2023-11-19 17:11:10 +04:00
public EntitiesGenericCollection(int PictureWidth, int PictureHeight)
{
int width = PictureWidth / _placeSizeWidth;
int height = PictureHeight / _placeSizeHeight;
_pictureWidth = PictureWidth;
_pictureHeight = PictureHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(EntitiesGenericCollection<T, U> collect, T? obj)
{
if (obj is null || collect is null)
return -1;
return collect._collection.Insert(obj);
}
public static bool operator -(EntitiesGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection[pos];
2023-11-19 17:11:10 +04:00
if (obj != null)
return collect._collection.Remove(pos);
return false;
}
public U? GetU(int pos)
{
return (U?)_collection[pos]?.MovableObject;
2023-11-19 17:11:10 +04:00
}
public Bitmap ShowEntities()
{
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 i = -1;
foreach (T? Entity in _collection.GetEntities())
2023-11-19 17:11:10 +04:00
{
i++;
2023-11-19 17:11:10 +04:00
if (Entity is null)
continue;
/** Установка позиции */
int NumX = _pictureWidth / _placeSizeWidth;
int PosX = (NumX - 1 - i % NumX) * _placeSizeWidth;
int PosY = (i / NumX) * _placeSizeHeight;
Entity.SetPosition(PosX, PosY);
Entity.DrawEntity(g);
}
}
}
}