Лабораторная работа 4. Второй этап: коллекция карт + правки для первого этапа.

This commit is contained in:
Андрей Абазов 2022-10-18 12:13:43 +04:00
parent 7d300205a3
commit 3a0b45fdff
3 changed files with 103 additions and 9 deletions

View File

@ -88,13 +88,9 @@ namespace AirBomber
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
for (int i = 0; i < _setAirBombers.Count; i++) foreach (var airBomber in _setAirBombers.GetAirBombers())
{ {
var airBomber = _setAirBombers[i]; return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
if (airBomber != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
}
} }
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
@ -163,10 +159,12 @@ namespace AirBomber
private void DrawAirBombers(Graphics g) private void DrawAirBombers(Graphics g)
{ {
int numOfObjectsInRow = _pictureWidth / _placeSizeWidth; int numOfObjectsInRow = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _setAirBombers.Count; i++) int i = 0;
foreach (var airBomber in _setAirBombers.GetAirBombers())
{ {
_setAirBombers[i]?.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight, _pictureWidth, _pictureHeight); airBomber.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setAirBombers[i]?.DrawingObject(g); airBomber.DrawingObject(g);
i++;
} }
} }
} }

View File

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class MapsCollection
{
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetAirBombersGeneric<DrawingObjectAirBomber, 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, MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name))
{
MessageBox.Show("Карта с таким названием уже существует!");
return;
}
_mapStorages.Add(name, new MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
if (_mapStorages.ContainsKey(name))
{
_mapStorages.Remove(name);
}
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap> this[string ind]
{
get
{
if (_mapStorages.ContainsKey(ind)) return _mapStorages[ind];
return null;
}
}
}
}

View File

@ -82,5 +82,24 @@ namespace AirBomber
if (position < 0 || position >= _maxCount) Insert(value, position); if (position < 0 || position >= _maxCount) Insert(value, position);
} }
} }
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetAirBombers()
{
foreach (var airBomber in _places)
{
if (airBomber != null)
{
yield return airBomber;
}
else
{
yield break;
}
}
}
} }
} }