142 lines
4.6 KiB
C#
142 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AirBomber
|
|
{
|
|
internal class MapWithSetAirBomberGeneric<T, U>
|
|
where T : class, IDrawningObject
|
|
where U : AbstractMap
|
|
{
|
|
private readonly int _pictureWidth;
|
|
private readonly int _pictureHeight;
|
|
private readonly int _placeSizeWidth = 210;
|
|
private readonly int _placeSizeHeight = 120;
|
|
private readonly SetAirBomberGeneric<T> _setAirBomber;
|
|
private readonly U _map;
|
|
|
|
public MapWithSetAirBomberGeneric(int picWidth, int picHeight, U map)
|
|
{
|
|
int width = picWidth / _placeSizeWidth;
|
|
int height = picHeight / _placeSizeHeight;
|
|
_setAirBomber = new SetAirBomberGeneric<T>(width * height);
|
|
_pictureWidth = picWidth;
|
|
_pictureHeight = picHeight;
|
|
_map = map;
|
|
}
|
|
|
|
public static int operator +(MapWithSetAirBomberGeneric<T, U> map, T airBomber)
|
|
{
|
|
return map._setAirBomber.Insert(airBomber);
|
|
}
|
|
|
|
public static T operator -(MapWithSetAirBomberGeneric<T, U> map, int position)
|
|
{
|
|
return map._setAirBomber.Remove(position);
|
|
}
|
|
|
|
public Bitmap ShowSet()
|
|
{
|
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
DrawBackground(gr);
|
|
DrawAirBomber(gr);
|
|
return bmp;
|
|
}
|
|
|
|
public Bitmap ShowOnMap()
|
|
{
|
|
Shaking();
|
|
foreach (var airBomber in _setAirBomber.GetAirBomber())
|
|
{
|
|
return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
|
|
}
|
|
return new(_pictureWidth, _pictureHeight);
|
|
}
|
|
|
|
public Bitmap MoveObject(Direction direction)
|
|
{
|
|
if (_map != null)
|
|
{
|
|
return _map.MoveObject(direction);
|
|
}
|
|
return new(_pictureWidth, _pictureHeight);
|
|
}
|
|
|
|
public string GetData(char separatorType, char separatorData)
|
|
{
|
|
string data = $"{_map.GetType().Name}{separatorType}";
|
|
foreach (var airBomber in _setAirBomber.GetAirBomber())
|
|
{
|
|
data += $"{airBomber.GetInfo()}{separatorData}";
|
|
}
|
|
return data;
|
|
}
|
|
|
|
public void LoadData(string[] records)
|
|
{
|
|
foreach (var rec in records)
|
|
{
|
|
_setAirBomber.Insert(DrawningObjectBomber.Create(rec) as T);
|
|
}
|
|
}
|
|
|
|
private void Shaking()
|
|
{
|
|
int j = _setAirBomber.Count - 1;
|
|
for (int i = 0; i < _setAirBomber.Count; i++)
|
|
{
|
|
if (_setAirBomber[i] == null)
|
|
{
|
|
for (; j > i; j--)
|
|
{
|
|
var airBomber = _setAirBomber[j];
|
|
if (airBomber != null)
|
|
{
|
|
_setAirBomber.Insert(airBomber, i);
|
|
_setAirBomber.Remove(j);
|
|
break;
|
|
}
|
|
}
|
|
if (j <= i)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void DrawBackground(Graphics g)
|
|
{
|
|
Pen pen = new(Color.DarkGreen, 3);
|
|
SolidBrush brush = new(Color.DarkGreen);
|
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i)
|
|
{
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight; ++j)
|
|
{
|
|
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
|
g.DrawPie(pen, new Rectangle(i * _placeSizeWidth - 25, j * _placeSizeHeight, 50, 120), -90, 180);
|
|
g.FillPie(brush, new Rectangle((i + 1) * _placeSizeWidth - _placeSizeHeight - 10, j * _placeSizeHeight, 50, 120), -90, 180);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DrawAirBomber(Graphics g)
|
|
{
|
|
int i = 0;
|
|
foreach (var airBomber in _setAirBomber.GetAirBomber())
|
|
{
|
|
if (airBomber != null)
|
|
{
|
|
_setAirBomber[i].SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
|
airBomber.DrawningObject(g);
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
}
|