using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sailboat { class MapWithSetBoatsGeneric where T : class, IDrawingObject where U : AbstractMap { private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 210; private readonly int _placeSizeHeight = 90; private readonly SetBoatsGeneric _setBoats; private readonly U _map; public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setBoats = new SetBoatsGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; } public static int operator +(MapWithSetBoatsGeneric map, T boat) { return map._setBoats.Insert(boat); } public static T operator -(MapWithSetBoatsGeneric map, int position) { return map._setBoats.Remove(position); } public Bitmap ShowSet() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); DrawBoats(gr); return bmp; } public Bitmap ShowOnMap() { Shaking(); foreach (var bus in _setBoats.GetBoats()) { return _map.CreateMap(_pictureWidth, _pictureHeight, bus); } 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 = _setBoats.Count - 1; for (int i = 0; i < _setBoats.Count; i++) { if (_setBoats[i] == null) { for (; j > i; j--) { var boat = _setBoats[j]; if (boat != null) { _setBoats.Insert(boat, i); _setBoats.Remove(j); break; } } if (j <= i) { return; } } } } private void DrawBackground(Graphics g) { Pen pen = new(Color.Brown, 3); g.FillRectangle(Brushes.Aqua, 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 / 2, j * _placeSizeHeight); } g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } } private void DrawBoats(Graphics g) { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; int currentWidth = 0; int currentHeight = 0; foreach (var boat in _setBoats.GetBoats()) { boat?.SetObject(currentWidth * _placeSizeWidth, currentHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); boat?.DrawingObject(g); if (currentWidth < width - 1) { currentWidth++; } else { currentHeight++; currentWidth = 0; } if (currentHeight > height) return; } } } }