diff --git a/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs b/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..8b7128a --- /dev/null +++ b/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,116 @@ +using ProjectAirFighter.Drawnings; + +namespace ProjectAirFighter.CollectionGenericObjects; + +/// +/// Абстракция компании, хранящий коллекцию автомобилей +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 100; + + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 105; + + /// + /// Ширина окна2 + /// + 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, DrawningPlane plane) + { + return company._collection.Insert(plane); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningPlane operator -(AbstractCompany company, int position) + { + return company._collection.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningPlane? 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) + { + DrawningPlane? 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/AirFighter/AirFighter/CollectionGenericObjects/Angar.cs b/AirFighter/AirFighter/CollectionGenericObjects/Angar.cs new file mode 100644 index 0000000..0f8af18 --- /dev/null +++ b/AirFighter/AirFighter/CollectionGenericObjects/Angar.cs @@ -0,0 +1,64 @@ +using ProjectAirFighter.Drawnings; + +namespace ProjectAirFighter.CollectionGenericObjects; + +/// +/// Реализация абстрактной компании - каршеринг +/// +public class Angar : AbstractCompany +{ + /// + /// Конструктор + /// + /// + /// + /// + public Angar(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + Pen pen = new(Color.Black, 2); + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth + 5, j * _placeSizeHeight, i * _placeSizeWidth + 5 + _placeSizeWidth - 30, j * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth + 5, j * _placeSizeHeight, i * _placeSizeWidth + 5, j * _placeSizeHeight - _placeSizeHeight); + } + } + } + + protected override void SetObjectsPosition() + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = width - 1; + int curHeight = height - 1; + + for (int i = 0; i < (_collection?.Count ?? 0); i++) + { + if (_collection.Get(i) != null) + { + _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 10, curHeight * _placeSizeHeight + 10); + } + + if (curWidth > 0) + curWidth--; + else + { + curWidth = width - 1; + curHeight--; + } + if (curHeight > height) + { + return; + } + } + } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/CollectionGenericObjects/ICollectionGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/ICollectionGenericObjects.cs index 4987c50..37a865f 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -22,7 +22,7 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj); + int Insert(T obj); /// /// Добавление объекта в коллекцию на конкретную позицию @@ -30,14 +30,14 @@ public interface ICollectionGenericObjects /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj, int position); + int Insert(T obj, int position); /// /// Удаление объекта из коллекции с конкретной позиции /// /// Позиция /// true - удаление прошло удачно, false - удаление не удалось - bool Remove(int position); + T? Remove(int position); /// /// Получение объекта по позиции diff --git a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs index 5330096..489210a 100644 --- a/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs @@ -43,29 +43,75 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { // TODO проверка позиции + if (position >= _collection.Length || position < 0) + { return null; } return _collection[position]; } - public bool Insert(T obj) + public int Insert(T obj) { // TODO вставка в свободное место набора - return false; + int index = 0; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + + index++; + } + return -1; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то // ищется свободное место после этой позиции и идет вставка туда // если нет после, ищем до // TODO вставка - return false; + if (position >= _collection.Length || position < 0) + { return -1; } + + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + int index; + + for (index = position + 1; index < _collection.Length; ++index) + { + if (_collection[index] == null) + { + _collection[position] = obj; + return position; + } + } + + for (index = position - 1; index >= 0; --index) + { + if (_collection[index] == null) + { + _collection[position] = obj; + return position; + } + } + return -1; } - public bool Remove(int position) + public T? Remove(int position) { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null - return true; + if (position >= _collection.Length || position < 0) + { + return null; + } + T obj = _collection[position]; + _collection[position] = null; + return obj; } } \ No newline at end of file diff --git a/AirFighter/AirFighter/Drawnings/DrawningAirFighter.cs b/AirFighter/AirFighter/Drawnings/DrawningAirFighter.cs index 192246b..cdc7933 100644 --- a/AirFighter/AirFighter/Drawnings/DrawningAirFighter.cs +++ b/AirFighter/AirFighter/Drawnings/DrawningAirFighter.cs @@ -5,7 +5,7 @@ namespace ProjectAirFighter.Drawnings; /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// -public class DrawningAirFighter : DrawningFighter +public class DrawningAirFighter : DrawningPlane { /// @@ -19,22 +19,22 @@ public class DrawningAirFighter : DrawningFighter /// Признак наличия ракет public DrawningAirFighter (int speed, double weight, Color bodyColor, Color additionalColor, bool wings, bool rockets) : base (70, 70) { - EntityFighter = new EntityAirFighter(speed, weight, bodyColor, additionalColor, wings, rockets); + EntityPlane = new EntityAirFighter(speed, weight, bodyColor, additionalColor, wings, rockets); } public override void DrawTransport(Graphics g) { - if (EntityFighter == null || EntityFighter is not EntityAirFighter airFighter || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityPlane == null || EntityPlane is not EntityAirFighter planeFighter || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(airFighter.AdditionalColor); + Brush additionalBrush = new SolidBrush(planeFighter.AdditionalColor); base.DrawTransport(g); - if (airFighter.Wings) + if (planeFighter.Wings) { Point wings1 = new Point(_startPosX.Value + 45, _startPosY.Value + 30); Point wings2 = new Point(_startPosX.Value + 45, _startPosY.Value + 15); @@ -53,7 +53,7 @@ public class DrawningAirFighter : DrawningFighter g.DrawPolygon(pen, DownWing); } - if (airFighter.Rockets) + if (planeFighter.Rockets) { Point rocket1 = new Point(_startPosX.Value + 40, _startPosY.Value + 5); Point rocket2 = new Point(_startPosX.Value + 15, _startPosY.Value + 5); diff --git a/AirFighter/AirFighter/Drawnings/DrawningFighter.cs b/AirFighter/AirFighter/Drawnings/DrawningPlane.cs similarity index 87% rename from AirFighter/AirFighter/Drawnings/DrawningFighter.cs rename to AirFighter/AirFighter/Drawnings/DrawningPlane.cs index 2edcbee..e4e632c 100644 --- a/AirFighter/AirFighter/Drawnings/DrawningFighter.cs +++ b/AirFighter/AirFighter/Drawnings/DrawningPlane.cs @@ -5,12 +5,12 @@ namespace ProjectAirFighter.Drawnings; /// /// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности /// -public class DrawningFighter +public class DrawningPlane { /// /// Класс-сущность /// - public EntityFighter? EntityFighter { get; protected set; } + public EntityPlane? EntityPlane { get; protected set; } /// /// Ширина окна @@ -65,7 +65,7 @@ public class DrawningFighter /// /// Пустой конструктор /// - private DrawningFighter() + private DrawningPlane() { _pictureWidth = null; _pictureHeight = null; @@ -79,9 +79,9 @@ public class DrawningFighter /// Скорость /// Вес /// Основной цвет - public DrawningFighter (int speed, double weight, Color bodyColor) : this() + public DrawningPlane (int speed, double weight, Color bodyColor) : this() { - EntityFighter = new EntityFighter(speed, weight, bodyColor); + EntityPlane = new EntityPlane(speed, weight, bodyColor); } /// @@ -89,7 +89,7 @@ public class DrawningFighter /// /// Ширина прорисовки самолёта /// Высота прорисовки самолёта - protected DrawningFighter(int drawningFighterWidth, int drawningFighterHeight) : this() + protected DrawningPlane(int drawningFighterWidth, int drawningFighterHeight) : this() { _drawningFighterWidth = drawningFighterWidth; _drawningFighterHeight = drawningFighterHeight; @@ -146,7 +146,7 @@ public class DrawningFighter public bool MoveTransport(DirectionType direction) { - if (EntityFighter == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -154,27 +154,27 @@ public class DrawningFighter switch (direction) { case DirectionType.Left: - if (_startPosX.Value - EntityFighter.Step > 0) + if (_startPosX.Value - EntityPlane.Step > 0) { - _startPosX -= (int)EntityFighter.Step; + _startPosX -= (int)EntityPlane.Step; } return true; case DirectionType.Up: - if (_startPosY.Value - EntityFighter.Step > 0) + if (_startPosY.Value - EntityPlane.Step > 0) { - _startPosY -= (int)EntityFighter.Step; + _startPosY -= (int)EntityPlane.Step; } return true; case DirectionType.Right: - if (_startPosX.Value + _drawningFighterWidth + EntityFighter.Step < _pictureWidth) + if (_startPosX.Value + _drawningFighterWidth + EntityPlane.Step < _pictureWidth) { - _startPosX += (int)EntityFighter.Step; + _startPosX += (int)EntityPlane.Step; } return true; case DirectionType.Down: - if (_startPosY.Value + _drawningFighterHeight + EntityFighter.Step < _pictureHeight) + if (_startPosY.Value + _drawningFighterHeight + EntityPlane.Step < _pictureHeight) { - _startPosY += (int)EntityFighter.Step; + _startPosY += (int)EntityPlane.Step; } return true; default: @@ -184,13 +184,13 @@ public class DrawningFighter public virtual void DrawTransport(Graphics g) { - if (EntityFighter == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush br = new SolidBrush(EntityFighter.BodyColor); + Brush br = new SolidBrush(EntityPlane.BodyColor); g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 30, 60, 10); g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 30, 60, 10); diff --git a/AirFighter/AirFighter/Entities/EntityAirFighter.cs b/AirFighter/AirFighter/Entities/EntityAirFighter.cs index 5821924..4e6c8b3 100644 --- a/AirFighter/AirFighter/Entities/EntityAirFighter.cs +++ b/AirFighter/AirFighter/Entities/EntityAirFighter.cs @@ -3,7 +3,7 @@ /// /// Класс-сущность "Истребитель" /// -public class EntityAirFighter : EntityFighter +public class EntityAirFighter : EntityPlane { public Color AdditionalColor { get; private set; } @@ -11,7 +11,7 @@ public class EntityAirFighter : EntityFighter public bool Rockets { get; private set; } - public EntityAirFighter(int speed, double weight, Color bodyColor, Color additionalColor, bool wings, bool rockets) : base(5, 45, Color.Black) + public EntityAirFighter(int speed, double weight, Color bodyColor, Color additionalColor, bool wings, bool rockets) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; Rockets = rockets; diff --git a/AirFighter/AirFighter/Entities/EntityFighter.cs b/AirFighter/AirFighter/Entities/EntityPlane.cs similarity index 87% rename from AirFighter/AirFighter/Entities/EntityFighter.cs rename to AirFighter/AirFighter/Entities/EntityPlane.cs index 34694a6..df8a889 100644 --- a/AirFighter/AirFighter/Entities/EntityFighter.cs +++ b/AirFighter/AirFighter/Entities/EntityPlane.cs @@ -3,7 +3,7 @@ /// /// Класс-сущность "Истребитель" /// -public class EntityFighter +public class EntityPlane { public int Speed { get; private set; } @@ -20,7 +20,7 @@ public class EntityFighter /// Вес автомобиля /// Основной цвет - public EntityFighter (int speed, double weight, Color bodyColor) + public EntityPlane (int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; diff --git a/AirFighter/AirFighter/FormAirCollection.Designer.cs b/AirFighter/AirFighter/FormAirCollection.Designer.cs new file mode 100644 index 0000000..144c845 --- /dev/null +++ b/AirFighter/AirFighter/FormAirCollection.Designer.cs @@ -0,0 +1,173 @@ +namespace ProjectAirFighter +{ + partial class FormAirCollection + { + /// + /// 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(); + buttonRemoveAir = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddAirFighter = new Button(); + buttonAddAir = 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(buttonRemoveAir); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonAddAirFighter); + groupBoxTools.Controls.Add(buttonAddAir); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(594, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(206, 450); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 395); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(197, 29); + buttonRefresh.TabIndex = 7; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += buttonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(6, 328); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(197, 29); + buttonGoToCheck.TabIndex = 6; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += buttonGoToCheck_Click; + // + // buttonRemoveAir + // + buttonRemoveAir.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveAir.Location = new Point(5, 211); + buttonRemoveAir.Name = "buttonRemoveAir"; + buttonRemoveAir.Size = new Size(197, 29); + buttonRemoveAir.TabIndex = 5; + buttonRemoveAir.Text = "Удаление самолёта"; + buttonRemoveAir.UseVisualStyleBackColor = true; + buttonRemoveAir.Click += buttonRemoveAir_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(6, 161); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(194, 27); + maskedTextBoxPosition.TabIndex = 4; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddAirFighter + // + buttonAddAirFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddAirFighter.Location = new Point(6, 103); + buttonAddAirFighter.Name = "buttonAddAirFighter"; + buttonAddAirFighter.Size = new Size(197, 31); + buttonAddAirFighter.TabIndex = 2; + buttonAddAirFighter.Text = "Добавление истребителя"; + buttonAddAirFighter.UseVisualStyleBackColor = true; + buttonAddAirFighter.Click += buttonAddAirFighter_Click; + // + // buttonAddAir + // + buttonAddAir.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddAir.Location = new Point(6, 68); + buttonAddAir.Name = "buttonAddAir"; + buttonAddAir.Size = new Size(197, 29); + buttonAddAir.TabIndex = 1; + buttonAddAir.Text = "Добавление самолёта"; + buttonAddAir.UseVisualStyleBackColor = true; + buttonAddAir.Click += buttonAddAir_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(6, 26); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(194, 28); + 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(594, 450); + pictureBox.TabIndex = 3; + pictureBox.TabStop = false; + // + // FormAirCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormAirCollection"; + Text = "Коллекция самолётов"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddAir; + private Button buttonAddAirFighter; + private PictureBox pictureBox; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonRemoveAir; + private Button buttonRefresh; + private Button buttonGoToCheck; + } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirCollection.cs b/AirFighter/AirFighter/FormAirCollection.cs new file mode 100644 index 0000000..8b0b217 --- /dev/null +++ b/AirFighter/AirFighter/FormAirCollection.cs @@ -0,0 +1,181 @@ +using ProjectAirFighter.CollectionGenericObjects; +using ProjectAirFighter.Drawnings; + +namespace ProjectAirFighter; + +public partial class FormAirCollection : Form +{ + /// + /// Компания + /// + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormAirCollection() + { + InitializeComponent(); + } + + /// + /// Выбор компании + /// + /// + /// + private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new Angar(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + /// + /// Добавление самолёта + /// + /// + /// + private void buttonAddAir_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningPlane)); + + /// + /// Добавление истребителя + /// + /// + /// + private void buttonAddAirFighter_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirFighter)); + + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + + Random random = new(); + DrawningPlane drawningFighter; + switch (type) + { + case nameof(DrawningPlane): + drawningFighter = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningAirFighter): + drawningFighter = new DrawningAirFighter(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 + drawningFighter != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + + /// + /// Получение цвета + /// + /// Генератор случайных чисел + /// + 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 buttonRemoveAir_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; + } + + DrawningPlane? plane = null; + int counter = 100; + while (plane == null) + { + plane = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (plane == null) + { + return; + } + + FormAirFighter form = new() + { + SetAir = plane + }; + form.ShowDialog(); + } + + /// + /// Перерисовка коллекции + /// + /// + /// + private void buttonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + } +} diff --git a/AirFighter/AirFighter/FormAirCollection.resx b/AirFighter/AirFighter/FormAirCollection.resx new file mode 100644 index 0000000..39e9e6f --- /dev/null +++ b/AirFighter/AirFighter/FormAirCollection.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/AirFighter/AirFighter/FormAirFighter.Designer.cs b/AirFighter/AirFighter/FormAirFighter.Designer.cs index 4ed91c0..509406c 100644 --- a/AirFighter/AirFighter/FormAirFighter.Designer.cs +++ b/AirFighter/AirFighter/FormAirFighter.Designer.cs @@ -22,12 +22,10 @@ private void InitializeComponent() { pictureBoxAirFighter = new PictureBox(); - buttonCreateAirFighter = new Button(); buttonDown = new Button(); buttonRight = new Button(); buttonLeft = new Button(); buttonUp = new Button(); - buttonCreateFighter = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).BeginInit(); @@ -42,17 +40,6 @@ pictureBoxAirFighter.TabIndex = 0; pictureBoxAirFighter.TabStop = false; // - // buttonCreateAirFighter - // - buttonCreateAirFighter.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateAirFighter.Location = new Point(12, 409); - buttonCreateAirFighter.Name = "buttonCreateAirFighter"; - buttonCreateAirFighter.Size = new Size(169, 29); - buttonCreateAirFighter.TabIndex = 1; - buttonCreateAirFighter.Text = "Создать истребитель"; - buttonCreateAirFighter.UseVisualStyleBackColor = true; - buttonCreateAirFighter.Click += buttonCreateAirFighter_Click; - // // buttonDown // buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -101,17 +88,6 @@ buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; // - // buttonCreateFighter - // - buttonCreateFighter.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateFighter.Location = new Point(196, 409); - buttonCreateFighter.Name = "buttonCreateFighter"; - buttonCreateFighter.Size = new Size(169, 29); - buttonCreateFighter.TabIndex = 6; - buttonCreateFighter.Text = "Создать самолёт"; - buttonCreateFighter.UseVisualStyleBackColor = true; - buttonCreateFighter.Click += buttonCreateAir_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -139,12 +115,10 @@ ClientSize = new Size(800, 450); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateFighter); Controls.Add(buttonUp); Controls.Add(buttonLeft); Controls.Add(buttonRight); Controls.Add(buttonDown); - Controls.Add(buttonCreateAirFighter); Controls.Add(pictureBoxAirFighter); Name = "FormAirFighter"; Text = "Истребитель"; @@ -155,12 +129,10 @@ #endregion private PictureBox pictureBoxAirFighter; - private Button buttonCreateAirFighter; private Button buttonDown; private Button buttonRight; private Button buttonLeft; private Button buttonUp; - private Button buttonCreateFighter; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/AirFighter/AirFighter/FormAirFighter.cs b/AirFighter/AirFighter/FormAirFighter.cs index 4567e7b..eeb7a06 100644 --- a/AirFighter/AirFighter/FormAirFighter.cs +++ b/AirFighter/AirFighter/FormAirFighter.cs @@ -5,7 +5,7 @@ namespace ProjectAirFighter; public partial class FormAirFighter : Form { - private DrawningFighter? _drawningFighter; + private DrawningPlane? _drawningFighter; private AbstractStrategy? _strategy; @@ -15,6 +15,18 @@ public partial class FormAirFighter : Form _strategy = null; } + public DrawningPlane SetAir + { + set + { + _drawningFighter = value; + _drawningFighter.SetPictureSize(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + private void Draw() { if (_drawningFighter == null) @@ -28,36 +40,6 @@ public partial class FormAirFighter : Form pictureBoxAirFighter.Image = bmp; } - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawningFighter): - _drawningFighter = new DrawningFighter(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(DrawningAirFighter): - _drawningFighter = new DrawningAirFighter(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; - } - - _drawningFighter.SetPictureSize(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); - _drawningFighter.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - - private void buttonCreateAirFighter_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirFighter)); - - private void buttonCreateAir_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningFighter)); - private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningFighter == null) diff --git a/AirFighter/AirFighter/FormAirFighter.resx b/AirFighter/AirFighter/FormAirFighter.resx index af32865..39e9e6f 100644 --- a/AirFighter/AirFighter/FormAirFighter.resx +++ b/AirFighter/AirFighter/FormAirFighter.resx @@ -28,7 +28,7 @@ There are any number of "resheader" rows that contain simple - name/value pairs. + name/value pplanes. Each data row contains a name, and value. The row also contains a type or mimetype. Type corresponds to a .NET class that support diff --git a/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs b/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs index 3ade525..3238dcd 100644 --- a/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs +++ b/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs @@ -10,39 +10,39 @@ public class MoveableAir : IMoveableObject /// /// Поле-объект класса DrawningCar или его наследника /// - private readonly DrawningFighter? _air = null; + private readonly DrawningPlane? _plane = null; /// /// Конструктор /// /// Объект класса DrawningCar - public MoveableAir(DrawningFighter air) + public MoveableAir(DrawningPlane plane) { - _air = air; + _plane = plane; } public ObjectParameters? GetObjectPosition { get { - if (_air == null || _air.EntityFighter == null || !_air.GetPosX.HasValue || !_air.GetPosY.HasValue) + if (_plane == null || _plane.EntityPlane == null || !_plane.GetPosX.HasValue || !_plane.GetPosY.HasValue) { return null; } - return new ObjectParameters(_air.GetPosX.Value, _air.GetPosY.Value, _air.GetWidth, _air.GetHeight); + return new ObjectParameters(_plane.GetPosX.Value, _plane.GetPosY.Value, _plane.GetWidth, _plane.GetHeight); } } - public int GetStep => (int)(_air?.EntityFighter?.Step ?? 0); + public int GetStep => (int)(_plane?.EntityPlane?.Step ?? 0); public bool TryMoveObject(MovementDirection direction) { - if (_air == null || _air.EntityFighter == null) + if (_plane == null || _plane.EntityPlane == null) { return false; } - return _air.MoveTransport(GetDirectionType(direction)); + return _plane.MoveTransport(GetDirectionType(direction)); } /// diff --git a/AirFighter/AirFighter/Program.cs b/AirFighter/AirFighter/Program.cs index fc87305..ea8c50c 100644 --- a/AirFighter/AirFighter/Program.cs +++ b/AirFighter/AirFighter/Program.cs @@ -11,7 +11,7 @@ namespace ProjectAirFighter // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormAirFighter()); + Application.Run(new FormAirCollection()); } } } \ No newline at end of file diff --git a/AirFighter/AirFighter/Properties/Resources.resx b/AirFighter/AirFighter/Properties/Resources.resx index 8b5f71e..07a09cc 100644 --- a/AirFighter/AirFighter/Properties/Resources.resx +++ b/AirFighter/AirFighter/Properties/Resources.resx @@ -28,7 +28,7 @@ There are any number of "resheader" rows that contain simple - name/value pairs. + name/value pplanes. Each data row contains a name, and value. The row also contains a type or mimetype. Type corresponds to a .NET class that support