PIbd22_Kuznetsov_A.V._Excav.../Excavator/MashGenericCollection.cs

105 lines
3.6 KiB
C#
Raw Normal View History

2023-12-15 21:34:14 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excavator.DrawingObjects;
using Excavator.MovementStrategy;
namespace Excavator
{
internal class MashGenericCollection<T, U>
where T : DrawingMash
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 120;
private readonly SetGeneric<T> _collection;
public MashGenericCollection(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 +(MashGenericCollection<T, U> collect, T? obj)
{
if (obj != null)
{
return collect._collection.Insert(obj);
}
return -1;
}
public static bool operator -(MashGenericCollection<T, U> collect, int pos)
{
if (collect._collection.Get(pos) == null)
{
return false;
}
return collect?._collection.Remove(pos) ?? false;
}
public int ReturnLength()
{
return _collection.Count;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
public Bitmap ShowMash()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
private void DrawBackground(Graphics gr)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
}
private void DrawObjects(Graphics g)
{
int x = 0;
int y = 0;
int index = -1;
for (int i = 0; i <= _collection.Count; ++i)
{
DrawingMash _Mash = _collection.Get(i);
x = 0;
y = 0;
if (_Mash != null)
{
index = _collection.Count - i;
while ((index - _pictureWidth / _placeSizeWidth) >= 0)
{
y++;
index -= _pictureWidth / _placeSizeWidth;
}
if (index > 0)
{
x += index;
}
x = _pictureWidth / _placeSizeWidth - 1 - x;
_Mash.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
_Mash.DrawTransport(g);
}
}
}
}
}