145 lines
4.6 KiB
C#
145 lines
4.6 KiB
C#
|
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;
|
|||
|
private readonly int _placeSizeWidth = 180;
|
|||
|
private readonly int _placeSizeHeight = 150;
|
|||
|
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++)
|
|||
|
{
|
|||
|
var bus = _setTraktors.Get(i);
|
|||
|
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++)
|
|||
|
{
|
|||
|
if (_setTraktors.Get(i) == null)
|
|||
|
{
|
|||
|
for (; j > i; j--)
|
|||
|
{
|
|||
|
var bus = _setTraktors.Get(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)
|
|||
|
{
|
|||
|
int widthEl = _pictureWidth / _placeSizeWidth;
|
|||
|
int heightEl = _pictureHeight / _placeSizeHeight;
|
|||
|
|
|||
|
int curWidth = 0;
|
|||
|
int curHeight = 0;
|
|||
|
|
|||
|
for (int i = _setTraktors.Count; i >= 0; i--)
|
|||
|
{
|
|||
|
_setTraktors.Get(i)?.SetObject(
|
|||
|
_pictureWidth - _placeSizeWidth * curWidth - 20,
|
|||
|
curHeight * _placeSizeHeight + 30, _pictureWidth, _pictureHeight);
|
|||
|
_setTraktors.Get(i)?.DrawningObject(g);
|
|||
|
|
|||
|
if (curWidth < widthEl)
|
|||
|
curWidth++;
|
|||
|
else
|
|||
|
{
|
|||
|
curWidth = 1;
|
|||
|
curHeight++;
|
|||
|
}
|
|||
|
if (curHeight > heightEl)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|