diff --git a/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs b/AirFighter/AirFighter/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..268c3d5 --- /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, DrawningMilitaryAircraft militaryAircraft) + { + return company._collection.Insert(militaryAircraft); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningMilitaryAircraft operator -(AbstractCompany company, int position) + { + return company._collection.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningMilitaryAircraft? 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) + { + DrawningMilitaryAircraft? 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..bd921bb --- /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/MassiveGenericObjects.cs b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..45f854a --- /dev/null +++ b/AirFighter/AirFighter/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,108 @@ +namespace ProjectAirFighter.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 >= _collection.Length || position < 0) + { return null; } + return _collection[position]; + } + + public int Insert(T obj) + { + int index = 0; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + + index++; + } + return -1; + } + + public int Insert(T obj, int position) + { + 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 T? Remove(int position) + { + 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..588e8b7 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 : DrawningMilitaryAircraft { /// @@ -17,14 +17,14 @@ public class DrawningAirFighter : DrawningFighter /// Дополнительный цвет /// Признак наличия доп крыльев /// Признак наличия ракет - public DrawningAirFighter (int speed, double weight, Color bodyColor, Color additionalColor, bool wings, bool rockets) : base (70, 70) + 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); + EntityMilitaryAircraft = 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 (EntityMilitaryAircraft == null || EntityMilitaryAircraft is not EntityAirFighter airFighter || !_startPosX.HasValue || !_startPosY.HasValue) { return; } diff --git a/AirFighter/AirFighter/Drawnings/DrawningFighter.cs b/AirFighter/AirFighter/Drawnings/DrawningMilitaryAircraft.cs similarity index 69% rename from AirFighter/AirFighter/Drawnings/DrawningFighter.cs rename to AirFighter/AirFighter/Drawnings/DrawningMilitaryAircraft.cs index 2edcbee..05c716f 100644 --- a/AirFighter/AirFighter/Drawnings/DrawningFighter.cs +++ b/AirFighter/AirFighter/Drawnings/DrawningMilitaryAircraft.cs @@ -5,12 +5,12 @@ namespace ProjectAirFighter.Drawnings; /// /// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности /// -public class DrawningFighter +public class DrawningMilitaryAircraft { /// /// Класс-сущность /// - public EntityFighter? EntityFighter { get; protected set; } + public EntityMilitaryAircraft? EntityMilitaryAircraft { get; protected set; } /// /// Ширина окна @@ -23,24 +23,24 @@ public class DrawningFighter private int? _pictureHeight; /// - /// Левая координата прорисовки самолёта + /// Левая координата прорисовки самолёта /// protected int? _startPosX; /// - /// Верхняя кооридната прорисовки самолёта + /// Верхняя кооридната прорисовки военного самолёта /// protected int? _startPosY; /// - /// Ширина прорисовки самолёта + /// Ширина прорисовки военного самолёта /// - private readonly int _drawningFighterWidth = 70; + private readonly int _drawningMilitaryAircraftWidth = 70; /// - /// Высота прорисовки самолёта + /// Высота прорисовки военного самолёта /// - private readonly int _drawningFighterHeight = 70; + private readonly int _drawningMilitaryAircraftHeight = 70; /// /// Координата X объекта @@ -55,17 +55,17 @@ public class DrawningFighter /// /// Ширина объекта /// - public int GetWidth => _drawningFighterWidth; + public int GetWidth => _drawningMilitaryAircraftWidth; /// /// Высота объекта /// - public int GetHeight => _drawningFighterHeight; + public int GetHeight => _drawningMilitaryAircraftHeight; /// /// Пустой конструктор /// - private DrawningFighter() + private DrawningMilitaryAircraft() { _pictureWidth = null; _pictureHeight = null; @@ -79,25 +79,25 @@ public class DrawningFighter /// Скорость /// Вес /// Основной цвет - public DrawningFighter (int speed, double weight, Color bodyColor) : this() + public DrawningMilitaryAircraft(int speed, double weight, Color bodyColor) : this() { - EntityFighter = new EntityFighter(speed, weight, bodyColor); + EntityMilitaryAircraft = new EntityMilitaryAircraft(speed, weight, bodyColor); } /// /// Конструктор для наследников /// - /// Ширина прорисовки самолёта - /// Высота прорисовки самолёта - protected DrawningFighter(int drawningFighterWidth, int drawningFighterHeight) : this() + /// Ширина прорисовки военного самолёта + /// Высота прорисовки военного самолёта + protected DrawningMilitaryAircraft(int drawningMilitaryAircraftWidth, int drawningMilitaryAircraftHeight) : this() { - _drawningFighterWidth = drawningFighterWidth; - _drawningFighterHeight = drawningFighterHeight; + _drawningMilitaryAircraftWidth = drawningMilitaryAircraftWidth; + _drawningMilitaryAircraftHeight = drawningMilitaryAircraftHeight; } public bool SetPictureSize(int width, int height) { - if (_drawningFighterWidth < width && _drawningFighterHeight < height) + if (_drawningMilitaryAircraftWidth < width && _drawningMilitaryAircraftHeight < height) { _pictureWidth = width; _pictureHeight = height; @@ -118,9 +118,9 @@ public class DrawningFighter return; } - if (x + _drawningFighterWidth > _pictureWidth) + if (x + _drawningMilitaryAircraftWidth > _pictureWidth) { - _startPosX = _pictureWidth - _drawningFighterWidth; + _startPosX = _pictureWidth - _drawningMilitaryAircraftWidth; } else if (x < 0) { @@ -130,9 +130,9 @@ public class DrawningFighter { _startPosX = x; } - if (y + _drawningFighterHeight > _pictureHeight) + if (y + _drawningMilitaryAircraftHeight > _pictureHeight) { - _startPosY = _pictureHeight - _drawningFighterHeight; + _startPosY = _pictureHeight - _drawningMilitaryAircraftHeight; } else if (y < 0) { @@ -146,7 +146,7 @@ public class DrawningFighter public bool MoveTransport(DirectionType direction) { - if (EntityFighter == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityMilitaryAircraft == 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 - EntityMilitaryAircraft.Step > 0) { - _startPosX -= (int)EntityFighter.Step; + _startPosX -= (int)EntityMilitaryAircraft.Step; } return true; case DirectionType.Up: - if (_startPosY.Value - EntityFighter.Step > 0) + if (_startPosY.Value - EntityMilitaryAircraft.Step > 0) { - _startPosY -= (int)EntityFighter.Step; + _startPosY -= (int)EntityMilitaryAircraft.Step; } return true; case DirectionType.Right: - if (_startPosX.Value + _drawningFighterWidth + EntityFighter.Step < _pictureWidth) + if (_startPosX.Value + _drawningMilitaryAircraftWidth + EntityMilitaryAircraft.Step < _pictureWidth) { - _startPosX += (int)EntityFighter.Step; + _startPosX += (int)EntityMilitaryAircraft.Step; } return true; case DirectionType.Down: - if (_startPosY.Value + _drawningFighterHeight + EntityFighter.Step < _pictureHeight) + if (_startPosY.Value + _drawningMilitaryAircraftHeight + EntityMilitaryAircraft.Step < _pictureHeight) { - _startPosY += (int)EntityFighter.Step; + _startPosY += (int)EntityMilitaryAircraft.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 (EntityMilitaryAircraft == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush br = new SolidBrush(EntityFighter.BodyColor); + Brush br = new SolidBrush(EntityMilitaryAircraft.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..c42b4fb 100644 --- a/AirFighter/AirFighter/Entities/EntityAirFighter.cs +++ b/AirFighter/AirFighter/Entities/EntityAirFighter.cs @@ -3,7 +3,7 @@ /// /// Класс-сущность "Истребитель" /// -public class EntityAirFighter : EntityFighter +public class EntityAirFighter : EntityMilitaryAircraft { 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/EntityMilitaryAircraft.cs similarity index 77% rename from AirFighter/AirFighter/Entities/EntityFighter.cs rename to AirFighter/AirFighter/Entities/EntityMilitaryAircraft.cs index 34694a6..94cc9ea 100644 --- a/AirFighter/AirFighter/Entities/EntityFighter.cs +++ b/AirFighter/AirFighter/Entities/EntityMilitaryAircraft.cs @@ -1,9 +1,9 @@ namespace ProjectAirFighter.Entities; /// -/// Класс-сущность "Истребитель" +/// Класс-сущность "Военный самолёт" /// -public class EntityFighter +public class EntityMilitaryAircraft { public int Speed { get; private set; } @@ -20,7 +20,7 @@ public class EntityFighter /// Вес автомобиля /// Основной цвет - public EntityFighter (int speed, double weight, Color bodyColor) + public EntityMilitaryAircraft(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; 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..c6a3be9 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 DrawningMilitaryAircraft? _drawningMilitaryAircraft; private AbstractStrategy? _strategy; @@ -15,52 +15,34 @@ public partial class FormAirFighter : Form _strategy = null; } + public DrawningMilitaryAircraft SetAir + { + set + { + _drawningMilitaryAircraft = value; + _drawningMilitaryAircraft.SetPictureSize(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + private void Draw() { - if (_drawningFighter == null) + if (_drawningMilitaryAircraft == null) { return; } Bitmap bmp = new(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningFighter.DrawTransport(gr); + _drawningMilitaryAircraft.DrawTransport(gr); 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) + if (_drawningMilitaryAircraft == null) { return; } @@ -70,16 +52,16 @@ public partial class FormAirFighter : Form switch (name) { case "buttonUp": - result = _drawningFighter.MoveTransport(DirectionType.Up); + result = _drawningMilitaryAircraft.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawningFighter.MoveTransport(DirectionType.Down); + result = _drawningMilitaryAircraft.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawningFighter.MoveTransport(DirectionType.Left); + result = _drawningMilitaryAircraft.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawningFighter.MoveTransport(DirectionType.Right); + result = _drawningMilitaryAircraft.MoveTransport(DirectionType.Right); break; } @@ -91,7 +73,7 @@ public partial class FormAirFighter : Form private void ButtonStrategyStep_Click(object sender, EventArgs e) { - if (_drawningFighter == null) + if (_drawningMilitaryAircraft == null) { return; } @@ -108,7 +90,7 @@ public partial class FormAirFighter : Form { return; } - _strategy.SetData(new MoveableAir(_drawningFighter), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); + _strategy.SetData(new MoveableMilitaryAircraft(_drawningMilitaryAircraft), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); } if (_strategy == null) diff --git a/AirFighter/AirFighter/FormMilitaryAircraftCollection.Designer.cs b/AirFighter/AirFighter/FormMilitaryAircraftCollection.Designer.cs new file mode 100644 index 0000000..a814f69 --- /dev/null +++ b/AirFighter/AirFighter/FormMilitaryAircraftCollection.Designer.cs @@ -0,0 +1,174 @@ +namespace ProjectAirFighter +{ + partial class FormMilitaryAircraftCollection + { + /// + /// 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(); + buttonRemoveMilitaryAircraft = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddAirFighter = new Button(); + buttonAddMilitaryAircraft = 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(buttonRemoveMilitaryAircraft); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonAddAirFighter); + groupBoxTools.Controls.Add(buttonAddMilitaryAircraft); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(610, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(190, 450); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(9, 389); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(175, 49); + 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(9, 316); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(175, 49); + buttonGoToCheck.TabIndex = 6; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += buttonGoToCheck_Click; + // + // buttonRemoveMilitaryAircraft + // + buttonRemoveMilitaryAircraft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveMilitaryAircraft.Location = new Point(9, 234); + buttonRemoveMilitaryAircraft.Name = "buttonRemoveMilitaryAircraft"; + buttonRemoveMilitaryAircraft.Size = new Size(175, 49); + buttonRemoveMilitaryAircraft.TabIndex = 5; + buttonRemoveMilitaryAircraft.Text = "Удаление военного самолёта"; + buttonRemoveMilitaryAircraft.UseVisualStyleBackColor = true; + buttonRemoveMilitaryAircraft.Click += buttonRemoveMilitaryAircraft_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + maskedTextBoxPosition.Location = new Point(9, 201); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(175, 27); + maskedTextBoxPosition.TabIndex = 4; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddAirFighter + // + buttonAddAirFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddAirFighter.Location = new Point(9, 128); + buttonAddAirFighter.Name = "buttonAddAirFighter"; + buttonAddAirFighter.Size = new Size(175, 49); + buttonAddAirFighter.TabIndex = 2; + buttonAddAirFighter.Text = "Добавление истребителя"; + buttonAddAirFighter.UseVisualStyleBackColor = true; + buttonAddAirFighter.Click += buttonAddAirFighter_Click; + // + // buttonAddMilitaryAircraft + // + buttonAddMilitaryAircraft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddMilitaryAircraft.Location = new Point(9, 73); + buttonAddMilitaryAircraft.Name = "buttonAddMilitaryAircraft"; + buttonAddMilitaryAircraft.Size = new Size(175, 49); + buttonAddMilitaryAircraft.TabIndex = 1; + buttonAddMilitaryAircraft.Text = "Добавление военного самолёта"; + buttonAddMilitaryAircraft.UseVisualStyleBackColor = true; + buttonAddMilitaryAircraft.Click += buttonAddMilitaryAircraft_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(9, 26); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(175, 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(610, 450); + pictureBox.TabIndex = 3; + pictureBox.TabStop = false; + // + // FormMilitaryAircraftCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormMilitaryAircraftCollection"; + Text = "FormMilitaryAircraftCollection"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddAirFighter; + private Button buttonAddMilitaryAircraft; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + private Button buttonRefresh; + private Button buttonGoToCheck; + private Button buttonRemoveMilitaryAircraft; + } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs new file mode 100644 index 0000000..20434d1 --- /dev/null +++ b/AirFighter/AirFighter/FormMilitaryAircraftCollection.cs @@ -0,0 +1,181 @@ +using ProjectAirFighter.CollectionGenericObjects; +using ProjectAirFighter.Drawnings; + +namespace ProjectAirFighter; + +public partial class FormMilitaryAircraftCollection : Form +{ + /// + /// Компания + /// + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormMilitaryAircraftCollection() + { + 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 buttonAddMilitaryAircraft_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMilitaryAircraft)); + + /// + /// Добавление истребителя + /// + /// + /// + private void buttonAddAirFighter_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirFighter)); + + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + + Random random = new(); + DrawningMilitaryAircraft drawningMilitaryAircraft; + switch (type) + { + case nameof(DrawningMilitaryAircraft): + drawningMilitaryAircraft = new DrawningMilitaryAircraft(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningAirFighter): + drawningMilitaryAircraft = 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 + drawningMilitaryAircraft != -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 buttonRemoveMilitaryAircraft_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; + } + + DrawningMilitaryAircraft? militaryAircraft = null; + int counter = 100; + while (militaryAircraft == null) + { + militaryAircraft = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (militaryAircraft == null) + { + return; + } + + FormAirFighter form = new() + { + SetAir = militaryAircraft + }; + form.ShowDialog(); + } + + /// + /// Перерисовка коллекции + /// + /// + /// + private void buttonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + } +} diff --git a/AirFighter/AirFighter/FormMilitaryAircraftCollection.resx b/AirFighter/AirFighter/FormMilitaryAircraftCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AirFighter/AirFighter/FormMilitaryAircraftCollection.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/MovementStrategy/MoveToBorder.cs b/AirFighter/AirFighter/MovementStrategy/MoveToBorder.cs index 6fd964a..3804166 100644 --- a/AirFighter/AirFighter/MovementStrategy/MoveToBorder.cs +++ b/AirFighter/AirFighter/MovementStrategy/MoveToBorder.cs @@ -10,8 +10,10 @@ public class MoveToBorder : AbstractStrategy return false; } - return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && - objParams.DownBorder <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; } protected override void MoveToTarget() diff --git a/AirFighter/AirFighter/MovementStrategy/MoveToCenter.cs b/AirFighter/AirFighter/MovementStrategy/MoveToCenter.cs index b7dd88e..4b43b9c 100644 --- a/AirFighter/AirFighter/MovementStrategy/MoveToCenter.cs +++ b/AirFighter/AirFighter/MovementStrategy/MoveToCenter.cs @@ -13,8 +13,10 @@ public class MoveToCenter : AbstractStrategy return false; } - return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && - objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; } protected override void MoveToTarget() diff --git a/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs b/AirFighter/AirFighter/MovementStrategy/MoveableMilitaryAircraft.cs similarity index 52% rename from AirFighter/AirFighter/MovementStrategy/MoveableAir.cs rename to AirFighter/AirFighter/MovementStrategy/MoveableMilitaryAircraft.cs index 3ade525..c019b02 100644 --- a/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs +++ b/AirFighter/AirFighter/MovementStrategy/MoveableMilitaryAircraft.cs @@ -3,46 +3,46 @@ namespace ProjectAirFighter.MovementStrategy; /// -/// Класс-реализация IMoveableObject с использованием DrawningCar +/// Класс-реализация IMoveableObject с использованием DrawningMilitaryAircraft /// -public class MoveableAir : IMoveableObject +public class MoveableMilitaryAircraft : IMoveableObject { /// - /// Поле-объект класса DrawningCar или его наследника + /// Поле-объект класса DrawningMilitaryAircraft или его наследника /// - private readonly DrawningFighter? _air = null; + private readonly DrawningMilitaryAircraft? _militaryAircraft = null; /// /// Конструктор /// - /// Объект класса DrawningCar - public MoveableAir(DrawningFighter air) + /// Объект класса DrawningMilitaryAircraft + public MoveableMilitaryAircraft(DrawningMilitaryAircraft militaryAircraft) { - _air = air; + _militaryAircraft = militaryAircraft; } public ObjectParameters? GetObjectPosition { get { - if (_air == null || _air.EntityFighter == null || !_air.GetPosX.HasValue || !_air.GetPosY.HasValue) + if (_militaryAircraft == null || _militaryAircraft.EntityMilitaryAircraft == null || !_militaryAircraft.GetPosX.HasValue || !_militaryAircraft.GetPosY.HasValue) { return null; } - return new ObjectParameters(_air.GetPosX.Value, _air.GetPosY.Value, _air.GetWidth, _air.GetHeight); + return new ObjectParameters(_militaryAircraft.GetPosX.Value, _militaryAircraft.GetPosY.Value, _militaryAircraft.GetWidth, _militaryAircraft.GetHeight); } } - public int GetStep => (int)(_air?.EntityFighter?.Step ?? 0); + public int GetStep => (int)(_militaryAircraft?.EntityMilitaryAircraft?.Step ?? 0); public bool TryMoveObject(MovementDirection direction) { - if (_air == null || _air.EntityFighter == null) + if (_militaryAircraft == null || _militaryAircraft.EntityMilitaryAircraft == null) { return false; } - return _air.MoveTransport(GetDirectionType(direction)); + return _militaryAircraft.MoveTransport(GetDirectionType(direction)); } /// diff --git a/AirFighter/AirFighter/Program.cs b/AirFighter/AirFighter/Program.cs index fc87305..9d818ea 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 FormMilitaryAircraftCollection()); } } } \ No newline at end of file