generic classes
This commit is contained in:
parent
fae3a22b32
commit
e87456943d
119
AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs
Normal file
119
AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs
Normal file
@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
internal class MapWithSetAirBomberGeneric<T, U>
|
||||
where T : class, IDrawningObject
|
||||
where U : AbstractMap
|
||||
{
|
||||
private readonly int _pictureWidth;
|
||||
private readonly int _pictureHeight;
|
||||
private readonly int _placeSizeWidth = 210;
|
||||
private readonly int _placeSizeHeight = 90;
|
||||
private readonly SetAirBomberGeneric<T> _setAirBomber;
|
||||
private readonly U _map;
|
||||
|
||||
public MapWithSetAirBomberGeneric(int picWidth, int picHeight, U map)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setAirBomber = new SetAirBomberGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
|
||||
public static bool operator +(MapWithSetAirBomberGeneric<T, U> map, T airBomber)
|
||||
{
|
||||
return map._setAirBomber.Insert(airBomber);
|
||||
}
|
||||
|
||||
public static bool operator -(MapWithSetAirBomberGeneric<T, U> map, int position)
|
||||
{
|
||||
return map._setAirBomber.Remove(position);
|
||||
}
|
||||
|
||||
public Bitmap ShowSet()
|
||||
{
|
||||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
DrawBackground(gr);
|
||||
DrawCars(gr);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
public Bitmap ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setAirBomber.Count; i++)
|
||||
{
|
||||
var airBomber = _setAirBomber.Get(i);
|
||||
if (airBomber != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
|
||||
}
|
||||
}
|
||||
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 = _setAirBomber.Count - 1;
|
||||
for (int i = 0; i < _setAirBomber.Count; i++)
|
||||
{
|
||||
if (_setAirBomber.Get(i) == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var airBomber = _setAirBomber.Get(j);
|
||||
if (airBomber != null)
|
||||
{
|
||||
_setAirBomber.Insert(airBomber, i);
|
||||
_setAirBomber.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)
|
||||
{
|
||||
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
}
|
||||
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCars(Graphics g)
|
||||
{
|
||||
for (int i = 0; i < _setAirBomber.Count; ++i)
|
||||
{
|
||||
_setAirBomber.Get(i)?.DrawningObject(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
42
AirBomber/AirBomber/SetAirBomberGeneric.cs
Normal file
42
AirBomber/AirBomber/SetAirBomberGeneric.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
internal class SetAirBomberGeneric<T>
|
||||
where T : class
|
||||
{
|
||||
private readonly T[] _places;
|
||||
|
||||
public int Count => _places.Length;
|
||||
|
||||
public SetAirBomberGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
}
|
||||
|
||||
public bool Insert(T car)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Insert(T car, int position)
|
||||
{
|
||||
_places[position] = car;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(int position)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
return _places[position];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user