From bee6b03df26def8e7fb2c55497797ad545ede602 Mon Sep 17 00:00:00 2001 From: zolotovart Date: Thu, 7 Mar 2024 14:46:05 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=80=D0=BE=D0=B4=D0=B8=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=B9=20=D0=B8=20=D0=B2=D0=B2=D0=BE=D0=B4=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectAirbus/ProjectAirbus/DrawingLinkor.cs | 242 ------------------ .../{ => Drawnings}/DirectionType.cs | 2 +- .../ProjectAirbus/Drawnings/DrawingLinkor.cs | 70 +++++ .../ProjectAirbus/Drawnings/DrawingShip.cs | 227 ++++++++++++++++ .../{Linkor.cs => Entities/EntityLinkor.cs} | 29 +-- .../ProjectAirbus/Entities/EntityShip.cs | 45 ++++ .../ProjectAirbus/FormLinkor.Designer.cs | 22 +- ProjectAirbus/ProjectAirbus/FormLinkor.cs | 78 ++++-- 8 files changed, 420 insertions(+), 295 deletions(-) delete mode 100644 ProjectAirbus/ProjectAirbus/DrawingLinkor.cs rename ProjectAirbus/ProjectAirbus/{ => Drawnings}/DirectionType.cs (94%) create mode 100644 ProjectAirbus/ProjectAirbus/Drawnings/DrawingLinkor.cs create mode 100644 ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs rename ProjectAirbus/ProjectAirbus/{Linkor.cs => Entities/EntityLinkor.cs} (65%) create mode 100644 ProjectAirbus/ProjectAirbus/Entities/EntityShip.cs diff --git a/ProjectAirbus/ProjectAirbus/DrawingLinkor.cs b/ProjectAirbus/ProjectAirbus/DrawingLinkor.cs deleted file mode 100644 index 198cada..0000000 --- a/ProjectAirbus/ProjectAirbus/DrawingLinkor.cs +++ /dev/null @@ -1,242 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Linq; -using System.Reflection.Metadata.Ecma335; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectLinkor -{ - /// - /// Класс, отвечающий за прорисовку и перемещение объекта-сущности - /// - public class DrawingLinkor - { - /// - /// Класс-сущность - /// - public EntityLinkor? EntityLinkor { get; private set; } - /// - /// Ширина окна - /// - private int? _pictureWidth; - /// - /// Высота окна - /// - private int? _pictureHeight; - /// - /// Левая координата прорисовки автомобиля - /// - private int? _startPosX; - /// - /// Верхняя кооридната прорисовки автомобиля - /// - private int? _startPosY; - /// - /// Ширина прорисовки аэробуса - /// - private readonly int _drawningLinkorWidth = 110; - /// - /// Высота прорисовки аэробуса - /// - private readonly int _drawningLinkorHeight = 70; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия обвеса - /// Признак наличия антикрыла - /// Признак наличия гоночной полосы - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool gun) - { - EntityLinkor = new EntityLinkor(); - EntityLinkor.Init(speed, weight, bodyColor, additionalColor, rocket, gun); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - } - /// - /// Установка границ поля - /// - /// Ширина поля - /// Высота поля - /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах - public bool SetPictureSize(int width, int height) - { - // TODO проверка, что объект "влезает" в размеры поля - // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена - - if (width > 110 && height > 70) - { - _pictureWidth = width; - _pictureHeight = height; - - return true; - } - return false; - - } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) - { - return; - } - // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы - // то надо изменить координаты, чтобы он оставался в этих границах - else - { - _startPosX = x; - _startPosY = y; - - if (_startPosX.Value + 110 > _pictureWidth) - { - _startPosX = _pictureWidth - 110; - } - if (_startPosY.Value + 70 > _pictureHeight) - { - _startPosY = _pictureHeight - 70; - } - - } - - - } - public void CheckPosition() - { - if (_startPosX ==null || _startPosY==null) - { - return; - } - if (_startPosX.Value + _drawningLinkorWidth > _pictureWidth) - { - _startPosX = _pictureWidth - _drawningLinkorWidth; - } - if (_startPosY.Value + _drawningLinkorHeight > _pictureHeight) - { - _startPosY = _pictureHeight - _drawningLinkorHeight; - } - - } - - /// - /// Изменение направления перемещения - /// - /// Направление - /// true - перемещене выполнено, false - перемещение невозможно - public bool MoveTransport(DirectionType direction) - { - if (EntityLinkor == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return false; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX.Value - EntityLinkor.Step > 0) - { - _startPosX -= (int)EntityLinkor.Step; - } - return true; - //вверх - case DirectionType.Up: - if (_startPosY.Value - EntityLinkor.Step > 0) - { - _startPosY -= (int)EntityLinkor.Step; - } - return true; - // вправо - case DirectionType.Right: - if (_startPosX.Value + _drawningLinkorWidth + EntityLinkor.Step < _pictureWidth) - { - _startPosX += (int)EntityLinkor.Step; - } - return true; - //вниз - case DirectionType.Down: - if (_startPosY.Value + _drawningLinkorHeight + EntityLinkor.Step < _pictureHeight) - { - _startPosY += (int)EntityLinkor.Step; - } - return true; - default: - return false; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityLinkor == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return; - } - Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityLinkor.AdditionalColor); - - Point[] body = { new Point(_startPosX.Value + 70, _startPosY.Value + 20), - new Point(_startPosX.Value + 100, _startPosY.Value + 40), new Point(_startPosX.Value +70, _startPosY.Value + 60) }; - Point[] body2 = { new Point(_startPosX.Value + 50, _startPosY.Value + 10), - new Point(_startPosX.Value + 60, _startPosY.Value + 15), new Point(_startPosX.Value +50, _startPosY.Value + 20) }; - Point[] body3 = { new Point(_startPosX.Value + 50, _startPosY.Value + 60), - new Point(_startPosX.Value + 60, _startPosY.Value + 65), new Point(_startPosX.Value + 50, _startPosY.Value + 70) }; - - - //границы Линкора - g.DrawPolygon(pen, body); - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 20, 60, 40); - g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 25, 5, 10); - g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 45, 5, 10); - - - - //корпус - Brush br = new SolidBrush(EntityLinkor.BodyColor); - g.FillPolygon(br, body); - g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 20, 60, 40); - g.FillEllipse(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 35, 10, 10); - - //Мачта - Brush brBlack = new SolidBrush(Color.Black); - g.FillRectangle(brBlack, _startPosX.Value + 50, _startPosY.Value + 30, 10, 20); - g.FillRectangle(brBlack, _startPosX.Value + 30, _startPosY.Value + 35, 20, 10); - - //задние трубы - g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value + 25, 5, 10); - g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value + 45, 5, 10); - - // доп - if (EntityLinkor.Rocket) - { - //ракеты - g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 10, 20, 10); - g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 60, 20, 10); - g.FillPolygon(additionalBrush, body2); - g.FillPolygon(additionalBrush, body3); - - } - if (EntityLinkor.Gun) - { - //пушка - g.FillEllipse(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 35, 10, 10); - g.FillRectangle(additionalBrush, _startPosX.Value + 55, _startPosY.Value + 35, 30, 10); - } - - /* - */ - } - } -} diff --git a/ProjectAirbus/ProjectAirbus/DirectionType.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DirectionType.cs similarity index 94% rename from ProjectAirbus/ProjectAirbus/DirectionType.cs rename to ProjectAirbus/ProjectAirbus/Drawnings/DirectionType.cs index ea5f484..83d5360 100644 --- a/ProjectAirbus/ProjectAirbus/DirectionType.cs +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DirectionType.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectLinkor +namespace ProjectLinkor.Drawnings { /// /// Направление перемещения diff --git a/ProjectAirbus/ProjectAirbus/Drawnings/DrawingLinkor.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DrawingLinkor.cs new file mode 100644 index 0000000..f9c43af --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DrawingLinkor.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading.Tasks; +using ProjectLinkor.Entities; + +namespace ProjectLinkor.Drawnings +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawingLinkor : DrawingShip + { + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + /// Признак наличия гоночной полосы + + public DrawingLinkor(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool gun) : base(110, 70) + { + EntityShip = new EntityLinkor(speed, weight, bodyColor, additionalColor, rocket, gun); + } + + public override void DrawTransport(Graphics g) + { + if (EntityShip == null || EntityShip is not EntityLinkor linkor || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(linkor.AdditionalColor); + + Point[] body2 = { new Point(_startPosX.Value + 50, _startPosY.Value + 10), + new Point(_startPosX.Value + 60, _startPosY.Value + 15), new Point(_startPosX.Value +50, _startPosY.Value + 20) }; + Point[] body3 = { new Point(_startPosX.Value + 50, _startPosY.Value + 60), + new Point(_startPosX.Value + 60, _startPosY.Value + 65), new Point(_startPosX.Value + 50, _startPosY.Value + 70) }; + if (linkor.Rocket) + { + //ракеты + g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 10, 20, 10); + g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 60, 20, 10); + g.FillPolygon(additionalBrush, body2); + g.FillPolygon(additionalBrush, body3); + + } + _startPosX += 5; + _startPosY += 20; + base.DrawTransport(g); + _startPosX -= 5; + _startPosY -= 20; + + if (linkor.Gun) + { + //пушка + g.FillEllipse(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 35, 10, 10); + g.FillRectangle(additionalBrush, _startPosX.Value + 55, _startPosY.Value + 35, 30, 10); + } + } + + } +} diff --git a/ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs new file mode 100644 index 0000000..8da4fb7 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs @@ -0,0 +1,227 @@ +using ProjectLinkor.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.Drawnings; + +public class DrawingShip +{ + /// + /// Класс-сущность + /// + public EntityShip? EntityShip { get; protected set; } + /// + /// Ширина окна + /// + private int? _pictureWidth; + /// + /// Высота окна + /// + private int? _pictureHeight; + /// + /// Левая координата прорисовки корабля + /// + protected int? _startPosX; + /// + /// Верхняя кооридната прорисовки корабля + /// + protected int? _startPosY; + /// + /// Ширина прорисовки корабля + /// + private readonly int _drawningLinkorWidth = 95; + /// + /// Высота прорисовки корабля + /// + private readonly int _drawningLinkorHeight = 40; + + /// + /// Пустой конструктор + /// + private DrawingShip() + { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + + public DrawingShip(int speed, double weight, Color bodyColor) : this() + { + EntityShip = new EntityShip(speed, weight, bodyColor); + } + /// + /// Конструктор для наследоваников + /// + /// Ширина прорисовки корабля + /// Высота прорисовки корабля + protected DrawingShip(int drawningLinkorWidth, int drawningLinkorHeight) : this() + { + _drawningLinkorWidth = drawningLinkorWidth; + _drawningLinkorHeight = drawningLinkorHeight; + } + /// + /// Установка границ поля + /// + /// Ширина поля + /// Высота поля + /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах + public bool SetPictureSize(int width, int height) + { + // TODO проверка, что объект "влезает" в размеры поля + // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена + + if (width > _drawningLinkorWidth && height > _drawningLinkorHeight) + { + _pictureWidth = width; + _pictureHeight = height; + + if (_startPosX != null && _startPosY != null) + { + if (_startPosX.Value < 0) _startPosX = 0; + if (_startPosY.Value < 0) _startPosY = 0; + if (_startPosX.Value + _drawningLinkorWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningLinkorWidth; + } + if (_startPosY.Value + _drawningLinkorHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningLinkorHeight; + } + } + + return true; + } + return false; + + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы + // то надо изменить координаты, чтобы он оставался в этих границах + else + { + _startPosX = x; + _startPosY = y; + + if (_startPosX.Value < 0) _startPosX = 0; + if (_startPosY.Value < 0) _startPosY = 0; + if (_startPosX.Value + _drawningLinkorWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningLinkorWidth; + } + if (_startPosY.Value + _drawningLinkorHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningLinkorHeight; + } + + } + + + } + + /// + /// Изменение направления перемещения + /// + /// Направление + /// true - перемещене выполнено, false - перемещение невозможно + public bool MoveTransport(DirectionType direction) + { + if (EntityShip == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return false; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX.Value - EntityShip.Step > 0) + { + _startPosX -= (int)EntityShip.Step; + } + return true; + //вверх + case DirectionType.Up: + if (_startPosY.Value - EntityShip.Step > 0) + { + _startPosY -= (int)EntityShip.Step; + } + return true; + // вправо + case DirectionType.Right: + if (_startPosX.Value + _drawningLinkorWidth + EntityShip.Step < _pictureWidth) + { + _startPosX += (int)EntityShip.Step; + } + return true; + //вниз + case DirectionType.Down: + if (_startPosY.Value + _drawningLinkorHeight + EntityShip.Step < _pictureHeight) + { + _startPosY += (int)EntityShip.Step; + } + return true; + default: + return false; + } + } + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityShip == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + Pen pen = new(Color.Black); + + Point[] body = { new Point(_startPosX.Value + 65, _startPosY.Value), + new Point(_startPosX.Value + 95, _startPosY.Value + 20), new Point(_startPosX.Value +65, _startPosY.Value + 40) }; + + + + //границы Линкора + g.DrawPolygon(pen, body); + g.DrawRectangle(pen, _startPosX.Value+ 5, _startPosY.Value, 60, 40); + g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 5, 5, 10); + g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 25, 5, 10); + + + + //корпус + Brush br = new SolidBrush(EntityShip.BodyColor); + g.FillPolygon(br, body); + g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value, 60, 40); + g.FillEllipse(br, _startPosX.Value + 60, _startPosY.Value + 15, 10, 10); + + //Мачта + Brush brBlack = new SolidBrush(Color.Black); + g.FillRectangle(brBlack, _startPosX.Value + 45, _startPosY.Value + 10, 10, 20); + g.FillRectangle(brBlack, _startPosX.Value + 25, _startPosY.Value + 15, 20, 10); + + //задние трубы + g.FillRectangle(brBlack, _startPosX.Value, _startPosY.Value + 5, 5, 10); + g.FillRectangle(brBlack, _startPosX.Value, _startPosY.Value + 25, 5, 10); + + } +} diff --git a/ProjectAirbus/ProjectAirbus/Linkor.cs b/ProjectAirbus/ProjectAirbus/Entities/EntityLinkor.cs similarity index 65% rename from ProjectAirbus/ProjectAirbus/Linkor.cs rename to ProjectAirbus/ProjectAirbus/Entities/EntityLinkor.cs index 3bd947d..5fa3fe8 100644 --- a/ProjectAirbus/ProjectAirbus/Linkor.cs +++ b/ProjectAirbus/ProjectAirbus/Entities/EntityLinkor.cs @@ -4,28 +4,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectLinkor +namespace ProjectLinkor.Entities { /// /// Класс-сущность "Линкор" /// - public class EntityLinkor + public class EntityLinkor : EntityShip { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// public Color AdditionalColor { get; private set; } /// /// Признак (опция) наличия ракеты @@ -35,7 +20,7 @@ namespace ProjectLinkor /// Признак (опция) наличия оружия /// public bool Gun { get; private set; } - + /// /// Шаг перемещения линкора /// @@ -50,16 +35,12 @@ namespace ProjectLinkor /// Признак наличия обвеса /// Признак наличия антикрыла /// Признак наличия гоночной полосы - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool rocket, bool gun) + public EntityLinkor(int speed, double weight, Color bodyColor, Color + additionalColor, bool rocket, bool gun) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Rocket = rocket; Gun = gun; - //SportLine = sportLine; } } diff --git a/ProjectAirbus/ProjectAirbus/Entities/EntityShip.cs b/ProjectAirbus/ProjectAirbus/Entities/EntityShip.cs new file mode 100644 index 0000000..7da6fd6 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Entities/EntityShip.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.Entities; +/// +/// Класс-сущность "Корабль" +/// +public class EntityShip +{ + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public double Weight { get; private set; } + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + + /// + /// Шаг перемещения корабля + /// + public double Step => Speed * 100 / Weight; + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + + public EntityShip(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs b/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs index b25da0d..68059e5 100644 --- a/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs +++ b/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs @@ -34,14 +34,15 @@ buttonRight = new Button(); buttonDown = new Button(); buttonUp = new Button(); + buttonCreateShip = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLinkor).BeginInit(); SuspendLayout(); // - // pictureBoxAirBus + // pictureBoxLinkor // pictureBoxLinkor.Dock = DockStyle.Fill; pictureBoxLinkor.Location = new Point(0, 0); - pictureBoxLinkor.Name = "pictureBoxAirBus"; + pictureBoxLinkor.Name = "pictureBoxLinkor"; pictureBoxLinkor.Size = new Size(800, 414); pictureBoxLinkor.TabIndex = 0; pictureBoxLinkor.TabStop = false; @@ -51,9 +52,9 @@ buttonCreateAirBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateAirBus.Location = new Point(12, 368); buttonCreateAirBus.Name = "buttonCreateAirBus"; - buttonCreateAirBus.Size = new Size(112, 34); + buttonCreateAirBus.Size = new Size(170, 34); buttonCreateAirBus.TabIndex = 1; - buttonCreateAirBus.Text = "Создать"; + buttonCreateAirBus.Text = "Создать линкор"; buttonCreateAirBus.UseVisualStyleBackColor = true; buttonCreateAirBus.Click += ButtonCreateLinkor_Click; // @@ -105,11 +106,23 @@ buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; // + // buttonCreateShip + // + buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateShip.Location = new Point(188, 368); + buttonCreateShip.Name = "buttonCreateShip"; + buttonCreateShip.Size = new Size(170, 34); + buttonCreateShip.TabIndex = 6; + buttonCreateShip.Text = "Создать корабль"; + buttonCreateShip.UseVisualStyleBackColor = true; + buttonCreateShip.Click += ButtonCreateShip_Click; + // // FormLinkor // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 414); + Controls.Add(buttonCreateShip); Controls.Add(buttonUp); Controls.Add(buttonDown); Controls.Add(buttonRight); @@ -132,5 +145,6 @@ private Button buttonRight; private Button buttonDown; private Button buttonUp; + private Button buttonCreateShip; } } \ No newline at end of file diff --git a/ProjectAirbus/ProjectAirbus/FormLinkor.cs b/ProjectAirbus/ProjectAirbus/FormLinkor.cs index 011dade..0e9dc0a 100644 --- a/ProjectAirbus/ProjectAirbus/FormLinkor.cs +++ b/ProjectAirbus/ProjectAirbus/FormLinkor.cs @@ -7,49 +7,78 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using ProjectLinkor.Drawnings; namespace ProjectLinkor { public partial class FormLinkor : Form { - private DrawingLinkor? _drawingLinkor; + private DrawingShip? _drawingShip; + public FormLinkor() { InitializeComponent(); } - + private void Draw() { - if (_drawingLinkor == null) + if (_drawingShip == null) { return; } Bitmap bmp = new(pictureBoxLinkor.Width, pictureBoxLinkor.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingLinkor.DrawTransport(gr); + _drawingShip.DrawTransport(gr); pictureBoxLinkor.Image = bmp; } - + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawingShip): + _drawingShip = new DrawingShip(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); + break; + case nameof(DrawingLinkor): + _drawingShip = new DrawingLinkor(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + _drawingShip.SetPictureSize(pictureBoxLinkor.Width, pictureBoxLinkor.Height); + _drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + /// + /// Обработка нажатия кнопки "Создать линкор" + /// + /// + /// private void ButtonCreateLinkor_Click(object sender, EventArgs e) { - Random random = new Random(); - _drawingLinkor = new DrawingLinkor(); - _drawingLinkor.Init(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - _drawingLinkor.SetPictureSize(pictureBoxLinkor.Width, pictureBoxLinkor.Height); - _drawingLinkor.SetPosition(random.Next(40, 100), random.Next(40, 100)); - Draw(); - + CreateObject(nameof(DrawingLinkor)); } + /// + /// Обработка нажатия кнопки "Создать корабль" + /// + /// + /// + private void ButtonCreateShip_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingShip)); + } private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawingLinkor == null) + if (_drawingShip == null) { return; } @@ -58,37 +87,38 @@ namespace ProjectLinkor switch (name) { case "buttonUp": - result = _drawingLinkor.MoveTransport(DirectionType.Up); + result = _drawingShip.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawingLinkor.MoveTransport(DirectionType.Down); + result = _drawingShip.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawingLinkor.MoveTransport(DirectionType.Left); + result = _drawingShip.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawingLinkor.MoveTransport(DirectionType.Right); + result = _drawingShip.MoveTransport(DirectionType.Right); break; } if (result) { Draw(); - } + } } private void FormLinkor_SizeChanged(object sender, EventArgs e) { - if (_drawingLinkor == null) + if (_drawingShip == null) { return; } - if (_drawingLinkor.SetPictureSize(pictureBoxLinkor.Width, pictureBoxLinkor.Height)) + if (_drawingShip.SetPictureSize(pictureBoxLinkor.Width, pictureBoxLinkor.Height)) { - _drawingLinkor.CheckPosition(); Draw(); } } + + } } -- 2.25.1 From 7c96c833556886bf834df0ddbdeec00c25f04a8e Mon Sep 17 00:00:00 2001 From: zolotovart Date: Mon, 11 Mar 2024 19:33:37 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectAirbus/Drawnings/DirectionType.cs | 4 + .../ProjectAirbus/Drawnings/DrawingShip.cs | 47 ++++--- .../ProjectAirbus/FormLinkor.Designer.cs | 26 ++++ ProjectAirbus/ProjectAirbus/FormLinkor.cs | 52 +++++++- .../MovementStrategy/AbstractStrategy.cs | 125 ++++++++++++++++++ .../MovementStrategy/IMoveableObject.cs | 25 ++++ .../MovementStrategy/MoveToBorder.cs | 39 ++++++ .../MovementStrategy/MoveToCenter.cs | 55 ++++++++ .../MovementStrategy/MoveableShip.cs | 66 +++++++++ .../MovementStrategy/MovementDirection.cs | 27 ++++ .../MovementStrategy/ObjectParametrs.cs | 65 +++++++++ .../MovementStrategy/StrategyStatus.cs | 16 +++ 12 files changed, 530 insertions(+), 17 deletions(-) create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableShip.cs create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParametrs.cs create mode 100644 ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs diff --git a/ProjectAirbus/ProjectAirbus/Drawnings/DirectionType.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DirectionType.cs index 83d5360..9d5386e 100644 --- a/ProjectAirbus/ProjectAirbus/Drawnings/DirectionType.cs +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DirectionType.cs @@ -11,6 +11,10 @@ namespace ProjectLinkor.Drawnings /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, /// /// Вверх /// diff --git a/ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs b/ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs index 8da4fb7..a3e5dd6 100644 --- a/ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs +++ b/ProjectAirbus/ProjectAirbus/Drawnings/DrawingShip.cs @@ -32,11 +32,28 @@ public class DrawingShip /// /// Ширина прорисовки корабля /// - private readonly int _drawningLinkorWidth = 95; + private readonly int _drawningShipWidth = 95; /// /// Высота прорисовки корабля /// - private readonly int _drawningLinkorHeight = 40; + private readonly int _drawningShipHeight = 40; + + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + // + /// Ширина объекта + /// + public int GetWidth => _drawningShipWidth; + // + /// Высота объекта + /// + public int GetHeight => _drawningShipHeight; /// /// Пустой конструктор @@ -66,8 +83,8 @@ public class DrawingShip /// Высота прорисовки корабля protected DrawingShip(int drawningLinkorWidth, int drawningLinkorHeight) : this() { - _drawningLinkorWidth = drawningLinkorWidth; - _drawningLinkorHeight = drawningLinkorHeight; + _drawningShipWidth = drawningLinkorWidth; + _drawningShipHeight = drawningLinkorHeight; } /// /// Установка границ поля @@ -80,7 +97,7 @@ public class DrawingShip // TODO проверка, что объект "влезает" в размеры поля // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена - if (width > _drawningLinkorWidth && height > _drawningLinkorHeight) + if (width > _drawningShipWidth && height > _drawningShipHeight) { _pictureWidth = width; _pictureHeight = height; @@ -89,13 +106,13 @@ public class DrawingShip { if (_startPosX.Value < 0) _startPosX = 0; if (_startPosY.Value < 0) _startPosY = 0; - if (_startPosX.Value + _drawningLinkorWidth > _pictureWidth) + if (_startPosX.Value + _drawningShipWidth > _pictureWidth) { - _startPosX = _pictureWidth - _drawningLinkorWidth; + _startPosX = _pictureWidth - _drawningShipWidth; } - if (_startPosY.Value + _drawningLinkorHeight > _pictureHeight) + if (_startPosY.Value + _drawningShipHeight > _pictureHeight) { - _startPosY = _pictureHeight - _drawningLinkorHeight; + _startPosY = _pictureHeight - _drawningShipHeight; } } @@ -124,13 +141,13 @@ public class DrawingShip if (_startPosX.Value < 0) _startPosX = 0; if (_startPosY.Value < 0) _startPosY = 0; - if (_startPosX.Value + _drawningLinkorWidth > _pictureWidth) + if (_startPosX.Value + _drawningShipWidth > _pictureWidth) { - _startPosX = _pictureWidth - _drawningLinkorWidth; + _startPosX = _pictureWidth - _drawningShipWidth; } - if (_startPosY.Value + _drawningLinkorHeight > _pictureHeight) + if (_startPosY.Value + _drawningShipHeight > _pictureHeight) { - _startPosY = _pictureHeight - _drawningLinkorHeight; + _startPosY = _pictureHeight - _drawningShipHeight; } } @@ -167,14 +184,14 @@ public class DrawingShip return true; // вправо case DirectionType.Right: - if (_startPosX.Value + _drawningLinkorWidth + EntityShip.Step < _pictureWidth) + if (_startPosX.Value + _drawningShipWidth + EntityShip.Step < _pictureWidth) { _startPosX += (int)EntityShip.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + _drawningLinkorHeight + EntityShip.Step < _pictureHeight) + if (_startPosY.Value + _drawningShipHeight + EntityShip.Step < _pictureHeight) { _startPosY += (int)EntityShip.Step; } diff --git a/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs b/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs index 68059e5..b277cc7 100644 --- a/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs +++ b/ProjectAirbus/ProjectAirbus/FormLinkor.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonUp = new Button(); buttonCreateShip = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLinkor).BeginInit(); SuspendLayout(); // @@ -117,11 +119,33 @@ buttonCreateShip.UseVisualStyleBackColor = true; buttonCreateShip.Click += ButtonCreateShip_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(606, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(182, 33); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(676, 51); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(112, 34); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormLinkor // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 414); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateShip); Controls.Add(buttonUp); Controls.Add(buttonDown); @@ -146,5 +170,7 @@ private Button buttonDown; private Button buttonUp; private Button buttonCreateShip; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectAirbus/ProjectAirbus/FormLinkor.cs b/ProjectAirbus/ProjectAirbus/FormLinkor.cs index 0e9dc0a..e74b39e 100644 --- a/ProjectAirbus/ProjectAirbus/FormLinkor.cs +++ b/ProjectAirbus/ProjectAirbus/FormLinkor.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectLinkor.Drawnings; +using ProjectLinkor.MovementStrategy; namespace ProjectLinkor { @@ -15,9 +16,15 @@ namespace ProjectLinkor { private DrawingShip? _drawingShip; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + public FormLinkor() { InitializeComponent(); + _strategy = null; } @@ -54,6 +61,8 @@ namespace ProjectLinkor } _drawingShip.SetPictureSize(pictureBoxLinkor.Width, pictureBoxLinkor.Height); _drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } /// @@ -106,7 +115,6 @@ namespace ProjectLinkor } - private void FormLinkor_SizeChanged(object sender, EventArgs e) { if (_drawingShip == null) @@ -118,7 +126,47 @@ namespace ProjectLinkor Draw(); } } + /* + private void pictureBoxLinkor_Click(object sender, EventArgs e) + { - + } + */ + + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingShip == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableShip(_drawingShip), + pictureBoxLinkor.Width, pictureBoxLinkor.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + + } } } diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..e046289 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; + +public abstract class AbstractStrategy +{ + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private StrategyStatus _state = StrategyStatus.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public StrategyStatus GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(MovementDirection.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(MovementDirection.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(MovementDirection.Down); + /// + /// Параметры объекта + /// + protected ObjectParametrs? GetObjectParameters => + _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } + +} diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..b12ad5c --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParametrs? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..f287d2c --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParametrs? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + ObjectParametrs? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } +} diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..c17e205 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParametrs? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 + && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 + && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + ObjectParametrs? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableShip.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableShip.cs new file mode 100644 index 0000000..42a2135 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableShip.cs @@ -0,0 +1,66 @@ +using ProjectLinkor.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; +/// +/// Класс-реализация IMoveableObject с использованием DrawingShip +/// +public class MoveableShip : IMoveableObject +{ + /// + /// Поле-объект класса DrawningCar или его наследника + /// + private readonly DrawingShip? _ship = null; + /// + /// Конструктор + /// + /// Объект класса DrawningCar + public MoveableShip(DrawingShip ship) + { + _ship = ship; + } + + public ObjectParametrs? GetObjectPosition + { + get + { + if (_ship == null || _ship.EntityShip == null || + !_ship.GetPosX.HasValue || !_ship.GetPosY.HasValue) + { + return null; + } + return new ObjectParametrs(_ship.GetPosX.Value, _ship.GetPosY.Value, _ship.GetWidth, _ship.GetHeight); + } + } + public int GetStep => (int)(_ship?.EntityShip?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_ship == null || _ship.EntityShip == null) + { + return false; + } + return _ship.MoveTransport(GetDirectionType(direction)); + } + + + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknow, + }; + } +} diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..34c9dd6 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + // + /// Вправо + /// + Right = 4 +} diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParametrs.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParametrs.cs new file mode 100644 index 0000000..dfc3f85 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParametrs.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; + +public class ObjectParametrs +{ + /// + /// Координата X + /// + private readonly int _x; + /// + /// Координата Y + /// + private readonly int _y; + /// + /// Ширина объекта + /// + private readonly int _width; + /// + /// Высота объекта + /// + private readonly int _height; + /// + /// Левая граница + /// + public int LeftBorder => _x; + /// + /// Верхняя граница + /// + public int TopBorder => _y; + /// + /// Правая граница + /// + public int RightBorder => _x + _width; + /// + /// Нижняя граница + /// + public int DownBorder => _y + _height; + /// + /// Середина объекта + /// + public int ObjectMiddleHorizontal => _x + _width / 2; + /// + /// Середина объекта + /// + public int ObjectMiddleVertical => _y + _height / 2; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина объекта + /// Высота объекта + public ObjectParametrs(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..4f934ae --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLinkor.MovementStrategy; +public enum StrategyStatus +{ + //всё готово к началу + NotInit, + //выполняется + InProgress, + //завершено + Finish +} -- 2.25.1