diff --git a/ProjectMotorBoat/ProjectMotorBoat/Drawings/DirectionType.cs b/ProjectMotorBoat/ProjectMotorBoat/Drawings/DirectionType.cs new file mode 100644 index 0000000..6bdfdbc --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/Drawings/DirectionType.cs @@ -0,0 +1,28 @@ +namespace ProjectMotorBoat.Drawings; + +/// +/// Направление перемещения +/// +public enum DirectionType +{ + /// + /// Неизвестное направление + /// + Unknow = -1, + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} \ No newline at end of file diff --git a/ProjectMotorBoat/ProjectMotorBoat/DrawingMotorBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/Drawings/DrawingBoat.cs similarity index 56% rename from ProjectMotorBoat/ProjectMotorBoat/DrawingMotorBoat.cs rename to ProjectMotorBoat/ProjectMotorBoat/Drawings/DrawingBoat.cs index 5f3e88c..4f895bf 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/DrawingMotorBoat.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/Drawings/DrawingBoat.cs @@ -1,17 +1,16 @@ -using System; -using System.DirectoryServices.ActiveDirectory; +using ProjectMotorBoat.Entities; -namespace ProjectMotorBoat; +namespace ProjectMotorBoat.Drawings; /// -/// Класс, отвечающий за прорисовку и перемещение объекта-сущности +/// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности /// -public class DrawingMotorBoat +public class DrawingBoat { /// /// Класс-сущность "Катер" /// - public EntityMotorBoat? EntityMotorBoat { get; private set; } + public EntityBoat? EntityBoat { get; protected set; } /// /// Ширина окна @@ -24,46 +23,78 @@ public class DrawingMotorBoat private int? _pictureHeight; /// - /// Левая координата прорисовки катера + /// Левая координата прорисовки лодки /// - private int? _startPosX; + protected int? _startPosX; /// - /// Верхняя кооридната прорисовки катера + /// Верхняя кооридната прорисовки лодки /// - private int? _startPosY; - + protected int? _startPosY; /// - /// Ширина прорисовки катера + /// Ширина прорисовки лодки /// private readonly int _drawningBoatWidth = 100; /// - /// Высота прорисовки катера + /// Высота прорисовки лодки /// - private readonly int _drawningBoatHeight = 50; + private readonly int _drawningBoatHeight = 30; /// - /// Инициализация свойств + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта /// - /// - /// - /// - /// - /// - /// - /// - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool inboardEngine, bool oar, bool protectiveGlass) + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningBoatWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningBoatHeight; + + /// + /// Пустой конструктор + /// + private DrawingBoat() { - EntityMotorBoat = new EntityMotorBoat(); - EntityMotorBoat.Init(speed, weight, bodyColor, additionalColor, inboardEngine, oar, protectiveGlass); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Конструктор + /// + /// + /// + /// + public DrawingBoat(int speed, double weight, Color bodyColor) : this() + { + EntityBoat = new EntityBoat(speed, weight, bodyColor); + } + + /// + /// Конструктор для наследников + /// + /// + /// + protected DrawingBoat(int drawningBoatWidth, int drawningBoatHeight) : this() + { + _drawningBoatWidth = drawningBoatWidth; + _drawningBoatHeight = drawningBoatHeight; + } + /// /// Установка границ поля /// @@ -111,7 +142,7 @@ public class DrawingMotorBoat x = (int)_pictureWidth - _drawningBoatWidth; } - if (y + _drawningBoatHeight > _pictureHeight) + if (y + _drawningBoatHeight > _pictureHeight) { y = (int)_pictureHeight - _drawningBoatHeight; } @@ -130,7 +161,7 @@ public class DrawingMotorBoat /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntityMotorBoat == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityBoat == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -139,34 +170,34 @@ public class DrawingMotorBoat { //влево case DirectionType.Left: - if (_startPosX.Value - EntityMotorBoat.Step > 0) + if (_startPosX.Value - EntityBoat.Step > 0) { - _startPosX -= (int)EntityMotorBoat.Step; + _startPosX -= (int)EntityBoat.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntityMotorBoat.Step > 0) + if (_startPosY.Value - EntityBoat.Step > 0) { - _startPosY -= (int)EntityMotorBoat.Step; + _startPosY -= (int)EntityBoat.Step; } return true; // вправо case DirectionType.Right: //TODO прописать логику сдвига в право ✔ - if (_startPosX.Value + EntityMotorBoat.Step < _pictureWidth - _drawningBoatWidth) + if (_startPosX.Value + EntityBoat.Step < _pictureWidth - _drawningBoatWidth) { - _startPosX += (int)EntityMotorBoat.Step; + _startPosX += (int)EntityBoat.Step; } return true; //вниз case DirectionType.Down: //TODO прописать логику сдвига в низ ✔ - if (_startPosY.Value + EntityMotorBoat.Step < _pictureHeight - _drawningBoatHeight) + if (_startPosY.Value + EntityBoat.Step < _pictureHeight - _drawningBoatHeight) { - _startPosY += (int)EntityMotorBoat.Step; + _startPosY += (int)EntityBoat.Step; } return true; default: @@ -178,96 +209,54 @@ public class DrawingMotorBoat /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityMotorBoat == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityBoat == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityMotorBoat.AdditionalColor); - Brush br = new SolidBrush(EntityMotorBoat.BodyColor); + Brush br = new SolidBrush(EntityBoat.BodyColor); // Границы катера Point[] points = { - new Point(_startPosX.Value, _startPosY.Value + 10), - new Point(_startPosX.Value + 70, _startPosY.Value + 10), - new Point(_startPosX.Value + 100, _startPosY.Value + 25), - new Point(_startPosX.Value + 70, _startPosY.Value + 40), - new Point(_startPosX.Value, _startPosY.Value + 40), - new Point(_startPosX.Value, _startPosY.Value + 10), + new Point(_startPosX.Value, _startPosY.Value), + new Point(_startPosX.Value + 70, _startPosY.Value), + new Point(_startPosX.Value + 100, _startPosY.Value + 15), + new Point(_startPosX.Value + 70, _startPosY.Value + 30), + new Point(_startPosX.Value, _startPosY.Value + 30), + new Point(_startPosX.Value, _startPosY.Value), }; g.DrawLines(pen, points); points = new Point[] { - new Point(_startPosX.Value + 1, _startPosY.Value + 11), - new Point(_startPosX.Value + 72, _startPosY.Value + 11), - new Point(_startPosX.Value + 99, _startPosY.Value + 25), - new Point(_startPosX.Value + 70, _startPosY.Value + 40), - new Point(_startPosX.Value + 1, _startPosY.Value + 40), - new Point(_startPosX.Value + 1, _startPosY.Value + 11), + new Point(_startPosX.Value + 1, _startPosY.Value + 1), + new Point(_startPosX.Value + 72, _startPosY.Value + 1), + new Point(_startPosX.Value + 99, _startPosY.Value + 15), + new Point(_startPosX.Value + 70, _startPosY.Value + 30), + new Point(_startPosX.Value + 1, _startPosY.Value + 30), + new Point(_startPosX.Value + 1, _startPosY.Value + 1), }; g.FillPolygon(br, points); // Палуба - g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 20, 65, 10); - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 15, 55, 20); + g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 10, 65, 10); + g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 5, 55, 20); - g.FillRectangle(br, _startPosX.Value + 6, _startPosY.Value + 20, 64, 11); - g.FillRectangle(br, _startPosX.Value + 11, _startPosY.Value + 16, 56, 19); + g.FillRectangle(br, _startPosX.Value + 6, _startPosY.Value + 10, 64, 11); + g.FillRectangle(br, _startPosX.Value + 11, _startPosY.Value + 6, 56, 19); + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 5, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 5, 10, 10); g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 15, 10, 10); g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 15, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 25, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 25, 10, 10); - g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 15, 11, 11); - g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 15, 11, 11); - g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 24, 11, 11); - g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 24, 11, 11); - - // Мотор в корме - if (EntityMotorBoat.InboardEngine) - { - g.DrawRectangle(pen, _startPosX.Value + 3, _startPosY.Value + 13, 20, 24); - g.FillRectangle(additionalBrush, _startPosX.Value + 4, _startPosY.Value + 14, 19, 23); - - g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 15, 6, 6); - g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 15, 6, 6); - g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 28, 6, 6); - g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 28, 6, 6); - g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 16, 4, 4); - g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 16, 4, 4); - g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 29, 4, 4); - g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 29, 4, 4); - } - - // Вёсла - if (EntityMotorBoat.Oar) - { - g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 3, 2, 17); - g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 30, 2, 17); - g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 3, 1, 17); - g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 31, 1, 16); - - g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value, 6, 8); - g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8); - g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value, 6, 8); - g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8); - } - - // Защитное стекло - if (EntityMotorBoat.ProtectiveGlass) - { - Brush brBlue = new SolidBrush(Color.LightBlue); - g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 20, 25, 11); - g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 16, 17, 19); - - g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 15, 11, 11); - g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 24, 11, 11); - } + g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 5, 11, 11); + g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 5, 11, 11); + g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 14, 11, 11); + g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 14, 11, 11); } -} \ No newline at end of file +} diff --git a/ProjectMotorBoat/ProjectMotorBoat/Drawings/DrawingMotorBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/Drawings/DrawingMotorBoat.cs new file mode 100644 index 0000000..f939b5f --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/Drawings/DrawingMotorBoat.cs @@ -0,0 +1,85 @@ +using ProjectMotorBoat.Entities; +using System.Drawing; + +namespace ProjectMotorBoat.Drawings; + +/// +/// Класс, отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawingMotorBoat : DrawingBoat +{ + /// + /// Конструктор + /// + /// + /// + /// + /// + /// + /// + /// + public DrawingMotorBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool inboardEngine, bool oar, bool protectiveGlass) : base(100, 50) + { + EntityBoat = new EntityMotorBoat(speed, weight, bodyColor, additionalColor, inboardEngine, oar, protectiveGlass); + } + + /// + /// Прорисовка объекта + /// + /// + public override void DrawTransport(Graphics g) + { + if (EntityBoat == null || EntityBoat is not EntityMotorBoat motorBoat || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(motorBoat.AdditionalColor); + + _startPosY += 10; + base.DrawTransport(g); + _startPosY -= 10; + + // Мотор в корме + if (motorBoat.InboardEngine) + { + g.DrawRectangle(pen, _startPosX.Value + 3, _startPosY.Value + 13, 20, 24); + g.FillRectangle(additionalBrush, _startPosX.Value + 4, _startPosY.Value + 14, 19, 23); + + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 15, 6, 6); + g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 15, 6, 6); + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 28, 6, 6); + g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 28, 6, 6); + g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 16, 4, 4); + g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 16, 4, 4); + g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 29, 4, 4); + g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 29, 4, 4); + } + + // Вёсла + if (motorBoat.Oar) + { + g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 3, 2, 17); + g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 30, 2, 17); + g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 3, 1, 17); + g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 31, 1, 16); + + g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value, 6, 8); + g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8); + g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value, 6, 8); + g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8); + } + + // Защитное стекло + if (motorBoat.ProtectiveGlass) + { + Brush brBlue = new SolidBrush(Color.LightBlue); + g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 20, 25, 11); + g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 16, 17, 19); + + g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 15, 11, 11); + g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 24, 11, 11); + } + } +} \ No newline at end of file diff --git a/ProjectMotorBoat/ProjectMotorBoat/Entities/EntityBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/Entities/EntityBoat.cs new file mode 100644 index 0000000..313bc2c --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/Entities/EntityBoat.cs @@ -0,0 +1,40 @@ +namespace ProjectMotorBoat.Entities; + +/// +/// Класс-сущность "Лодка" +/// +public class EntityBoat +{ + /// + /// Скорость + /// + 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 EntityBoat(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectMotorBoat/ProjectMotorBoat/EntityMotorBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/Entities/EntityMotorBoat.cs similarity index 64% rename from ProjectMotorBoat/ProjectMotorBoat/EntityMotorBoat.cs rename to ProjectMotorBoat/ProjectMotorBoat/Entities/EntityMotorBoat.cs index 9a9e846..6b80a0f 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/EntityMotorBoat.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/Entities/EntityMotorBoat.cs @@ -1,25 +1,10 @@ -namespace ProjectMotorBoat; +namespace ProjectMotorBoat.Entities; /// /// Класс-сущность "Катер" /// -public class EntityMotorBoat +public class EntityMotorBoat : EntityBoat { - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } - - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// /// Дополнительный цвет (для опциональных элементов) /// @@ -40,10 +25,6 @@ public class EntityMotorBoat /// public bool ProtectiveGlass { get; private set; } - /// - /// Шаг перемещения катера - /// - public double Step => Speed * 100 / Weight; /// /// Инициализация полей объекта-класса катера @@ -55,11 +36,8 @@ public class EntityMotorBoat /// /// /// - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool inboardEngine, bool oar, bool protectiveGlass) + public EntityMotorBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool inboardEngine, bool oar, bool protectiveGlass) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; InboardEngine = inboardEngine; Oar = oar; diff --git a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs index f95b315..333d251 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs @@ -34,6 +34,9 @@ buttonUp = new Button(); buttonRight = new Button(); buttonDown = new Button(); + buttonCreateBoat = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxMotorBoat).BeginInit(); SuspendLayout(); // @@ -52,9 +55,9 @@ buttonCreateMotorBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateMotorBoat.Location = new Point(12, 448); buttonCreateMotorBoat.Name = "buttonCreateMotorBoat"; - buttonCreateMotorBoat.Size = new Size(75, 23); + buttonCreateMotorBoat.Size = new Size(160, 23); buttonCreateMotorBoat.TabIndex = 1; - buttonCreateMotorBoat.Text = "Создать"; + buttonCreateMotorBoat.Text = "Создать моторную лодку"; buttonCreateMotorBoat.UseVisualStyleBackColor = true; buttonCreateMotorBoat.Click += ButtonCreateMotorBoat_Click; // @@ -106,11 +109,45 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMoveMotorBoat_click; // + // buttonCreateBoat + // + buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBoat.Location = new Point(178, 448); + buttonCreateBoat.Name = "buttonCreateBoat"; + buttonCreateBoat.Size = new Size(160, 23); + buttonCreateBoat.TabIndex = 6; + buttonCreateBoat.Text = "Создать лодку"; + buttonCreateBoat.UseVisualStyleBackColor = true; + buttonCreateBoat.Click += ButtonCreateBoat_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(823, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(869, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormMotorBoat // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(956, 483); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateBoat); Controls.Add(buttonDown); Controls.Add(buttonRight); Controls.Add(buttonUp); @@ -132,5 +169,8 @@ private Button buttonUp; private Button buttonRight; private Button buttonDown; + private Button buttonCreateBoat; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs index b12a68c..3c11644 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs @@ -1,4 +1,7 @@ -namespace ProjectMotorBoat; +using ProjectMotorBoat.Drawings; +using ProjectMotorBoat.MovementStrategy; + +namespace ProjectMotorBoat; /// /// Форма работы с объектом "Катер" @@ -8,7 +11,9 @@ public partial class FormMotorBoat : Form /// /// Поле-объект для прорисовки объекта /// - private DrawingMotorBoat? _drawingMotorBoat; + private DrawingBoat? _drawingBoat; + + private AbstractStrategy? _strategy; /// /// Конструктор формы @@ -16,6 +21,7 @@ public partial class FormMotorBoat : Form public FormMotorBoat() { InitializeComponent(); + _strategy = null; } /// @@ -23,36 +29,60 @@ public partial class FormMotorBoat : Form /// private void Draw() { - if (_drawingMotorBoat == null) + if (_drawingBoat == null) { return; } Bitmap bmp = new(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingMotorBoat.DrawTransport(gr); + _drawingBoat.DrawTransport(gr); pictureBoxMotorBoat.Image = bmp; } /// - /// Обработка нажатия кнопки "Создать" + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawingBoat): + _drawingBoat = new DrawingBoat(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(DrawingMotorBoat): + _drawingBoat = new DrawingMotorBoat(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)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + + _drawingBoat.SetPictureSize(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); + _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; + Draw(); + } + + /// + /// Обработка нажатия кнопки "Создать моторную лодку" /// /// /// - private void ButtonCreateMotorBoat_Click(object sender, EventArgs e) - { - Random random = new(); - _drawingMotorBoat = new DrawingMotorBoat(); - _drawingMotorBoat.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)), - Convert.ToBoolean(random.Next(0, 2))); - _drawingMotorBoat.SetPictureSize(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); - _drawingMotorBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + private void ButtonCreateMotorBoat_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingMotorBoat)); - Draw(); - } + /// + /// Обработка нажатия кнопки "Создать лодку" + /// + /// + /// + private void ButtonCreateBoat_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingBoat)); /// /// Перемещение объекта по форме (нажатие кнопок навигации) @@ -61,7 +91,7 @@ public partial class FormMotorBoat : Form /// private void ButtonMoveMotorBoat_click(object sender, EventArgs e) { - if (_drawingMotorBoat == null) + if (_drawingBoat == null) { return; } @@ -71,16 +101,16 @@ public partial class FormMotorBoat : Form switch (name) { case "buttonUp": - result = _drawingMotorBoat.MoveTransport(DirectionType.Up); + result = _drawingBoat.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawingMotorBoat.MoveTransport(DirectionType.Down); + result = _drawingBoat.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawingMotorBoat.MoveTransport(DirectionType.Left); + result = _drawingBoat.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawingMotorBoat.MoveTransport(DirectionType.Right); + result = _drawingBoat.MoveTransport(DirectionType.Right); break; } @@ -89,4 +119,47 @@ public partial class FormMotorBoat : Form Draw(); } } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingBoat == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableBoat(_drawingBoat), pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } diff --git a/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/AbstractStrategy.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..a4c0eb9 --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,139 @@ +namespace ProjectMotorBoat.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 ObjectParameters? 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/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/IMoveableObject.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..22f2cda --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,24 @@ +namespace ProjectMotorBoat.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveToBorder.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..e354b4f --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,51 @@ +namespace ProjectMotorBoat.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder - GetStep() <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveToCenter.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..32569bc --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,54 @@ +namespace ProjectMotorBoat.MovementStrategy; + +/// +/// Стратегия перемещения объекта в центр экрана +/// +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? 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() + { + ObjectParameters? 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/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveableBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveableBoat.cs new file mode 100644 index 0000000..4e648b5 --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MoveableBoat.cs @@ -0,0 +1,64 @@ +using ProjectMotorBoat.Drawings; + +namespace ProjectMotorBoat.MovementStrategy; + +/// +/// Класс-реализация IMoveableObject с использованием DrawingBoat +/// +public class MoveableBoat : IMoveableObject +{ + /// + /// Поле-объект класса DrawingBoat или его наследника + /// + private readonly DrawingBoat? _boat = null; + + /// + /// Конструктор + /// + /// Объект класса DrawingBoat + public MoveableBoat(DrawingBoat boat) + { + _boat = boat; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_boat == null || _boat.EntityBoat == null || !_boat.GetPosX.HasValue || !_boat.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_boat.GetPosX.Value, _boat.GetPosY.Value, _boat.GetWidth, _boat.GetHeight); + } + } + + public int GetStep => (int)(_boat?.EntityBoat?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_boat == null || _boat.EntityBoat == null) + { + return false; + } + + return _boat.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/ProjectMotorBoat/ProjectMotorBoat/DirectionType.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MovementDirection.cs similarity index 81% rename from ProjectMotorBoat/ProjectMotorBoat/DirectionType.cs rename to ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MovementDirection.cs index af7b6d7..ec34b7b 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/DirectionType.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/MovementDirection.cs @@ -1,9 +1,9 @@ -namespace ProjectMotorBoat; +namespace ProjectMotorBoat.MovementStrategy; /// /// Направление перемещения /// -public enum DirectionType +public enum MovementDirection { /// /// Вверх @@ -21,4 +21,4 @@ public enum DirectionType /// Вправо /// Right = 4 -} \ No newline at end of file +} diff --git a/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/ObjectParameters.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..b196609 --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,72 @@ +namespace ProjectMotorBoat.MovementStrategy; + +/// +/// Параметры-координаты объекта +/// +public class ObjectParameters +{ + /// + /// Координата 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 ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} \ No newline at end of file diff --git a/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/StrategyStatus.cs b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..a11fde2 --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,22 @@ +namespace ProjectMotorBoat.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +}