generic classes

This commit is contained in:
Дамир Нугаев 2022-10-21 13:08:07 +04:00
parent b57c2bf5cb
commit 1ced71cb23
2 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class MapWithSetDoubleDeckerBusGeneric<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 SetDoubleDeckerBusGeneric<T> _setBus;
private readonly U _map;
public MapWithSetDoubleDeckerBusGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBus = new SetDoubleDeckerBusGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithSetDoubleDeckerBusGeneric<T, U> map, T bus)
{
return map._setBus.Insert(bus);
}
public static bool operator -(MapWithSetDoubleDeckerBusGeneric<T, U> map, int position)
{
return map._setBus.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawBus(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i< _setBus.Count; i++)
{
var bus = _setBus.Get(i);
if (bus != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
}
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 = _setBus.Count - 1;
for (int i = 0; i<_setBus.Count;i++)
{
if(_setBus.Get(i) == null)
{
for(; j > i; j--)
{
var bus = _setBus.Get(j);
if (bus != null)
{
_setBus.Insert(bus, i);
_setBus.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth/_placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight/_placeSizeHeight; j++)
{
// TODO
}
}
}
private void DrawBus(Graphics g)
{
for (int i = 0; i<_setBus.Count; i++)
{
_setBus.Get(i)?.DrawingObject(g);
}
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class SetDoubleDeckerBusGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetDoubleDeckerBusGeneric(int count)
{
_places = new T[count];
}
public bool Insert(T bus)
{
return true;
}
public bool Insert(T bus, int position)
{
_places[position] = bus;
return true;
}
public bool Remove(int position)
{
return true;
}
public T Get(int position)
{
if (position < 0 || position >= _places.Length) return null;
return _places[position];
}
}
}