From 17cb3342de7b2bcb2a52c640a10b72d2c5343bad Mon Sep 17 00:00:00 2001 From: ekallin Date: Thu, 5 Oct 2023 13:29:41 +0400 Subject: [PATCH] All lab 2 for push --- .../AbstractStrategy.cs | 14 +-------- .../DrawingElectricLocomotive.cs | 4 +-- .../DrawingLocomotive.cs | 17 ++++------- .../FormElectricLocomotive.Designer.cs | 3 +- .../FormElectricLocomotive.cs | 2 +- .../ProjectElectricLocomotive/MoveToCenter.cs | 2 +- .../MoveToRightCorner.cs | 29 +++++++++++++++++++ 7 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightCorner.cs diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs index 7fa8d67..85e6338 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs @@ -14,11 +14,8 @@ namespace ProjectElectricLocomotive.MovementStrategy private IMoveableObject? _moveableObject; private Status _state = Status.NotInit; - protected int FieldWidth { get; private set; } - protected int FieldHeight { get; private set; } - public Status GetStatus() { return _state; } public void SetData(IMoveableObject moveableObject, int width, int height) @@ -57,7 +54,6 @@ namespace ProjectElectricLocomotive.MovementStrategy protected bool MoveDown() => MoveTo(DirectionType.Down); /// Параметры объекта - protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; protected int? GetStep() @@ -68,17 +64,10 @@ namespace ProjectElectricLocomotive.MovementStrategy } return _moveableObject?.GetStep; } - /// - /// Перемещение к цели - /// protected abstract void MoveToTarget(); protected abstract bool IsTargetDestinaion(); - /// - /// Попытка перемещения в требуемом направлении - /// - /// Направление - /// Результат попытки (true - удалось переместиться, false -неудача) + private bool MoveTo(DirectionType directionType) { if (_state != Status.InProgress) @@ -91,7 +80,6 @@ namespace ProjectElectricLocomotive.MovementStrategy return true; } return false; - } } } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs index facebe4..92e2421 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs @@ -10,14 +10,13 @@ namespace ProjectElectricLocomotive.DrawingObjects public class DrawingElectricLocomotive : DrawingLocomotive { public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, - bool horns, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 150, 50) + bool horns, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 85, 50) { if (EntityLocomotive != null) { EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); } } - public override void DrawTransport(Graphics g) { if (EntityLocomotive is not EntityElectricLocomotive electricLocomotive) @@ -44,7 +43,6 @@ namespace ProjectElectricLocomotive.DrawingObjects if (electricLocomotive.SeifBatteries) { g.FillRectangle(blackBrush, _startPosX + 80, _startPosY + 30, 5, 10); - } base.DrawTransport(g); } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingLocomotive.cs index d87751d..fc1c21f 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingLocomotive.cs @@ -13,24 +13,20 @@ namespace ProjectElectricLocomotive.DrawingObjects { public EntityLocomotive? EntityLocomotive { get; protected set; } - private int _pictureWidth; + protected int _pictureWidth; - private int _pictureHeight; + protected int _pictureHeight; protected int _startPosX; protected int _startPosY; - protected readonly int _locoWidth = 150; + protected readonly int _locoWidth = 85; protected readonly int _locoHeight = 50; - public int GetPosX => _startPosX; - public int GetPosY => _startPosY; - public int GetWidth => _locoWidth; - public int GetHeight => _locoHeight; public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth) @@ -134,7 +130,6 @@ namespace ProjectElectricLocomotive.DrawingObjects } ); - g.DrawPolygon(pen, new Point[] { new Point(_startPosX, _startPosY + 40), @@ -179,10 +174,8 @@ namespace ProjectElectricLocomotive.DrawingObjects g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 5, 5); g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 5, 5); g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 5, 5); - } - public bool CanMove(DirectionType direction) { if (EntityLocomotive == null) @@ -196,9 +189,9 @@ namespace ProjectElectricLocomotive.DrawingObjects //вверх DirectionType.Up => _startPosY - EntityLocomotive.Step > 0, // вправо - DirectionType.Right => _startPosX + EntityLocomotive.Speed < _pictureWidth, + DirectionType.Right => _startPosX + EntityLocomotive.Step < _pictureWidth, //вниз - DirectionType.Down => _startPosY + EntityLocomotive.Speed < _pictureHeight, + DirectionType.Down => _startPosY + EntityLocomotive.Step < _pictureHeight, }; } } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs index 8dadd24..afaeb3c 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs @@ -116,7 +116,8 @@ this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxStrategy.FormattingEnabled = true; this.comboBoxStrategy.Items.AddRange(new object[] { - "MoveToCenter"}); + "MoveToCenter", + "MoveToRightCorner"}); this.comboBoxStrategy.Location = new System.Drawing.Point(1058, 12); this.comboBoxStrategy.Name = "comboBoxStrategy"; this.comboBoxStrategy.Size = new System.Drawing.Size(160, 33); diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs index 97dd5d4..fd8841c 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs @@ -87,7 +87,7 @@ namespace ElectricLocomotive _abstractStrategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), - //1 => new MoveToBorder(), + 1 => new MoveToRightCorner(), _ => null, }; if (_abstractStrategy == null) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs index 997b71c..6c9e3e2 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectElectricLocomotive.MovementStrategy { - internal class MoveToCenter : AbstractStrategy + public class MoveToCenter : AbstractStrategy { protected override bool IsTargetDestinaion() { diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightCorner.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightCorner.cs new file mode 100644 index 0000000..9a24d5e --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightCorner.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.MovementStrategy +{ + public class MoveToRightCorner : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) return false; + + return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep(); + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) return; + + if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight(); + if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown(); + + } + } +}