generic classes

This commit is contained in:
Володя 2022-10-10 18:08:45 +03:00
parent bc3f888f2f
commit b066252a72
2 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirPlaneWithRadar
{
internal class MapWithSetPlainGeneric
<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 180;
private readonly int _placeSizeHeight = 120;
private readonly SetPlaneGeneric<T> _setPlains;
private readonly U _map;
public MapWithSetPlainGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setPlains = new SetPlaneGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithSetPlainGeneric<T, U> map, T plain)
{
return map._setPlains.Insert(plain);
}
public static bool operator -(MapWithSetPlainGeneric<T, U> map, int position)
{
return map._setPlains.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawPlains(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setPlains.Count; i++)
{
var plain = _setPlains.Get(i);
if (plain != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, plain);
}
}
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 = _setPlains.Count - 1;
for (int i = 0; i < _setPlains.Count; i++)
{
if (_setPlains.Get(i) == null)
{
for (; j > i; j--)
{
var plain = _setPlains.Get(j);
if (plain != null)
{
_setPlains.Insert(plain, i);
_setPlains.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Brush BrushRazmetka = new SolidBrush(Color.DarkGray);
Pen pen = new(Color.White, 5);
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);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawPlains(Graphics g)
{
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirPlaneWithRadar
{
internal class SetPlaneGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetPlaneGeneric(int count)
{
_places = new T[count];
}
public bool Insert(T plain)
{
return true;
}
public bool Insert(T plain, int position)
{
return true;
}
public bool Remove(int position)
{
return false;
}
public T Get(int position)
{
return _places[position];
}
}
}