diff --git a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs index a2115ac..7c008e4 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.Designer.cs @@ -34,14 +34,20 @@ this.buttonRemoveAntiAircraftGun = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddAntiAircraftGun = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); this.groupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.groupBoxMaps.SuspendLayout(); this.SuspendLayout(); // // groupBox @@ -51,22 +57,21 @@ this.groupBox.Controls.Add(this.buttonRemoveAntiAircraftGun); this.groupBox.Controls.Add(this.maskedTextBoxPosition); this.groupBox.Controls.Add(this.buttonAddAntiAircraftGun); - this.groupBox.Controls.Add(this.comboBoxSelectorMap); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonRight); this.groupBox.Controls.Add(this.buttonLeft); this.groupBox.Controls.Add(this.buttonUp); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(562, 0); + this.groupBox.Location = new System.Drawing.Point(574, 0); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(238, 450); + this.groupBox.Size = new System.Drawing.Size(238, 589); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(34, 309); + this.buttonShowOnMap.Location = new System.Drawing.Point(20, 465); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(194, 29); this.buttonShowOnMap.TabIndex = 16; @@ -76,7 +81,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(34, 245); + this.buttonShowStorage.Location = new System.Drawing.Point(20, 430); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(194, 29); this.buttonShowStorage.TabIndex = 15; @@ -86,7 +91,7 @@ // // buttonRemoveAntiAircraftGun // - this.buttonRemoveAntiAircraftGun.Location = new System.Drawing.Point(34, 177); + this.buttonRemoveAntiAircraftGun.Location = new System.Drawing.Point(20, 395); this.buttonRemoveAntiAircraftGun.Name = "buttonRemoveAntiAircraftGun"; this.buttonRemoveAntiAircraftGun.Size = new System.Drawing.Size(194, 29); this.buttonRemoveAntiAircraftGun.TabIndex = 14; @@ -96,7 +101,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(34, 144); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 362); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(194, 27); @@ -104,7 +109,7 @@ // // buttonAddAntiAircraftGun // - this.buttonAddAntiAircraftGun.Location = new System.Drawing.Point(34, 95); + this.buttonAddAntiAircraftGun.Location = new System.Drawing.Point(20, 327); this.buttonAddAntiAircraftGun.Name = "buttonAddAntiAircraftGun"; this.buttonAddAntiAircraftGun.Size = new System.Drawing.Size(194, 29); this.buttonAddAntiAircraftGun.TabIndex = 12; @@ -112,25 +117,12 @@ this.buttonAddAntiAircraftGun.UseVisualStyleBackColor = true; this.buttonAddAntiAircraftGun.Click += new System.EventHandler(this.ButtonAddAntiAircraftGun_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Простая карта 2"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(34, 40); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(194, 28); - this.comboBoxSelectorMap.TabIndex = 11; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); - // // buttonDown // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(120, 408); + this.buttonDown.Location = new System.Drawing.Point(108, 547); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 29); this.buttonDown.TabIndex = 10; @@ -142,7 +134,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(155, 408); + this.buttonRight.Location = new System.Drawing.Point(143, 547); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 29); this.buttonRight.TabIndex = 9; @@ -154,7 +146,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(83, 408); + this.buttonLeft.Location = new System.Drawing.Point(71, 547); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 29); this.buttonLeft.TabIndex = 8; @@ -166,27 +158,91 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AntiAircraftGun.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(120, 370); + this.buttonUp.Location = new System.Drawing.Point(108, 509); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 29); this.buttonUp.TabIndex = 7; this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Простая карта 2"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(14, 59); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(194, 28); + this.comboBoxSelectorMap.TabIndex = 11; + // // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(562, 450); + this.pictureBox.Size = new System.Drawing.Size(574, 589); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.buttonAddMap); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(580, 26); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(226, 277); + this.groupBoxMaps.TabIndex = 17; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(14, 126); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(194, 104); + this.listBoxMaps.TabIndex = 14; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(14, 236); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(194, 29); + this.buttonDeleteMap.TabIndex = 13; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(14, 93); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(194, 29); + this.buttonAddMap.TabIndex = 12; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(14, 26); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(194, 27); + this.textBoxNewMapName.TabIndex = 0; + // // FormMapWithSetAntiAircraftGuns // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(812, 589); + this.Controls.Add(this.groupBoxMaps); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Name = "FormMapWithSetAntiAircraftGuns"; @@ -194,6 +250,8 @@ this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); this.ResumeLayout(false); } @@ -212,5 +270,10 @@ private MaskedTextBox maskedTextBoxPosition; private Button buttonAddAntiAircraftGun; private ComboBox comboBoxSelectorMap; + private GroupBox groupBoxMaps; + private ListBox listBoxMaps; + private Button buttonDeleteMap; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs index 939321f..1dc659b 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs @@ -14,41 +14,92 @@ namespace AntiAircraftGun public partial class FormMapWithSetAntiAircraftGuns : Form { /// - /// Объект от класса карты с набором объектов + /// Словарь для выпадающего списка /// - private MapWithSetAntiAircraftGunsGeneric _mapAntiAircraftGunsCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap()}, { "Простая карта2", new SimpleMap2()} + }; /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; /// Конструктор /// public FormMapWithSetAntiAircraftGuns() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } + } + /// + /// Заполнение listBoxMaps + /// + private void ReloadMaps() + { + int index = listBoxMaps.SelectedIndex; + listBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) + { + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); + } + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) + { + listBoxMaps.SelectedIndex = 0; + } + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) + { + listBoxMaps.SelectedIndex = index; + } + } + /// + /// Добавление карты + /// + /// + /// + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); + ReloadMaps(); } /// /// Выбор карты /// /// /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// + /// Удаление карты + /// + /// + /// + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Простая карта 2": - map = new SimpleMap2(); - break; + return; } - if (map != null) + if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _mapAntiAircraftGunsCollectionGeneric = new MapWithSetAntiAircraftGunsGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapAntiAircraftGunsCollectionGeneric = null; + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); } } /// @@ -56,10 +107,9 @@ namespace AntiAircraftGun /// /// /// - /// private void ButtonAddAntiAircraftGun_Click(object sender, EventArgs e) { - if (_mapAntiAircraftGunsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -67,10 +117,10 @@ namespace AntiAircraftGun if (form.ShowDialog() == DialogResult.OK) { DrawingObjectAntiAircraftGun antiAircraftGun = new(form.SelectedAntiAircraftGun); - if (_mapAntiAircraftGunsCollectionGeneric + antiAircraftGun >= 0) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + antiAircraftGun >= 0) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapAntiAircraftGunsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -85,6 +135,10 @@ namespace AntiAircraftGun /// private void ButtonRemoveAntiAircraftGun_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -94,10 +148,10 @@ namespace AntiAircraftGun return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapAntiAircraftGunsCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapAntiAircraftGunsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -111,11 +165,11 @@ namespace AntiAircraftGun /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapAntiAircraftGunsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAntiAircraftGunsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -124,11 +178,11 @@ namespace AntiAircraftGun /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapAntiAircraftGunsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAntiAircraftGunsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -137,7 +191,7 @@ namespace AntiAircraftGun /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapAntiAircraftGunsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -159,7 +213,7 @@ namespace AntiAircraftGun dir = Direction.Right; break; } - pictureBox.Image = _mapAntiAircraftGunsCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } } diff --git a/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs b/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs index 75f7020..a4b8b30 100644 --- a/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs +++ b/AntiAircraftGun/AntiAircraftGun/MapWithSetAntiAircraftGunsGeneric.cs @@ -93,13 +93,9 @@ namespace AntiAircraftGun public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setAntiAircraftGuns.Count; i++) + foreach (var antiAircraftGun in _setAntiAircraftGuns.GetAntiAircraftGuns()) { - var antiAircraftGun = _setAntiAircraftGuns.Get(i); - if (antiAircraftGun != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, antiAircraftGun); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, antiAircraftGun); } return new(_pictureWidth, _pictureHeight); } @@ -124,11 +120,11 @@ namespace AntiAircraftGun int j = _setAntiAircraftGuns.Count - 1; for (int i = 0; i < _setAntiAircraftGuns.Count; i++) { - if (_setAntiAircraftGuns.Get(i) == null) + if (_setAntiAircraftGuns[i] == null) { for (; j > i; j--) { - var antiAircraftGun = _setAntiAircraftGuns.Get(j); + var antiAircraftGun = _setAntiAircraftGuns[j]; if (antiAircraftGun != null) { _setAntiAircraftGuns.Insert(antiAircraftGun, i); @@ -168,10 +164,10 @@ namespace AntiAircraftGun { int xPositionInStorage = _placeSizeWidth / 10; int yPositionInStorage = _placeSizeHeight / 10; - for (int i = 0; i < _setAntiAircraftGuns.Count; i++) + foreach (var antiAircraftGun in _setAntiAircraftGuns.GetAntiAircraftGuns()) { - _setAntiAircraftGuns.Get(i)?.SetObject(xPositionInStorage, yPositionInStorage, _pictureWidth, _pictureHeight); - _setAntiAircraftGuns.Get(i)?.DrawingObjectAntiAircraftGun(g); + antiAircraftGun.SetObject(xPositionInStorage, yPositionInStorage, _pictureWidth, _pictureHeight); + antiAircraftGun.DrawingObjectAntiAircraftGun(g); xPositionInStorage += _placeSizeWidth; if (xPositionInStorage > _pictureWidth - _placeSizeWidth) { diff --git a/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs new file mode 100644 index 0000000..7d61c5c --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/MapsCollection.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun +{ + internal class MapsCollection + { + /// + /// Словарь (хранилище) с картами + /// + readonly Dictionary> _mapStorages; + /// + /// Возвращение списка названий карт + /// + public List Keys => _mapStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление карты + /// + /// Название карты + /// Карта + public void AddMap(string name, AbstractMap map) + { + _mapStorages.Add(name, new MapWithSetAntiAircraftGunsGeneric(_pictureWidth, _pictureHeight, map)); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetAntiAircraftGunsGeneric this[string ind] + { + get + { + if (_mapStorages.TryGetValue(ind, out var map)) return map; + return null; + } + } + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs b/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs index 36fbfde..1270d41 100644 --- a/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs +++ b/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs @@ -14,36 +14,37 @@ namespace AntiAircraftGun where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + private readonly int _maxCount; /// /// Конструктор /// /// public SetAntiAircraftGunsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор /// - /// Добавляемый автомобиль + /// Добавляемое орудие /// public int Insert(T antiAircraftGun) { - if (_places[Count - 1] == null) + if (_places.Count < _maxCount) { - for(int i = Count - 1; i > 0; i--) + _places.Add(antiAircraftGun); + for (int i = 0; i < _places.Count; i++) { - _places[i] = _places[i - 1]; + if (_places[i] == antiAircraftGun) return i; } - _places[0] = antiAircraftGun; - return 0; } return -1; } @@ -55,25 +56,9 @@ namespace AntiAircraftGun /// public int Insert(T antiAircraftGun, int position) { - if (position < 0 || position >= Count) return -1; - if (_places[position] == null) - { - _places[position] = antiAircraftGun; - return position; - } - else - { - if (_places[Count - 1] == null) - { - for (int i = Count - 1; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = antiAircraftGun; - return position; - } - return -1; - } + if (position < 0 || position > _places.Count) return -1; + _places.Insert(position, antiAircraftGun); + return position; } /// /// Удаление объекта из набора с конкретной позиции @@ -82,31 +67,47 @@ namespace AntiAircraftGun /// public T Remove(int position) { - if (position < 0 || position >= Count) return null; - if (_places[position] != null) - { - T removed = _places[position]; - _places[position] = null; - if (position < Count - 1) - { - for (int k = position; k < Count - 1; k++) - { - _places[k] = _places[k + 1]; - } - } - return removed; - } - return null; + if (position < 0 || position > _places.Count) return null; + if (_places[position] == null) return null; + T removed = _places[position]; + _places.RemoveAt(position); + return removed; } /// /// Получение объекта из набора по позиции /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) return null; - return _places[position]; + get + { + if (position < 0 || position > _places.Count) return null; + return _places[position]; + } + set + { + if (position < 0 || position > _places.Count) return; + _places.Insert(position, value); + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetAntiAircraftGuns() + { + foreach (var antiAircraftGun in _places) + { + if (antiAircraftGun != null) + { + yield return antiAircraftGun; + } + else + { + yield break; + } + } } } }