From 618d9a5ae7a0173dd1117f0bcad52730b199755c Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 10 Dec 2023 13:55:42 +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=20PlanesGenericCollection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirBomber/PlanesGenericCollection.cs | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 AirBomber/AirBomber/PlanesGenericCollection.cs diff --git a/AirBomber/AirBomber/PlanesGenericCollection.cs b/AirBomber/AirBomber/PlanesGenericCollection.cs new file mode 100644 index 0000000..820ad0c --- /dev/null +++ b/AirBomber/AirBomber/PlanesGenericCollection.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class PlanesGenericCollection + where T : DrawningAirPlane + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 170; + private readonly int _placeSizeHeight = 120; + private readonly SetGeneric _collection; + public PlanesGenericCollection(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 +(PlanesGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj) ?? -1; + } + public static bool operator -(PlanesGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + return true; + } + return false; + } + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowPlanes() + { + 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 widthObjCount = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count; i++) + { + T? type = _collection.Get(i); + if (type != null) + { + int row = i / widthObjCount; + int col = widthObjCount - 1 - (i % widthObjCount); + + type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); + type?.DrawPlane(g); + } + } + } + } +}