From fc86d0a67c1f72eddaff9e55a04030e01b92945b Mon Sep 17 00:00:00 2001 From: LivelyPuer Date: Thu, 11 Apr 2024 13:28:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 1 + .../Drawnings/DrawningAirBomber.cs | 2 +- .../Drawnings/DrawningBomber.cs | 29 ++++- .../Entities/EntityAirBomber.cs | 6 +- .../FormAirBomber.Designer.cs | 122 ++++++++++------- .../ProjectAirBomber/FormAirBomber.cs | 105 ++++++++++----- .../MovementStrategy/AbstractStrategy.cs | 123 ++++++++++++++++++ .../MovementStrategy/IMoveableObject.cs | 22 ++++ .../MovementStrategy/MoveToBorder.cs | 52 ++++++++ .../MovementStrategy/MoveToCenter.cs | 50 +++++++ .../MovementStrategy/MoveableTrans.cs | 60 +++++++++ .../MovementStrategy/MovementDirection.cs | 21 +++ .../MovementStrategy/ObjectParameters.cs | 60 +++++++++ .../MovementStrategy/StrategyStatus.cs | 17 +++ 14 files changed, 577 insertions(+), 93 deletions(-) create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/IMoveableObject.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/MovementDirection.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/ObjectParameters.cs create mode 100644 ProjectAirBomber/ProjectAirBomber/MovementStrategy/StrategyStatus.cs diff --git a/ProjectAirBomber/ProjectAirBomber/Drawnings/DirectionType.cs b/ProjectAirBomber/ProjectAirBomber/Drawnings/DirectionType.cs index 83700a7..df80ecf 100644 --- a/ProjectAirBomber/ProjectAirBomber/Drawnings/DirectionType.cs +++ b/ProjectAirBomber/ProjectAirBomber/Drawnings/DirectionType.cs @@ -5,6 +5,7 @@ /// public enum DirectionType { + Unknow = -1, /// /// Вверх /// diff --git a/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningAirBomber.cs b/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningAirBomber.cs index f9fc76c..5d0e8c3 100644 --- a/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningAirBomber.cs +++ b/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningAirBomber.cs @@ -22,7 +22,7 @@ public class DrawningAirBomber : DrawningBomber //TO DO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - public DrawningAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, bool fuelTanks, bool bombs, bool v) : base(200, 160) + public DrawningAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, bool fuelTanks, bool bombs) : base(200, 160) { EntityBomber = new EntityAirBomber(speed, weight, bodyColor, additionalColor, fuelTanks, bombs); } diff --git a/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningBomber.cs b/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningBomber.cs index b7ea6ce..5bc6542 100644 --- a/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningBomber.cs +++ b/ProjectAirBomber/ProjectAirBomber/Drawnings/DrawningBomber.cs @@ -44,7 +44,22 @@ public class DrawningBomber /// private readonly int _drawningAirBomberHeight = 160; - + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _drawningAirBomberWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningAirBomberHeight; /// /// Пустой конструктор @@ -189,7 +204,7 @@ public class DrawningBomber } } - /// + /// /// Прорисовка объекта /// /// @@ -214,16 +229,16 @@ public class DrawningBomber g.FillPolygon(brGray, new Point[] { new Point(_startPosX.Value + 95, _startPosY.Value), - new Point(_startPosX.Value + 115, _startPosY.Value + 5), - new Point(_startPosX.Value + 125, _startPosY.Value + 85) + new Point(_startPosX.Value + 110, _startPosY.Value + 5), + new Point(_startPosX.Value + 118, _startPosY.Value + 85) }); ////левое крыло g.FillPolygon(brGray, new Point[] { new Point(_startPosX.Value + 95, _startPosY.Value + 85), - new Point(_startPosX.Value + 115, _startPosY.Value + 135), - new Point(_startPosX.Value + 121, _startPosY.Value + 85) + new Point(_startPosX.Value + 110, _startPosY.Value + 135), + new Point(_startPosX.Value + 115, _startPosY.Value + 85) }); //задние крылья @@ -257,4 +272,4 @@ public class DrawningBomber new Point(_startPosX.Value, _startPosY.Value + 65) }); } -} +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/Entities/EntityAirBomber.cs b/ProjectAirBomber/ProjectAirBomber/Entities/EntityAirBomber.cs index be5688d..d26052c 100644 --- a/ProjectAirBomber/ProjectAirBomber/Entities/EntityAirBomber.cs +++ b/ProjectAirBomber/ProjectAirBomber/Entities/EntityAirBomber.cs @@ -5,9 +5,7 @@ /// public class EntityAirBomber : EntityBomber { - public EntityAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, bool fuelTanks, bool bombs) : base(speed, weight, bodyColor) - { - } + /// @@ -41,7 +39,7 @@ public class EntityAirBomber : EntityBomber /// Дополнительный цвет /// Признак наличия дополнительных топливных баков /// Признак наличия бомб - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool fuelTanks, bool bombs) + public EntityAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, bool fuelTanks, bool bombs): base(speed, weight, bodyColor) { AdditionalColor = additionalColor; FuelTanks = fuelTanks; diff --git a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs index 014ef18..815856a 100644 --- a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs +++ b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.Designer.cs @@ -1,11 +1,15 @@ -namespace ProjectAirBomber +using static System.Net.Mime.MediaTypeNames; +using System.Windows.Forms; +using System.Xml.Linq; + +namespace ProjectAirBomber { partial class FormAirBomber { /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. @@ -31,30 +35,32 @@ pictureBoxAirBomber = new PictureBox(); buttonCreateAirBomber = new Button(); buttonLeft = new Button(); + buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); - buttonUp = new Button(); - buttonCreateBomber = new Button(); + buttonCreateCar = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); SuspendLayout(); // - // pictureBoxAirBomber + // pictureBoxElectroTrans // pictureBoxAirBomber.Dock = DockStyle.Fill; pictureBoxAirBomber.Location = new Point(0, 0); - pictureBoxAirBomber.Name = "pictureBoxAirBomber"; - pictureBoxAirBomber.Size = new Size(880, 510); + pictureBoxAirBomber.Name = "pictureBoxElectroTrans"; + pictureBoxAirBomber.Size = new Size(923, 597); pictureBoxAirBomber.TabIndex = 0; pictureBoxAirBomber.TabStop = false; // - // buttonCreateAirBomber + // buttonCreateElectroTrans // buttonCreateAirBomber.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateAirBomber.Location = new Point(12, 475); + buttonCreateAirBomber.Location = new Point(12, 562); buttonCreateAirBomber.Name = "buttonCreateAirBomber"; - buttonCreateAirBomber.Size = new Size(233, 23); + buttonCreateAirBomber.Size = new Size(223, 23); buttonCreateAirBomber.TabIndex = 1; - buttonCreateAirBomber.Text = "Создать военный самолёт"; + buttonCreateAirBomber.Text = "Создать бомбардировщик"; buttonCreateAirBomber.UseVisualStyleBackColor = true; buttonCreateAirBomber.Click += ButtonCreateAirBomber_Click; // @@ -63,22 +69,34 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(750, 469); + buttonLeft.Location = new Point(787, 550); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(35, 35); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; // + // buttonUp + // + buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonUp.BackgroundImage = Properties.Resources.arrowUp; + buttonUp.BackgroundImageLayout = ImageLayout.Stretch; + buttonUp.Location = new Point(828, 509); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new Size(35, 35); + buttonUp.TabIndex = 3; + buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += ButtonMove_Click; + // // buttonDown // buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.arrowDown; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(786, 469); + buttonDown.Location = new Point(828, 550); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(35, 35); - buttonDown.TabIndex = 3; + buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // @@ -87,50 +105,60 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.arrowRight; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(822, 469); + buttonRight.Location = new Point(869, 550); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(35, 35); - buttonRight.TabIndex = 4; + buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // - // buttonUp + // buttonCreateCar // - buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonUp.BackgroundImage = Properties.Resources.arrowUp; - buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(786, 433); - buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(35, 35); - buttonUp.TabIndex = 5; - buttonUp.UseVisualStyleBackColor = true; - buttonUp.Click += ButtonMove_Click; + buttonCreateCar.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateCar.Location = new Point(250, 562); + buttonCreateCar.Name = "buttonCreateTrans"; + buttonCreateCar.Size = new Size(223, 23); + buttonCreateCar.TabIndex = 6; + buttonCreateCar.Text = "Создать военный самолет"; + buttonCreateCar.UseVisualStyleBackColor = true; + buttonCreateCar.Click += ButtonCreateBomber_Click; // - // buttonCreateBomber + // comboBoxStrategy // - buttonCreateBomber.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateBomber.Location = new Point(261, 475); - buttonCreateBomber.Name = "buttonCreateBomber"; - buttonCreateBomber.Size = new Size(233, 23); - buttonCreateBomber.TabIndex = 6; - buttonCreateBomber.Text = "Создать самолёт"; - buttonCreateBomber.UseVisualStyleBackColor = true; - buttonCreateBomber.Click += buttonCreateBomber_Click; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(790, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; // - // FormAirBomber + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(836, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // + // FormElectroTrans // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(880, 510); - Controls.Add(buttonCreateBomber); - Controls.Add(buttonUp); + ClientSize = new Size(923, 597); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateCar); Controls.Add(buttonRight); Controls.Add(buttonDown); + Controls.Add(buttonUp); Controls.Add(buttonLeft); Controls.Add(buttonCreateAirBomber); Controls.Add(pictureBoxAirBomber); - Name = "FormAirBomber"; - Text = "Бомбардировщик"; + Name = "FormElectroTrans"; + Text = "Электропоезд"; ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).EndInit(); ResumeLayout(false); } @@ -140,9 +168,11 @@ private PictureBox pictureBoxAirBomber; private Button buttonCreateAirBomber; private Button buttonLeft; + private Button buttonUp; private Button buttonDown; private Button buttonRight; - private Button buttonUp; - private Button buttonCreateBomber; + private Button buttonCreateCar; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } -} \ No newline at end of file +} diff --git a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs index 4bc61dd..bcdbd7b 100644 --- a/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs +++ b/ProjectAirBomber/ProjectAirBomber/FormAirBomber.cs @@ -1,90 +1,87 @@ using ProjectAirBomber.Drawnings; +using ProjectAirBomber.MovementStrategy; namespace ProjectAirBomber; -/// -/// Форма работы с объектом "Бомбардировщик" -/// public partial class FormAirBomber : Form { /// - /// Поле-объект для прорисовки объекта + /// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà /// - private DrawningBomber? _drawningBomber; + private DrawningBomber? _drawingBomber; /// - /// Конструктор формы + /// Ñòðàòåãèÿ ïåðåìåùåíèÿ /// + private AbstractStrategy? _strategy; + public FormAirBomber() { InitializeComponent(); + _strategy = null; } + /// - /// Метод прорисовки бомбардировщика + /// Ìåòîä ïðîðèñîâêè ìàøèíû /// private void Draw() { - if (_drawningBomber == null) + if (_drawingBomber == null) { return; } Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningBomber.DrawTransport(gr); + _drawingBomber.DrawTransport(gr); pictureBoxAirBomber.Image = bmp; } - private void CreateObject(string type) { Random random = new(); switch (type) { case nameof(DrawningBomber): - _drawningBomber = new DrawningBomber(random.Next(100, 300), random.Next(1000, 3000), + _drawingBomber = new DrawningBomber(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(DrawningAirBomber): - _drawningBomber = new DrawningAirBomber(random.Next(100, 300), random.Next(1000, 3000), + _drawingBomber = new DrawningAirBomber(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))); + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); break; default: return; } - _drawningBomber.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); - _drawningBomber.SetPosition(random.Next(10, 100), random.Next(10, 100)); - - + _drawingBomber.SetPictureSize(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + _drawingBomber.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } - - /// - /// Обработка нажатия кнопки "Создать военный самолёт" + /// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ñïîðòèâíûé àâòîìîáèëü" /// /// /// private void ButtonCreateAirBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirBomber)); /// - /// Обработка нажатия кнопки "Создать самолёт" + /// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü àâòîìîáèëü" /// /// /// - - private void buttonCreateBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBomber)); - + private void ButtonCreateBomber_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBomber)); /// - /// Перемещение объекта по форме (нажатие кнопок навигации) - /// - /// - /// - private void ButtonMove_Click(object sender, EventArgs e) + /// Ïåðåìåùåíèå îáúåêòà ïî ôîðìå (íàæàòèå êíîïîê íàâèãàöèè) + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningBomber == null) + if (_drawingBomber == null) { return; } @@ -94,16 +91,16 @@ public partial class FormAirBomber : Form switch (name) { case "buttonUp": - result = _drawningBomber.MoveTransport(DirectionType.Up); + result = _drawingBomber.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawningBomber.MoveTransport(DirectionType.Down); + result = _drawingBomber.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawningBomber.MoveTransport(DirectionType.Left); + result = _drawingBomber.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawningBomber.MoveTransport(DirectionType.Right); + result = _drawingBomber.MoveTransport(DirectionType.Right); break; } @@ -112,8 +109,46 @@ public partial class FormAirBomber : Form Draw(); } } + /// + /// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingBomber == null) + { + return; + } - + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableTrans(_drawingBomber), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/MovementStrategy/AbstractStrategy.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..937f9a0 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,123 @@ + +namespace ProjectAirBomber.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/ProjectAirBomber/ProjectAirBomber/MovementStrategy/IMoveableObject.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..1dfe070 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,22 @@ + + +namespace ProjectAirBomber.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно +bool TryMoveObject(MovementDirection direction); +} + diff --git a/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToBorder.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..d3c61f8 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,52 @@ +using ProjectAirBomber.MovementStrategy; + +namespace ProjectAirBomber.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth + && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight + && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth - 5; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight - 5; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} \ No newline at end of file diff --git a/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToCenter.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..6966932 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,50 @@ + +namespace ProjectAirBomber.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/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs new file mode 100644 index 0000000..e3ebfd3 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MoveableTrans.cs @@ -0,0 +1,60 @@ +using ProjectAirBomber.Drawnings; + + +namespace ProjectAirBomber.MovementStrategy; +public class MoveableTrans : IMoveableObject +{ + /// + /// Поле-объект класса DrawningTrans или его наследника + /// + private readonly DrawningBomber? _bomber = null; + /// + /// Конструктор + /// + /// Объект класса DrawningTrans + public MoveableTrans(DrawningBomber bomber) + { + _bomber = bomber; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_bomber == null || _bomber.EntityBomber == null || + !_bomber.GetPosX.HasValue || !_bomber.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_bomber.GetPosX.Value, + _bomber.GetPosY.Value, _bomber.GetWidth, _bomber.GetHeight); + } + } + public int GetStep => (int)(_bomber?.EntityBomber?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_bomber == null || _bomber.EntityBomber == null) + { + return false; + } + return _bomber.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/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MovementDirection.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..e562c1c --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/MovementDirection.cs @@ -0,0 +1,21 @@ +namespace ProjectAirBomber.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} diff --git a/ProjectAirBomber/ProjectAirBomber/MovementStrategy/ObjectParameters.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..e8af281 --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,60 @@ +namespace ProjectAirBomber.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/ProjectAirBomber/ProjectAirBomber/MovementStrategy/StrategyStatus.cs b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..9dc71cb --- /dev/null +++ b/ProjectAirBomber/ProjectAirBomber/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,17 @@ +namespace ProjectAirBomber.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +}