diff --git a/AirBomber/AirBomber/FormMapWithSetAirplanes.cs b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs index 7b45a8d..8e620b7 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs @@ -9,7 +9,6 @@ namespace AirBomber /// private MapWithSetAirplanesGeneric _mapAirplanesCollectionGeneric; private GeneratorAirplane _generatorAirplane; - private AbstractMap? _map; /// /// Конструктор /// diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index f1b5c96..a039593 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -90,7 +90,7 @@ namespace AirBomber Shaking(); for (int i = 0; i < _setAirplanes.Count; i++) { - var airplane = _setAirplanes.Get(i); + var airplane = _setAirplanes[i]; if (airplane != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, airplane); @@ -119,11 +119,11 @@ namespace AirBomber int j = _setAirplanes.Count - 1; for (int i = 0; i < _setAirplanes.Count; i++) { - if (_setAirplanes.Get(i) == null) + if (_setAirplanes[i] == null) { for (; j > i; j--) { - var airplane = _setAirplanes.Get(j); + var airplane = _setAirplanes[j]; if (airplane != null) { _setAirplanes.Insert(airplane, i); @@ -150,7 +150,6 @@ namespace AirBomber for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; j++) { DrawHangar(g, pen, new RectangleF(i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth / 1.8F, _placeSizeHeight / 1.6F)); - // g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); } } } @@ -175,7 +174,7 @@ namespace AirBomber int maxLeft = (countInLine - 1) * _placeSizeWidth; for (int i = 0; i < _setAirplanes.Count; i++) { - var airplane = _setAirplanes.Get(i); + var airplane = _setAirplanes[i]; airplane?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); airplane?.DrawningObject(g); } diff --git a/AirBomber/AirBomber/MapsCollection.cs b/AirBomber/AirBomber/MapsCollection.cs new file mode 100644 index 0000000..f2e3362 --- /dev/null +++ b/AirBomber/AirBomber/MapsCollection.cs @@ -0,0 +1,75 @@ +using AirBomber; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + /// + /// Класс для хранения коллекции карт + /// + 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(_pictureWidth, _pictureHeight, map)); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + /// + /// Доступ к аэродрому + /// + /// + /// + public MapWithSetAirplanesGeneric this[string ind] + { + get + { + _mapStorages.TryGetValue(ind, out var mapWithSetAirplanesGeneric); + return mapWithSetAirplanesGeneric; + } + } + } +} diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index 178d798..7cc4649 100644 --- a/AirBomber/AirBomber/SetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/SetAirplanesGeneric.cs @@ -14,20 +14,23 @@ namespace AirBomber 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 SetAirplanesGeneric(int count) { - _places = new T[count]; + _maxcount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -41,7 +44,7 @@ namespace AirBomber private bool isCorrectPosition(int position) { - return 0 <= position && position < Count; + return 0 <= position && position < _maxcount; } /// /// Добавление объекта в набор на конкретную позицию @@ -51,22 +54,11 @@ namespace AirBomber /// public bool Insert(T airplane, int position) { - int positionNullElement = position; - while (Get(positionNullElement) != null) - { - positionNullElement++; - } - // Если изначальная позиция была некорректной или пустых элементов справа не оказалось возвращаем false - if (!isCorrectPosition(positionNullElement)) + if (!isCorrectPosition(position)) { return false; } - while (positionNullElement != position) // Смещение вправо - { - _places[positionNullElement] = _places[positionNullElement - 1]; - positionNullElement--; - } - _places[position] = airplane; + _places.Insert(position, airplane); return true; } /// @@ -78,7 +70,7 @@ namespace AirBomber { if (!isCorrectPosition(position)) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -86,9 +78,35 @@ namespace AirBomber /// /// /// - public T Get(int position) + public T this[int position] { - return isCorrectPosition(position) ? _places[position] : null; + get + { + return isCorrectPosition(position) && position < Count ? _places[position] : null; + } + set + { + Insert(value, position); + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetAirplanes() + { + foreach (var airplane in _places) + { + if (airplane != null) + { + yield return airplane; + } + else + { + yield break; + } + } + } } }