PIbd-22_Kashin_M.I_Gasoline.../GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs

139 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class MapWithSetGasolienTankerGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;
private readonly int _placeSizeHeight = 90;
private readonly SetGasolineTankerGeneric<T> _setGasolineTanker;
private readonly U _map;
public MapWithSetGasolienTankerGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setGasolineTanker = new SetGasolineTankerGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static int operator +(MapWithSetGasolienTankerGeneric<T, U> map, T gasolineTanker)
{
return map._setGasolineTanker.Insert(gasolineTanker);
}
public static T operator -(MapWithSetGasolienTankerGeneric<T, U> map, int position)
{
return map._setGasolineTanker.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawGasolineTanker(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
foreach (var gasolineTanker in _setGasolineTanker.GetGasolineTanker())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker);
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var gasolineTanker in _setGasolineTanker.GetGasolineTanker())
{
data += $"{gasolineTanker.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setGasolineTanker.Insert(DrawingObjectGasolineTanker.Create(rec) as T);
}
}
private void Shaking()
{
int j = _setGasolineTanker.Count - 1;
for (int i = 0; i < _setGasolineTanker.Count; i++)
{
if (_setGasolineTanker[i] == null)
{
for (; j > i; j--)
{
var gasolineTanker = _setGasolineTanker[j];
if (gasolineTanker != null)
{
_setGasolineTanker.Insert(gasolineTanker, i);
_setGasolineTanker.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 5);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth + 10, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2);
g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20);
g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20);
}
}
}
private void DrawGasolineTanker(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int i = 0;
foreach (var gasolineTanker in _setGasolineTanker.GetGasolineTanker())
{
gasolineTanker.SetObject(i % width * _placeSizeWidth+10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
gasolineTanker.DrawningObject(g);
i++;
}
}
}
}