2023-10-10 22:08:33 +04:00
|
|
|
|
using RoadTrain.Generics;
|
|
|
|
|
using RoadTrain.MovementStrategy;
|
|
|
|
|
using System;
|
|
|
|
|
using RoadTrain.DrawningObjects;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RoadTrain.Generics
|
|
|
|
|
{
|
|
|
|
|
internal class RoadTrainGenericCollection<T, U>
|
|
|
|
|
where T : DrawningRoadTrain
|
|
|
|
|
where U : IMoveableObject
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна прорисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Высота окна прорисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Размер занимаемого объектом места (ширина)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _placeSizeWidth = 110;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Размер занимаемого объектом места (высота)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _placeSizeHeight = 50;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly SetGeneric<T> _collection;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="picWidth"></param>
|
|
|
|
|
/// <param name="picHeight"></param>
|
|
|
|
|
public RoadTrainGenericCollection(int picWidth, int picHeight)
|
|
|
|
|
{
|
|
|
|
|
int width = picWidth / _placeSizeWidth;
|
|
|
|
|
int height = picHeight / _placeSizeHeight;
|
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
|
_collection = new SetGeneric<T>(width * height);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перегрузка оператора сложения
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collect"></param>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int operator +(RoadTrainGenericCollection<T, U> collect, T?
|
|
|
|
|
obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return collect._collection.Insert(obj);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перегрузка оператора вычитания
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collect"></param>
|
|
|
|
|
/// <param name="pos"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static T? operator -(RoadTrainGenericCollection<T, U> collect, int
|
|
|
|
|
pos)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
T? obj = collect._collection[pos];
|
2023-10-10 22:08:33 +04:00
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
collect._collection.Remove(pos);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение объекта IMoveableObject
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pos"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public U? GetU(int pos)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
return (U?)_collection[pos]?.GetMoveableObject;
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вывод всего набора объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Bitmap ShowTrains()
|
|
|
|
|
{
|
|
|
|
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
|
|
|
DrawBackground(gr);
|
|
|
|
|
DrawObjects(gr);
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод отрисовки фона
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="g"></param>
|
|
|
|
|
private void DrawBackground(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
Pen pen = new(Color.Black, 3);
|
|
|
|
|
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 / 2, j *
|
|
|
|
|
_placeSizeHeight);
|
|
|
|
|
}
|
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
|
|
|
|
|
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод прорисовки объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="g"></param>
|
|
|
|
|
private void DrawObjects(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
int width = _pictureWidth / _placeSizeWidth;
|
|
|
|
|
for (int i = 0; i < _collection.Count; i++)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
DrawningRoadTrain? train = _collection[i];
|
|
|
|
|
if (train == null)
|
2023-10-10 22:08:33 +04:00
|
|
|
|
continue;
|
2023-10-24 18:58:46 +04:00
|
|
|
|
train.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight);
|
|
|
|
|
train.DrawTransport(g);
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|