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>
|
2022-12-23 01:49:51 +04:00
|
|
|
|
where T : class, IDrawningObject, IEquatable<T>
|
2022-11-14 21:25:33 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 01:49:12 +04:00
|
|
|
|
public static int operator +(MapWithSetTraktorGeneric<T, U> map, T traktor)
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-12-13 01:49:12 +04:00
|
|
|
|
return map._setTraktors.Insert(traktor);
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2022-11-29 01:53:02 +04:00
|
|
|
|
foreach (var traktor in _setTraktors.GetTraktor())
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-11-29 01:53:02 +04:00
|
|
|
|
return _map.CreateMap(_pictureWidth, _pictureHeight, traktor);
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
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-12-13 01:49:12 +04:00
|
|
|
|
var traktor = _setTraktors[j];
|
|
|
|
|
if (traktor != null)
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-12-13 01:49:12 +04:00
|
|
|
|
_setTraktors.Insert(traktor, i);
|
2022-11-14 21:25:33 +04:00
|
|
|
|
_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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2022-11-29 01:53:02 +04:00
|
|
|
|
|
2022-12-12 23:38:47 +04:00
|
|
|
|
int widthEl = _pictureWidth / _placeSizeWidth;
|
|
|
|
|
int heightEl = _pictureHeight / _placeSizeHeight;
|
|
|
|
|
int curWidth = 1;
|
|
|
|
|
int curHeight = 0;
|
2022-11-29 01:53:02 +04:00
|
|
|
|
foreach (var traktor in _setTraktors.GetTraktor())
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-12-12 23:38:47 +04:00
|
|
|
|
traktor?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 130,
|
|
|
|
|
curHeight * _placeSizeHeight + 20, _pictureWidth, _pictureHeight);
|
2022-11-29 01:53:02 +04:00
|
|
|
|
traktor?.DrawningObject(g);
|
2022-11-14 21:25:33 +04:00
|
|
|
|
|
2022-12-12 23:38:47 +04:00
|
|
|
|
if (curWidth < widthEl)
|
|
|
|
|
curWidth++;
|
|
|
|
|
else
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-12-12 23:38:47 +04:00
|
|
|
|
curWidth = 1;
|
|
|
|
|
curHeight++;
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
2022-12-12 23:38:47 +04:00
|
|
|
|
if (curHeight > heightEl)
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-12-12 23:38:47 +04:00
|
|
|
|
return;
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-13 01:49:12 +04:00
|
|
|
|
|
|
|
|
|
public string GetData(char separatorType, char separatorData)
|
|
|
|
|
{
|
|
|
|
|
string data = $"{_map.GetType().Name}{separatorType}";
|
|
|
|
|
foreach (var traktor in _setTraktors.GetTraktor())
|
|
|
|
|
{
|
|
|
|
|
data += $"{traktor.GetInfo()}{separatorData}";
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadData(string[] records)
|
|
|
|
|
{
|
|
|
|
|
foreach (var rec in records)
|
|
|
|
|
{
|
|
|
|
|
_setTraktors.Insert(DrawningObjectTractor.Create(rec) as T);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|