PIbd22_NikiforovaMV_Contain.../ContainerShip/ShipsGenericCollection.cs
2023-12-16 11:16:37 +04:00

96 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContainerShip.Generic;
using ContainerShip.MovementStrategy;
using ContainerShip.DrawningObjects;
namespace ContainerShip.Generic
{
internal class ShipsGenericCollection<T, U>
where T : DrawningShip
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;
private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection;
public ShipsGenericCollection(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 +(ShipsGenericCollection<T, U> collect, T? obj)
{
if (obj != null)
{
return collect._collection.Insert(obj);
}
return -1;
}
public static bool operator -(ShipsGenericCollection<T, U> collect, int pos)
{
if (collect._collection[pos] == null)
{
return false;
}
return collect?._collection.Remove(pos) ?? false;
}
public U? GetU(int pos)
{
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowShips()
{
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 x = _pictureWidth / _placeSizeWidth - 1;
int y = _pictureHeight / _placeSizeHeight - 1;
for (int i = 0; i < _collection.Count; i++)
{
DrawningShip _ship = _collection[i];
if (_ship != null) {
if (x < 0)
{
x = _pictureWidth / _placeSizeWidth - 1;
y--;
}
_ship.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
_ship.DrawTransport(g);
x--;
}
}
}
}
}