From d7c9234ae865ce577d6577c96533a807f4dc5c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 11 Oct 2023 10:50:39 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20generic?= =?UTF-8?q?=20+=20=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20TODO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hydroplane/DrawningPlane.cs | 7 + .../FormHydroplaneCollection.Designer.cs | 39 +++++ Hydroplane/FormHydroplaneCollection.cs | 20 +++ Hydroplane/FormHydroplaneCollection.resx | 120 +++++++++++++++ Hydroplane/PlanesGenericCollection.cs | 139 ++++++++++++++++++ Hydroplane/SetGeneric.cs | 106 +++++++++++++ 6 files changed, 431 insertions(+) create mode 100644 Hydroplane/FormHydroplaneCollection.Designer.cs create mode 100644 Hydroplane/FormHydroplaneCollection.cs create mode 100644 Hydroplane/FormHydroplaneCollection.resx create mode 100644 Hydroplane/PlanesGenericCollection.cs create mode 100644 Hydroplane/SetGeneric.cs diff --git a/Hydroplane/DrawningPlane.cs b/Hydroplane/DrawningPlane.cs index d224024..c30bdb6 100644 --- a/Hydroplane/DrawningPlane.cs +++ b/Hydroplane/DrawningPlane.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Hydroplane.Entities; +using Hydroplane.MovementStrategy; namespace Hydroplane.DrawningObjects { @@ -45,6 +46,12 @@ namespace Hydroplane.DrawningObjects } EntityPlane = new EntityPlane(speed, weight, bodyColor); } + + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectPlane(this); + public void SetPosition(int x, int y) { _startPosX = Math.Min(x, _pictureWidth - _planeWidth); diff --git a/Hydroplane/FormHydroplaneCollection.Designer.cs b/Hydroplane/FormHydroplaneCollection.Designer.cs new file mode 100644 index 0000000..40e7d62 --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.Designer.cs @@ -0,0 +1,39 @@ +namespace Hydroplane +{ + partial class FormHydroplaneCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "FormHydroplaneCollection"; + } + + #endregion + } +} \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.cs b/Hydroplane/FormHydroplaneCollection.cs new file mode 100644 index 0000000..b0acfee --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Hydroplane +{ + public partial class FormHydroplaneCollection : Form + { + public FormHydroplaneCollection() + { + InitializeComponent(); + } + } +} diff --git a/Hydroplane/FormHydroplaneCollection.resx b/Hydroplane/FormHydroplaneCollection.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Hydroplane/PlanesGenericCollection.cs b/Hydroplane/PlanesGenericCollection.cs new file mode 100644 index 0000000..1743715 --- /dev/null +++ b/Hydroplane/PlanesGenericCollection.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hydroplane.DrawningObjects; +using Hydroplane.MovementStrategy; + +namespace Hydroplane.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningPlane + /// + /// + /// + internal class PlanesGenericCollection + where T : DrawningPlane + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 175; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 80; + /// + /// Набор объектов + /// + 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 bool operator +(PlanesGenericCollection collect, T? + obj) + { + if (obj == null) + { + return false; + } + return collect?._collection.Insert(obj) ?? false; + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(PlanesGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return obj; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowCars() + { + 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++) + { + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + } + } + } +} diff --git a/Hydroplane/SetGeneric.cs b/Hydroplane/SetGeneric.cs new file mode 100644 index 0000000..63e312d --- /dev/null +++ b/Hydroplane/SetGeneric.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hydroplane.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый самолёт + /// + public bool Insert(T plane) + { + try + { + for (int i = _places.Length - 1; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = plane; + return true; + } + catch + { + return false; + } + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public bool Insert(T plane, int position) + { + if (position < 0 || position >= _places.Count() || plane == null) + { + return false; + } + if (_places[position] == null) + { + return false; + + } + int positionNull = Array.FindIndex(_places, position, x => x == null); + if (positionNull == -1) + return false; + for (int i = positionNull; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = plane; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= _places.Count()) + return false; + + _places[position] = null; + + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position >= _places.Count()) + return null; + return _places[position]; + } + } +}