From 4ff5df5cf594259e93f610128cdba356164dc444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Tue, 28 Nov 2023 00:45:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BD=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=BE=D1=82=20DrawningBoat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatsGenericCollection.cs | 148 ++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 Sailboat/Sailboat/BoatsGenericCollection.cs diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs new file mode 100644 index 0000000..2228a36 --- /dev/null +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -0,0 +1,148 @@ +using Sailboat.DrawingObjects; +using Sailboat.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.MovementStrategy; + +namespace Sailboat.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawingBoat + /// + /// + /// + internal class BoatsGenericCollection + where T : DrawingBoat + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 160; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 160; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public BoatsGenericCollection(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 +(BoatsGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(BoatsGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowBoats() + { + 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) + { + for (int i = 0; i < _collection.Count; i++) + { + DrawingBoat boat = _collection.Get(i); + + if (boat != null) + { + int width = _pictureWidth / _placeSizeWidth; + boat.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight); + boat.DrawTransport(g); + } + } + } + + } +}