diff --git a/Cruiser/Cruiser/AbstractStrategy.cs b/Cruiser/Cruiser/AbstractStrategy.cs index 966de1f..9d40e09 100644 --- a/Cruiser/Cruiser/AbstractStrategy.cs +++ b/Cruiser/Cruiser/AbstractStrategy.cs @@ -46,7 +46,7 @@ namespace ProjectCruiser.MovementStrategy } MoveToTarget(); } - + protected bool MoveLeft() => MoveTo(DirectionType.Left); protected bool MoveRight() => MoveTo(DirectionType.Right); diff --git a/Cruiser/Cruiser/CruiserGenericCollection.cs b/Cruiser/Cruiser/CruiserGenericCollection.cs index 0f2fb48..ff07fa3 100644 --- a/Cruiser/Cruiser/CruiserGenericCollection.cs +++ b/Cruiser/Cruiser/CruiserGenericCollection.cs @@ -8,7 +8,6 @@ using System.Threading.Tasks; namespace ProjectCruiser { - internal class CruiserGenericCollection where T : DrawningCruiser where U : IMoveableObject @@ -43,17 +42,17 @@ namespace ProjectCruiser public static bool operator -(CruiserGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } return false; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowCruiser() @@ -86,16 +85,18 @@ namespace ProjectCruiser { int Ix = 3; int Iy = 15; - for (int i = 0; i < _collection.Count - 1; i++) + int i = 0; + foreach (var cruiser in _collection.GetCruiser()) { - _collection.Get(i)?.SetPosition(Ix, Iy); - _collection.Get(i)?.DrawTransport(g); + _collection[i]?.SetPosition(Ix, Iy); + _collection[i]?.DrawTransport(g); Ix += _placeSizeWidth; if (Ix + _placeSizeHeight > _pictureWidth) { Ix = 0; Iy = _placeSizeHeight; } + i++; } } } diff --git a/Cruiser/Cruiser/CruiserGenericStorage.cs b/Cruiser/Cruiser/CruiserGenericStorage.cs new file mode 100644 index 0000000..4504d49 --- /dev/null +++ b/Cruiser/Cruiser/CruiserGenericStorage.cs @@ -0,0 +1,59 @@ +using ProjectCruiser.DrawningObjects; +using ProjectCruiser.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser +{ + internal class CruiserGenericStorage + { + readonly Dictionary> _CruiserStorages; + + public List Keys => _CruiserStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + public CruiserGenericStorage(int pictureWidth, int pictureHeight) + { + _CruiserStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(string name) + { + if (!_CruiserStorages.ContainsKey(name)) + { + var ustaCollection = new CruiserGenericCollection(_pictureWidth, _pictureHeight); + _CruiserStorages.Add(name, ustaCollection); + } + } + + public void DelSet(string name) + { + if (_CruiserStorages.ContainsKey(name)) + { + _CruiserStorages.Remove(name); + } + } + + public CruiserGenericCollection? this[string ind] + { + get + { + if (_CruiserStorages.ContainsKey(ind)) + { + return _CruiserStorages[ind]; + } + return null; + } + } + } +} diff --git a/Cruiser/Cruiser/DrawningCruiser.cs b/Cruiser/Cruiser/DrawningCruiser.cs index bcbea50..788c48d 100644 --- a/Cruiser/Cruiser/DrawningCruiser.cs +++ b/Cruiser/Cruiser/DrawningCruiser.cs @@ -29,7 +29,7 @@ namespace ProjectCruiser.DrawningObjects public DrawningCruiser(int speed, double weight, Color bodyColor, int width, int height) - { + { _pictureWidth = width; _pictureHeight = height; EntityCruiser = new EntityCruiser(speed, weight, bodyColor); @@ -63,7 +63,7 @@ namespace ProjectCruiser.DrawningObjects } public IMoveableObject GetMoveableObject => new DrawningObjectCruiser(this); - + protected int PictureWidth { get { return _pictureWidth; } @@ -131,7 +131,6 @@ namespace ProjectCruiser.DrawningObjects return; } Pen pen = new(Color.Black); - Brush BodyColor = new SolidBrush(EntityCruiser.BodyColor); GraphicsPath path1 = new GraphicsPath(); diff --git a/Cruiser/Cruiser/DrawningCruiserDou.cs b/Cruiser/Cruiser/DrawningCruiserDou.cs index d4ac836..ee35dfc 100644 --- a/Cruiser/Cruiser/DrawningCruiserDou.cs +++ b/Cruiser/Cruiser/DrawningCruiserDou.cs @@ -29,7 +29,6 @@ namespace ProjectCruiser.DrawningObjects return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(cruiserDou.AdditionalColor); Brush brBlack = new SolidBrush(Color.Black); @@ -50,7 +49,9 @@ namespace ProjectCruiser.DrawningObjects g.FillEllipse(additionalBrush, _startPosX + 8, _startPosY + 18, 15, 12); g.DrawEllipse(pen, _startPosX + 8, _startPosY + 33, 15, 12); g.FillEllipse(additionalBrush, _startPosX + 8, _startPosY + 33, 15, 12); + } + } } } diff --git a/Cruiser/Cruiser/FormCruiser.cs b/Cruiser/Cruiser/FormCruiser.cs index 3832fa2..332c9ba 100644 --- a/Cruiser/Cruiser/FormCruiser.cs +++ b/Cruiser/Cruiser/FormCruiser.cs @@ -5,7 +5,6 @@ namespace ProjectCruiser { public partial class FormCruiser : Form { - private DrawningCruiser? _drawningCruiser; private AbstractStrategy? _abstractStrategy; @@ -59,7 +58,7 @@ namespace ProjectCruiser _drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,100)); Draw(); } - + private void ButtonCreateCruiser_Click(object sender, EventArgs e) { Random random = new(); diff --git a/Cruiser/Cruiser/FormCruiserCollection.Designer.cs b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs index 3c68c72..442245f 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.Designer.cs +++ b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs @@ -28,90 +28,151 @@ /// private void InitializeComponent() { - groupBox1 = new GroupBox(); - maskedTextBoxNumber = new MaskedTextBox(); - ButtonRefreshCollection = new Button(); - ButtonRemoveCruiser = new Button(); - buttonAddCruiser = new Button(); - pictureBoxCollection = new PictureBox(); - groupBox1.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); - SuspendLayout(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.listBoxStorage = new System.Windows.Forms.ListBox(); + this.textBoxStorageName = new System.Windows.Forms.MaskedTextBox(); + this.ButtonAddObject = new System.Windows.Forms.Button(); + this.ButtonDelObject = new System.Windows.Forms.Button(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.ButtonRefreshCollection = new System.Windows.Forms.Button(); + this.ButtonRemoveCruiser = new System.Windows.Forms.Button(); + this.buttonAddCruiser = new System.Windows.Forms.Button(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.SuspendLayout(); // // groupBox1 // - groupBox1.Controls.Add(maskedTextBoxNumber); - groupBox1.Controls.Add(ButtonRefreshCollection); - groupBox1.Controls.Add(ButtonRemoveCruiser); - groupBox1.Controls.Add(buttonAddCruiser); - groupBox1.Location = new Point(844, 12); - groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(196, 319); - groupBox1.TabIndex = 0; - groupBox1.TabStop = false; - groupBox1.Text = "Инструменты"; + this.groupBox1.Controls.Add(this.groupBox2); + this.groupBox1.Controls.Add(this.maskedTextBoxNumber); + this.groupBox1.Controls.Add(this.ButtonRefreshCollection); + this.groupBox1.Controls.Add(this.ButtonRemoveCruiser); + this.groupBox1.Controls.Add(this.buttonAddCruiser); + this.groupBox1.Location = new System.Drawing.Point(643, 12); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(196, 436); + this.groupBox1.TabIndex = 0; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Инструменты"; + // + // groupBox2 + // + this.groupBox2.Controls.Add(this.listBoxStorage); + this.groupBox2.Controls.Add(this.textBoxStorageName); + this.groupBox2.Controls.Add(this.ButtonAddObject); + this.groupBox2.Controls.Add(this.ButtonDelObject); + this.groupBox2.Location = new System.Drawing.Point(9, 24); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(179, 256); + this.groupBox2.TabIndex = 4; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Наборы"; + // + // listBoxStorage + // + this.listBoxStorage.FormattingEnabled = true; + this.listBoxStorage.ItemHeight = 15; + this.listBoxStorage.Location = new System.Drawing.Point(6, 93); + this.listBoxStorage.Name = "listBoxStorage"; + this.listBoxStorage.Size = new System.Drawing.Size(167, 109); + this.listBoxStorage.TabIndex = 5; + listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; + // + // textBoxStorageName + // + this.textBoxStorageName.Font = new System.Drawing.Font("Lucida Sans Unicode", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.textBoxStorageName.Location = new System.Drawing.Point(6, 22); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(167, 26); + this.textBoxStorageName.TabIndex = 4; + // + // ButtonAddObject + // + this.ButtonAddObject.Location = new System.Drawing.Point(6, 54); + this.ButtonAddObject.Name = "ButtonAddObject"; + this.ButtonAddObject.Size = new System.Drawing.Size(167, 33); + this.ButtonAddObject.TabIndex = 3; + this.ButtonAddObject.Text = "Добавить набор"; + this.ButtonAddObject.UseVisualStyleBackColor = true; + this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // + // ButtonDelObject + // + this.ButtonDelObject.Location = new System.Drawing.Point(6, 217); + this.ButtonDelObject.Name = "ButtonDelObject"; + this.ButtonDelObject.Size = new System.Drawing.Size(167, 33); + this.ButtonDelObject.TabIndex = 2; + this.ButtonDelObject.Text = "Удалить набор"; + this.ButtonDelObject.UseVisualStyleBackColor = true; + this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); // // maskedTextBoxNumber // - maskedTextBoxNumber.Font = new Font("Lucida Sans Unicode", 9F, FontStyle.Regular, GraphicsUnit.Point); - maskedTextBoxNumber.Location = new Point(29, 78); - maskedTextBoxNumber.Mask = "00"; - maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(130, 26); - maskedTextBoxNumber.TabIndex = 3; + this.maskedTextBoxNumber.Font = new System.Drawing.Font("Lucida Sans Unicode", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(34, 325); + this.maskedTextBoxNumber.Mask = "00"; + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(130, 26); + this.maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(6, 195); - ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - ButtonRefreshCollection.Size = new Size(184, 33); - ButtonRefreshCollection.TabIndex = 2; - ButtonRefreshCollection.Text = "Обовить коллекцию"; - ButtonRefreshCollection.UseVisualStyleBackColor = true; - ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + this.ButtonRefreshCollection.Location = new System.Drawing.Point(6, 396); + this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + this.ButtonRefreshCollection.Size = new System.Drawing.Size(184, 33); + this.ButtonRefreshCollection.TabIndex = 2; + this.ButtonRefreshCollection.Text = "Обовить коллекцию"; + this.ButtonRefreshCollection.UseVisualStyleBackColor = true; + this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); // // ButtonRemoveCruiser // - ButtonRemoveCruiser.Location = new Point(6, 136); - ButtonRemoveCruiser.Name = "ButtonRemoveCruiser"; - ButtonRemoveCruiser.Size = new Size(184, 33); - ButtonRemoveCruiser.TabIndex = 1; - ButtonRemoveCruiser.Text = "Удалить Крейсер"; - ButtonRemoveCruiser.UseVisualStyleBackColor = true; - ButtonRemoveCruiser.Click += ButtonRemoveCruiser_Click; + this.ButtonRemoveCruiser.Location = new System.Drawing.Point(6, 357); + this.ButtonRemoveCruiser.Name = "ButtonRemoveCruiser"; + this.ButtonRemoveCruiser.Size = new System.Drawing.Size(184, 33); + this.ButtonRemoveCruiser.TabIndex = 1; + this.ButtonRemoveCruiser.Text = "Удалить крейсер"; + this.ButtonRemoveCruiser.UseVisualStyleBackColor = true; + this.ButtonRemoveCruiser.Click += new System.EventHandler(this.ButtonRemoveCruiser_Click); // // buttonAddCruiser // - buttonAddCruiser.Location = new Point(6, 22); - buttonAddCruiser.Name = "buttonAddCruiser"; - buttonAddCruiser.Size = new Size(184, 33); - buttonAddCruiser.TabIndex = 0; - buttonAddCruiser.Text = "Добавить Крейсер"; - buttonAddCruiser.UseVisualStyleBackColor = true; - buttonAddCruiser.Click += buttonAddCruiser_Click; + this.buttonAddCruiser.Location = new System.Drawing.Point(6, 286); + this.buttonAddCruiser.Name = "buttonAddCruiser"; + this.buttonAddCruiser.Size = new System.Drawing.Size(184, 33); + this.buttonAddCruiser.TabIndex = 0; + this.buttonAddCruiser.Text = "Добавить крейсер"; + this.buttonAddCruiser.UseVisualStyleBackColor = true; + this.buttonAddCruiser.Click += new System.EventHandler(this.buttonAddCruiser_Click); // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(0, -1); - pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(838, 432); - pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; - pictureBoxCollection.TabIndex = 1; - pictureBoxCollection.TabStop = false; + this.pictureBoxCollection.Location = new System.Drawing.Point(0, -1); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(637, 449); + this.pictureBoxCollection.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; + this.pictureBoxCollection.TabIndex = 1; + this.pictureBoxCollection.TabStop = false; // // FormCruiserCollection // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1062, 489); - Controls.Add(pictureBoxCollection); - Controls.Add(groupBox1); - Name = "FormCruiserCollection"; - Text = "FormCruiserCollection"; - groupBox1.ResumeLayout(false); - groupBox1.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); - ResumeLayout(false); + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(851, 458); + this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.groupBox1); + this.Name = "FormCruiserCollection"; + this.Text = "FormCruiserCollection"; + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.groupBox2.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.ResumeLayout(false); + } #endregion @@ -122,5 +183,10 @@ private Button ButtonRemoveCruiser; private Button buttonAddCruiser; private PictureBox pictureBoxCollection; + private GroupBox groupBox2; + private ListBox listBoxStorage; + private MaskedTextBox textBoxStorageName; + private Button ButtonAddObject; + private Button ButtonDelObject; } } \ No newline at end of file diff --git a/Cruiser/Cruiser/FormCruiserCollection.cs b/Cruiser/Cruiser/FormCruiserCollection.cs index 84bf114..707dcd2 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.cs +++ b/Cruiser/Cruiser/FormCruiserCollection.cs @@ -14,23 +14,87 @@ namespace ProjectCruiser { public partial class FormCruiserCollection : Form { - private readonly CruiserGenericCollection _cruiser; + private readonly CruiserGenericStorage _storage; public FormCruiserCollection() { InitializeComponent(); - _cruiser = new CruiserGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new CruiserGenericStorage(pictureBoxCollection.Width, + pictureBoxCollection.Height); + } + + private void ReloadObjects() + { + int index = listBoxStorage.SelectedIndex; + listBoxStorage.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorage.Items.Add(_storage.Keys[i]); + } + if (listBoxStorage.Items.Count > 0 && (index == -1 || index + >= listBoxStorage.Items.Count)) + { + listBoxStorage.SelectedIndex = 0; + } + else if (listBoxStorage.Items.Count > 0 && index > -1 && + index < listBoxStorage.Items.Count) + { + listBoxStorage.SelectedIndex = index; + } + } + + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + + private void listBoxStorage_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowCruiser(); + } + + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorage.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } } private void buttonAddCruiser_Click(object sender, EventArgs e) { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormCruiser form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_cruiser + form.SelectedCruiser > -1) + if (obj + form.SelectedCruiser > -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _cruiser.ShowCruiser(); + pictureBoxCollection.Image = obj.ShowCruiser(); } else { @@ -41,15 +105,26 @@ namespace ProjectCruiser private void ButtonRemoveCruiser_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_cruiser - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _cruiser.ShowCruiser(); + pictureBoxCollection.Image = obj.ShowCruiser(); } else { @@ -59,7 +134,17 @@ namespace ProjectCruiser private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _cruiser.ShowCruiser(); + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowCruiser(); } } } diff --git a/Cruiser/Cruiser/FormCruiserCollection.resx b/Cruiser/Cruiser/FormCruiserCollection.resx index af32865..f298a7b 100644 --- a/Cruiser/Cruiser/FormCruiserCollection.resx +++ b/Cruiser/Cruiser/FormCruiserCollection.resx @@ -1,64 +1,4 @@ - - - + diff --git a/Cruiser/Cruiser/IMoveableObject.cs b/Cruiser/Cruiser/IMoveableObject.cs index c91449a..40c4ebe 100644 --- a/Cruiser/Cruiser/IMoveableObject.cs +++ b/Cruiser/Cruiser/IMoveableObject.cs @@ -9,11 +9,10 @@ namespace ProjectCruiser.MovementStrategy { public interface IMoveableObject { - ObjectParameters? GetObjectPosition { get; } int GetStep { get; } - + bool CheckCanMove(DirectionType direction); void MoveObject(DirectionType direction); diff --git a/Cruiser/Cruiser/IMoveableObject_Realise.cs b/Cruiser/Cruiser/IMoveableObject_Realise.cs index 5b463a8..23c742e 100644 --- a/Cruiser/Cruiser/IMoveableObject_Realise.cs +++ b/Cruiser/Cruiser/IMoveableObject_Realise.cs @@ -9,7 +9,6 @@ using ProjectCruiser.Drawnings; namespace ProjectCruiser.MovementStrategy { - public class DrawningObjectCruiser : IMoveableObject { private readonly DrawningCruiser? _drawningCruiser = null; diff --git a/Cruiser/Cruiser/ObjectParameters.cs b/Cruiser/Cruiser/ObjectParameters.cs index a0605ff..6e9810e 100644 --- a/Cruiser/Cruiser/ObjectParameters.cs +++ b/Cruiser/Cruiser/ObjectParameters.cs @@ -6,25 +6,27 @@ using System.Threading.Tasks; namespace ProjectCruiser.MovementStrategy { + /// + /// Параметры-координаты объекта + /// public class ObjectParameters { private readonly int _x; private readonly int _y; private readonly int _width; private readonly int _height; - public int LeftBorder => _x; public int TopBorder => _y; - + public int RightBorder => _x + _width; public int DownBorder => _y + _height; public int ObjectMiddleHorizontal => _x + _width / 2; - + public int ObjectMiddleVertical => _y + _height / 2; - + public ObjectParameters(int x, int y, int width, int height) { _x = x; diff --git a/Cruiser/Cruiser/SetGeneric.cs b/Cruiser/Cruiser/SetGeneric.cs index 6f5a3c4..997af79 100644 --- a/Cruiser/Cruiser/SetGeneric.cs +++ b/Cruiser/Cruiser/SetGeneric.cs @@ -9,16 +9,18 @@ namespace ProjectCruiser internal class SetGeneric where T : class { + private readonly List _places; - private readonly T?[] _places; + public int Count => _places.Count; - public int Count => _places.Length; + private readonly int _maxCount; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } - + public int Insert(T cruiser) { return Insert(cruiser, 0); @@ -26,51 +28,49 @@ namespace ProjectCruiser public int Insert(T cruiser, int position) { - if (position < 0 && position > Count) - { + + if (position < 0 || position >= _maxCount) 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]; - } - } - _places[position] = cruiser; + if (Count >= _maxCount) + return -1; + _places.Insert(position, cruiser); return position; } public bool Remove(int position) { - if (position < 0 || position >= _places.Length) - { - return false; - } - _places[position] = null; + + if ((position < 0) || (position > _maxCount)) return false; + _places.RemoveAt(position); return true; } - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= _places.Length) + get { - return null; + if (position < 0 || position > _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position > _maxCount) + return; + _places[position] = value; + } + } + + public IEnumerable GetCruiser(int? maxCruiser = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCruiser.HasValue && i == maxCruiser.Value) + { + yield break; + } } - return _places[position]; } } }