PIbd-23_Lisov_N.A._AirFight.../AirFighter/MapWithSetAircraftsGeneric.cs

150 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class MapWithSetAircraftsGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 145;
private readonly int _placeSizeHeight = 145;
private readonly SetAircraftsGeneric<T> _setAircrafts;
private readonly U _map;
public MapWithSetAircraftsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setAircrafts = new SetAircraftsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static int operator +(MapWithSetAircraftsGeneric<T, U> map, T aircraft)
{
return map._setAircrafts.Insert(aircraft);
}
public static T operator -(MapWithSetAircraftsGeneric<T, U> map, int position)
{
return map._setAircrafts.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawAircrafts(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setAircrafts.Count; i++)
{
var aircraft = _setAircrafts.Get(i);
if (aircraft != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, aircraft);
}
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
private void Shaking()
{
int j = _setAircrafts.Count - 1;
for (int i = 0; i < _setAircrafts.Count; i++)
{
if (_setAircrafts.Get(i) == null)
{
for (; j > i; j--)
{
var aircraft = _setAircrafts.Get(i);
if (aircraft != null)
{
_setAircrafts.Insert(aircraft, i);
_setAircrafts.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 2);
for (int i = 0; i < _pictureWidth / _placeSizeWidth + 1; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
{
g.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + 135, _pictureWidth - 80, j * _placeSizeHeight + 135);
g.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, i * _placeSizeWidth + _placeSizeWidth /2 + 20, j * _placeSizeHeight + 45);
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight + 45, i * _placeSizeWidth + _placeSizeWidth + 20 , j * _placeSizeHeight + 60);
g.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + 135, i * _placeSizeWidth + 20, j * _placeSizeHeight + 60);
}
}
}
private void DrawAircrafts(Graphics g)
{
int curX = _pictureWidth / _placeSizeWidth - 1;
int curY = _pictureHeight / _placeSizeHeight - 1;
for (int i = 0; i < _setAircrafts.Count; i++)
{
_setAircrafts.Get(i)?.SetObject(curX * _placeSizeWidth + 30, curY * _placeSizeHeight + 40, _pictureWidth, _pictureHeight);
_setAircrafts.Get(i)?.DrawingObject(g);
if (curX <= 0)
{
curX = _pictureWidth / _placeSizeWidth - 1;
curY--;
}
else
{
curX--;
}
}
}
}
}