Изменения из 3 базовой части

This commit is contained in:
Данияр Аглиуллов 2022-10-01 03:20:21 +04:00
parent e46d5da591
commit 9619209384
4 changed files with 118 additions and 27 deletions

View File

@ -9,7 +9,6 @@ namespace AirBomber
/// </summary>
private MapWithSetAirplanesGeneric<DrawningObject, AbstractMap> _mapAirplanesCollectionGeneric;
private GeneratorAirplane<EntityAirplane, IAirplaneEngines> _generatorAirplane;
private AbstractMap? _map;
/// <summary>
/// Конструктор
/// </summary>

View File

@ -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);
}

View File

@ -0,0 +1,75 @@
using AirBomber;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
/// <summary>
/// Класс для хранения коллекции карт
/// </summary>
internal class MapsCollection
{
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetAirplanesGeneric<DrawningObject,
AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string,
MapWithSetAirplanesGeneric<DrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
_mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map));
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
_mapStorages.Remove(name);
}
/// <summary>
/// Доступ к аэродрому
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetAirplanesGeneric<DrawningObject, AbstractMap> this[string ind]
{
get
{
_mapStorages.TryGetValue(ind, out var mapWithSetAirplanesGeneric);
return mapWithSetAirplanesGeneric;
}
}
}
}

View File

@ -14,20 +14,23 @@ namespace AirBomber
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
private readonly int _maxcount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetAirplanesGeneric(int count)
{
_places = new T[count];
_maxcount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -41,7 +44,7 @@ namespace AirBomber
private bool isCorrectPosition(int position)
{
return 0 <= position && position < Count;
return 0 <= position && position < _maxcount;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -51,22 +54,11 @@ namespace AirBomber
/// <returns></returns>
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;
}
/// <summary>
@ -78,7 +70,7 @@ namespace AirBomber
{
if (!isCorrectPosition(position))
return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
/// <summary>
@ -86,9 +78,35 @@ namespace AirBomber
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
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);
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetAirplanes()
{
foreach (var airplane in _places)
{
if (airplane != null)
{
yield return airplane;
}
else
{
yield break;
}
}
}
}
}