From 03b29c72f7ccb1cb53c74c979cf080d9fcb0847b Mon Sep 17 00:00:00 2001 From: RozhVan Date: Tue, 12 Mar 2024 11:05:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectPlane/ProjectPlane/DrawCont.cs | 176 ------------------ .../{ => Drawnings}/DirectionType.cs | 6 +- .../ProjectPlane/Drawnings/DrawCont.cs | 41 ++++ .../ProjectPlane/Drawnings/DrawningShip.cs | 175 +++++++++++++++++ .../ProjectPlane/Entities/EntityContainer.cs | 28 +++ .../ProjectPlane/Entities/EntityShip.cs | 24 +++ ProjectPlane/ProjectPlane/EntityContainer.cs | 48 ----- .../ProjectPlane/FormContainer.Designer.cs | 68 +++++-- ProjectPlane/ProjectPlane/FormContainer.cs | 168 +++++++++++------ .../MovementStrategy/AbstractStrategy.cs | 74 ++++++++ .../MovementStrategy/IMoveableObject.cs | 10 + .../MovementStrategy/MoveToBorder.cs | 52 ++++++ .../MovementStrategy/MoveToCenter.cs | 50 +++++ .../MovementStrategy/MoveableShip.cs | 49 +++++ .../MovementStrategy/MovementDirection.cs | 13 ++ .../MovementStrategy/ObjectParameters.cs | 33 ++++ .../MovementStrategy/StrategyStatus.cs | 11 ++ 17 files changed, 726 insertions(+), 300 deletions(-) delete mode 100644 ProjectPlane/ProjectPlane/DrawCont.cs rename ProjectPlane/ProjectPlane/{ => Drawnings}/DirectionType.cs (75%) create mode 100644 ProjectPlane/ProjectPlane/Drawnings/DrawCont.cs create mode 100644 ProjectPlane/ProjectPlane/Drawnings/DrawningShip.cs create mode 100644 ProjectPlane/ProjectPlane/Entities/EntityContainer.cs create mode 100644 ProjectPlane/ProjectPlane/Entities/EntityShip.cs delete mode 100644 ProjectPlane/ProjectPlane/EntityContainer.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/IMoveableObject.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/MoveableShip.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/MovementDirection.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/ObjectParameters.cs create mode 100644 ProjectPlane/ProjectPlane/MovementStrategy/StrategyStatus.cs diff --git a/ProjectPlane/ProjectPlane/DrawCont.cs b/ProjectPlane/ProjectPlane/DrawCont.cs deleted file mode 100644 index d26610b..0000000 --- a/ProjectPlane/ProjectPlane/DrawCont.cs +++ /dev/null @@ -1,176 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectPlane; - -public class DrawCont -{ - public EntityContainer? EntityContainer { get; private set; } - - private int? _PictureWidth; - - private int? _PictureHeight; - - private int? _StartPosX; - - private int? _StartPosY; - - private readonly int _drawingContWidth = 160; - - private readonly int _drawingContHeight = 90; - - public void Init(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane) - { - EntityContainer = new EntityContainer(); - EntityContainer.Init(speed, weight, shipColor, containerColor, container, crane); - _PictureWidth = null; - _PictureHeight = null; - _StartPosX = null; - _StartPosY = null; - } - - public bool SetPictureSize(int width, int height) - { - if (EntityContainer == null) - { - return false; - } - - if (width >= _drawingContWidth && height >= _drawingContHeight) - { - _PictureWidth = width; - _PictureHeight = height; - - if (_StartPosX.HasValue && _StartPosY.HasValue) - { - if (_StartPosX.Value + _drawingContWidth > _PictureWidth) - { - _StartPosX = _PictureWidth - _drawingContWidth; - } - if (_StartPosY.Value + _drawingContHeight > _PictureHeight) - { - _StartPosY = _PictureHeight - _drawingContHeight; - } - } - return true; - } - - return false; - } - - public void SetPosition(int x, int y) - { - if (!_PictureHeight.HasValue || !_PictureWidth.HasValue) - { - return; - } - - if (x < 0) - { - x = 0; - } - else if (x + _drawingContWidth > _PictureWidth) - { - x = _PictureWidth.Value - _drawingContWidth; - } - - if (y < 0) - { - y = 0; - } - else if (y + _drawingContHeight > _PictureHeight) - { - y = _PictureHeight.Value - _drawingContHeight; - } - - _StartPosX = x; - _StartPosY = y; - } - - public bool MoveTransport(DirectionType direction) - { - if (EntityContainer == null || !_StartPosX.HasValue || !_StartPosY.HasValue) - { - return false; - } - - switch (direction) - { - case DirectionType.Left: - if (_StartPosX.Value - EntityContainer.Step > 0) - { - _StartPosX -= (int)EntityContainer.Step; - } - return true; - - case DirectionType.Right: - if (_StartPosX.Value + EntityContainer.Step < _PictureWidth - _drawingContWidth) - { - _StartPosX += (int)EntityContainer.Step; - } - return true; - - case DirectionType.Up: - - if (_StartPosY.Value - EntityContainer.Step > 0) - { - _StartPosY -= (int)EntityContainer.Step; - } - return true; - - case DirectionType.Down: - if (_StartPosY.Value + EntityContainer.Step < _PictureHeight - _drawingContHeight) - { - _StartPosY += (int)EntityContainer.Step; - } - return true; - default: - return false; - } - - } - - public void DrawTransport(Graphics g) - { - if (EntityContainer == null || !_StartPosX.HasValue || !_StartPosY.HasValue) - { - return; - } - - Pen pen = new(Color.Black); - Brush ContainerBrush = new SolidBrush(EntityContainer.ContainerColor); - - - // отрисовка контейнера - if (EntityContainer.Container) - { - g.DrawRectangle(pen, _StartPosX.Value + 80, _StartPosY.Value, 60, 40); - g.FillRectangle(ContainerBrush, _StartPosX.Value + 81, _StartPosY.Value + 1, 59, 39); - } - - Brush ShipBrush = new SolidBrush(EntityContainer.ShipColor); - - //отрисовка корабля - Point[] points = - { - new Point(_StartPosX.Value, _StartPosY.Value + 40), - new Point(_StartPosX.Value + 160, _StartPosY.Value + 40), - new Point(_StartPosX.Value + 150, _StartPosY.Value + 90), - new Point(_StartPosX.Value + 10, _StartPosY.Value + 90), - }; - - g.DrawPolygon(pen, points); - g.FillPolygon(ShipBrush, points); - - //отрисовка крана - if (EntityContainer.Crane) - { - g.DrawLine(pen, _StartPosX.Value + 30, _StartPosY.Value + 50, _StartPosX.Value + 30, _StartPosY.Value + 70); - g.DrawLine(pen, _StartPosX.Value + 20, _StartPosY.Value + 60, _StartPosX.Value + 40, _StartPosY.Value + 60); - } - - } -} diff --git a/ProjectPlane/ProjectPlane/DirectionType.cs b/ProjectPlane/ProjectPlane/Drawnings/DirectionType.cs similarity index 75% rename from ProjectPlane/ProjectPlane/DirectionType.cs rename to ProjectPlane/ProjectPlane/Drawnings/DirectionType.cs index 01c2988..4446242 100644 --- a/ProjectPlane/ProjectPlane/DirectionType.cs +++ b/ProjectPlane/ProjectPlane/Drawnings/DirectionType.cs @@ -4,15 +4,17 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectPlane; +namespace ProjectPlane.Drawnings; public enum DirectionType { + Unknow = -1, + Up = 1, Down = 2, Left = 3, - Right = 4, + Right = 4 } diff --git a/ProjectPlane/ProjectPlane/Drawnings/DrawCont.cs b/ProjectPlane/ProjectPlane/Drawnings/DrawCont.cs new file mode 100644 index 0000000..9baad12 --- /dev/null +++ b/ProjectPlane/ProjectPlane/Drawnings/DrawCont.cs @@ -0,0 +1,41 @@ +using ProjectPlane.Entities; + +namespace ProjectPlane.Drawnings; + +public class DrawCont : DrawningShip +{ + public DrawCont(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane) : base(160, 90) + { + EntityShip = new EntityContainer(speed, weight, shipColor, containerColor, container, crane); + } + + public override void DrawTransport(Graphics g) + { + if (EntityShip == null || !_StartPosX.HasValue || !_StartPosY.HasValue || EntityShip is not EntityContainer container) + { + return; + } + + Pen pen = new(Color.Black); + Brush ContainerBrush = new SolidBrush(container.ContainerColor); + + // отрисовка контейнера + if (container.Container) + { + g.DrawRectangle(pen, _StartPosX.Value + 80, _StartPosY.Value, 60, 40); + g.FillRectangle(ContainerBrush, _StartPosX.Value + 81, _StartPosY.Value + 1, 59, 39); + } + + _StartPosY += 40; + base.DrawTransport(g); + _StartPosY -= 40; + + //отрисовка крана + if (container.Crane) + { + g.DrawLine(pen, _StartPosX.Value + 30, _StartPosY.Value + 50, _StartPosX.Value + 30, _StartPosY.Value + 70); + g.DrawLine(pen, _StartPosX.Value + 20, _StartPosY.Value + 60, _StartPosX.Value + 40, _StartPosY.Value + 60); + } + } + +} diff --git a/ProjectPlane/ProjectPlane/Drawnings/DrawningShip.cs b/ProjectPlane/ProjectPlane/Drawnings/DrawningShip.cs new file mode 100644 index 0000000..6731722 --- /dev/null +++ b/ProjectPlane/ProjectPlane/Drawnings/DrawningShip.cs @@ -0,0 +1,175 @@ +using ProjectPlane.Entities; + +namespace ProjectPlane.Drawnings; + +public class DrawningShip +{ + public EntityShip? EntityShip { get; protected set; } + + private int? _PictureWidth; + + private int? _PictureHeight; + + protected int? _StartPosX; + + protected int? _StartPosY; + + private readonly int _drawingContWidth = 160; + + private readonly int _drawingContHeight = 50; + + public int? GetPosX => _StartPosX; + + public int? GetPosY => _StartPosY; + + public int GetWidth => _drawingContWidth; + + public int GetHeight => _drawingContHeight; + + private DrawningShip() + { + _PictureWidth = null; + _PictureHeight = null; + _StartPosX = null; + _StartPosY = null; + } + + public DrawningShip(int speed, double weight, Color shipColor) : this() + { + EntityShip = new EntityShip(speed, weight, shipColor); + } + + protected DrawningShip(int drawingShipWidth, int drawingShipHeight) : this() + { + _drawingContWidth = drawingShipWidth; + _drawingContHeight = drawingShipHeight; + } + + public bool SetPictureSize(int width, int height) + { + if (EntityShip == null) + { + return false; + } + + if (width >= _drawingContWidth && height >= _drawingContHeight) + { + _PictureWidth = width; + _PictureHeight = height; + + if (_StartPosX.HasValue && _StartPosY.HasValue) + { + if (_StartPosX.Value + _drawingContWidth > width) + { + _StartPosX = width - _drawingContWidth; + } + if (_StartPosY.Value + _drawingContHeight > height) + { + _StartPosY = _PictureHeight - height; + } + } + return true; + } + + return false; + } + + public void SetPosition(int x, int y) + { + if (!_PictureHeight.HasValue || !_PictureWidth.HasValue) + { + return; + } + + if (x < 0) + { + x = 0; + } + else if (x + _drawingContWidth > _PictureWidth) + { + x = _PictureWidth.Value - _drawingContWidth; + } + + if (y < 0) + { + y = 0; + } + else if (y + _drawingContHeight > _PictureHeight) + { + y = _PictureHeight.Value - _drawingContHeight; + } + + _StartPosX = x; + _StartPosY = y; + } + + public bool MoveTransport(DirectionType direction) + { + if (EntityShip == null || !_StartPosX.HasValue || !_StartPosY.HasValue) + { + return false; + } + + switch (direction) + { + case DirectionType.Left: + if (_StartPosX.Value - EntityShip.Step > 0) + { + _StartPosX -= (int)EntityShip.Step; + } + return true; + + case DirectionType.Right: + if (_StartPosX.Value + EntityShip.Step < _PictureWidth - _drawingContWidth) + { + _StartPosX += (int)EntityShip.Step; + } + return true; + + case DirectionType.Up: + + if (_StartPosY.Value - EntityShip.Step > 0) + { + _StartPosY -= (int)EntityShip.Step; + } + return true; + + case DirectionType.Down: + if (_StartPosY.Value + EntityShip.Step < _PictureHeight - _drawingContHeight) + { + _StartPosY += (int)EntityShip.Step; + } + return true; + default: + return false; + } + + } + + public virtual void DrawTransport(Graphics g) + { + if (EntityShip == null || !_StartPosX.HasValue || !_StartPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + + + Brush ShipBrush = new SolidBrush(EntityShip.ShipColor); + + //отрисовка корабля + Point[] points = + { + new Point(_StartPosX.Value, _StartPosY.Value), + new Point(_StartPosX.Value + 160, _StartPosY.Value), + new Point(_StartPosX.Value + 150, _StartPosY.Value + 50), + new Point(_StartPosX.Value + 10, _StartPosY.Value + 50), + }; + + g.DrawPolygon(pen, points); + g.FillPolygon(ShipBrush, points); + + + } +} diff --git a/ProjectPlane/ProjectPlane/Entities/EntityContainer.cs b/ProjectPlane/ProjectPlane/Entities/EntityContainer.cs new file mode 100644 index 0000000..a23e130 --- /dev/null +++ b/ProjectPlane/ProjectPlane/Entities/EntityContainer.cs @@ -0,0 +1,28 @@ +namespace ProjectPlane.Entities; +/// +/// Класс-сущность "Контейнеровоз" +/// +public class EntityContainer : EntityShip +{ + /// + /// Цвет контейнера + /// + public Color ContainerColor { get; private set; } + + /// + /// Признак наличия контейнера + /// + public bool Container { get; private set; } + + /// + /// Признак наличия крана + /// + public bool Crane { get; private set; } + + public EntityContainer(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane) : base(speed, weight, shipColor) + { + ContainerColor = containerColor; + Container = container; + Crane = crane; + } +} diff --git a/ProjectPlane/ProjectPlane/Entities/EntityShip.cs b/ProjectPlane/ProjectPlane/Entities/EntityShip.cs new file mode 100644 index 0000000..f2a8815 --- /dev/null +++ b/ProjectPlane/ProjectPlane/Entities/EntityShip.cs @@ -0,0 +1,24 @@ +namespace ProjectPlane.Entities; +public class EntityShip +{ + public int Speed { get; private set; } + + /// + /// Вес + /// + public double Weight { get; private set; } + + /// + /// Цвет контейнеровоза + /// + public Color ShipColor { get; private set; } + + public double Step => Speed * 10 / Weight; + + public EntityShip(int speed, double weight, Color shipColor) + { + Speed = speed; + Weight = weight; + ShipColor = shipColor; + } +} \ No newline at end of file diff --git a/ProjectPlane/ProjectPlane/EntityContainer.cs b/ProjectPlane/ProjectPlane/EntityContainer.cs deleted file mode 100644 index 5bc1409..0000000 --- a/ProjectPlane/ProjectPlane/EntityContainer.cs +++ /dev/null @@ -1,48 +0,0 @@ -namespace ProjectPlane; -/// -/// Класс-сущность "Контейнеровоз" -/// -public class EntityContainer -{ - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } - - /// - /// Цвет контейнеровоза - /// - public Color ShipColor { get; private set; } - - /// - /// Цвет контейнера - /// - public Color ContainerColor { get; private set; } - - /// - /// Признак наличия контейнера - /// - public bool Container { get; private set; } - - /// - /// Признак наличия крана - /// - public bool Crane { get; private set; } - - public double Step => Speed * 10 / Weight; - - public void Init(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane) - { - Speed = speed; - Weight = weight; - ContainerColor = containerColor; - ShipColor = shipColor; - Container = container; - Crane = crane; - } -} diff --git a/ProjectPlane/ProjectPlane/FormContainer.Designer.cs b/ProjectPlane/ProjectPlane/FormContainer.Designer.cs index 241a988..d1a821d 100644 --- a/ProjectPlane/ProjectPlane/FormContainer.Designer.cs +++ b/ProjectPlane/ProjectPlane/FormContainer.Designer.cs @@ -34,6 +34,9 @@ buttonRight = new Button(); buttonUp = new Button(); buttonDown = new Button(); + buttonCreateShip = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCont).BeginInit(); SuspendLayout(); // @@ -42,18 +45,18 @@ pictureBoxCont.Dock = DockStyle.Fill; pictureBoxCont.Location = new Point(0, 0); pictureBoxCont.Name = "pictureBoxCont"; - pictureBoxCont.Size = new Size(804, 468); + pictureBoxCont.Size = new Size(898, 554); pictureBoxCont.TabIndex = 0; pictureBoxCont.TabStop = false; // // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 433); + buttonCreate.Location = new Point(12, 519); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); + buttonCreate.Size = new Size(167, 23); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать контейнеровоз"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += ButtonCreate_Click; // @@ -62,9 +65,9 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.влево; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(643, 416); + buttonLeft.Location = new Point(748, 507); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(40, 40); + buttonLeft.Size = new Size(34, 35); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; @@ -74,9 +77,9 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.вправо; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(735, 416); + buttonRight.Location = new Point(828, 507); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(40, 40); + buttonRight.Size = new Size(34, 35); buttonRight.TabIndex = 3; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; @@ -86,9 +89,9 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.вверх; buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(689, 370); + buttonUp.Location = new Point(788, 466); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(40, 40); + buttonUp.Size = new Size(34, 35); buttonUp.TabIndex = 4; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; @@ -98,18 +101,52 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.вниз; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(689, 416); + buttonDown.Location = new Point(788, 507); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(40, 40); + buttonDown.Size = new Size(34, 35); buttonDown.TabIndex = 5; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // + // buttonCreateShip + // + buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateShip.Location = new Point(185, 519); + buttonCreateShip.Name = "buttonCreateShip"; + buttonCreateShip.Size = new Size(167, 23); + buttonCreateShip.TabIndex = 6; + buttonCreateShip.Text = "Создать корабль"; + buttonCreateShip.UseVisualStyleBackColor = true; + buttonCreateShip.Click += ButtonCreateShip_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(765, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(811, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormContainer // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(804, 468); + ClientSize = new Size(898, 554); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateShip); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonRight); @@ -117,7 +154,7 @@ Controls.Add(buttonCreate); Controls.Add(pictureBoxCont); Name = "FormContainer"; - Text = "FormContainer"; + Text = "Контейнеровоз"; ((System.ComponentModel.ISupportInitialize)pictureBoxCont).EndInit(); ResumeLayout(false); } @@ -130,5 +167,8 @@ private Button buttonRight; private Button buttonUp; private Button buttonDown; + private Button buttonCreateShip; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectPlane/ProjectPlane/FormContainer.cs b/ProjectPlane/ProjectPlane/FormContainer.cs index 343a696..a1bb4b5 100644 --- a/ProjectPlane/ProjectPlane/FormContainer.cs +++ b/ProjectPlane/ProjectPlane/FormContainer.cs @@ -1,83 +1,131 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using ProjectPlane.Drawnings; +using ProjectPlane.MovementStrategy; -namespace ProjectPlane +namespace ProjectPlane; + +public partial class FormContainer : Form { - public partial class FormContainer : Form + + private DrawningShip? _drawShip; + + private AbstractStrategy? _strategy; + + public FormContainer() { + InitializeComponent(); + _strategy = null; + } - private DrawCont? _drawCont; - - public FormContainer() + private void Draw() + { + if (_drawShip == null) { - InitializeComponent(); + return; } - private void Draw() + Bitmap bmp = new(pictureBoxCont.Width, pictureBoxCont.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawShip.DrawTransport(gr); + pictureBoxCont.Image = bmp; + } + + private void CreateObject(string type) + { + Random random = new Random(); + switch (type) { - if (_drawCont == null) - { + case nameof(DrawningShip): + _drawShip = new DrawningShip(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(DrawCont): + _drawShip = new DrawCont(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; - } + } + _drawShip.SetPictureSize(pictureBoxCont.Width, pictureBoxCont.Height); + _drawShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; + Draw(); + } - Bitmap bmp = new(pictureBoxCont.Width, pictureBoxCont.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawCont.DrawTransport(gr); - pictureBoxCont.Image = bmp; + private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawCont)); + + private void ButtonCreateShip_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningShip)); + + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawShip == null) + { + return; } - - private void ButtonCreate_Click(object sender, EventArgs e) + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) { - Random random = new Random(); - _drawCont = new DrawCont(); - _drawCont.Init(random.Next(100, 300), random.Next(100, 300), - 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))); - _drawCont.SetPictureSize(pictureBoxCont.Width, pictureBoxCont.Height); - _drawCont.SetPosition(random.Next(10, 100), random.Next(10, 100)); + case "buttonUp": + result = _drawShip.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = _drawShip.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + result = _drawShip.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + result = _drawShip.MoveTransport(DirectionType.Right); + break; + } + if (result) + { Draw(); } + } - private void ButtonMove_Click(object sender, EventArgs e) + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawShip == null) { - if (_drawCont == null) + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) { return; } - - string name = ((Button)sender)?.Name ?? string.Empty; - bool result = false; - switch (name) - { - case "buttonUp": - result = _drawCont.MoveTransport(DirectionType.Up); - break; - case "buttonDown": - result = _drawCont.MoveTransport(DirectionType.Down); - break; - case "buttonLeft": - result = _drawCont.MoveTransport(DirectionType.Left); - break; - case "buttonRight": - result = _drawCont.MoveTransport(DirectionType.Right); - break; - } - - if (result) - { - Draw(); - } - + _strategy.SetData(new MoveableShip(_drawShip), pictureBoxCont.Width, pictureBoxCont.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/ProjectPlane/ProjectPlane/MovementStrategy/AbstractStrategy.cs b/ProjectPlane/ProjectPlane/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..5c54025 --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,74 @@ +namespace ProjectPlane.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(); + } + + + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + + protected bool MoveRight() => MoveTo(MovementDirection.Right); + + protected bool MoveUp() => MoveTo(MovementDirection.Up); + + 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(); + + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} diff --git a/ProjectPlane/ProjectPlane/MovementStrategy/IMoveableObject.cs b/ProjectPlane/ProjectPlane/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..4c110f6 --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,10 @@ +namespace ProjectPlane.MovementStrategy; + +public interface IMoveableObject +{ + ObjectParameters? GetObjectPosition { get; } + + int GetStep { get; } + + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectPlane/ProjectPlane/MovementStrategy/MoveToBorder.cs b/ProjectPlane/ProjectPlane/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..5c94686 --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,52 @@ +namespace ProjectPlane.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.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectPlane/ProjectPlane/MovementStrategy/MoveToCenter.cs b/ProjectPlane/ProjectPlane/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..05c9ed7 --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,50 @@ +namespace ProjectPlane.MovementStrategy; + +internal 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/ProjectPlane/ProjectPlane/MovementStrategy/MoveableShip.cs b/ProjectPlane/ProjectPlane/MovementStrategy/MoveableShip.cs new file mode 100644 index 0000000..289b219 --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/MoveableShip.cs @@ -0,0 +1,49 @@ +using ProjectPlane.Drawnings; + +namespace ProjectPlane.MovementStrategy; + +public class MoveableShip : IMoveableObject +{ + private readonly DrawningShip? _ship = null; + + public MoveableShip(DrawningShip ship) + { + _ship = ship; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_ship == null || _ship.EntityShip == null || !_ship.GetPosX.HasValue || !_ship.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_ship.GetPosX.Value, _ship.GetPosY.Value, _ship.GetWidth, _ship.GetHeight); + } + } + + public int GetStep => (int)(_ship?.EntityShip?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_ship == null || _ship.EntityShip == null) + { + return false; + } + return _ship.MoveTransport(GetDirectionType(direction)); + } + + 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/ProjectPlane/ProjectPlane/MovementStrategy/MovementDirection.cs b/ProjectPlane/ProjectPlane/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..9e09395 --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/MovementDirection.cs @@ -0,0 +1,13 @@ + +namespace ProjectPlane.MovementStrategy; + +public enum MovementDirection +{ + Up = 1, + + Down = 2, + + Left = 3, + + Right = 4 +} diff --git a/ProjectPlane/ProjectPlane/MovementStrategy/ObjectParameters.cs b/ProjectPlane/ProjectPlane/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..9ea8bc6 --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,33 @@ +namespace ProjectPlane.MovementStrategy; + +public class ObjectParameters +{ + private readonly int _x; + + 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; + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + +} diff --git a/ProjectPlane/ProjectPlane/MovementStrategy/StrategyStatus.cs b/ProjectPlane/ProjectPlane/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..d8840bc --- /dev/null +++ b/ProjectPlane/ProjectPlane/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,11 @@ + +namespace ProjectPlane.MovementStrategy; + +public enum StrategyStatus +{ + NotInit, + + InProgress, + + Finish +} -- 2.25.1