using ProjectTank.Entities; namespace ProjectTank.Drawnings { public class DrawningTank2 { /// /// класс - сущность /// public EntityTank2? EntityTank2 { get; protected set; } /// /// ширина окна /// private int? _pictureWidth; /// /// высота окна /// private int? _pictureHeight; /// /// Левая координата прорисовки танка /// protected int? _startPosX; /// /// Верхняя координата прорисовки танка /// protected int? _startPosY; /// /// ширина прорисовки танка /// /// private readonly int _drawningTankWidth = 150; /// /// высота прорисовки танка /// private readonly int _drawningTankHeight = 67; /// /// Координата X объекта /// public int? GetPosX => _startPosX; /// /// Координата Y объекта /// public int? GetPosY => _startPosY; /// /// Ширина объекта /// public int GetWidth => _drawningTankWidth; /// /// Высота объекта /// public int GetHeight => _drawningTankHeight; /// /// Пустой конструктор /// private DrawningTank2() { _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } /// /// Конструктор /// /// Скорость /// Вес танка /// Основной цвет public DrawningTank2(int speed, double weight, Color bodyColor) : this() { EntityTank2 = new EntityTank2(speed, weight, bodyColor); } /// /// Конструктор /// /// Ширина прорисовки танка /// Высота прорисовки танка protected DrawningTank2(int drawningTankWidth, int drawningTankHeight) : this() { _drawningTankWidth = drawningTankWidth; _drawningTankHeight = drawningTankHeight; } /// /// Установка границ поля /// /// Ширина поля /// Высота поля /// true - границы заданы, false - проверка не пройдена , нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { if (height < _drawningTankHeight || width < _drawningTankWidth) { return false; } _pictureHeight = height; _pictureWidth = width; if (_startPosX != null && _startPosY != null) { if (_startPosX < 0) _startPosX = 0; if (_startPosY < 0) _startPosY = 0; if (_pictureHeight.Value < _startPosY + _drawningTankHeight) { _startPosY = _pictureHeight - _drawningTankHeight; } if (_pictureWidth.Value < _startPosX + _drawningTankWidth) { _startPosX = _pictureWidth - _drawningTankWidth; } } return true; } /// /// Установка позиции /// /// Координата X /// Координата Y public void SetPosition(int x, int y) { if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) { return; } _startPosX = x; _startPosY = y; if (_startPosX < 0) _startPosX = 0; if (_startPosY < 0) _startPosY = 0; if (_pictureHeight.Value < _startPosY + _drawningTankHeight) { _startPosY = _pictureHeight - _drawningTankHeight; } if (_pictureWidth.Value < _startPosX + _drawningTankWidth) { _startPosX = _pictureWidth - _drawningTankWidth; } } /// /// Изменение направления перемещения /// /// направление /// public bool MoveTransport(DirectionType dicretion) { if (EntityTank2 == null || _startPosX == null || _startPosY == null) { return false; } switch (dicretion) { //влево case DirectionType.Left: if (_startPosX.Value - EntityTank2.Step > 0) { _startPosX -= (int)EntityTank2.Step; } return true; //вправо case DirectionType.Right: if (_startPosX.Value + _drawningTankWidth + EntityTank2.Step < _pictureWidth) { _startPosX += (int)EntityTank2.Step; } return true; //вверх case DirectionType.Up: if (_startPosY.Value - EntityTank2.Step > 0) { _startPosY -= (int)EntityTank2.Step; } return true; //вниз case DirectionType.Down: if (_startPosY + EntityTank2.Step + _drawningTankHeight < _pictureHeight) { _startPosY += (int)EntityTank2.Step; } return true; default: return false; } } /// /// прорисовка танка /// /// public virtual void DrawTransport(Graphics g) { if (EntityTank2 == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); Brush Br = new SolidBrush(EntityTank2.BodyColor); //основная часть танка g.DrawRectangle(pen, _startPosX.Value + 6, _startPosY.Value + 20, 135, 20); g.DrawRectangle(pen, _startPosX.Value + 45, _startPosY.Value, 55, 20); g.FillRectangle(Br, _startPosX.Value + 6, _startPosY.Value + 20, 135, 20); g.FillRectangle(Br, _startPosX.Value + 45, _startPosY.Value, 55, 20); //гусеницы g.DrawArc(pen, _startPosX.Value, _startPosY.Value + 40, 15, 30, 100, 170); g.DrawArc(pen, _startPosX.Value + 135, _startPosY.Value + 40, 15, 30, -90, 170); g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 40, _startPosX.Value + 145, _startPosY.Value + 40); g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 70, _startPosX.Value + 145, _startPosY.Value + 70); Pen WidePen = new(Color.Black); WidePen.Width = 2; g.DrawEllipse(WidePen, _startPosX.Value, _startPosY.Value + 40, 24, 24); g.DrawEllipse(WidePen, _startPosX.Value + 125, _startPosY.Value + 40, 24, 24); //катки g.DrawEllipse(pen, _startPosX.Value + 26, _startPosY.Value + 50, 20, 20); g.DrawEllipse(pen, _startPosX.Value + 52, _startPosY.Value + 50, 20, 20); g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 50, 20, 20); g.DrawEllipse(pen, _startPosX.Value + 104, _startPosY.Value + 50, 20, 20); g.DrawEllipse(pen, _startPosX.Value + 49, _startPosY.Value + 40, 5, 5); g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 40, 5, 5); g.DrawEllipse(pen, _startPosX.Value + 101, _startPosY.Value + 40, 5, 5); } } }