2022-09-30 17:24:13 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Artilleries
|
|
|
|
|
{
|
2022-10-02 16:55:05 +04:00
|
|
|
|
internal class MapWithSetArtilleriesGeneric<T, U>
|
2022-11-21 20:27:15 +04:00
|
|
|
|
where T : class, IDrawingObject, IEquatable<T>
|
2022-09-30 17:24:13 +04:00
|
|
|
|
where U : AbstractMap
|
|
|
|
|
{
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
private readonly int _placeSizeWidth = 210;
|
|
|
|
|
private readonly int _placeSizeHeight = 90;
|
|
|
|
|
private readonly SetArtilleriesGeneric<T> _setArtilleries;
|
|
|
|
|
private readonly U _map;
|
|
|
|
|
|
2022-10-02 16:55:05 +04:00
|
|
|
|
public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map)
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
|
|
|
|
int width = picWidth / _placeSizeWidth;
|
|
|
|
|
int height = picHeight / _placeSizeHeight;
|
|
|
|
|
_setArtilleries = new SetArtilleriesGeneric<T>(width * height);
|
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
|
_map = map;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 16:01:05 +04:00
|
|
|
|
public static int operator +(MapWithSetArtilleriesGeneric<T, U> map, T artillery)
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
2022-10-03 16:01:05 +04:00
|
|
|
|
return map._setArtilleries.Insert(artillery);
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 16:01:05 +04:00
|
|
|
|
public static T operator -(MapWithSetArtilleriesGeneric<T, U> map, int position)
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
2022-10-03 16:01:05 +04:00
|
|
|
|
return map._setArtilleries.Remove(position);
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-02 16:55:05 +04:00
|
|
|
|
public Bitmap ShowSet()
|
|
|
|
|
{
|
|
|
|
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
|
|
|
DrawBackground(gr);
|
|
|
|
|
DrawArtilleries(gr);
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 17:24:13 +04:00
|
|
|
|
public Bitmap ShowOnMap()
|
|
|
|
|
{
|
|
|
|
|
Shaking();
|
2022-10-09 23:07:19 +04:00
|
|
|
|
foreach (var artillery in _setArtilleries.GetArtilleries())
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
2022-10-09 23:07:19 +04:00
|
|
|
|
return _map.CreateMap(_pictureWidth, _pictureHeight, artillery);
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
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 = _setArtilleries.Count - 1;
|
|
|
|
|
for (int i = 0; i < _setArtilleries.Count; i++)
|
|
|
|
|
{
|
2022-10-09 23:07:19 +04:00
|
|
|
|
if (_setArtilleries[i] == null)
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
|
|
|
|
for (; j > i; j--)
|
|
|
|
|
{
|
2022-10-09 23:07:19 +04:00
|
|
|
|
var car = _setArtilleries[j];
|
2022-09-30 17:24:13 +04:00
|
|
|
|
if (car != null)
|
|
|
|
|
{
|
|
|
|
|
_setArtilleries.Insert(car, i);
|
|
|
|
|
_setArtilleries.Remove(j);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (j <= i)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrawBackground(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
Pen pen = new(Color.Black, 3);
|
2022-10-03 00:55:54 +04:00
|
|
|
|
Brush boxBrush = new SolidBrush(Color.DarkGreen);
|
|
|
|
|
Pen thinPen = new Pen(Color.Black, 2);
|
|
|
|
|
Brush flagBrush = new SolidBrush(Color.Red);
|
2022-09-30 17:24:13 +04:00
|
|
|
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
2022-10-03 00:55:54 +04:00
|
|
|
|
{
|
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
|
|
|
|
g.FillRectangle(boxBrush, i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5, _placeSizeWidth / 3 - 5, _placeSizeHeight / 3 - 5);
|
|
|
|
|
g.DrawLine(thinPen, i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 + _placeSizeHeight / 3 - 10);
|
|
|
|
|
g.DrawLine(thinPen, i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 + _placeSizeHeight / 3 - 10, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5);
|
|
|
|
|
g.FillRectangle(flagBrush, i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight * 5 / 8 - 5, _placeSizeWidth / 5, _placeSizeHeight / 5);
|
|
|
|
|
g.DrawLine(thinPen, i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight - 5, i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight * 5 / 8 - 5);
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
2022-10-03 00:55:54 +04:00
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrawArtilleries(Graphics g)
|
|
|
|
|
{
|
2022-10-02 17:42:23 +04:00
|
|
|
|
int width = _pictureWidth / _placeSizeWidth;
|
|
|
|
|
int height = _pictureHeight / _placeSizeHeight;
|
|
|
|
|
|
2022-10-09 23:07:19 +04:00
|
|
|
|
int index = 0;
|
|
|
|
|
foreach (var artillery in _setArtilleries.GetArtilleries())
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
2022-10-09 23:07:19 +04:00
|
|
|
|
artillery.SetObject(index % width * _placeSizeWidth + 10, (height - 1 - index / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
|
|
|
|
artillery.DrawingObject(g);
|
|
|
|
|
index++;
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-04 16:49:23 +04:00
|
|
|
|
|
|
|
|
|
public string GetData(char separatorType, char separatorData)
|
|
|
|
|
{
|
|
|
|
|
string data = $"{_map.GetType().Name}{separatorType}";
|
2022-11-05 03:24:53 +04:00
|
|
|
|
foreach (var artillery in _setArtilleries.GetArtilleries().Reverse())
|
2022-11-04 16:49:23 +04:00
|
|
|
|
{
|
|
|
|
|
data += $"{artillery.GetInfo()}{separatorData}";
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadData(string[] records)
|
|
|
|
|
{
|
|
|
|
|
foreach (var record in records)
|
|
|
|
|
{
|
|
|
|
|
_setArtilleries.Insert(DrawingObjectArtillery.Create(record) as T);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-21 20:27:15 +04:00
|
|
|
|
|
|
|
|
|
public void Sort(IComparer<T> comparer)
|
|
|
|
|
{
|
|
|
|
|
_setArtilleries.SortSet(comparer);
|
|
|
|
|
}
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|