ISEbd-22_Baygulov_A.A._Elec.../ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs
2023-12-11 00:17:08 +04:00

96 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.MovementStrategy;
namespace ProjectElectricLocomotive.Generics
{
internal class LocomotivesGenericCollection<T, U>
where T : DrawingLocomotive
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 130;
private readonly SetGeneric<T> _collection;
public LocomotivesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(LocomotivesGenericCollection<T, U> collect, T? locomotive)
{
if (locomotive == null)
{
return -1;
}
return collect._collection.Insert(locomotive);
}
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
public U? GetU(int pos)
{
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowLocomotives()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
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);
}
}
private void DrawObjects(Graphics g)
{
int HLoco = _pictureHeight / _placeSizeHeight;
int Wloco = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++)
{
T? type = _collection[i];
if (type != null)
{
type.SetPosition(
(int)(i / HLoco * _placeSizeWidth),
(HLoco - 1) * _placeSizeHeight - (int)(i % HLoco * _placeSizeHeight)
);
type?.DrawTransport(g);
}
}
}
/// <summary>
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetLocomotives => _collection.GetLocomotives();
}
}