PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/MapWithSetTraktorGeneric.cs

140 lines
4.5 KiB
C#
Raw Normal View History

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;
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();
2022-11-29 01:53:02 +04:00
foreach (var traktor in _setTraktors.GetTraktor())
{
2022-11-29 01:53:02 +04:00
return _map.CreateMap(_pictureWidth, _pictureHeight, traktor);
}
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++)
{
if (_setTraktors[i] == null)
{
for (; j > i; j--)
{
var bus = _setTraktors[j];
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)
{
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-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-12-12 23:38:47 +04:00
if (curWidth < widthEl)
curWidth++;
else
{
2022-12-12 23:38:47 +04:00
curWidth = 1;
curHeight++;
}
2022-12-12 23:38:47 +04:00
if (curHeight > heightEl)
{
2022-12-12 23:38:47 +04:00
return;
}
}
}
}
}