PIbd-22_Fedorenko_G.Y._Hydr.../Hydroplane/PlanesGenericCollection.cs

94 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hydroplane.DrawningObjects;
using Hydroplane.MovementStrategy;
namespace Hydroplane.Generics
{
internal class PlanesGenericCollection<T, U>
where T : DrawningPlane
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 175;
private readonly int _placeSizeHeight = 85;
private readonly SetGeneric<T> _collection;
public PlanesGenericCollection(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 +(PlanesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
public static bool operator -(PlanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj == null)
{
return false;
}
return collect._collection.Remove(pos);
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
public Bitmap ShowPlanes()
{
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)
{
for (int i = 0; i < _collection.Count; i++)
{
T? t = _collection.Get(i);
if (t != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t is DrawningPlane) (t as DrawningPlane).DrawTransport(g);
else t.DrawTransport(g);
}
}
}
}
}