PIbd-21_Yaruskin_S.A_AirBom.../AirBomber/BomberGenericCollection.cs
2023-11-28 00:26:29 +04:00

108 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.DrawningObjects;
using AirBomber.MovementStrategy;
using ProjectBomber.Generics;
namespace AirBomber.Generics
{
internal class BomberGenericCollection<T, U>
where T : DrawningBomber
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 155;
private readonly int _placeSizeHeight = 185;
private readonly SetGeneric<T> _collection;
public BomberGenericCollection(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 +(BomberGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
}
return collect._collection.Insert(obj);
}
public static bool operator -(BomberGenericCollection<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 ShowBomber()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
private void DrawBackground(Graphics g)
{
Pen pen = new Pen(Color.Black, 3);
int numColumns = _pictureWidth / _placeSizeWidth;
int numRows = _pictureHeight / _placeSizeHeight;
for (int i = 0; i <= numColumns; i++)
{
for (int j = 0; j <= numRows; ++j)
{
int x = i * _placeSizeWidth;
int y = j * _placeSizeHeight;
g.DrawLine(pen, x, y, x + _placeSizeWidth / 2, y);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, numRows * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
int numColumns = _pictureWidth / _placeSizeWidth;
int numRows = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
DrawningBomber air = _collection.Get(i);
if (air != null)
{
int row = i / numColumns;
int column = numColumns - 1 - (i % numColumns);
int x = column * _placeSizeWidth;
int y = row * _placeSizeHeight;
air.SetPosition(x, y);
air.DrawBomber(g);
}
}
}
}
}