187 lines
6.8 KiB
C#
187 lines
6.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Catamaran
|
||
{
|
||
/// <summary>
|
||
/// Карта с набром объектов под нее
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <typeparam name="U"></typeparam>
|
||
internal class MapWithSetBoatsGeneric<T, U>
|
||
where T : class, IDrawingObject
|
||
where U : AbstractMap
|
||
{
|
||
/// <summary>
|
||
/// Ширина окна отрисовки
|
||
/// </summary>
|
||
private readonly int _pictureWidth;
|
||
/// <summary>
|
||
/// Высота окна отрисовки
|
||
/// </summary>
|
||
private readonly int _pictureHeight;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (ширина)
|
||
/// </summary>
|
||
private readonly int _placeSizeWidth = 210;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (высота)
|
||
/// </summary>
|
||
private readonly int _placeSizeHeight = 90;
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
private readonly SetBoatsGeneric<T> _setBoats;
|
||
/// <summary>
|
||
/// Карта
|
||
/// </summary>
|
||
private readonly U _map;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="picWidth"></param>
|
||
/// <param name="picHeight"></param>
|
||
/// <param name="map"></param>
|
||
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map)
|
||
{
|
||
int width = picWidth / _placeSizeWidth;
|
||
int height = picHeight / _placeSizeHeight;
|
||
_setBoats = new SetBoatsGeneric<T>(width * height);
|
||
_pictureWidth = picWidth;
|
||
_pictureHeight = picHeight;
|
||
_map = map;
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора сложения
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="Boat"></param>
|
||
/// <returns></returns>
|
||
public static int operator +(MapWithSetBoatsGeneric<T, U> map, T Boat)
|
||
{
|
||
return map._setBoats.Insert(Boat);
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора вычитания
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public static T operator -(MapWithSetBoatsGeneric<T, U> map, int position)
|
||
{
|
||
return map._setBoats.Remove(position);
|
||
}
|
||
/// <summary>
|
||
/// Вывод всего набора объектов
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowSet()
|
||
{
|
||
Bitmap bmp = new Bitmap(_pictureWidth, _pictureHeight);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
DrawBackground(gr);
|
||
DrawBoats(gr);
|
||
return bmp;
|
||
}
|
||
/// <summary>
|
||
/// Просмотр объекта на карте
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowOnMap()
|
||
{
|
||
Shaking();
|
||
foreach (var boat in _setBoats.GetBoats())
|
||
{
|
||
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
|
||
}
|
||
return new Bitmap(_pictureWidth, _pictureHeight);
|
||
}
|
||
/// <summary>
|
||
/// Перемещение объекта по крате
|
||
/// </summary>
|
||
/// <param name="direction"></param>
|
||
/// <returns></returns>
|
||
public Bitmap MoveObject(Direction direction)
|
||
{
|
||
if (_map != null)
|
||
{
|
||
return _map.MoveObject(direction);
|
||
}
|
||
return new Bitmap(_pictureWidth, _pictureHeight);
|
||
}
|
||
/// <summary>
|
||
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||
/// </summary>
|
||
private void Shaking()
|
||
{
|
||
int j = _setBoats.Count - 1;
|
||
for (int i = 0; i < _setBoats.Count; i++)
|
||
{
|
||
if (_setBoats[i] == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var Boat = _setBoats[j];
|
||
if (Boat != null)
|
||
{
|
||
_setBoats.Insert(Boat, i);
|
||
_setBoats.Remove(j);
|
||
break;
|
||
}
|
||
}
|
||
if (j <= i)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Метод отрисовки фона
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawBackground(Graphics g)
|
||
{
|
||
Pen pen = new Pen(Color.Black, 3);
|
||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||
{
|
||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||
{//линия рамзетки места
|
||
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
|
||
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||
for (int k = 0; k < 7; k++)
|
||
{
|
||
g.DrawEllipse(pen, i * _placeSizeWidth + 15 * k, j * _placeSizeHeight, 15, 15);
|
||
g.DrawEllipse(pen, i * _placeSizeWidth + 15 * k, j * _placeSizeHeight - 15, 15, 15);
|
||
}
|
||
}
|
||
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||
for (int k = 0; k < 6 * 4; k++)
|
||
{
|
||
g.DrawEllipse(pen, i * _placeSizeWidth, 0 + 15 * k, 15, 15);
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Метод прорисовки объектов
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawBoats(System.Drawing.Graphics g)
|
||
{
|
||
int numberOfSeatsInWidth = _pictureWidth / _placeSizeWidth;
|
||
int numberOfSeatsInHeight = _pictureHeight / _placeSizeHeight;
|
||
int bottomLine = (numberOfSeatsInHeight - 1) * _placeSizeHeight;
|
||
for (int i = 0; i < _setBoats.Count; i++)
|
||
{
|
||
// TODO установка позиции
|
||
_setBoats[i]?.SetObject(i % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - i / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||
_setBoats[i]?.DrawingObject(g);
|
||
}
|
||
}
|
||
}
|
||
}
|