179 lines
6.5 KiB
C#
179 lines
6.5 KiB
C#
namespace Locomotives
|
||
{
|
||
internal class MapWithSetLocomotivesGeneric<T, U>
|
||
where T : class, IDrawningObject
|
||
where U : AbstractMap
|
||
{
|
||
/// <summary>
|
||
/// Ширина окна отрисовки
|
||
/// </summary>
|
||
private readonly int _pictureWidth;
|
||
/// <summary>
|
||
/// Высота окна отрисовки
|
||
/// </summary>
|
||
private readonly int _pictureHeight;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (ширина)
|
||
/// </summary>
|
||
private readonly int _placeSizeWidth = 210;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (высота)
|
||
/// </summary>
|
||
private readonly int _placeSizeHeight = 110;
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
private readonly SetLocomotivesGeneric<T> _setLocomotives;
|
||
/// <summary>
|
||
/// Карта
|
||
/// </summary>
|
||
private readonly U _map;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="picWidth"></param>
|
||
/// <param name="picHeight"></param>
|
||
/// <param name="map"></param>
|
||
public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map)
|
||
{
|
||
int width = picWidth / _placeSizeWidth;
|
||
int height = picHeight / _placeSizeHeight;
|
||
_setLocomotives = new SetLocomotivesGeneric<T>(width * height);
|
||
_pictureWidth = picWidth;
|
||
_pictureHeight = picHeight;
|
||
_map = map;
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора сложения
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="locomotive"></param>
|
||
/// <returns></returns>
|
||
public static int operator +(MapWithSetLocomotivesGeneric<T, U> map, T locomotive)
|
||
{
|
||
return map._setLocomotives.Insert(locomotive);
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора вычитания
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public static int operator -(MapWithSetLocomotivesGeneric<T, U> map, int position)
|
||
{
|
||
return map._setLocomotives.Remove(position);
|
||
}
|
||
/// <summary>
|
||
/// Вывод всего набора объектов
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowSet()
|
||
{
|
||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
DrawBackground(gr);
|
||
DrawLocomotives(gr);
|
||
return bmp;
|
||
}
|
||
/// <summary>
|
||
/// Просмотр объекта на карте
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowOnMap()
|
||
{
|
||
Shaking();
|
||
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||
{
|
||
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
||
}
|
||
return new(_pictureWidth, _pictureHeight);
|
||
}
|
||
/// <summary>
|
||
/// Перемещение объекта по крате
|
||
/// </summary>
|
||
/// <param name="direction"></param>
|
||
/// <returns></returns>
|
||
public Bitmap MoveObject(Direction direction)
|
||
{
|
||
if (_map != null)
|
||
{
|
||
return _map.MoveObject(direction);
|
||
}
|
||
return new(_pictureWidth, _pictureHeight);
|
||
}
|
||
/// <summary>
|
||
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||
/// </summary>
|
||
private void Shaking()
|
||
{
|
||
int j = _setLocomotives.Count - 1;
|
||
for (int i = 0; i < _setLocomotives.Count; i++)
|
||
{
|
||
if (_setLocomotives[i] == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var locomotive = _setLocomotives[j];
|
||
if (locomotive != null)
|
||
{
|
||
_setLocomotives.Insert(locomotive, i);
|
||
_setLocomotives.Remove(j);
|
||
break;
|
||
}
|
||
}
|
||
if (j <= i)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Метод отрисовки фона
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawBackground(Graphics g)
|
||
{
|
||
Pen pen = new(Color.Brown, 3);
|
||
Brush brBackground = new SolidBrush(Color.Yellow);
|
||
g.FillRectangle(brBackground, 0, 0, _pictureWidth, _pictureHeight);
|
||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||
{
|
||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||
{//линия разметки места
|
||
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
|
||
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||
}
|
||
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Метод прорисовки объектов
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawLocomotives(Graphics g)
|
||
{
|
||
int LocomotivesInLine = _pictureWidth / _placeSizeWidth;
|
||
int CurrentVertPos = 20;
|
||
int CurrentHorPos = 0;
|
||
int CurrentLocomotiveNumber = 0;
|
||
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||
{
|
||
_setLocomotives[CurrentLocomotiveNumber]?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight);
|
||
_setLocomotives[CurrentLocomotiveNumber]?.DrawningObject(g);
|
||
if (CurrentHorPos < LocomotivesInLine)
|
||
{
|
||
CurrentHorPos += _placeSizeWidth;
|
||
}
|
||
else
|
||
{
|
||
CurrentHorPos = 0;
|
||
CurrentVertPos += _placeSizeHeight;
|
||
}
|
||
CurrentLocomotiveNumber++;
|
||
}
|
||
}
|
||
}
|
||
}
|