diff --git a/ProjectSeaplane/ProjectSeaplane/Drawnings/DirectionType.cs b/ProjectSeaplane/ProjectSeaplane/Drawnings/DirectionType.cs new file mode 100644 index 0000000..2b84860 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Drawnings/DirectionType.cs @@ -0,0 +1,34 @@ +namespace ProjectSeaplane.Drawnings; +/// +/// Направление перемещения +/// +public enum DirectionType +{ + /// + /// Неизвестное направление + /// + Unknown = -1, + /// + /// Вверх + /// + + Up = 1, + + /// + /// Вниз + /// + + Down = 2, + + /// + /// Влево + /// + + Left = 3, + + /// + /// Вправо + /// + + Right = 4, +} diff --git a/ProjectSeaplane/ProjectSeaplane/DrawingSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawingBasicSeaplane.cs similarity index 64% rename from ProjectSeaplane/ProjectSeaplane/DrawingSeaplane.cs rename to ProjectSeaplane/ProjectSeaplane/Drawnings/DrawingBasicSeaplane.cs index 2ea84ca..6e7ab09 100644 --- a/ProjectSeaplane/ProjectSeaplane/DrawingSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawingBasicSeaplane.cs @@ -1,13 +1,13 @@ -namespace ProjectSeaplane; -/// -/// Отрисовка и перемещение -/// -public class DrawingSeaplane +using ProjectSeaplane.Entities; + +namespace ProjectSeaplane.Drawnings; + +public class DrawingBasicSeaplane { /// - /// Класс-сущность - /// - public EntitySeaplane? EntitySeaplane { get; private set; } + /// Класс-сущность + /// + public EntityBasicSeaplane? EntityBasicSeaplane { get; protected set; } /// /// Ширина окна @@ -22,41 +22,77 @@ public class DrawingSeaplane /// /// Левая координата прорисовки автомобиля /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя кооридната прорисовки автомобиля /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки автомобиля /// - private readonly int _drawningSeaplaneWidth = 155; + private readonly int _drawningSeaplaneWidth = 170; /// /// Высота прорисовки автомобиля /// - private readonly int _drawningSeaplaneHeight = 70; + private readonly int _drawningSeaplaneHeight = 90; /// - /// Инициализация свойств + /// Координата X объекта /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Тип "шасси" (0 - поплавки, 1 - лодочный тип) - /// Признак наличия радара - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool landingGear, bool radar) + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningSeaplaneWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningSeaplaneHeight; + /// + /// Пустой конструктор + /// + private DrawingBasicSeaplane() { - EntitySeaplane = new EntitySeaplane(); - EntitySeaplane.Init(speed, weight, bodyColor, additionalColor, landingGear, radar); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + + public DrawingBasicSeaplane(int speed, double weight, Color bodyColor) : this() + { + EntityBasicSeaplane = new EntityBasicSeaplane(speed, weight, bodyColor); + + } + /// + /// Конструктор для наследников + /// + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + + + protected DrawingBasicSeaplane(int drawningSeaplaneWidth, int drawningSeaplaneHeight) : this() + { + _drawningSeaplaneWidth = drawningSeaplaneWidth; + _drawningSeaplaneHeight = drawningSeaplaneHeight; + + } /// /// Установка границ поля @@ -78,14 +114,14 @@ public class DrawingSeaplane if (_startPosX + _drawningSeaplaneWidth > width) { _startPosX = width - (_drawningSeaplaneWidth + 1); - + } if (_startPosY + _drawningSeaplaneWidth > height) { _startPosY = height - (_drawningSeaplaneHeight + 1); } - - + + } @@ -142,7 +178,7 @@ public class DrawingSeaplane /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityBasicSeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -151,32 +187,32 @@ public class DrawingSeaplane { //влево case DirectionType.Left: - if (_startPosX.Value - EntitySeaplane.Step > 0) + if (_startPosX.Value - EntityBasicSeaplane.Step > 0) { - _startPosX -= (int)EntitySeaplane.Step; + _startPosX -= (int)EntityBasicSeaplane.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntitySeaplane.Step > 0) + if (_startPosY.Value - EntityBasicSeaplane.Step > 0) { - _startPosY -= (int)EntitySeaplane.Step; + _startPosY -= (int)EntityBasicSeaplane.Step; } return true; // вправо case DirectionType.Right: - if (_startPosX + (int)EntitySeaplane.Step < _pictureWidth - _drawningSeaplaneWidth) + if (_startPosX + (int)EntityBasicSeaplane.Step < _pictureWidth - _drawningSeaplaneWidth) { - _startPosX += (int)EntitySeaplane.Step; + _startPosX += (int)EntityBasicSeaplane.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY + (int)EntitySeaplane.Step < _pictureHeight - _drawningSeaplaneHeight) + if (_startPosY + (int)EntityBasicSeaplane.Step < _pictureHeight - _drawningSeaplaneHeight) { - _startPosY += (int)EntitySeaplane.Step; + _startPosY += (int)EntityBasicSeaplane.Step; } return true; default: @@ -188,16 +224,16 @@ public class DrawingSeaplane /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityBasicSeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); Pen penKraya = new(Color.Black, 2); - Brush additionalBrush = new SolidBrush(EntitySeaplane.AdditionalColor); + //Начинаем рисовать @@ -217,14 +253,11 @@ public class DrawingSeaplane Point point6 = new Point(_startPosX.Value + 45, _startPosY.Value + 30); Point point7 = new Point(_startPosX.Value + 55, _startPosY.Value + 30); Point point8 = new Point(_startPosX.Value + 50, _startPosY.Value + 22); - Point[] Radar = - { - point5, point6 , point7 , point8 - }; + - //Кисти для основного цвета и дополнительного - Brush brBody = new SolidBrush(EntitySeaplane.BodyColor); - Brush brAdditional = new SolidBrush(EntitySeaplane.AdditionalColor); + //Кисти для основного цвета + Brush brBody = new SolidBrush(EntityBasicSeaplane.BodyColor); + Brush brBlack = new SolidBrush(Color.Black); Brush brWhity = new SolidBrush(Color.GhostWhite); //Хвост g.FillPolygon(brBody, Hvost); @@ -239,12 +272,12 @@ public class DrawingSeaplane g.FillEllipse(brBody, _startPosX.Value - 5, _startPosY.Value + 30, 100, 24); g.DrawEllipse(penKraya, _startPosX.Value, _startPosY.Value + 27, 17, 6); - g.FillEllipse(brAdditional, _startPosX.Value, _startPosY.Value + 27, 17, 6); + g.FillEllipse(brBlack, _startPosX.Value, _startPosY.Value + 27, 17, 6); //Крыло - g.FillEllipse(brAdditional, _startPosX.Value + 45, _startPosY.Value + 43, 50, 7); + g.FillEllipse(brBlack, _startPosX.Value + 45, _startPosY.Value + 43, 50, 7); //Иллюминаторы @@ -256,41 +289,9 @@ public class DrawingSeaplane } - - //Поплавки - - if (EntitySeaplane.LandingGear) - { - g.FillEllipse(brAdditional, _startPosX.Value + 60, _startPosY.Value + 50, 20, 7); - g.DrawLine(penKraya, _startPosX.Value + 70, _startPosY.Value + 48, _startPosX.Value + 70, _startPosY.Value + 52); - } - else - { - g.FillEllipse(brAdditional, _startPosX.Value + 60, _startPosY.Value + 60, 70, 10); - g.FillEllipse(brAdditional, _startPosX.Value + 10, _startPosY.Value + 60, 20, 10); - g.DrawLine(penKraya, _startPosX.Value + 20, _startPosY.Value + 53, _startPosX.Value + 20, _startPosY.Value + 63); - g.DrawLine(penKraya, _startPosX.Value + 110, _startPosY.Value + 53, _startPosX.Value + 90, _startPosY.Value + 63); - g.DrawLine(penKraya, _startPosX.Value + 70, _startPosY.Value + 53, _startPosX.Value + 90, _startPosY.Value + 63); - } - - if (EntitySeaplane.Radar) - { - g.FillEllipse(brAdditional, _startPosX.Value + 30, _startPosY.Value + 12, 40, 15); - g.FillPolygon(brAdditional, Radar); - } - else - { - - } //Пилоты g.FillEllipse(brWhity, _startPosX.Value + 115, _startPosY.Value + 34, 20, 8); - - - - - } } - diff --git a/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawingSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawingSeaplane.cs new file mode 100644 index 0000000..bbe56ad --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawingSeaplane.cs @@ -0,0 +1,71 @@ +using ProjectSeaplane.Entities; + +namespace ProjectSeaplane.Drawnings; +/// +/// Отрисовка и перемещение +/// +public class DrawingSeaplane : DrawingBasicSeaplane +{ + + /// + /// Ширина прорисовки автомобиля + /// + private readonly int _drawningSeaplaneWidth = 170; + + /// + /// Высота прорисовки автомобиля + /// + private readonly int _drawningSeaplaneHeight = 90; + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Тип "шасси" (0 - поплавки, 1 - лодочный тип) + /// Признак наличия радара + public DrawingSeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool landingGear, bool radar) : base(155, 70) + { + EntityBasicSeaplane = new EntitySeaplane(speed, weight, bodyColor, additionalColor, landingGear, radar); + + } + + public override void DrawTransport(Graphics g) + { + if (EntityBasicSeaplane == null || EntityBasicSeaplane is not EntitySeaplane seaplane || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + Brush brAdditional = new SolidBrush(seaplane.AdditionalColor); + Pen penKraya = new(Color.Black, 2); + if (seaplane.LandingGear) + { + g.FillEllipse(brAdditional, _startPosX.Value + 60, _startPosY.Value + 60, 70, 10); + g.FillEllipse(brAdditional, _startPosX.Value + 10, _startPosY.Value + 60, 20, 10); + g.DrawLine(penKraya, _startPosX.Value + 20, _startPosY.Value + 53, _startPosX.Value + 20, _startPosY.Value + 63); + g.DrawLine(penKraya, _startPosX.Value + 110, _startPosY.Value + 53, _startPosX.Value + 90, _startPosY.Value + 63); + g.DrawLine(penKraya, _startPosX.Value + 70, _startPosY.Value + 53, _startPosX.Value + 90, _startPosY.Value + 63); + } + + //Чет надо но не понял + base.DrawTransport(g); + //also + if (seaplane.Radar) + { + Point point5 = new Point(_startPosX.Value + 50, _startPosY.Value + 22); + Point point6 = new Point(_startPosX.Value + 45, _startPosY.Value + 30); + Point point7 = new Point(_startPosX.Value + 55, _startPosY.Value + 30); + Point point8 = new Point(_startPosX.Value + 50, _startPosY.Value + 22); + Point[] Radar = + { + point5, point6 , point7 , point8 + }; + g.FillEllipse(brAdditional, _startPosX.Value + 30, _startPosY.Value + 12, 40, 15); + g.FillPolygon(brAdditional, Radar); + } + } +} + + diff --git a/ProjectSeaplane/ProjectSeaplane/Entities/EntityBasicSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Entities/EntityBasicSeaplane.cs new file mode 100644 index 0000000..ac8659e --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Entities/EntityBasicSeaplane.cs @@ -0,0 +1,37 @@ +namespace ProjectSeaplane.Entities; +/// +/// Класс-сущность "Простой Гидросамолет" +/// +public class EntityBasicSeaplane +{ + /// + /// Скорость + /// + 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 EntityBasicSeaplane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} + diff --git a/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs new file mode 100644 index 0000000..45d0ba0 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs @@ -0,0 +1,37 @@ + +namespace ProjectSeaplane.Entities; +/// +/// Класс-сущность "Гидросамолет" +/// +public class EntitySeaplane : EntityBasicSeaplane +{ + + /// + /// Дополнительный (для опциональных частей) + /// + public Color AdditionalColor { get; private set; } + /// + /// Тип "шасси" (0 - лодочный, 1 - поплавковый) + /// + public bool LandingGear { get; private set; } + /// + /// Признак наличия радара + /// + public bool Radar { get; private set; } + + /// + /// Конструктор + /// + /// Дополнительный цвет + /// Тип "шасси" + /// При + + public EntitySeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool landingGear, bool radar) : base(speed, weight, bodyColor) + { + + AdditionalColor = additionalColor; + LandingGear = landingGear; + Radar = radar; + + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs b/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs deleted file mode 100644 index 4a80778..0000000 --- a/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs +++ /dev/null @@ -1,54 +0,0 @@ -namespace ProjectSeaplane; -/// -/// Класс-сущность Гидросамолета -/// -public class EntitySeaplane -{ - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный (для опциональных частей) - /// - public Color AdditionalColor { get; private set; } - /// - /// Тип "шасси" (0 - лодочный, 1 - поплавковый) - /// - public bool LandingGear { get; private set; } - /// - /// Признак наличия радара - /// - public bool Radar { get; private set; } - /// - /// Расстояние шага передвижения - /// - public double Step => Speed * 100 / Weight; - /// - /// - /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Тип "шасси" - /// При - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool landingGear, bool radar) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - LandingGear = landingGear; - Radar = radar; - - } -} diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs index 051d03d..c496ad8 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBoxSeaplane = new PictureBox(); - buttonCreate = new Button(); + ButtonCreateSeaplane = new Button(); buttonLeft = new Button(); buttonDown = new Button(); buttonUp = new Button(); buttonRight = new Button(); + ButtonCreateBasicSeaplane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit(); SuspendLayout(); // @@ -42,20 +45,20 @@ pictureBoxSeaplane.Dock = DockStyle.Fill; pictureBoxSeaplane.Location = new Point(0, 0); pictureBoxSeaplane.Name = "pictureBoxSeaplane"; - pictureBoxSeaplane.Size = new Size(590, 379); + pictureBoxSeaplane.Size = new Size(757, 379); pictureBoxSeaplane.TabIndex = 0; pictureBoxSeaplane.TabStop = false; // - // buttonCreate + // ButtonCreateSeaplane // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 349); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += ButtonCreate_Click; + ButtonCreateSeaplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreateSeaplane.Location = new Point(12, 349); + ButtonCreateSeaplane.Name = "ButtonCreateSeaplane"; + ButtonCreateSeaplane.Size = new Size(256, 23); + ButtonCreateSeaplane.TabIndex = 1; + ButtonCreateSeaplane.Text = "Создать гидросамолет с обвесами"; + ButtonCreateSeaplane.UseVisualStyleBackColor = true; + ButtonCreateSeaplane.Click += ButtonCreateSeaplane_Click; // // buttonLeft // @@ -63,7 +66,7 @@ buttonLeft.BackColor = Color.Snow; buttonLeft.BackgroundImage = Properties.Resources.arrow11; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(465, 337); + buttonLeft.Location = new Point(632, 337); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(35, 35); buttonLeft.TabIndex = 2; @@ -76,7 +79,7 @@ buttonDown.BackColor = Color.Snow; buttonDown.BackgroundImage = Properties.Resources.arrow4; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(506, 337); + buttonDown.Location = new Point(673, 337); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(35, 35); buttonDown.TabIndex = 4; @@ -89,7 +92,7 @@ buttonUp.BackColor = Color.Snow; buttonUp.BackgroundImage = Properties.Resources.arrow2; buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(506, 297); + buttonUp.Location = new Point(673, 297); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(35, 35); buttonUp.TabIndex = 5; @@ -102,23 +105,57 @@ buttonRight.BackColor = Color.Snow; buttonRight.BackgroundImage = Properties.Resources.arrow3; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(547, 337); + buttonRight.Location = new Point(714, 337); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(35, 35); buttonRight.TabIndex = 7; buttonRight.UseVisualStyleBackColor = false; buttonRight.Click += ButtonMove_Click; // + // ButtonCreateBasicSeaplane + // + ButtonCreateBasicSeaplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreateBasicSeaplane.Location = new Point(288, 349); + ButtonCreateBasicSeaplane.Name = "ButtonCreateBasicSeaplane"; + ButtonCreateBasicSeaplane.Size = new Size(234, 23); + ButtonCreateBasicSeaplane.TabIndex = 8; + ButtonCreateBasicSeaplane.Text = "Создать гидросамолет без обвесов"; + ButtonCreateBasicSeaplane.UseVisualStyleBackColor = true; + ButtonCreateBasicSeaplane.Click += ButtonCreateBasicSeaplane_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(632, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(113, 23); + comboBoxStrategy.TabIndex = 9; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(673, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(72, 27); + buttonStrategyStep.TabIndex = 10; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormSeaplane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(590, 379); + ClientSize = new Size(757, 379); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); + Controls.Add(ButtonCreateBasicSeaplane); Controls.Add(buttonRight); Controls.Add(buttonUp); Controls.Add(buttonDown); Controls.Add(buttonLeft); - Controls.Add(buttonCreate); + Controls.Add(ButtonCreateSeaplane); Controls.Add(pictureBoxSeaplane); Name = "FormSeaplane"; Text = "Гидросамолет"; @@ -129,10 +166,13 @@ #endregion private PictureBox pictureBoxSeaplane; - private Button buttonCreate; + private Button ButtonCreateSeaplane; private Button buttonLeft; private Button buttonDown; private Button buttonUp; private Button buttonRight; + private Button ButtonCreateBasicSeaplane; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs index df68e98..485c1b8 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs @@ -1,4 +1,7 @@ -namespace ProjectSeaplane +using ProjectSeaplane.Drawnings; +using ProjectSeaplane.MovementStrategy; + +namespace ProjectSeaplane /// /// Форма работы с объектом "Гидросамолет" /// @@ -11,11 +14,16 @@ public FormSeaplane() { InitializeComponent(); + _strategy = null; } /// /// Поле-объект для прорисовки объекта /// - private DrawingSeaplane? _drawingSeaplane; + private DrawingBasicSeaplane? _drawingSeaplane; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; /// /// Метод прорисовки самолета /// @@ -36,20 +44,50 @@ /// /// /// - private void ButtonCreate_Click(object sender, EventArgs e) + + private void CreateObject(string type) { Random random = new(); - _drawingSeaplane = new DrawingSeaplane(); - _drawingSeaplane.Init(random.Next(100, 300), random.Next(1000, 3000), + switch (type) + { + case nameof(DrawingBasicSeaplane): + _drawingSeaplane = new DrawingBasicSeaplane(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(DrawingSeaplane): + _drawingSeaplane = new DrawingSeaplane(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; + } _drawingSeaplane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); _drawingSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); - + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } /// + /// Обработка нажатия кнопки "Создать гидросамолет с обвесами" + /// + /// + /// + private void ButtonCreateSeaplane_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingSeaplane)); + } + /// + /// Обработка нажатия кнопки "Создать гидросамолет без обвесов" + /// + /// + /// + private void ButtonCreateBasicSeaplane_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingBasicSeaplane)); + } + /// /// Перемещение объекта по форме (нажатие кнопок навигации) /// /// @@ -84,7 +122,48 @@ Draw(); } } + /// + /// + /// + /// + /// + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingSeaplane == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableSeaplane(_drawingSeaplane), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + } + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + + } } } diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/AbstractStrategy.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..4725f99 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,141 @@ + + +namespace ProjectSeaplane.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/ProjectSeaplane/ProjectSeaplane/MovementStrategy/IMoveableObject.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..a90216a --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,24 @@ +namespace ProjectSeaplane.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToBorder.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..1ed6789 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,52 @@ +namespace ProjectSeaplane.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + + if (diffX > 0) + { + MoveLeft(); + } + if (diffX < 0) + { + MoveRight(); + } + } + int diffY = objParams.DownBorder - FieldWidth; + if (Math.Abs(diffY) > GetStep()) + { + + if (diffX > 0) + { + MoveUp(); + } + if (diffY < 0) + { + MoveDown(); + } + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToCenter.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..ead5b29 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,54 @@ +namespace ProjectSeaplane.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/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveableSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveableSeaplane.cs new file mode 100644 index 0000000..260cf7b --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveableSeaplane.cs @@ -0,0 +1,66 @@ +using ProjectSeaplane.Drawnings; + + + +namespace ProjectSeaplane.MovementStrategy; + +/// +/// Класс-реализация IMoveableObject с использованием Seaplane +/// +public class MoveableSeaplane : IMoveableObject +{ + /// + /// Поле-объект класса Seaplane или его наследника + /// + private readonly DrawingBasicSeaplane? _seaplane = null; + + /// + /// Конструктор + /// + /// Объект класса Seaplane + public MoveableSeaplane(DrawingBasicSeaplane Seaplane) + { + _seaplane = Seaplane; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_seaplane == null || _seaplane.EntityBasicSeaplane == null || !_seaplane.GetPosX.HasValue || !_seaplane.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_seaplane.GetPosX.Value, _seaplane.GetPosY.Value, _seaplane.GetWidth, _seaplane.GetHeight); + } + } + + public int GetStep => (int)(_seaplane?.EntityBasicSeaplane?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_seaplane == null || _seaplane.EntityBasicSeaplane == null) + { + return false; + } + + return _seaplane.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.Unknown, + }; + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/DirectionType.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MovementDirection.cs similarity index 59% rename from ProjectSeaplane/ProjectSeaplane/DirectionType.cs rename to ProjectSeaplane/ProjectSeaplane/MovementStrategy/MovementDirection.cs index c462152..4a916c5 100644 --- a/ProjectSeaplane/ProjectSeaplane/DirectionType.cs +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MovementDirection.cs @@ -1,30 +1,31 @@ -namespace ProjectSeaplane; + +namespace ProjectSeaplane.MovementStrategy; /// /// Направление перемещения /// -public enum DirectionType +public enum MovementDirection { /// /// Вверх /// - + Up = 1, - + /// - /// Вниз + /// Вниз /// - + Down = 2, - + /// - /// Влево - /// - + /// Влево + /// + Left = 3, - + /// - /// Вправо - /// - + /// Вправо + /// + Right = 4, } diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/ObjectParameters.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..2ed45e5 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,72 @@ +namespace ProjectSeaplane.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; + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/StrategyStatus.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..0e480a0 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,22 @@ +namespace ProjectSeaplane.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +} \ No newline at end of file