diff --git a/AirFighter/AirFighter/AirFighterGenericCollection.cs b/AirFighter/AirFighter/AirFighterGenericCollection.cs new file mode 100644 index 0000000..1d7a5bc --- /dev/null +++ b/AirFighter/AirFighter/AirFighterGenericCollection.cs @@ -0,0 +1,97 @@ +using AirFighter.Drawnings; +using AirFighter.MovementStrategy; +using AirFighter.DrawningObjects; +using AirFighter.Generics; +using AirFighter.MovementStrategy; + +namespace AirFighter.Generics +{ + internal class AirFighterGenericCollection + where T : DrawningAirFighter + where U : IMoveableObject + { + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + private readonly int _placeSizeWidth = 210; + + private readonly int _placeSizeHeight = 180; + + private readonly SetGeneric _collection; + public AirFighterGenericCollection(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 +(AirFighterGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + public static bool operator -(AirFighterGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowAirFighter() + { + 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, 2); + 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, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + private void DrawObjects(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _collection.Count; i++) + { + DrawningAirFighter? fighter = _collection.Get(i); + + if (fighter == null) + continue; + + int row = height - 1 - (i / width); + int col = width - 1 - (i % width); + + fighter.SetPosition(col * _placeSizeWidth + 10, row * _placeSizeHeight + 5); + + fighter.DrawTransport(g); + } + } + } +} + diff --git a/AirFighter/AirFighter/DrawningAirFighter.cs b/AirFighter/AirFighter/DrawningAirFighter.cs index 586a028..0e08b76 100644 --- a/AirFighter/AirFighter/DrawningAirFighter.cs +++ b/AirFighter/AirFighter/DrawningAirFighter.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using AirFighter.Entities; using AirFighter.Drawnings; using System.Drawing.Drawing2D; +using AirFighter.MovementStrategy; namespace AirFighter.DrawningObjects { @@ -69,14 +70,7 @@ namespace AirFighter.DrawningObjects _startPosX = x; _startPosY = y; } - protected int PictureWidth - { - get { return _pictureWidth; } - } - protected int PictureHeight - { - get { return _pictureHeight; } - } + public IMoveableObject GetMoveableObject => new DrawningObjectAirFighter(this); public int GetPosX => _startPosX; public int GetPosY => _startPosY; public int GetWidth => _fighterWidth; diff --git a/AirFighter/AirFighter/FormAirFighter.Designer.cs b/AirFighter/AirFighter/FormAirFighter.Designer.cs index 89dc618..90cc53b 100644 --- a/AirFighter/AirFighter/FormAirFighter.Designer.cs +++ b/AirFighter/AirFighter/FormAirFighter.Designer.cs @@ -34,9 +34,10 @@ buttonRight = new Button(); buttonUp = new Button(); buttonCreate = new Button(); - ButtonStepAirFighter = new Button(); - comboBoxAirFighter = new ComboBox(); + ButtonStep = new Button(); + comboBoxStrategy = new ComboBox(); ButtonCreateFighter = new Button(); + buttonSelectFighter = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).BeginInit(); SuspendLayout(); // @@ -110,27 +111,27 @@ buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += buttonCreateAirFighterMilitary_Click; // - // ButtonStepAirFighter + // ButtonStep // - ButtonStepAirFighter.BackColor = Color.Azure; - ButtonStepAirFighter.Font = new Font("Times New Roman", 9.75F, FontStyle.Bold, GraphicsUnit.Point); - ButtonStepAirFighter.Location = new Point(713, 57); - ButtonStepAirFighter.Name = "ButtonStepAirFighter"; - ButtonStepAirFighter.Size = new Size(75, 23); - ButtonStepAirFighter.TabIndex = 3; - ButtonStepAirFighter.Text = "Шаг"; - ButtonStepAirFighter.UseVisualStyleBackColor = true; - ButtonStepAirFighter.Click += ButtonStep_Click; + ButtonStep.BackColor = Color.Azure; + ButtonStep.Font = new Font("Times New Roman", 9.75F, FontStyle.Bold, GraphicsUnit.Point); + ButtonStep.Location = new Point(713, 57); + ButtonStep.Name = "ButtonStep"; + ButtonStep.Size = new Size(75, 23); + ButtonStep.TabIndex = 3; + ButtonStep.Text = "Шаг"; + ButtonStep.UseVisualStyleBackColor = true; + ButtonStep.Click += ButtonStep_Click; // - // comboBoxAirFighter + // comboBoxStrategy // - comboBoxAirFighter.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxAirFighter.FormattingEnabled = true; - comboBoxAirFighter.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder", "-" }); - comboBoxAirFighter.Location = new Point(667, 12); - comboBoxAirFighter.Name = "comboBoxAirFighter"; - comboBoxAirFighter.Size = new Size(121, 23); - comboBoxAirFighter.TabIndex = 6; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder", "-" }); + comboBoxStrategy.Location = new Point(667, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 6; // // ButtonCreateFighter // @@ -143,14 +144,26 @@ ButtonCreateFighter.UseVisualStyleBackColor = true; ButtonCreateFighter.Click += ButtonCreateFighter_Click; // + // buttonSelectFighter + // + buttonSelectFighter.Font = new Font("Times New Roman", 12F, FontStyle.Regular, GraphicsUnit.Point); + buttonSelectFighter.Location = new Point(352, 390); + buttonSelectFighter.Name = "buttonSelectFighter"; + buttonSelectFighter.Size = new Size(164, 47); + buttonSelectFighter.TabIndex = 7; + buttonSelectFighter.Text = "Выбор"; + buttonSelectFighter.UseVisualStyleBackColor = true; + buttonSelectFighter.Click += ButtonSelectAirFighter_Click; + // // FormAirFighter // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonSelectFighter); Controls.Add(ButtonCreateFighter); - Controls.Add(comboBoxAirFighter); - Controls.Add(ButtonStepAirFighter); + Controls.Add(comboBoxStrategy); + Controls.Add(ButtonStep); Controls.Add(buttonCreate); Controls.Add(buttonUp); Controls.Add(buttonRight); @@ -172,8 +185,9 @@ private Button buttonRight; private Button buttonUp; private Button buttonCreate; - private Button ButtonStepAirFighter; - private ComboBox comboBoxAirFighter; + private Button ButtonStep; + private ComboBox comboBoxStrategy; private Button ButtonCreateFighter; + private Button buttonSelectFighter; } } \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirFighter.cs b/AirFighter/AirFighter/FormAirFighter.cs index 2875458..9ec5d8a 100644 --- a/AirFighter/AirFighter/FormAirFighter.cs +++ b/AirFighter/AirFighter/FormAirFighter.cs @@ -8,9 +8,12 @@ namespace AirFighter { private DrawningAirFighter? _drawningAirFighter; private AbstractStrategy? _abstractStrategy; + public DrawningAirFighter? SelectedAirFighter { get; private set; } public FormAirFighter() { InitializeComponent(); + _abstractStrategy = null; + SelectedAirFighter = null; } private void Draw() { @@ -27,12 +30,26 @@ namespace AirFighter private void buttonCreateAirFighterMilitary_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; + } + _drawningAirFighter = new DrawningAirFighterMilitary(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)), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); _drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10, @@ -42,10 +59,17 @@ namespace AirFighter private void ButtonCreateFighter_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; + } + _drawningAirFighter = 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, pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); _drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -82,9 +106,9 @@ namespace AirFighter { return; } - if (comboBoxAirFighter.Enabled) + if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxAirFighter.SelectedIndex + _abstractStrategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), @@ -98,7 +122,7 @@ namespace AirFighter _abstractStrategy.SetData(new DrawningObjectAirFighter(_drawningAirFighter), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); - comboBoxAirFighter.Enabled = false; + comboBoxStrategy.Enabled = false; } if (_abstractStrategy != null) { @@ -107,12 +131,18 @@ namespace AirFighter if (_abstractStrategy.GetStatus() == Status.Finish) { - comboBoxAirFighter.Enabled = true; + comboBoxStrategy.Enabled = true; _abstractStrategy = null; } } } + + private void ButtonSelectAirFighter_Click(object sender, EventArgs e) + { + SelectedAirFighter = _drawningAirFighter; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/AirFighter/AirFighter/FormFighterCollection.Designer.cs b/AirFighter/AirFighter/FormFighterCollection.Designer.cs new file mode 100644 index 0000000..0cd3e7c --- /dev/null +++ b/AirFighter/AirFighter/FormFighterCollection.Designer.cs @@ -0,0 +1,127 @@ +namespace AirFighter +{ + partial class FormFighterCollection + { + /// + /// 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() + { + groupBox1 = new GroupBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRemoveFighter = new Button(); + buttonRefreshCollection = new Button(); + buttonAddFighter = new Button(); + pictureBoxCollection = new PictureBox(); + groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBox1 + // + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(buttonRemoveFighter); + groupBox1.Controls.Add(buttonRefreshCollection); + groupBox1.Controls.Add(buttonAddFighter); + groupBox1.Location = new Point(740, 0); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(213, 448); + groupBox1.TabIndex = 0; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point); + maskedTextBoxNumber.Location = new Point(37, 103); + maskedTextBoxNumber.Mask = "00"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(100, 22); + maskedTextBoxNumber.TabIndex = 5; + // + // buttonRemoveFighter + // + buttonRemoveFighter.Location = new Point(37, 155); + buttonRemoveFighter.Name = "buttonRemoveFighter"; + buttonRemoveFighter.Size = new Size(166, 31); + buttonRemoveFighter.TabIndex = 3; + buttonRemoveFighter.Text = "Удалить истребитель"; + buttonRemoveFighter.UseVisualStyleBackColor = true; + buttonRemoveFighter.Click += ButtonRemoveAirFighter_Click; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(37, 209); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(166, 31); + buttonRefreshCollection.TabIndex = 4; + buttonRefreshCollection.Text = "Обновить коллекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // buttonAddFighter + // + buttonAddFighter.Location = new Point(37, 36); + buttonAddFighter.Name = "buttonAddFighter"; + buttonAddFighter.Size = new Size(166, 31); + buttonAddFighter.TabIndex = 2; + buttonAddFighter.Text = "Добавить истребитель "; + buttonAddFighter.UseVisualStyleBackColor = true; + buttonAddFighter.Click += ButtonAddAirFighter_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(734, 448); + pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + pictureBoxCollection.Click += ButtonAddAirFighter_Click; + // + // FormFighterCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(953, 448); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBox1); + Name = "FormFighterCollection"; + Text = "Набор истребителя"; + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBox1; + private PictureBox pictureBoxCollection; + private Button buttonRemoveFighter; + private Button buttonRefreshCollection; + private Button buttonAddFighter; + private MaskedTextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/AirFighter/AirFighter/FormFighterCollection.cs b/AirFighter/AirFighter/FormFighterCollection.cs new file mode 100644 index 0000000..339ac16 --- /dev/null +++ b/AirFighter/AirFighter/FormFighterCollection.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 AirFighter.DrawningObjects; +using AirFighter.Drawnings; +using AirFighter.Generics; +using AirFighter.MovementStrategy; + +namespace AirFighter +{ + public partial class FormFighterCollection : Form + { + private readonly AirFighterGenericCollection _fighter; + public FormFighterCollection() + { + InitializeComponent(); + _fighter = new AirFighterGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void ButtonAddAirFighter_Click(object sender, EventArgs e) + { + FormAirFighter form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_fighter + form.SelectedAirFighter != null) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _fighter.ShowAirFighter(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + + } + + } + + private void ButtonRemoveAirFighter_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_fighter - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _fighter.ShowAirFighter(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _fighter.ShowAirFighter(); + } + } +} diff --git a/AirFighter/AirFighter/FormFighterCollection.resx b/AirFighter/AirFighter/FormFighterCollection.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/AirFighter/AirFighter/FormFighterCollection.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/IMoveableObject.cs b/AirFighter/AirFighter/IMoveableObject.cs index dd3d71e..777a662 100644 --- a/AirFighter/AirFighter/IMoveableObject.cs +++ b/AirFighter/AirFighter/IMoveableObject.cs @@ -14,5 +14,7 @@ namespace AirFighter.MovementStrategy int GetStep { get; } bool CheckCanMove(DirectionAirFighter direction); void MoveObject(DirectionAirFighter direction); + void SetPosition(int x, int y); + void Draw(Graphics g); } } diff --git a/AirFighter/AirFighter/IMoveableObject_Realise.cs b/AirFighter/AirFighter/IMoveableObject_Realise.cs index d2e1ede..3f5238c 100644 --- a/AirFighter/AirFighter/IMoveableObject_Realise.cs +++ b/AirFighter/AirFighter/IMoveableObject_Realise.cs @@ -33,5 +33,19 @@ namespace AirFighter.MovementStrategy _drawningAirFighter?.CanMove(direction) ?? false; public void MoveObject(DirectionAirFighter direction) => _drawningAirFighter?.MoveTransport(direction); + public void SetPosition(int x, int y) + { + if (_drawningAirFighter != null) + { + _drawningAirFighter.SetPosition(x, y); + } + } + public void Draw(Graphics g) + { + if (_drawningAirFighter != null) + { + _drawningAirFighter.DrawTransport(g); + } + } } } diff --git a/AirFighter/AirFighter/MoveToBorder.cs b/AirFighter/AirFighter/MoveToBorder.cs index 9469a5f..e13afe6 100644 --- a/AirFighter/AirFighter/MoveToBorder.cs +++ b/AirFighter/AirFighter/MoveToBorder.cs @@ -6,9 +6,6 @@ using System.Threading.Tasks; namespace AirFighter.MovementStrategy { - /// - /// Стратегия перемещения объекта к границе экрана - /// public class MoveToBorder : AbstractStrategy { protected override bool IsTargetDestinaion() diff --git a/AirFighter/AirFighter/MoveToCenter.cs b/AirFighter/AirFighter/MoveToCenter.cs index 2cd9077..f876ba8 100644 --- a/AirFighter/AirFighter/MoveToCenter.cs +++ b/AirFighter/AirFighter/MoveToCenter.cs @@ -6,9 +6,6 @@ using System.Threading.Tasks; namespace AirFighter.MovementStrategy { - /// - /// Стратегия перемещения объекта в центр экрана - /// public class MoveToCenter : AbstractStrategy { protected override bool IsTargetDestinaion() diff --git a/AirFighter/AirFighter/Program.cs b/AirFighter/AirFighter/Program.cs index 61bec04..0020ca3 100644 --- a/AirFighter/AirFighter/Program.cs +++ b/AirFighter/AirFighter/Program.cs @@ -11,7 +11,7 @@ namespace AirFighter // 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 FormFighterCollection()); } } } \ No newline at end of file diff --git a/AirFighter/AirFighter/SetGeneric.cs b/AirFighter/AirFighter/SetGeneric.cs new file mode 100644 index 0000000..5546ae5 --- /dev/null +++ b/AirFighter/AirFighter/SetGeneric.cs @@ -0,0 +1,56 @@ +namespace AirFighter.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 fighter) + { + for (int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + _places[i] = fighter; + return i; + } + } + return -1; + + } + public int Insert(T fighter, int position) + { + int index = position; + + while (_places[index] != null && index < _places.Length) index++; + + if (index == _places.Length) return -1; + for (int i = index; i > position; --i) _places[i] = _places[i - 1]; + + _places[position] = fighter; + return position; + } + public bool Remove(int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + _places[position] = null; + return true; + } + public T? Get(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + return _places[position]; + } + } +} +