From f10745536b02a8489c10159595aa8c70feb2b924 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Mon, 4 Mar 2024 14:08:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 4 +- .../DoubleDeckerBus/Drawnings/DrawingBus.cs | 4 +- .../Drawnings/DrawingDoubleDeckerBus.cs | 2 +- .../FormDoubleDeckerBus.Designer.cs | 28 +++++++++ .../DoubleDeckerBus/FormDoubleDeckerBus.cs | 52 ++++++++++++++++- .../MovementStrategy/MoveToCenter.cs | 58 +++++++++++++++++++ .../MovementStrategy/MoveableBus.cs | 48 +++++++++++++-- .../MovementStrategy/MovetoBorder.cs | 41 +++++++++++++ 8 files changed, 226 insertions(+), 11 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs index abde908..e569314 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs @@ -1,7 +1,9 @@ namespace DoubleDeckerBus.Drawnings; public enum DirectionType -{ /// +{ + Unknown = -1, + /// /// вверх /// Up = 1, diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs index 6df7581..1035a15 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs @@ -27,9 +27,9 @@ public class DrawingBus public int? GetPosY => _startPosY; - public int? GetWidth => _drawingBusWidth; + public int GetWidth => _drawingBusWidth; - public int? GetHeight => _drawingBusHeight; + public int GetHeight => _drawingBusHeight; private DrawingBus() { diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs index 0716c14..6e24d22 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs @@ -6,7 +6,7 @@ namespace DoubleDeckerBus.Drawnings; /// public class DrawingDoubleDeckerBus : DrawingBus { - public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,50) + public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,55) { EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs index e2bb000..a56dad7 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonRight = new Button(); CreateBus = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); SuspendLayout(); // @@ -117,11 +119,35 @@ CreateBus.UseVisualStyleBackColor = true; CreateBus.Click += buttonCreateBus_Click; // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "To center", "To border" }); + comboBoxStrategy.Location = new Point(667, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 8; + // + // buttonStrategyStep + // + buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStrategyStep.Location = new Point(713, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 9; + buttonStrategyStep.Text = "Do step"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormDoubleDeckerBus // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(CreateBus); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -144,5 +170,7 @@ private Button buttonDown; private Button buttonRight; private Button CreateBus; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs index dc611b8..e3168c9 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs @@ -8,15 +8,20 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using DoubleDeckerBus.Drawnings; +using DoubleDeckerBus.MovementStrategy; namespace DoubleDeckerBus { public partial class FormDoubleDeckerBus : Form { private DrawingBus? _drawingBus; + + private AbstractStrategy? _strategy; public FormDoubleDeckerBus() { InitializeComponent(); + _strategy = null; + } private void Draw() @@ -39,7 +44,7 @@ namespace DoubleDeckerBus { case nameof(DrawingBus): _drawingBus = new DrawingBus(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))); break; case nameof(DrawingDoubleDeckerBus): _drawingBus = new DrawingDoubleDeckerBus(random.Next(100, 300), random.Next(1000, 3000), @@ -53,6 +58,8 @@ namespace DoubleDeckerBus _drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -99,5 +106,48 @@ namespace DoubleDeckerBus } } + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingBus == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MovetoBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableBus(_drawingBus), pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + + } + else + { + if (_strategy == null) + { + return; + } + _strategy.MakeStep(); + Draw(); + } + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..86cf34f --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParametrs? objParams = GetObjectParaments; + if (objParams == null) + { + return false; + } + + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParametrs? objParams = GetObjectParaments; + 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/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs index a6a07dc..36006fd 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs @@ -1,4 +1,5 @@ -using System; +using DoubleDeckerBus.Drawnings; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,15 +7,50 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; -private DrawningBus? _drawningBus; + public class MoveableBus : IMoveableObject { - public ObjectParametrs? GetObjectPosition => throw new NotImplementedException(); + private DrawingBus? _bus = null; - public int GetStep => throw new NotImplementedException(); + + public MoveableBus(DrawingBus bus) + { + _bus = bus; + } + public ObjectParametrs? GetObjectPosition + { + get + { + if (_bus == null || _bus.EntityBus == null || !_bus.GetPosX.HasValue || !_bus.GetPosY.HasValue) + { + return null; + } + return new ObjectParametrs(_bus.GetPosX.Value, _bus.GetPosY.Value, _bus.GetWidth, _bus.GetHeight); + } + } + + public int GetStep => (int)(_bus?.EntityBus?.Step ?? 0); public bool TryMoveObject(MovementDirection direction) { - throw new NotImplementedException(); + if (_bus == null || _bus.EntityBus == null) + { + return false; + } + + return _bus.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.Unknown, + } ; + } + +} \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs new file mode 100644 index 0000000..f2edf42 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +internal class MovetoBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParametrs? objParams = GetObjectParaments; + if (objParams == null) + { + return false; + } + return objParams.DownBorder + GetStep() > FieldHeight - 5 && objParams.RightBorder + GetStep() > FieldWidth - 5; + } + + protected override void MoveToTarget() + { + ObjectParametrs? objParams = GetObjectParaments; + if (objParams == null) + { + return; + } + + int diffX = objParams.RightBorder - FieldWidth; + if(Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + + int diffY = objParams.DownBorder - FieldHeight; + if(Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } +}