generic classes
This commit is contained in:
parent
07fe09f7e1
commit
9a8d51e3aa
205
AirBomber/AirBomber/MapWithSetJetsGeneric.cs
Normal file
205
AirBomber/AirBomber/MapWithSetJetsGeneric.cs
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AirBomber
|
||||||
|
{
|
||||||
|
internal class MapWithSetJetsGeneric<T, U>
|
||||||
|
where T : class, IDrawningObject
|
||||||
|
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 = 122;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Набор объектов
|
||||||
|
/// </summary>
|
||||||
|
private readonly SetJetGeneric<T> _setJets;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Карта
|
||||||
|
/// </summary>
|
||||||
|
private readonly U _map;
|
||||||
|
public U Map => _map;
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="picWidth">Ширина</param>
|
||||||
|
/// <param name="picHeight">Высота</param>
|
||||||
|
/// <param name="map">Карта</param>
|
||||||
|
public MapWithSetJetsGeneric(int picWidth, int picHeight, U map)
|
||||||
|
{
|
||||||
|
int width = picWidth / _placeSizeWidth;
|
||||||
|
int height = picHeight / _placeSizeHeight;
|
||||||
|
_setJets = new SetJetGeneric<T>(width * height);
|
||||||
|
_pictureWidth = picWidth;
|
||||||
|
_pictureHeight = picHeight;
|
||||||
|
_map = map;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перегрузка оператора сложения
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="map"></param>
|
||||||
|
/// <param name="jet"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static int operator +(MapWithSetJetsGeneric<T, U> map, T jet)
|
||||||
|
{
|
||||||
|
return map._setJets.Insert(jet);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перегрузка оператора вычитания
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="map"></param>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T operator -(MapWithSetJetsGeneric<T, U> map, int position)
|
||||||
|
{
|
||||||
|
return map._setJets.Remove(position);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Вывод всего набора объектов
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Bitmap ShowSet()
|
||||||
|
{
|
||||||
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||||||
|
Graphics gr = Graphics.FromImage(bmp);
|
||||||
|
DrawBackground(gr);
|
||||||
|
DrawJets(gr);
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Просмотр объекта на карте
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Bitmap ShowOnMap()
|
||||||
|
{
|
||||||
|
Shaking();
|
||||||
|
for (int i = 0; i < _setJets.Count; i++)
|
||||||
|
{
|
||||||
|
var car = _setJets.Get(i);
|
||||||
|
if (car != null)
|
||||||
|
{
|
||||||
|
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new(_pictureWidth, _pictureHeight);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение объекта по крате
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Bitmap MoveObject(Direction direction)
|
||||||
|
{
|
||||||
|
if (_map != null)
|
||||||
|
{
|
||||||
|
return _map.MoveObject(direction);
|
||||||
|
}
|
||||||
|
return new(_pictureWidth, _pictureHeight);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||||||
|
/// </summary>
|
||||||
|
private void Shaking()
|
||||||
|
{
|
||||||
|
int j = _setJets.Count - 1;
|
||||||
|
for (int i = 0; i < _setJets.Count; i++)
|
||||||
|
{
|
||||||
|
if (_setJets.Get(i) == null)
|
||||||
|
{
|
||||||
|
for (; j > i; j--)
|
||||||
|
{
|
||||||
|
var car = _setJets.Get(j);
|
||||||
|
if (car != null)
|
||||||
|
{
|
||||||
|
_setJets.Insert(car, i);
|
||||||
|
_setJets.Remove(j);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j <= i)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Метод отрисовки фона
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
private void DrawBackground(Graphics g)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < _pictureWidth; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < _pictureWidth; y++)
|
||||||
|
{
|
||||||
|
Brush hangarColor = new SolidBrush(Color.Silver);
|
||||||
|
g.FillRectangle(hangarColor, x * x, y * y, x * (x + 1), y * (y + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Pen pen = new(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 * 3 / 4, j * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||||
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Метод прорисовки объектов
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
private void DrawJets(Graphics g)
|
||||||
|
{
|
||||||
|
// максимальное количество колонок и строк
|
||||||
|
int cols = _pictureWidth / _placeSizeWidth;
|
||||||
|
//int rows = _pictureHeight / _placeSizeHeight;
|
||||||
|
int rows = (cols - 1) * _placeSizeWidth;
|
||||||
|
// счетчик колонок и строк
|
||||||
|
int ccol = 0;
|
||||||
|
int crow = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < _setJets.Count; i++)
|
||||||
|
{
|
||||||
|
// установка позиции
|
||||||
|
//_setJets.Get(i)?.SetObject(ccol * _placeSizeWidth, crow * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||||||
|
_setJets.Get(i)?.SetObject(rows - i % cols * _placeSizeWidth, i / cols * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
|
||||||
|
_setJets.Get(i)?.DrawningObject(g);
|
||||||
|
// (сначала передвигаемся влево)
|
||||||
|
ccol++;
|
||||||
|
if (ccol >= cols)
|
||||||
|
{
|
||||||
|
//(потом двигаемся вниз)
|
||||||
|
crow++;
|
||||||
|
ccol = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
87
AirBomber/AirBomber/SetJetGeneric.cs
Normal file
87
AirBomber/AirBomber/SetJetGeneric.cs
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AirBomber
|
||||||
|
{
|
||||||
|
internal class SetJetGeneric<T> where T : class
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Массив объектов, которые храним (самолетов)
|
||||||
|
/// </summary>
|
||||||
|
private readonly T[] _places;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Количество объектов в массиве
|
||||||
|
/// </summary>
|
||||||
|
public int Count => _places.Length;
|
||||||
|
private int JetPlaces = 0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="count">Количество самолетов</param>
|
||||||
|
public SetJetGeneric(int count)
|
||||||
|
{
|
||||||
|
_places = new T[count];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Добавление самолета в набор на конкретную позицию
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="jet">Добавляемый самолет</param>
|
||||||
|
/// <param name="position">Позиция</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public int Insert(T jet)
|
||||||
|
{
|
||||||
|
return Insert(jet, 0);
|
||||||
|
}
|
||||||
|
public int Insert(T jet, int position = 0)
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= _places.Length || JetPlaces == _places.Length)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
JetPlaces++;
|
||||||
|
while (_places[position] != null)
|
||||||
|
{
|
||||||
|
for (int i = _places.Length - 1; i > 0; --i)
|
||||||
|
{
|
||||||
|
if (_places[i] == null && _places[i - 1] != null)
|
||||||
|
{
|
||||||
|
_places[i] = _places[i - 1];
|
||||||
|
_places[i - 1] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_places[position] = jet;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Удаление объекта из набора с конкретной позиции
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public T Remove(int position)
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= _places.Length) return null;
|
||||||
|
T savedJet = _places[position];
|
||||||
|
_places[position] = null;
|
||||||
|
return savedJet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получение объекта из набора по позиции
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">Позиция получаемого самолета</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public T Get(int position)
|
||||||
|
{
|
||||||
|
// проверка позиции
|
||||||
|
if (position < 0 || position >= _places.Length) return null;
|
||||||
|
return _places[position];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user