PIbd-21_KozyrevSS_GasolineT.../Lab/CarsGenericCollection.cs

95 lines
3.1 KiB
C#
Raw Permalink Normal View History

2023-10-04 12:52:06 +04:00
using Lab.Generics;
using Lab.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lab.DrawningObjects;
namespace Lab.Generics
{
internal class CarsGenericCollection<T, U>
where T : DrawTanker
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 110;
private readonly int _placeSizeHeight = 80;
private readonly SetGeneric<T> _collection;
2023-11-15 10:25:49 +04:00
public IEnumerable<T?> GetCars => _collection.GetCars();
2023-10-04 12:52:06 +04:00
public CarsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
2023-10-18 12:24:01 +04:00
public static bool operator +(CarsGenericCollection<T, U> collect, T?
2023-10-04 12:52:06 +04:00
obj)
{
if (obj == null)
{
2023-10-18 12:24:01 +04:00
return false;
2023-10-04 12:52:06 +04:00
}
2023-10-18 12:24:01 +04:00
return (bool)collect?._collection.Insert(obj);
2023-10-04 12:52:06 +04:00
}
2023-10-18 12:24:01 +04:00
public static T? operator -(CarsGenericCollection<T, U> collect, int pos)
2023-10-04 12:52:06 +04:00
{
2023-10-18 12:24:01 +04:00
T? obj = collect._collection[pos];
if (obj != null)
2023-10-04 12:52:06 +04:00
{
2023-10-18 12:24:01 +04:00
collect._collection.Remove(pos);
2023-10-04 12:52:06 +04:00
}
2023-10-18 12:24:01 +04:00
return obj;
2023-10-04 12:52:06 +04:00
}
public U? GetU(int pos)
{
2023-10-18 12:24:01 +04:00
return (U?)_collection[pos]?.GetMoveableObject;
2023-10-04 12:52:06 +04:00
}
public Bitmap ShowCars()
{
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 width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
2023-10-18 12:24:01 +04:00
int i = 0;
foreach (var tank in _collection.GetCars())
2023-10-04 12:52:06 +04:00
{
2023-10-18 12:24:01 +04:00
if (tank != null)
{
tank.SetPosition(i % (width) * _placeSizeWidth, (height - i / width - 1) * _placeSizeHeight);
tank.DrawTransport(g);
}
i++;
2023-10-04 12:52:06 +04:00
}
}
2023-10-18 12:24:01 +04:00
}
}