163 lines
5.2 KiB
C#
163 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ContainerShip
|
|
{
|
|
internal class MapWithSetShipGeneric<T, U>
|
|
where T : class, IDrawingObject
|
|
where U : AbstractMap
|
|
{
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
private readonly int _placeSizeWidth = 210;
|
|
|
|
private readonly int _placeSizeHeight = 90;
|
|
|
|
private readonly SetShipGeneric<T> _setShip;
|
|
|
|
private readonly U _map;
|
|
|
|
public MapWithSetShipGeneric(int picWidth, int picHeight, U map)
|
|
{
|
|
int width = picWidth / _placeSizeWidth;
|
|
int height = picHeight / _placeSizeHeight;
|
|
_setShip = new SetShipGeneric<T>(width * height);
|
|
_pictureWidth = picWidth;
|
|
_pictureHeight = picHeight;
|
|
_map = map;
|
|
}
|
|
|
|
public static int operator +(MapWithSetShipGeneric<T, U> map, T ship)
|
|
{
|
|
return map._setShip.Insert(ship);
|
|
}
|
|
|
|
public static T operator -(MapWithSetShipGeneric<T, U> map, int
|
|
position)
|
|
{
|
|
return map._setShip.Remove(position);
|
|
}
|
|
|
|
public Bitmap ShowSet()
|
|
{
|
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
DrawBackground(gr);
|
|
DrawShip(gr);
|
|
return bmp;
|
|
}
|
|
|
|
public Bitmap ShowOnMap()
|
|
{
|
|
Shaking();
|
|
foreach (var ship in _setShip.GetShip())
|
|
{
|
|
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
|
|
}
|
|
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 = _setShip.Count - 1;
|
|
for (int i = 0; i < _setShip.Count; i++)
|
|
{
|
|
if (_setShip[i] == null)
|
|
{
|
|
for (; j > i; j--)
|
|
{
|
|
var ship = _setShip[j];
|
|
if (ship != null)
|
|
{
|
|
_setShip.Insert(ship, i);
|
|
_setShip.Remove(j);
|
|
break;
|
|
}
|
|
}
|
|
if (j <= i)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DrawBackground(Graphics g)
|
|
{
|
|
SolidBrush brWater = new SolidBrush(Color.LightSkyBlue);
|
|
g.FillRectangle(brWater, 0,0,_pictureWidth, _pictureHeight);
|
|
|
|
Pen pen = new(Color.Brown, 3);
|
|
Pen penBold = new(Color.Brown, 5);
|
|
Pen penThin = new(Color.Black, 1);
|
|
|
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
|
{
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight; ++j)
|
|
{
|
|
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2, i *
|
|
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight + _placeSizeHeight / 2);
|
|
|
|
for(int k = 1; k < 3; ++k)
|
|
{
|
|
g.DrawLine(penThin, i * _placeSizeWidth, j * _placeSizeHeight + k * 21, i *
|
|
_placeSizeWidth + 3 * _placeSizeWidth / 10, j * _placeSizeHeight + k * 21);
|
|
}
|
|
|
|
for (int k = 0; k < 4; ++k)
|
|
{
|
|
g.DrawLine(penBold, i * _placeSizeWidth + k * _placeSizeWidth / 10, j * _placeSizeHeight + 20, i * _placeSizeWidth + k * _placeSizeWidth / 10,
|
|
(j + 1) * _placeSizeHeight - 20);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private void DrawShip(Graphics g)
|
|
{
|
|
int yNumOfPlaces = _pictureHeight / _placeSizeHeight;
|
|
int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
|
|
|
|
int RowIndex = yNumOfPlaces - 1;
|
|
int ColumnIndex = xNumOfPlaces - 1;
|
|
|
|
foreach (var ship in _setShip.GetShip())
|
|
{
|
|
if (ship != null)
|
|
{
|
|
(float Left, float Top, float Right, float Bottom) = ship.GetCurrentPosition();
|
|
ship.SetObject(ColumnIndex * _placeSizeWidth,
|
|
RowIndex * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)),
|
|
_pictureWidth, _pictureHeight);
|
|
ship.DrawingObject(g);
|
|
}
|
|
if (ColumnIndex == 0)
|
|
{
|
|
ColumnIndex = xNumOfPlaces - 1;
|
|
RowIndex--;
|
|
}
|
|
else
|
|
{
|
|
ColumnIndex--;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|