using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WinFormsApp1 { internal class MapWithSetTraktorGeneric where T : class, IDrawningObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 250; private readonly int _placeSizeHeight = 250; private readonly SetTraktorGeneric _setTraktors; private readonly U _map; public MapWithSetTraktorGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setTraktors = new SetTraktorGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; } public static int operator +(MapWithSetTraktorGeneric map, T traktor) { return map._setTraktors.Insert(traktor); } public static T operator -(MapWithSetTraktorGeneric map, int position) { return map._setTraktors.Remove(position); } public Bitmap ShowSet() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); DrawTraktors(gr); return bmp; } public Bitmap ShowOnMap() { Shaking(); foreach (var traktor in _setTraktors.GetTraktor()) { return _map.CreateMap(_pictureWidth, _pictureHeight, traktor); } return new(_pictureWidth, _pictureHeight); } public Bitmap MoveObject(Direction direction) { if (_map != null) { return _map.MoveObject(direction); } return new(_pictureWidth, _pictureHeight); } private void Shaking() { int j = _setTraktors.Count - 1; for (int i = 0; i < _setTraktors.Count; i++) { if (_setTraktors[i] == null) { for (; j > i; j--) { var traktor = _setTraktors[j]; if (traktor != null) { _setTraktors.Insert(traktor, i); _setTraktors.Remove(j); break; } } if (j <= i) { return; } } } } private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); Brush brush = new SolidBrush(Color.LightSlateGray); g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight); 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, j * _placeSizeHeight); g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10); } g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } } private void DrawTraktors(Graphics g) { int widthEl = _pictureWidth / _placeSizeWidth; int heightEl = _pictureHeight / _placeSizeHeight; int curWidth = 1; int curHeight = 0; foreach (var traktor in _setTraktors.GetTraktor()) { traktor?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 130, curHeight * _placeSizeHeight + 20, _pictureWidth, _pictureHeight); traktor?.DrawningObject(g); if (curWidth < widthEl) curWidth++; else { curWidth = 1; curHeight++; } if (curHeight > heightEl) { return; } } } public string GetData(char separatorType, char separatorData) { string data = $"{_map.GetType().Name}{separatorType}"; foreach (var traktor in _setTraktors.GetTraktor()) { data += $"{traktor.GetInfo()}{separatorData}"; } return data; } public void LoadData(string[] records) { foreach (var rec in records) { _setTraktors.Insert(DrawningObjectTractor.Create(rec) as T); } } } }