From f8fac0b87536248a8417f3afdbf393d2d455ce99 Mon Sep 17 00:00:00 2001 From: SAliulov <146759803+SAliulov@users.noreply.github.com> Date: Wed, 17 Apr 2024 23:45:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BF=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 116 ++++++++++ .../BomberHungarService.cs | 55 +++++ .../ICollectionGenericObjects.cs | 50 +++++ .../MassiveGenericObjects.cs | 133 ++++++++++++ .../FormAirBomber.Designer.cs | 36 +--- .../ProjectAirBomber/FormAirBomber.cs | 100 ++++----- .../FormBomberCollection.Designer.cs | 173 +++++++++++++++ .../ProjectAirBomber/FormBomberCollection.cs | 199 ++++++++++++++++++ .../FormBomberCollection.resx | 120 +++++++++++ .../MovementStrategy/MoveableTrans.cs | 4 +- ProjectAirBomber/ProjectAirBomber/Program.cs | 2 +- 11 files changed, 895 insertions(+), 93 deletions(-) create mode 100644 ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/AbstractCompany.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/BomberHungarService.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/MassiveGenericObjects.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/FormBomberCollection.Designer.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/FormBomberCollection.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/FormBomberCollection.resx diff --git a/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/AbstractCompany.cs b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..1b07dec --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,116 @@ +using ProjectAirBomber.Drawnings; + +namespace ProjectAirBomber.CollectionGenericObjects; + +/// +/// Абстракция компании, хранящий коллекцию самолетов +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 210; + + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 170; + + /// + /// Ширина окна + /// + protected readonly int _pictureWidth; + + /// + /// Высота окна + /// + protected readonly int _pictureHeight; + + /// + /// Коллекция поездов + /// + protected ICollectionGenericObjects? _collection = null; + + /// + /// Вычисление максимального количества элементов, который можно разместить в окне + /// + private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + + /// + /// Конструктор + /// + /// Ширина окна + /// Высота окна + /// Коллекция поездов + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static int operator +(AbstractCompany company, DrawningBomber bomber) + { + return company._collection.Insert(bomber); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningBomber? operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningBomber? GetRandomObject() + { + Random rnd = new(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + + /// + /// Вывод всей коллекции + /// + /// + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackgound(graphics); + + SetObjectsPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningBomber? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/BomberHungarService.cs b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/BomberHungarService.cs new file mode 100644 index 0000000..c813d04 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/BomberHungarService.cs @@ -0,0 +1,55 @@ +using ProjectAirBomber.Drawnings; +using ProjectAirBomber.Entities; +using System; + +namespace ProjectAirBomber.CollectionGenericObjects; + +/// +/// Реализация абстрактной компании - Ангар самолёта +/// +public class BomberHungarService : AbstractCompany +{ + /// + /// Конструктор + /// + /// + /// + /// + public BomberHungarService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + Pen pen = new(Color.Black); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for(int j = 0; j < _pictureHeight / _placeSizeHeight; j++) + { + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * j), new((int)(_placeSizeWidth * (i + 0.5f)), _placeSizeHeight * j)); + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * j), new(_placeSizeWidth * i, _placeSizeHeight * (j + 1))); + } + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * (_pictureHeight / _placeSizeHeight)), new((int)(_placeSizeWidth * (i + 0.5f)), _placeSizeHeight * (_pictureHeight / _placeSizeHeight))); + } + + + } + + protected override void SetObjectsPosition() + { + int n = 0; + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++) + { + DrawningBomber? drawingTrans = _collection?.Get(n); + n++; + if (drawingTrans != null) + { + drawingTrans.SetPictureSize(_pictureWidth, _pictureHeight); + drawingTrans.SetPosition(i * _placeSizeWidth + 5, j * _placeSizeHeight + 5); + } + } + } + } +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..e1c31a3 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,50 @@ +using ProjectAirBomber.Drawnings; + +namespace ProjectAirBomber.CollectionGenericObjects; + +/// +/// Интерфейс описания действий для набора хранимых объектов +/// +/// Параметр: ограничение - ссылочный тип +public interface ICollectionGenericObjects + where T : class +{ + /// + /// Количество объектов в коллекции + /// + int Count { get; } + + /// + /// Установка максимального количества элементов + /// + int SetMaxCount { set; } + + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj); + + /// + /// Добавление объекта в коллекцию на конкретную позицию + /// + /// Добавляемый объект + /// Позиция + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj, int position); + + /// + /// Удаление объекта из коллекции с конкретной позиции + /// + /// Позиция + /// true - удаление прошло удачно, false - удаление не удалось + T? Remove(int position); + + /// + /// Получение объекта по позиции + /// + /// Позиция + /// Объект + T? Get(int position); +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..52078d6 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,133 @@ +using System.Runtime.Remoting; +using ProjectAirBomber.Drawnings; + +namespace ProjectAirBomber.CollectionGenericObjects; + +/// +/// Параметризованный набор объектов +/// +/// Параметр: ограничение - ссылочный тип +public class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + /// + /// Массив объектов, которые храним + /// + private T?[] _collection; + + public int Count => _collection.Length; + + public int SetMaxCount + { + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } + + /// + /// Конструктор + /// + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) + { + if (position >= 0 && position < Count) + { + return _collection[position]; + } + + return null; + } + + public int Insert(T obj) + { + // вставка в свободное место набора + for (int i = 0; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + + return -1; + } + + public int Insert(T obj, int position) + { + // проверка позиции + if (position < 0 || position >= Count) + { + return -1; + } + + // проверка, что элемент массива по этой позиции пустой, если нет, то + // ищется свободное место после этой позиции и идет вставка туда + // если нет после, ищем до + if (_collection[position] != null) + { + bool pushed = false; + for (int index = position + 1; index < Count; index++) + { + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } + } + + if (!pushed) + { + for (int index = position - 1; index >= 0; index--) + { + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } + } + } + + if (!pushed) + { + return position; + } + } + + // вставка + _collection[position] = obj; + return position; + } + + public T? Remove(int position) + { + // проверка позиции + if (position < 0 || position >= Count) + { + return null; + } + + if (_collection[position] == null) return null; + + T? temp = _collection[position]; + _collection[position] = null; + return temp; + } +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs index 815856a..d2e5bd5 100644 --- a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs +++ b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs @@ -33,37 +33,24 @@ namespace ProjectAirBomber private void InitializeComponent() { pictureBoxAirBomber = new PictureBox(); - buttonCreateAirBomber = new Button(); buttonLeft = new Button(); buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); - buttonCreateCar = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); SuspendLayout(); // - // pictureBoxElectroTrans + // pictureBoxAirBomber // pictureBoxAirBomber.Dock = DockStyle.Fill; pictureBoxAirBomber.Location = new Point(0, 0); - pictureBoxAirBomber.Name = "pictureBoxElectroTrans"; + pictureBoxAirBomber.Name = "pictureBoxAirBomber"; pictureBoxAirBomber.Size = new Size(923, 597); pictureBoxAirBomber.TabIndex = 0; pictureBoxAirBomber.TabStop = false; // - // buttonCreateElectroTrans - // - buttonCreateAirBomber.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateAirBomber.Location = new Point(12, 562); - buttonCreateAirBomber.Name = "buttonCreateAirBomber"; - buttonCreateAirBomber.Size = new Size(223, 23); - buttonCreateAirBomber.TabIndex = 1; - buttonCreateAirBomber.Text = "Создать бомбардировщик"; - buttonCreateAirBomber.UseVisualStyleBackColor = true; - buttonCreateAirBomber.Click += ButtonCreateAirBomber_Click; - // // buttonLeft // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -112,17 +99,6 @@ namespace ProjectAirBomber buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // - // buttonCreateCar - // - buttonCreateCar.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateCar.Location = new Point(250, 562); - buttonCreateCar.Name = "buttonCreateTrans"; - buttonCreateCar.Size = new Size(223, 23); - buttonCreateCar.TabIndex = 6; - buttonCreateCar.Text = "Создать военный самолет"; - buttonCreateCar.UseVisualStyleBackColor = true; - buttonCreateCar.Click += ButtonCreateBomber_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -143,21 +119,19 @@ namespace ProjectAirBomber buttonStrategyStep.UseVisualStyleBackColor = true; buttonStrategyStep.Click += ButtonStrategyStep_Click; // - // FormElectroTrans + // FormAirBomber // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(923, 597); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateCar); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonLeft); - Controls.Add(buttonCreateAirBomber); Controls.Add(pictureBoxAirBomber); - Name = "FormElectroTrans"; + Name = "FormAirBomber"; Text = "Электропоезд"; ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).EndInit(); ResumeLayout(false); @@ -166,12 +140,10 @@ namespace ProjectAirBomber #endregion private PictureBox pictureBoxAirBomber; - private Button buttonCreateAirBomber; private Button buttonLeft; private Button buttonUp; private Button buttonDown; private Button buttonRight; - private Button buttonCreateCar; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs index bcdbd7b..0bb923b 100644 --- a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs +++ b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs @@ -2,19 +2,40 @@ using ProjectAirBomber.MovementStrategy; namespace ProjectAirBomber; - +/// +/// Форма работы с объектом "Бомбардировщик" +/// public partial class FormAirBomber : Form { /// - /// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà + /// Поле-объект для прорисовки объекта /// - private DrawningBomber? _drawingBomber; + private DrawningBomber? _drawningBomber; /// - /// Ñòðàòåãèÿ ïåðåìåùåíèÿ + /// Стратегия перемещения /// private AbstractStrategy? _strategy; + + /// + /// Получение объекта + /// + public DrawningBomber SetBomber + { + set + { + _drawningBomber = value; + _drawningBomber.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + + /// + /// Конструктор формы + /// public FormAirBomber() { InitializeComponent(); @@ -22,66 +43,29 @@ public partial class FormAirBomber : Form } /// - /// Ìåòîä ïðîðèñîâêè ìàøèíû + /// Метод прорисовки бомбардировщика /// private void Draw() { - if (_drawingBomber == null) + if (_drawningBomber == null) { return; } Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingBomber.DrawTransport(gr); + _drawningBomber.DrawTransport(gr); pictureBoxAirBomber.Image = bmp; } - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawningBomber): - _drawingBomber = new DrawningBomber(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); - break; - case nameof(DrawningAirBomber): - _drawingBomber = new DrawningAirBomber(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - break; - default: - return; - } - _drawingBomber.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); - _drawingBomber.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } /// - /// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ñïîðòèâíûé àâòîìîáèëü" + /// Перемещение объекта по форме (нажатие кнопок навигации) /// /// /// - private void ButtonCreateAirBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirBomber)); - - /// - /// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü àâòîìîáèëü" - /// - /// - /// - private void ButtonCreateBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBomber)); - /// - /// Ïåðåìåùåíèå îáúåêòà ïî ôîðìå (íàæàòèå êíîïîê íàâèãàöèè) - /// - /// - /// - private void ButtonMove_Click(object sender, EventArgs e) + private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawingBomber == null) + if (_drawningBomber == null) { return; } @@ -91,16 +75,16 @@ public partial class FormAirBomber : Form switch (name) { case "buttonUp": - result = _drawingBomber.MoveTransport(DirectionType.Up); + result = _drawningBomber.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawingBomber.MoveTransport(DirectionType.Down); + result = _drawningBomber.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawingBomber.MoveTransport(DirectionType.Left); + result = _drawningBomber.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawingBomber.MoveTransport(DirectionType.Right); + result = _drawningBomber.MoveTransport(DirectionType.Right); break; } @@ -110,13 +94,13 @@ public partial class FormAirBomber : Form } } /// - /// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã" - /// - /// - /// - private void ButtonStrategyStep_Click(object sender, EventArgs e) + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) { - if (_drawingBomber == null) + if (_drawningBomber == null) { return; } @@ -133,7 +117,7 @@ public partial class FormAirBomber : Form { return; } - _strategy.SetData(new MoveableTrans(_drawingBomber), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + _strategy.SetData(new MoveableBomber(_drawningBomber), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); } if (_strategy == null) diff --git a/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.Designer.cs b/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.Designer.cs new file mode 100644 index 0000000..8b07e99 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.Designer.cs @@ -0,0 +1,173 @@ +namespace ProjectAirBomber +{ + partial class FormBomberCollection + { + /// + /// 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() + { + groupBoxTools = new GroupBox(); + buttonRefresh = new Button(); + buttonGoToCheck = new Button(); + buttonRemoveBomber = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddAirBomber = new Button(); + buttonAddBomber = new Button(); + comboBoxSelectorCompany = new ComboBox(); + pictureBox = new PictureBox(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonRefresh); + groupBoxTools.Controls.Add(buttonGoToCheck); + groupBoxTools.Controls.Add(buttonRemoveBomber); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonAddAirBomber); + groupBoxTools.Controls.Add(buttonAddBomber); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(773, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(268, 666); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(20, 567); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(236, 42); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(20, 314); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(236, 42); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveBomber + // + buttonRemoveBomber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveBomber.Location = new Point(20, 255); + buttonRemoveBomber.Name = "buttonRemoveBomber"; + buttonRemoveBomber.Size = new Size(236, 42); + buttonRemoveBomber.TabIndex = 4; + buttonRemoveBomber.Text = "Удалить военный самолёт"; + buttonRemoveBomber.UseVisualStyleBackColor = true; + buttonRemoveBomber.Click += ButtonRemoveBomber_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(20, 210); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(236, 23); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddAirBomber + // + buttonAddAirBomber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddAirBomber.Location = new Point(20, 149); + buttonAddAirBomber.Name = "buttonAddAirBomber"; + buttonAddAirBomber.Size = new Size(236, 42); + buttonAddAirBomber.TabIndex = 2; + buttonAddAirBomber.Text = "Добавление бомбардировщика"; + buttonAddAirBomber.UseVisualStyleBackColor = true; + buttonAddAirBomber.Click += ButtonAddAirBomber_Click; + // + // buttonAddBomber + // + buttonAddBomber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddBomber.Location = new Point(20, 91); + buttonAddBomber.Name = "buttonAddBomber"; + buttonAddBomber.Size = new Size(236, 42); + buttonAddBomber.TabIndex = 1; + buttonAddBomber.Text = "Добавление военного самолёта"; + buttonAddBomber.UseVisualStyleBackColor = true; + buttonAddBomber.Click += ButtonAddBomber_Click; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(20, 22); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(236, 23); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(773, 666); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormBomberCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1041, 666); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormBomberCollection"; + Text = "Коллекция самолетов"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddAirBomber; + private Button buttonAddBomber; + private PictureBox pictureBox; + private Button buttonGoToCheck; + private Button buttonRemoveBomber; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonRefresh; + } +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.cs b/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.cs new file mode 100644 index 0000000..bbd61d6 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.cs @@ -0,0 +1,199 @@ +using ProjectAirBomber.CollectionGenericObjects; +using ProjectAirBomber.Drawnings; +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 ProjectAirBomber; + + +/// +/// Форма работы с компанией и ее коллекцией +/// +public partial class FormBomberCollection : Form +{ + /// + /// Компания + /// + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormBomberCollection() + { + InitializeComponent(); + } + + /// + /// Выбор компании + /// + /// + /// + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new BomberHungarService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + /// + /// Добавление обычного военного самолета + /// + /// + /// + private void ButtonAddBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBomber)); + + /// + /// Добавление бомбардировщика + /// + /// + /// + private void ButtonAddAirBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirBomber)); + + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + + Random random = new(); + DrawningBomber drawningBomber; + switch (type) + { + case nameof(DrawningBomber): + drawningBomber = new DrawningBomber(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningAirBomber): + // вызов диалогового окна для выбора цвета + drawningBomber = new DrawningAirBomber(random.Next(100, 300), random.Next(1000, 3000), + GetColor(random), + GetColor(random), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + + if (_company + drawningBomber != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + _ = MessageBox.Show(drawningBomber.ToString()); + } + } + + /// + /// Получение цвета + /// + /// Генератор случайных чисел + /// + private static Color GetColor(Random random) + { + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + + return color; + } + + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveBomber_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) + { + return; + } + + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_company - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + /// + /// Передача объекта в другую форму + /// + /// + /// + private void ButtonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + DrawningBomber? bomber = null; + int counter = 100; + while (bomber == null) + { + bomber = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (bomber == null) + { + return; + } + + FormAirBomber form = new() + { + SetBomber = bomber + }; + form.ShowDialog(); + } + + /// + /// Перерисовка коллекции + /// + /// + /// + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + } +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.resx b/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/FormBomberCollection.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/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs index e3ebfd3..2349cc7 100644 --- a/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs @@ -2,7 +2,7 @@ namespace ProjectAirBomber.MovementStrategy; -public class MoveableTrans : IMoveableObject +public class MoveableBomber : IMoveableObject { /// /// Поле-объект класса DrawningTrans или его наследника @@ -12,7 +12,7 @@ public class MoveableTrans : IMoveableObject /// Конструктор /// /// Объект класса DrawningTrans - public MoveableTrans(DrawningBomber bomber) + public MoveableBomber(DrawningBomber bomber) { _bomber = bomber; } diff --git a/ProjectAirBomber/ProjectAirBomber/Program.cs b/ProjectAirBomber/ProjectAirBomber/Program.cs index 7f59104..0a98864 100644 --- a/ProjectAirBomber/ProjectAirBomber/Program.cs +++ b/ProjectAirBomber/ProjectAirBomber/Program.cs @@ -11,7 +11,7 @@ namespace ProjectAirBomber // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormAirBomber()); + Application.Run(new FormBomberCollection()); } } } \ No newline at end of file