using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AirBomber.DrawningObjects; using AirBomber.MovementStrategy; using ProjectBomber.Generics; namespace AirBomber.Generics { internal class BomberGenericCollection where T : DrawningBomber where U : IMoveableObject { private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 155; private readonly int _placeSizeHeight = 185; private readonly SetGeneric _collection; public BomberGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } public static int operator +(BomberGenericCollection collect, T? obj) { if (obj == null) { return -1; } return collect._collection.Insert(obj); } public static bool operator -(BomberGenericCollection collect, int pos) { T? obj = collect._collection[pos]; if (obj == null) { return false; } return collect._collection.Remove(pos); } public U? GetU(int pos) { return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowBomber() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); DrawObjects(gr); return bmp; } private void DrawBackground(Graphics g) { Pen pen = new Pen(Color.Black, 3); int numColumns = _pictureWidth / _placeSizeWidth; int numRows = _pictureHeight / _placeSizeHeight; for (int i = 0; i <= numColumns; i++) { for (int j = 0; j <= numRows; ++j) { int x = i * _placeSizeWidth; int y = j * _placeSizeHeight; g.DrawLine(pen, x, y, x + _placeSizeWidth / 2, y); } g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, numRows * _placeSizeHeight); } } private void DrawObjects(Graphics g) { foreach (var air in _collection.GetPlane()) { if (air != null) { // Вычисляем позицию объекта int row = _collection.GetPlane().ToList().IndexOf(air) / (_pictureWidth / _placeSizeWidth); int column = (_pictureWidth / _placeSizeWidth) - 1 - (_collection.GetPlane().ToList().IndexOf(air) % (_pictureWidth / _placeSizeWidth)); int x = column * _placeSizeWidth; int y = row * _placeSizeHeight; air.SetPosition(x, y); air.DrawBomber(g); } } } } }