diff --git a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs new file mode 100644 index 0000000..f18f006 --- /dev/null +++ b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs @@ -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 + 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 _setAirBomber; + private readonly U _map; + + public MapWithSetAirBomberGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setAirBomber = new SetAirBomberGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetAirBomberGeneric map, T airBomber) + { + return map._setAirBomber.Insert(airBomber); + } + + public static bool operator -(MapWithSetAirBomberGeneric 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); + } + } + } +} diff --git a/AirBomber/AirBomber/SetAirBomberGeneric.cs b/AirBomber/AirBomber/SetAirBomberGeneric.cs new file mode 100644 index 0000000..11d1258 --- /dev/null +++ b/AirBomber/AirBomber/SetAirBomberGeneric.cs @@ -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 + 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]; + } + } +}