2022-11-14 21:25:33 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WinFormsApp1
|
|
|
|
|
{
|
|
|
|
|
internal class MapWithSetTraktorGeneric<T, U>
|
|
|
|
|
where T : class, IDrawningObject
|
|
|
|
|
where U : AbstractMap
|
|
|
|
|
{
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
private readonly int _pictureHeight;
|
2022-11-28 22:39:41 +04:00
|
|
|
|
private readonly int _placeSizeWidth = 250;
|
|
|
|
|
private readonly int _placeSizeHeight = 250;
|
2022-11-14 21:25:33 +04:00
|
|
|
|
private readonly SetTraktorGeneric<T> _setTraktors;
|
|
|
|
|
private readonly U _map;
|
|
|
|
|
|
|
|
|
|
public MapWithSetTraktorGeneric(int picWidth, int picHeight, U map)
|
|
|
|
|
{
|
|
|
|
|
int width = picWidth / _placeSizeWidth;
|
|
|
|
|
int height = picHeight / _placeSizeHeight;
|
|
|
|
|
_setTraktors = new SetTraktorGeneric<T>(width * height);
|
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
|
_map = map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int operator +(MapWithSetTraktorGeneric<T, U> map, T bus)
|
|
|
|
|
{
|
|
|
|
|
return map._setTraktors.Insert(bus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T operator -(MapWithSetTraktorGeneric<T, U> map, int position)
|
|
|
|
|
{
|
|
|
|
|
return map._setTraktors.Remove(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bitmap ShowSet()
|
|
|
|
|
{
|
|
|
|
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
|
|
|
DrawBackground(gr);
|
|
|
|
|
DrawTraktors(gr);
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bitmap ShowOnMap()
|
|
|
|
|
{
|
|
|
|
|
Shaking();
|
|
|
|
|
for (int i = 0; i < _setTraktors.Count; i++)
|
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
var bus = _setTraktors[i];
|
2022-11-14 21:25:33 +04:00
|
|
|
|
if (bus != null)
|
|
|
|
|
{
|
|
|
|
|
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new(_pictureWidth, _pictureHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bitmap MoveObject(Direction direction)
|
|
|
|
|
{
|
|
|
|
|
if (_map != null)
|
|
|
|
|
{
|
|
|
|
|
return _map.MoveObject(direction);
|
|
|
|
|
}
|
|
|
|
|
return new(_pictureWidth, _pictureHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Shaking()
|
|
|
|
|
{
|
|
|
|
|
int j = _setTraktors.Count - 1;
|
|
|
|
|
for (int i = 0; i < _setTraktors.Count; i++)
|
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
if (_setTraktors[i] == null)
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
|
|
|
|
for (; j > i; j--)
|
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
var bus = _setTraktors[j];
|
2022-11-14 21:25:33 +04:00
|
|
|
|
if (bus != null)
|
|
|
|
|
{
|
|
|
|
|
_setTraktors.Insert(bus, i);
|
|
|
|
|
_setTraktors.Remove(j);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (j <= i)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrawBackground(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
Pen pen = new(Color.Black, 3);
|
|
|
|
|
Brush brush = new SolidBrush(Color.LightSlateGray);
|
|
|
|
|
g.FillRectangle(brush, 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, j * _placeSizeHeight);
|
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10);
|
|
|
|
|
}
|
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrawTraktors(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
int widthEl = _pictureWidth / _placeSizeWidth;
|
|
|
|
|
int heightEl = _pictureHeight / _placeSizeHeight;
|
|
|
|
|
|
|
|
|
|
int curWidth = 0;
|
|
|
|
|
int curHeight = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = _setTraktors.Count; i >= 0; i--)
|
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
_setTraktors[i]?.SetObject(
|
2022-11-28 22:39:41 +04:00
|
|
|
|
_pictureWidth - _placeSizeWidth * curWidth - 60,
|
2022-11-14 21:25:33 +04:00
|
|
|
|
curHeight * _placeSizeHeight + 30, _pictureWidth, _pictureHeight);
|
2022-11-29 01:44:43 +04:00
|
|
|
|
_setTraktors[i]?.DrawningObject(g);
|
2022-11-14 21:25:33 +04:00
|
|
|
|
|
|
|
|
|
if (curWidth < widthEl)
|
|
|
|
|
curWidth++;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
curWidth = 1;
|
|
|
|
|
curHeight++;
|
|
|
|
|
}
|
|
|
|
|
if (curHeight > heightEl)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|