From e7bc34adb63ce7865fe2332cf2ec04ce5d21a14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=AF=D1=88=D0=B8=D0=BD?= Date: Thu, 12 Oct 2023 20:34:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=87=D1=82=D0=B8=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawingPlane.cs | 5 +- AirBomber/AirBomber/FormAirBomber.Designer.cs | 13 ++ AirBomber/AirBomber/FormAirBomber.cs | 77 +++++++--- .../AirBomber/FormPlaneCollection.Designer.cs | 125 ++++++++++++++++ AirBomber/AirBomber/FormPlaneCollection.cs | 87 +++++++++++ AirBomber/AirBomber/FormPlaneCollection.resx | 120 +++++++++++++++ .../AirBomber/PlanesGenericCollection.cs | 140 ++++++++++++++++++ AirBomber/AirBomber/Program.cs | 2 +- AirBomber/AirBomber/SetGeneric.cs | 60 ++++++++ 9 files changed, 610 insertions(+), 19 deletions(-) create mode 100644 AirBomber/AirBomber/FormPlaneCollection.Designer.cs create mode 100644 AirBomber/AirBomber/FormPlaneCollection.cs create mode 100644 AirBomber/AirBomber/FormPlaneCollection.resx create mode 100644 AirBomber/AirBomber/PlanesGenericCollection.cs create mode 100644 AirBomber/AirBomber/SetGeneric.cs diff --git a/AirBomber/AirBomber/DrawingPlane.cs b/AirBomber/AirBomber/DrawingPlane.cs index 55e7219..e799eb2 100644 --- a/AirBomber/AirBomber/DrawingPlane.cs +++ b/AirBomber/AirBomber/DrawingPlane.cs @@ -1,5 +1,6 @@ using ProjectAirBomber; using ProjectAirBomber.Entities; +using ProjectAirBomber.MovementStrategy; using System; using System.Collections.Generic; using System.Linq; @@ -52,6 +53,8 @@ namespace ProjectAirBomber.DrawingObjects EntityPlane = new EntityPlane(speed, weight, bodyColor); } + public IMoveableObject GetMoveableObject => new DrawingObjectPlane(this); + public void SetPosition(int x, int y) { if (x < 0 || y < 0 || x + _planeWidth >= _pictureWidth || y + _planeHeight >= _pictureHeight) @@ -87,7 +90,7 @@ namespace ProjectAirBomber.DrawingObjects /// /// Изменение направления перемещения /// - /// Направление + /// Направление new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new + _strategy.SetData(new DrawingObjectPlane(_drawingPlane), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); comboBoxStrategy.Enabled = false; } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } } + + private void buttonSelectPlane_Click(object sender, EventArgs e) + { + SelectedPlane = _drawingPlane; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormPlaneCollection.Designer.cs b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs new file mode 100644 index 0000000..d7b1cfa --- /dev/null +++ b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs @@ -0,0 +1,125 @@ +namespace ProjectAirBomber +{ + partial class FormPlaneCollection + { + /// + /// 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() + { + pictureBoxCollection = new PictureBox(); + groupBox1 = new GroupBox(); + ButtonRefreshCollection = new Button(); + ButtonRemovePlane = new Button(); + maskedTextBoxNumber = new MaskedTextBox(); + ButtonAddPlane = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBox1.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Left; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(669, 667); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; + // + // groupBox1 + // + groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right; + groupBox1.Controls.Add(ButtonRefreshCollection); + groupBox1.Controls.Add(ButtonRemovePlane); + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(ButtonAddPlane); + groupBox1.Location = new Point(669, 9); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(213, 477); + groupBox1.TabIndex = 1; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new Point(6, 269); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(190, 50); + ButtonRefreshCollection.TabIndex = 3; + ButtonRefreshCollection.Text = "Обновить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // ButtonRemovePlane + // + ButtonRemovePlane.Location = new Point(6, 213); + ButtonRemovePlane.Name = "ButtonRemovePlane"; + ButtonRemovePlane.Size = new Size(190, 50); + ButtonRemovePlane.TabIndex = 2; + ButtonRemovePlane.Text = "Удалить самолет"; + ButtonRemovePlane.UseVisualStyleBackColor = true; + ButtonRemovePlane.Click += ButtonRemovePlane_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(23, 123); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(162, 27); + maskedTextBoxNumber.TabIndex = 1; + // + // ButtonAddPlane + // + ButtonAddPlane.Location = new Point(6, 26); + ButtonAddPlane.Name = "ButtonAddPlane"; + ButtonAddPlane.Size = new Size(190, 50); + ButtonAddPlane.TabIndex = 0; + ButtonAddPlane.Text = "Добавить самолет"; + ButtonAddPlane.UseVisualStyleBackColor = true; + ButtonAddPlane.Click += ButtonAddPlane_Click; + // + // FormPlaneCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(882, 667); + Controls.Add(groupBox1); + Controls.Add(pictureBoxCollection); + Name = "FormPlaneCollection"; + Text = "Набор автомобилей"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private GroupBox groupBox1; + private Button ButtonRefreshCollection; + private Button ButtonRemovePlane; + private MaskedTextBox maskedTextBoxNumber; + private Button ButtonAddPlane; + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs new file mode 100644 index 0000000..4404a7a --- /dev/null +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -0,0 +1,87 @@ +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; +using ProjectAirBomber.DrawingObjects; +using ProjectAirBomber.Generics; +using ProjectAirBomber.MovementStrategy; + +namespace ProjectAirBomber +{ + public partial class FormPlaneCollection : Form + { + /// + /// Набор объектов + /// + private readonly PlanesGenericCollection _planes; + /// + /// Конструктор + /// + public FormPlaneCollection() + { + InitializeComponent(); + _planes = new PlanesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddPlane_Click(object sender, EventArgs e) + { + FormAirBomber form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_planes + form.SelectedPlane != null) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemovePlane_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_planes - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + + } +} diff --git a/AirBomber/AirBomber/FormPlaneCollection.resx b/AirBomber/AirBomber/FormPlaneCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AirBomber/AirBomber/FormPlaneCollection.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/AirBomber/AirBomber/PlanesGenericCollection.cs b/AirBomber/AirBomber/PlanesGenericCollection.cs new file mode 100644 index 0000000..30db7e6 --- /dev/null +++ b/AirBomber/AirBomber/PlanesGenericCollection.cs @@ -0,0 +1,140 @@ +using ProjectAirBomber.Generics; +using ProjectAirBomber.DrawingObjects; +using ProjectAirBomber.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirBomber.Generics +{ + internal class PlanesGenericCollection + where T : DrawingPlane + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 200; + /// + /// Набор объектов + /// + 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 T? operator +(PlanesGenericCollection collect, T? obj) + { + if (obj == null) + { + return null; + } + return collect?._collection.Insert(obj) ?? null; + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + 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 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 + 10, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 10, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth + 10, 0, i * + _placeSizeWidth + 10, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + for (int i = 0; i < _collection.Count; i++) + { + + DrawingPlane? plane = _collection.Get(i); + if (plane != null) + { + int inRow = _pictureWidth / _placeSizeWidth; + plane.SetPosition(i % inRow * _placeSizeWidth + 10, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 40); + plane.DrawTransport(g); + } + } + } + + } +} diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index 585a160..94b7c28 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -7,7 +7,7 @@ namespace ProjectAirBomber static void Main() { ApplicationConfiguration.Initialize(); - Application.Run(new FormAirBomber()); + Application.Run(new FormPlaneCollection()); } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs new file mode 100644 index 0000000..a57f0ab --- /dev/null +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirBomber.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 T? Insert(T plane) + { + if (_places[Count - 1] != null) + return null; + return Insert(plane, 0); ; + } + + public T? Insert(T plane, int position) + { + if (!(position >= 0 && position < Count)) + return null; + if (_places[position] != null) + { + int ind = position; + while (ind < Count && _places[ind] != null) + ind++; + if (ind == Count) + return null; + for (int i = ind - 1; i >= position; i--) + _places[i + 1] = _places[i]; + } + _places[position] = plane; + return plane; + } + public bool Remove(int position) + { + if (!(position >= 0 && position < Count) || _places[position] == null) + return false; + _places[position] = null; + return true; + } + + public T? Get(int position) + { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + } +} \ No newline at end of file