From 45e5eb57af238dbff0e2eb7196ad59a3db667ed7 Mon Sep 17 00:00:00 2001 From: marusya Date: Sun, 5 Nov 2023 21:18:24 +0400 Subject: [PATCH] lab3 --- .../DrawningUsta.cs | 20 +-- ...FormSelfPropelledArtilleryUnit.Designer.cs | 27 ++- .../FormSelfPropelledArtilleryUnit.cs | 52 +++++- ...opelledArtilleryUnitCollection.Designer.cs | 125 ++++++++++++++ ...ormSelfPropelledArtilleryUnitCollection.cs | 69 ++++++++ ...mSelfPropelledArtilleryUnitCollection.resx | 120 ++++++++++++++ .../IMoveableObject.cs | 5 + .../IMoveableObject_Realise.cs | 17 +- .../SelfPropelledArtilleryUnit/Program.cs | 6 +- .../SelfPropelledArtilleryUnit/SetGeneric.cs | 110 +++++++++++++ .../UstaGenericCollection.cs | 154 ++++++++++++++++++ 11 files changed, 677 insertions(+), 28 deletions(-) create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.Designer.cs create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.resx create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs index e24ad32..796f752 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using SelfPropelledArtilleryUnit.Entities; using SelfPropelledArtilleryUnit.Drawnings; using System.Drawing.Drawing2D; +using SelfPropelledArtilleryUnit.MovementStrategy; namespace SelfPropelledArtilleryUnit.DrawningObjects { @@ -102,15 +103,11 @@ namespace SelfPropelledArtilleryUnit.DrawningObjects } - protected int PictureWidth - { - get { return _pictureWidth; } - } + /// + /// Получение объекта IMoveableObject из объекта DrawningUsta + /// + public IMoveableObject GetMoveableObject => new DrawningObjectUsta(this); - protected int PictureHeight - { - get { return _pictureHeight; } - } /// /// Изменение направления перемещения /// @@ -201,7 +198,6 @@ namespace SelfPropelledArtilleryUnit.DrawningObjects Brush BodyColor = new SolidBrush(EntityUsta.BodyColor); //зеленый темнее - int lineWidth = 10; Color color1 = Color.FromArgb(65, 72, 51); Brush pen1 = new SolidBrush(color1); @@ -229,7 +225,7 @@ namespace SelfPropelledArtilleryUnit.DrawningObjects path.CloseFigure(); // Замкнуть путь // отрисовка прямоугольника с кругленными краями - g.FillPath(pen1, path); + g.FillPath(BodyColor, path); g.DrawPath(pen, path); @@ -238,7 +234,7 @@ namespace SelfPropelledArtilleryUnit.DrawningObjects g.FillEllipse(pen3, _startPosX + 77, _startPosY + 44, 13, 13); - g.FillRectangle(pen2, _startPosX + 45, _startPosY + 20, 40, 20); + g.FillRectangle(BodyColor, _startPosX + 45, _startPosY + 20, 40, 20); g.DrawRectangle(pen, _startPosX + 45, _startPosY + 20, 40, 20); g.DrawEllipse(pen, _startPosX + 40, _startPosY + 65, 13, 13); @@ -246,7 +242,7 @@ namespace SelfPropelledArtilleryUnit.DrawningObjects g.DrawEllipse(pen, _startPosX + 70, _startPosY + 65, 13, 13); g.DrawEllipse(pen, _startPosX + 85, _startPosY + 65, 13, 13); - g.FillRectangle(pen2, _startPosX + 10, _startPosY + 40, 120, 10); + g.FillRectangle(BodyColor, _startPosX + 10, _startPosY + 40, 120, 10); g.DrawRectangle(pen, _startPosX + 10, _startPosY + 40, 120, 10); g.FillEllipse(pen3, _startPosX + 10, _startPosY + 52, 26, 26); diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.Designer.cs index 1fad7f0..cea8e51 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.Designer.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.Designer.cs @@ -38,6 +38,7 @@ buttonLeft = new Button(); buttonRight = new Button(); pictureBoxUsta = new PictureBox(); + ButtonSelectUsta = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxUsta).BeginInit(); SuspendLayout(); // @@ -83,8 +84,9 @@ // // buttonUp // + buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = (Image)resources.GetObject("buttonUp.BackgroundImage"); - buttonUp.Location = new Point(617, 314); + buttonUp.Location = new Point(622, 326); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(50, 50); buttonUp.TabIndex = 4; @@ -93,8 +95,9 @@ // // buttonDown // + buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = (Image)resources.GetObject("buttonDown.BackgroundImage"); - buttonDown.Location = new Point(617, 370); + buttonDown.Location = new Point(622, 382); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(50, 50); buttonDown.TabIndex = 5; @@ -103,8 +106,9 @@ // // buttonLeft // + buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = (Image)resources.GetObject("buttonLeft.BackgroundImage"); - buttonLeft.Location = new Point(561, 370); + buttonLeft.Location = new Point(566, 382); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(50, 50); buttonLeft.TabIndex = 6; @@ -113,8 +117,9 @@ // // buttonRight // + buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = (Image)resources.GetObject("buttonRight.BackgroundImage"); - buttonRight.Location = new Point(673, 370); + buttonRight.Location = new Point(678, 382); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(50, 50); buttonRight.TabIndex = 7; @@ -131,11 +136,22 @@ pictureBoxUsta.TabIndex = 8; pictureBoxUsta.TabStop = false; // + // ButtonSelectUsta + // + ButtonSelectUsta.Location = new Point(348, 393); + ButtonSelectUsta.Name = "ButtonSelectUsta"; + ButtonSelectUsta.Size = new Size(157, 42); + ButtonSelectUsta.TabIndex = 9; + ButtonSelectUsta.Text = "Выбор"; + ButtonSelectUsta.UseVisualStyleBackColor = true; + ButtonSelectUsta.Click += ButtonSelectUsta_Click; + // // FormSelfPropelledArtilleryUnit // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(ButtonSelectUsta); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonDown); @@ -146,7 +162,7 @@ Controls.Add(comboBoxStrategy); Controls.Add(pictureBoxUsta); Name = "FormSelfPropelledArtilleryUnit"; - Text = "SelfPropelledArtilleryUnit"; + Text = "Создание арт. установки"; ((System.ComponentModel.ISupportInitialize)pictureBoxUsta).EndInit(); ResumeLayout(false); PerformLayout(); @@ -162,5 +178,6 @@ private Button buttonLeft; private Button buttonRight; private PictureBox pictureBoxUsta; + private Button ButtonSelectUsta; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.cs index ea3c4e4..27f6e50 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnit.cs @@ -16,12 +16,21 @@ namespace SelfPropelledArtilleryUnit /// /// private AbstractStrategy? _abstractStrategy; + + /// + /// + /// + public DrawningUsta? SelectedUsta { get; private set; } + /// /// /// public FormSelfPropelledArtilleryUnit() { InitializeComponent(); + _abstractStrategy = null; + SelectedUsta = null; + } /// /// @@ -39,19 +48,33 @@ namespace SelfPropelledArtilleryUnit pictureBoxUsta.Image = bmp; } /// - /// " " + /// " . " /// /// /// private void ButtonCreateUstaBat_Click(object sender, EventArgs e) { Random random = new(); + + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + //TODO + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + //TODO + ColorDialog dialog_dop = new(); + if (dialog_dop.ShowDialog() == DialogResult.OK) + { + dopColor = dialog_dop.Color; + } + _drawningUsta = new DrawningUstaBat(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)), + color, + dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxUsta.Width, pictureBoxUsta.Height); _drawningUsta.SetPosition(random.Next(10, 100), random.Next(10, @@ -59,17 +82,24 @@ namespace SelfPropelledArtilleryUnit Draw(); } /// - /// " " + /// " . " /// /// /// private void ButtonCreateUsta_Click(object sender, EventArgs e) { Random random = new(); + 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; + } + _drawningUsta = new DrawningUsta(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + color, pictureBoxUsta.Width, pictureBoxUsta.Height); _drawningUsta.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -147,5 +177,11 @@ namespace SelfPropelledArtilleryUnit } + + private void ButtonSelectUsta_Click(object sender, EventArgs e) + { + SelectedUsta = _drawningUsta; + DialogResult = DialogResult.OK; + } } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.Designer.cs new file mode 100644 index 0000000..61b48ee --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.Designer.cs @@ -0,0 +1,125 @@ +namespace SelfPropelledArtilleryUnit +{ + partial class FormSelfPropelledArtilleryUnitCollection + { + /// + /// 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() + { + ButtonAddUsta = new Button(); + ButtonRemoveUsta = new Button(); + ButtonRefreshCollection = new Button(); + pictureBoxCollection = new PictureBox(); + maskedTextBoxNumber = new MaskedTextBox(); + groupBox1 = new GroupBox(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // ButtonAddUsta + // + ButtonAddUsta.Location = new Point(665, 45); + ButtonAddUsta.Name = "ButtonAddUsta"; + ButtonAddUsta.Size = new Size(166, 31); + ButtonAddUsta.TabIndex = 0; + ButtonAddUsta.Text = "Добавить арт. установку"; + ButtonAddUsta.UseVisualStyleBackColor = true; + ButtonAddUsta.Click += ButtonAddUsta_Click; + // + // ButtonRemoveUsta + // + ButtonRemoveUsta.Location = new Point(665, 129); + ButtonRemoveUsta.Name = "ButtonRemoveUsta"; + ButtonRemoveUsta.Size = new Size(166, 31); + ButtonRemoveUsta.TabIndex = 1; + ButtonRemoveUsta.Text = "Удалить арт. установку"; + ButtonRemoveUsta.UseVisualStyleBackColor = true; + ButtonRemoveUsta.Click += ButtonRemoveUsta_Click; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new Point(665, 215); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(166, 31); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = "Обновить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(643, 423); + pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; + pictureBoxCollection.TabIndex = 3; + pictureBoxCollection.TabStop = false; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point); + maskedTextBoxNumber.Location = new Point(695, 100); + maskedTextBoxNumber.Mask = "00"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(100, 22); + maskedTextBoxNumber.TabIndex = 4; + maskedTextBoxNumber.ValidatingType = typeof(int); + // + // groupBox1 + // + groupBox1.Location = new Point(643, 12); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(209, 411); + groupBox1.TabIndex = 5; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // FormSelfPropelledArtilleryUnitCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(859, 435); + Controls.Add(ButtonRefreshCollection); + Controls.Add(ButtonRemoveUsta); + Controls.Add(maskedTextBoxNumber); + Controls.Add(ButtonAddUsta); + Controls.Add(groupBox1); + Controls.Add(pictureBoxCollection); + Name = "FormSelfPropelledArtilleryUnitCollection"; + Text = "Набор арт. установок"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button ButtonAddUsta; + private Button ButtonRemoveUsta; + private Button ButtonRefreshCollection; + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBox1; + } +} \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs new file mode 100644 index 0000000..8b6336c --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using SelfPropelledArtilleryUnit.DrawningObjects; +using SelfPropelledArtilleryUnit.Drawnings; +using SelfPropelledArtilleryUnit.Generics; +using SelfPropelledArtilleryUnit.MovementStrategy; + + +namespace SelfPropelledArtilleryUnit +{ + public partial class FormSelfPropelledArtilleryUnitCollection : Form + { + private readonly UstaGenericCollection _usta; + public FormSelfPropelledArtilleryUnitCollection() + { + InitializeComponent(); + _usta = new UstaGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void ButtonAddUsta_Click(object sender, EventArgs e) + { + FormSelfPropelledArtilleryUnit form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_usta + form.SelectedUsta > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _usta.ShowUsta(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + + } + + private void ButtonRemoveUsta_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_usta - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _usta.ShowUsta(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _usta.ShowUsta(); + } + } +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.resx b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs index 58eaa68..62d6e6a 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject.cs @@ -31,6 +31,11 @@ namespace SelfPropelledArtilleryUnit.MovementStrategy /// /// Направление void MoveObject(DirectionType direction); + + void SetPosition(int x, int y); + + void Draw(Graphics g); + } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject_Realise.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject_Realise.cs index 469934f..89a26d6 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject_Realise.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/IMoveableObject_Realise.cs @@ -10,7 +10,7 @@ using SelfPropelledArtilleryUnit.Drawnings; namespace SelfPropelledArtilleryUnit.MovementStrategy { /// - /// Реализация интерфейса IDrawningObject для работы с объектом DrawningUsta (паттерн Adapter) + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter) /// public class DrawningObjectUsta : IMoveableObject { @@ -37,5 +37,20 @@ namespace SelfPropelledArtilleryUnit.MovementStrategy _drawningUsta?.CanMove(direction) ?? false; public void MoveObject(DirectionType direction) => _drawningUsta?.MoveTransport(direction); + public void SetPosition(int x, int y) + { + if (_drawningUsta != null) + { + _drawningUsta.SetPosition(x, y); + } + } + + public void Draw(Graphics g) + { + if (_drawningUsta != null) + { + _drawningUsta.DrawTransport(g); + } + } } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs index bc15e02..6bdd353 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs @@ -1,4 +1,6 @@ -namespace SelfPropelledArtilleryUnit +using SelfPropelledArtilleryUnit; + +namespace ProjectUsta { internal static class Program { @@ -11,7 +13,7 @@ namespace SelfPropelledArtilleryUnit // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormSelfPropelledArtilleryUnit()); + Application.Run(new FormSelfPropelledArtilleryUnitCollection()); } } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs new file mode 100644 index 0000000..11567c2 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs @@ -0,0 +1,110 @@ +namespace SelfPropelledArtilleryUnit.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемая установка + /// + public int Insert(T usta) + { + return Insert(usta, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемая установка + /// Позиция + /// + public int Insert(T usta, int position) + { + // TODO проверка позиции + if (position < 0 && position > Count) + { + return -1; + } + if (_places[position] != null) + { + int d = 0; + for (int j = 1; j < Count - position; j++) + { + if (_places[position + j] == null) + { + d = position + j; + break; + } + } + if (d == 0) + { + return -1; + } + for (int j = d; j > position; j--) + { + _places[j] = _places[j - 1]; + } + } + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + _places[position] = usta; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // Проверка позиции + if (position < 0 || position >= _places.Length) + { + // Позиция недопустима + return false; + } + // TODO удаление объекта из массива, присвоив элементу массива значение null + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + // TODO проверка позиции + if (position < 0 || position >= _places.Length) + { + // Позиция недопустима + return null; + } + return _places[position]; + } + } +} + diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs new file mode 100644 index 0000000..1cae4af --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs @@ -0,0 +1,154 @@ +using SelfPropelledArtilleryUnit.Drawnings; +using SelfPropelledArtilleryUnit.MovementStrategy; +using SelfPropelledArtilleryUnit.DrawningObjects; + +namespace SelfPropelledArtilleryUnit.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningUsta + /// + /// + /// + internal class UstaGenericCollection + where T : DrawningUsta + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 100; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public UstaGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int? operator +(UstaGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(UstaGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowUsta() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) + {//линия разметки места + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 2, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int c = 11; + + for (int i = 0; i < _collection.Count; i++) + { + if (i % 3 == 0 && i != 0) + { + c = c - 3; + + } + T? obj = _collection.Get(i); + if (obj != null) + { + // Получение объекта + IMoveableObject moveableObject = obj.GetMoveableObject; + // Установка позиции + + int x = i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth + 3; + int y = c / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight + 10; + moveableObject.SetPosition(x, y); + + // Прорисовка объекта + moveableObject.Draw(g); + } + } + } + } +} +