From a1a6743ce10491c4b854731fef18ce6e4f53e9e1 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Thu, 12 Oct 2023 18:32:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Trolleybus/Trolleybus/AbstractStrategy.cs | 72 ++++++++ Trolleybus/Trolleybus/DirectionType.cs | 2 +- Trolleybus/Trolleybus/DrawingBus.cs | 155 ++++++++++++++++++ Trolleybus/Trolleybus/DrawingObjectBus.cs | 36 ++++ Trolleybus/Trolleybus/DrawingTrolleybus.cs | 142 ++-------------- Trolleybus/Trolleybus/EntityBus.cs | 26 +++ Trolleybus/Trolleybus/EntityTrolleybus.cs | 38 +---- .../Trolleybus/FormTrolleybus.Designer.cs | 87 ++++++++-- Trolleybus/Trolleybus/FormTrolleybus.cs | 80 +++++++-- Trolleybus/Trolleybus/IMoveableObject.cs | 29 ++++ Trolleybus/Trolleybus/MoveToBorder.cs | 42 +++++ Trolleybus/Trolleybus/MoveToCenter.cs | 56 +++++++ Trolleybus/Trolleybus/ObjectParameters.cs | 56 +++++++ Trolleybus/Trolleybus/Program.cs | 2 +- Trolleybus/Trolleybus/Status.cs | 15 ++ 15 files changed, 646 insertions(+), 192 deletions(-) create mode 100644 Trolleybus/Trolleybus/AbstractStrategy.cs create mode 100644 Trolleybus/Trolleybus/DrawingBus.cs create mode 100644 Trolleybus/Trolleybus/DrawingObjectBus.cs create mode 100644 Trolleybus/Trolleybus/EntityBus.cs create mode 100644 Trolleybus/Trolleybus/IMoveableObject.cs create mode 100644 Trolleybus/Trolleybus/MoveToBorder.cs create mode 100644 Trolleybus/Trolleybus/MoveToCenter.cs create mode 100644 Trolleybus/Trolleybus/ObjectParameters.cs create mode 100644 Trolleybus/Trolleybus/Status.cs diff --git a/Trolleybus/Trolleybus/AbstractStrategy.cs b/Trolleybus/Trolleybus/AbstractStrategy.cs new file mode 100644 index 0000000..44693cb --- /dev/null +++ b/Trolleybus/Trolleybus/AbstractStrategy.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTrolleybus.MovementStrategy +{ + public abstract class AbstractStrategy + { + 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) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + protected bool MoveLeft() => MoveTo(DirectionType.Left); + protected bool MoveRight() => MoveTo(DirectionType.Right); + protected bool MoveUp() => MoveTo(DirectionType.Up); + protected bool MoveDown() => MoveTo(DirectionType.Down); + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + protected abstract void MoveToTarget(); + protected abstract bool IsTargetDestinaion(); + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + + } +} diff --git a/Trolleybus/Trolleybus/DirectionType.cs b/Trolleybus/Trolleybus/DirectionType.cs index f56c58b..d15293f 100644 --- a/Trolleybus/Trolleybus/DirectionType.cs +++ b/Trolleybus/Trolleybus/DirectionType.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Trolleybus +namespace ProjectTrolleybus { public enum DirectionType { diff --git a/Trolleybus/Trolleybus/DrawingBus.cs b/Trolleybus/Trolleybus/DrawingBus.cs new file mode 100644 index 0000000..6aec406 --- /dev/null +++ b/Trolleybus/Trolleybus/DrawingBus.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTrolleybus.Entities; + +namespace ProjectTrolleybus.DrawingObjects +{ + public class DrawingBus + { + public EntityBus? EntityBus { get; protected set; } + + private int _pictureWidth; + + private int _pictureHeight; + + protected int _startPosX; + + protected int _startPosY; + + protected readonly int _busWidth = 170; + + protected readonly int _busHeight = 124; + + public DrawingBus(int speed, double weight, Color bodyColor, int + width, int height) + + { + if (width < _busWidth || height < _busHeight) + return; + _pictureWidth = width; + _pictureHeight = height; + EntityBus = new EntityBus(speed, weight, bodyColor); + } + + protected DrawingBus(int speed, double weight, Color bodyColor, int + width, int height, int busWidth, int busHeight) + { + if (width < _busWidth || height < _busHeight) + return; + _pictureWidth = width; + _pictureHeight = height; + _busWidth = busWidth; + _busHeight = busHeight; + EntityBus = new EntityBus(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y) + { + if (x < 0 || y < 0 || x + _busWidth >= _pictureWidth || y + _busHeight >= _pictureHeight) + x = y = 10; + _startPosX = x; + _startPosY = y; + } + public int GetPosX => _startPosX; + + public int GetPosY => _startPosY; + + public int GetWidth => _busWidth; + + public int GetHeight => _busHeight; + + public bool CanMove(DirectionType direction) + { + if (EntityBus == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityBus.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityBus.Step > 0, + // вправо + DirectionType.Right => _startPosX + EntityBus.Step + _busWidth < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + EntityBus.Step + _busHeight < _pictureHeight, + _ => false, + }; + } + + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityBus == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + _startPosX -= (int)EntityBus.Step; + break; + //вверх + case DirectionType.Up: + _startPosY -= (int)EntityBus.Step; + break; + // вправо + case DirectionType.Right: + _startPosX += (int)EntityBus.Step; + break; + //вниз + case DirectionType.Down: + _startPosY += (int)EntityBus.Step; + break; + } + } + + public virtual void DrawTransport(Graphics g) + { + if (EntityBus == null) + { + return; + } + Pen pen = new(Color.Black); + //кузов + Brush br = new SolidBrush(EntityBus.BodyColor); + g.FillRectangle(br, _startPosX + 6, _startPosY + 31, 164, 79); + //задние фары + Brush brRed = new SolidBrush(Color.Red); + g.FillRectangle(brRed, _startPosX + 5, _startPosY + 85, 10, 20); + //передние фары + Brush brYellow = new SolidBrush(Color.Yellow); + g.FillRectangle(brYellow, _startPosX + 160, _startPosY + 85, 10, 20); + //стекла + Brush brBlue = new SolidBrush(Color.LightBlue); + g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 40, 20, 40); + g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 40, 20, 40); + g.FillEllipse(brBlue, _startPosX + 35, _startPosY + 40, 20, 40); + g.FillEllipse(brBlue, _startPosX + 95, _startPosY + 40, 20, 40); + g.FillEllipse(brBlue, _startPosX + 120, _startPosY + 40, 20, 40); + //дверь + Brush brDoor = new SolidBrush(EntityBus.BodyColor); + g.FillRectangle(brDoor, _startPosX + 60, _startPosY + 50, 30, 60); + //колеса + Brush brblack = new SolidBrush(Color.Black); + g.FillEllipse(brblack, _startPosX + 25, _startPosY + 95, 30, 30); + g.FillEllipse(brblack, _startPosX + 120, _startPosY + 95, 30, 30); + //границы троллейбуса + g.DrawRectangle(pen, _startPosX + 5, _startPosY + 30, 165, 80); + g.DrawEllipse(pen, _startPosX + 25, _startPosY + 95, 30, 30); + g.DrawEllipse(pen, _startPosX + 120, _startPosY + 95, 30, 30); + g.DrawRectangle(pen, _startPosX + 5, _startPosY + 85, 10, 20); + g.DrawRectangle(pen, _startPosX + 160, _startPosY + 85, 10, 20); + g.DrawRectangle(pen, _startPosX + 60, _startPosY + 50, 30, 60); + g.DrawRectangle(pen, _startPosX + 150, _startPosY + 40, 20, 40); + g.DrawEllipse(pen, _startPosX + 10, _startPosY + 40, 20, 40); + g.DrawEllipse(pen, _startPosX + 35, _startPosY + 40, 20, 40); + g.DrawEllipse(pen, _startPosX + 95, _startPosY + 40, 20, 40); + g.DrawEllipse(pen, _startPosX + 120, _startPosY + 40, 20, 40); + } + } +} diff --git a/Trolleybus/Trolleybus/DrawingObjectBus.cs b/Trolleybus/Trolleybus/DrawingObjectBus.cs new file mode 100644 index 0000000..2c4ca72 --- /dev/null +++ b/Trolleybus/Trolleybus/DrawingObjectBus.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTrolleybus.DrawingObjects; + +namespace ProjectTrolleybus.MovementStrategy +{ + public class DrawingObjectBus : IMoveableObject + { + private readonly DrawingBus? _drawningCar = null; + public DrawingObjectBus(DrawingBus drawningCar) + { + _drawningCar = drawningCar; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningCar == null || _drawningCar.EntityBus == + null) + { + return null; + } + return new ObjectParameters(_drawningCar.GetPosX, + _drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight); + } + } + public int GetStep => (int)(_drawningCar?.EntityBus?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawningCar?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawningCar?.MoveTransport(direction); + } +} diff --git a/Trolleybus/Trolleybus/DrawingTrolleybus.cs b/Trolleybus/Trolleybus/DrawingTrolleybus.cs index 504ea31..e2428e0 100644 --- a/Trolleybus/Trolleybus/DrawingTrolleybus.cs +++ b/Trolleybus/Trolleybus/DrawingTrolleybus.cs @@ -1,163 +1,53 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.NetworkInformation; using System.Numerics; using System.Text; using System.Threading.Tasks; +using ProjectTrolleybus.DrawingObjects; +using ProjectTrolleybus.Entities; -namespace Trolleybus +namespace ProjectTrolleybus { - /// - /// Класс, отвечающий за прорисовку и перемещение объекта-сущности - /// - public class DrawingTrolleybus + public class DrawingTrolleybus : DrawingBus { - - /// - /// Класс-сущность - /// - public EntityTrolleybus? EntityTrolleybus { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// Левая координата прорисовки автомобиля - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки автомобиля - /// - private int _startPosY; - /// - /// Ширина прорисовки автомобиля - /// - private readonly int _trolleybusWidth = 170; - /// - /// Высота прорисовки автомобиля - /// - private readonly int _trolleybusHeight = 124; - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool roga, bool battery, int width, int height) + public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, bool roga, bool battery, int width, int height) + : base(speed, weight, bodyColor, width, height, 170, 124) { - _pictureWidth = width; - _pictureHeight = height; - if (_trolleybusWidth > _pictureWidth || _trolleybusHeight > _pictureHeight) - return false; - EntityTrolleybus = new EntityTrolleybus(); - EntityTrolleybus.Init(speed, weight, bodyColor, additionalColor, roga, battery); - return true; - } - public void SetPosition(int x, int y) - { - if (EntityTrolleybus == null) + if (EntityBus != null) { - return; - } - if (x < 0 || y < 0 || x + _trolleybusWidth >= _pictureWidth || y + _trolleybusHeight >= _pictureHeight) - x = y = 10; - _startPosX = x; - _startPosY = y; - } - public void MoveTransport(DirectionType direction) - { - if (EntityTrolleybus == null) - { - return; - } - switch (direction) - { - case DirectionType.Left: - if (_startPosX - EntityTrolleybus.Step > 0) - { - _startPosX -= (int)EntityTrolleybus.Step; - } - break; - case DirectionType.Up: - if (_startPosY - EntityTrolleybus.Step > 0) - { - _startPosY -= (int)EntityTrolleybus.Step; - } - break; - case DirectionType.Right: - if (_startPosX + EntityTrolleybus.Step + _trolleybusWidth < _pictureWidth) - { - _startPosX += (int)EntityTrolleybus.Step; - } - break; - case DirectionType.Down: - if (_startPosY + EntityTrolleybus.Step + _trolleybusHeight < _pictureHeight) - { - _startPosY += (int)EntityTrolleybus.Step; - } - break; + EntityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, roga, battery); } } - public void DrawTransport(Graphics g) + public override void DrawTransport(Graphics g) { - if (EntityTrolleybus == null) + if (EntityBus is not EntityTrolleybus trolleybus) { return; } Pen pen = new(Color.Black); Brush additionalBrush = new - SolidBrush(EntityTrolleybus.AdditionalColor); - //кузов - Brush br = new SolidBrush(EntityTrolleybus.BodyColor); - g.FillRectangle(br, _startPosX + 6, _startPosY + 31, 164, 79); + SolidBrush(trolleybus.AdditionalColor); + base.DrawTransport(g); //"рога" - if (EntityTrolleybus.Roga) + if (trolleybus.Roga) { g.DrawLine(new Pen(Color.Black, 3), _startPosX + 120, _startPosY + 30, _startPosX + 20, _startPosY + 3); g.DrawLine(new Pen(Color.Black, 3), _startPosX + 140, _startPosY + 30, _startPosX + 40, _startPosY + 3); g.DrawLine(new Pen(Color.Black, 1), _startPosX + 40, _startPosY + 30, _startPosX + 20, _startPosY + 3); g.DrawLine(new Pen(Color.Black, 1), _startPosX + 60, _startPosY + 30, _startPosX + 40, _startPosY + 3); } - //задние фары - Brush brRed = new SolidBrush(Color.Red); - g.FillRectangle(brRed, _startPosX + 5, _startPosY + 85, 10, 20); - //передние фары - Brush brYellow = new SolidBrush(Color.Yellow); - g.FillRectangle(brYellow, _startPosX + 160, _startPosY + 85, 10, 20); - //стекла - Brush brBlue = new SolidBrush(Color.LightBlue); - g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 40, 20, 40); - g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 40, 20, 40); - g.FillEllipse(brBlue, _startPosX + 35, _startPosY + 40, 20, 40); - g.FillEllipse(brBlue, _startPosX + 95, _startPosY + 40, 20, 40); - g.FillEllipse(brBlue, _startPosX + 120, _startPosY + 40, 20, 40); - //дверь - Brush brDoor = new SolidBrush(EntityTrolleybus.BodyColor); - g.FillRectangle(brDoor, _startPosX + 60, _startPosY + 50, 30, 60); - //колеса - Brush brblack = new SolidBrush(Color.Black); - g.FillEllipse(brblack, _startPosX + 25, _startPosY + 95, 30, 30); - g.FillEllipse(brblack, _startPosX + 120, _startPosY + 95, 30, 30); //Батарея - if(EntityTrolleybus.Battery) + if(trolleybus.Battery) { - Brush brBattery = new SolidBrush(EntityTrolleybus.AdditionalColor); + Brush brBattery = new SolidBrush(trolleybus.AdditionalColor); g.FillRectangle(brBattery, _startPosX + 95, _startPosY + 85, 20, 25); g.DrawLine(new Pen(Color.Yellow, 2), _startPosX + 112, _startPosY + 90, _startPosX + 97, _startPosY + 100); g.DrawLine(new Pen(Color.Yellow, 2), _startPosX + 97, _startPosY + 100, _startPosX + 112, _startPosY + 100); g.DrawLine(new Pen(Color.Yellow, 2), _startPosX + 112, _startPosY + 100, _startPosX + 97, _startPosY + 110); g.DrawRectangle(pen, _startPosX + 95, _startPosY + 85, 20, 25); } - //границы троллейбуса - g.DrawRectangle(pen, _startPosX + 5, _startPosY + 30, 165, 80); - g.DrawEllipse(pen, _startPosX + 25, _startPosY + 95, 30, 30); - g.DrawEllipse(pen, _startPosX + 120, _startPosY + 95, 30, 30); - g.DrawRectangle(pen, _startPosX + 5, _startPosY + 85, 10, 20); - g.DrawRectangle(pen, _startPosX + 160, _startPosY + 85, 10, 20); - g.DrawRectangle(pen, _startPosX + 60, _startPosY + 50, 30, 60); - g.DrawRectangle(pen, _startPosX + 150, _startPosY + 40, 20, 40); - g.DrawEllipse(pen, _startPosX + 10, _startPosY + 40, 20, 40); - g.DrawEllipse(pen, _startPosX + 35, _startPosY + 40, 20, 40); - g.DrawEllipse(pen, _startPosX + 95, _startPosY + 40, 20, 40); - g.DrawEllipse(pen, _startPosX + 120, _startPosY + 40, 20, 40); } } } diff --git a/Trolleybus/Trolleybus/EntityBus.cs b/Trolleybus/Trolleybus/EntityBus.cs new file mode 100644 index 0000000..ecb9c71 --- /dev/null +++ b/Trolleybus/Trolleybus/EntityBus.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTrolleybus.Entities +{ + public class EntityBus + { + public int Speed { get; private set; } + + public double Weight { get; private set; } + + public Color BodyColor { get; private set; } + + public double Step => (double)Speed * 100 / Weight; + + public EntityBus(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/Trolleybus/Trolleybus/EntityTrolleybus.cs b/Trolleybus/Trolleybus/EntityTrolleybus.cs index 9016612..805e589 100644 --- a/Trolleybus/Trolleybus/EntityTrolleybus.cs +++ b/Trolleybus/Trolleybus/EntityTrolleybus.cs @@ -4,44 +4,20 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Trolleybus +namespace ProjectTrolleybus.Entities { - public class EntityTrolleybus + public class EntityTrolleybus : EntityBus { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// public Color AdditionalColor { get; private set; } - /// - /// Признак (опция) наличия рогов - /// + public bool Roga { get; private set; } - /// - /// Признак (опция) наличия батареи - /// + public bool Battery { get; private set; } - /// - /// Шаг перемещения автомобиля - /// - public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, double weight, Color bodyColor, Color + + public EntityTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, bool roga, bool battery) + : base (speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Roga = roga; Battery = battery; diff --git a/Trolleybus/Trolleybus/FormTrolleybus.Designer.cs b/Trolleybus/Trolleybus/FormTrolleybus.Designer.cs index 6405f4f..e6105e4 100644 --- a/Trolleybus/Trolleybus/FormTrolleybus.Designer.cs +++ b/Trolleybus/Trolleybus/FormTrolleybus.Designer.cs @@ -1,4 +1,4 @@ -namespace Trolleybus +namespace ProjectTrolleybus { partial class FormTrolleybus { @@ -29,14 +29,19 @@ private void InitializeComponent() { pictureBoxTrolleybus = new PictureBox(); - buttonCreate = new Button(); + buttonCreateTrolleybus = new Button(); buttonRight = new Button(); buttonDown = new Button(); buttonLeft = new Button(); buttonUp = new Button(); + buttonCreateBus = new Button(); + buttonStep = new Button(); + comboBoxStrategy = new ComboBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxTrolleybus).BeginInit(); SuspendLayout(); + // // pictureBoxTrolleybus + // pictureBoxTrolleybus.Dock = DockStyle.Fill; pictureBoxTrolleybus.Location = new Point(0, 0); pictureBoxTrolleybus.Name = "pictureBoxTrolleybus"; @@ -44,18 +49,22 @@ pictureBoxTrolleybus.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxTrolleybus.TabIndex = 0; pictureBoxTrolleybus.TabStop = false; - // buttonCreate - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 426); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += ButtonCreate_Click; + // + // buttonCreateTrolleybus + // + buttonCreateTrolleybus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateTrolleybus.Location = new Point(12, 411); + buttonCreateTrolleybus.Name = "buttonCreateTrolleybus"; + buttonCreateTrolleybus.Size = new Size(84, 38); + buttonCreateTrolleybus.TabIndex = 1; + buttonCreateTrolleybus.Text = "Создать троллейбус"; + buttonCreateTrolleybus.UseVisualStyleBackColor = true; + buttonCreateTrolleybus.Click += ButtonCreateTrolleybus_Click; + // // buttonRight + // buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonRight.BackgroundImage = Properties.Resources.Right; + buttonRight.BackgroundImage = Trolleybus.Properties.Resources.Right; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; buttonRight.Location = new Point(842, 419); buttonRight.Name = "buttonRight"; @@ -63,9 +72,11 @@ buttonRight.TabIndex = 2; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; + // // buttonDown + // buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonDown.BackgroundImage = Properties.Resources.Down; + buttonDown.BackgroundImage = Trolleybus.Properties.Resources.Down; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; buttonDown.Location = new Point(806, 419); buttonDown.Name = "buttonDown"; @@ -73,9 +84,11 @@ buttonDown.TabIndex = 3; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; + // // buttonLeft + // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonLeft.BackgroundImage = Properties.Resources.Left; + buttonLeft.BackgroundImage = Trolleybus.Properties.Resources.Left; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; buttonLeft.Location = new Point(770, 419); buttonLeft.Name = "buttonLeft"; @@ -83,9 +96,11 @@ buttonLeft.TabIndex = 4; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; + // // buttonUp + // buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonUp.BackgroundImage = Properties.Resources.Up; + buttonUp.BackgroundImage = Trolleybus.Properties.Resources.Up; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; buttonUp.Location = new Point(806, 383); buttonUp.Name = "buttonUp"; @@ -93,15 +108,50 @@ buttonUp.TabIndex = 5; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; + // + // buttonCreateBus + // + buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBus.Location = new Point(102, 411); + buttonCreateBus.Name = "buttonCreateBus"; + buttonCreateBus.Size = new Size(78, 38); + buttonCreateBus.TabIndex = 6; + buttonCreateBus.Text = "Создать автобус"; + buttonCreateBus.UseVisualStyleBackColor = true; + buttonCreateBus.Click += ButtonCreateBus_Click; + // + // buttonStep + // + buttonStep.Location = new Point(797, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 7; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += ButtonStep_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "Движение в центр", "Движение в правый угол" }); + comboBoxStrategy.Location = new Point(751, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 8; + // // FormTrolleybus + // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonStep); + Controls.Add(buttonCreateBus); Controls.Add(buttonUp); Controls.Add(buttonLeft); Controls.Add(buttonDown); Controls.Add(buttonRight); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateTrolleybus); Controls.Add(pictureBoxTrolleybus); Name = "FormTrolleybus"; StartPosition = FormStartPosition.CenterScreen; @@ -114,10 +164,13 @@ #endregion private PictureBox pictureBoxTrolleybus; - private Button buttonCreate; + private Button buttonCreateTrolleybus; private Button buttonRight; private Button buttonDown; private Button buttonLeft; private Button buttonUp; + private Button buttonCreateBus; + private Button buttonStep; + private ComboBox comboBoxStrategy; } } \ No newline at end of file diff --git a/Trolleybus/Trolleybus/FormTrolleybus.cs b/Trolleybus/Trolleybus/FormTrolleybus.cs index b7cdc74..5cf5d13 100644 --- a/Trolleybus/Trolleybus/FormTrolleybus.cs +++ b/Trolleybus/Trolleybus/FormTrolleybus.cs @@ -1,40 +1,52 @@ -namespace Trolleybus +using ProjectTrolleybus.MovementStrategy; +using ProjectTrolleybus.DrawingObjects; + +namespace ProjectTrolleybus { public partial class FormTrolleybus : Form { - private DrawingTrolleybus? _drawningTrolleybus; + private DrawingBus? _drawingBus; + + private AbstractStrategy? _abstractStrategy; + public FormTrolleybus() { InitializeComponent(); } private void Draw() { - if (_drawningTrolleybus == null) + if (_drawingBus == null) { return; } Bitmap bmp = new Bitmap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningTrolleybus.DrawTransport(gr); + _drawingBus.DrawTransport(gr); pictureBoxTrolleybus.Image = bmp; } - private void ButtonCreate_Click(object sender, EventArgs e) + private void ButtonCreateTrolleybus_Click(object sender, EventArgs e) { Random random = new Random(); - _drawningTrolleybus = new DrawingTrolleybus(); - _drawningTrolleybus.Init(random.Next(100, 300), random.Next(1000, 3000), + _drawingBus = new DrawingTrolleybus(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)), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); - { - _drawningTrolleybus.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); - } + _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void ButtonCreateBus_Click(object sender, EventArgs e) + { + Random random = new Random(); + _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)), + pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); + _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningTrolleybus == null) + if (_drawingBus == null) { return; } @@ -42,19 +54,55 @@ namespace Trolleybus switch (name) { case "buttonUp": - _drawningTrolleybus.MoveTransport(DirectionType.Up); + _drawingBus.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawningTrolleybus.MoveTransport(DirectionType.Down); + _drawingBus.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawningTrolleybus.MoveTransport(DirectionType.Left); + _drawingBus.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawningTrolleybus.MoveTransport(DirectionType.Right); + _drawingBus.MoveTransport(DirectionType.Right); break; } Draw(); } + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawingBus == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawingObjectBus(_drawingBus), pictureBoxTrolleybus.Width, + pictureBoxTrolleybus.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/Trolleybus/Trolleybus/IMoveableObject.cs b/Trolleybus/Trolleybus/IMoveableObject.cs new file mode 100644 index 0000000..a96a76b --- /dev/null +++ b/Trolleybus/Trolleybus/IMoveableObject.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +//using ProjectTrolleybus.Drawings; + +namespace ProjectTrolleybus.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/Trolleybus/Trolleybus/MoveToBorder.cs b/Trolleybus/Trolleybus/MoveToBorder.cs new file mode 100644 index 0000000..1f1dce4 --- /dev/null +++ b/Trolleybus/Trolleybus/MoveToBorder.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTrolleybus.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } + } +} + diff --git a/Trolleybus/Trolleybus/MoveToCenter.cs b/Trolleybus/Trolleybus/MoveToCenter.cs new file mode 100644 index 0000000..eaa92c4 --- /dev/null +++ b/Trolleybus/Trolleybus/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTrolleybus.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/Trolleybus/Trolleybus/ObjectParameters.cs b/Trolleybus/Trolleybus/ObjectParameters.cs new file mode 100644 index 0000000..153aaf9 --- /dev/null +++ b/Trolleybus/Trolleybus/ObjectParameters.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTrolleybus.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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } + +} diff --git a/Trolleybus/Trolleybus/Program.cs b/Trolleybus/Trolleybus/Program.cs index 1e98dfe..8a49dfe 100644 --- a/Trolleybus/Trolleybus/Program.cs +++ b/Trolleybus/Trolleybus/Program.cs @@ -1,4 +1,4 @@ -namespace Trolleybus +namespace ProjectTrolleybus { internal static class Program { diff --git a/Trolleybus/Trolleybus/Status.cs b/Trolleybus/Trolleybus/Status.cs new file mode 100644 index 0000000..8fc2230 --- /dev/null +++ b/Trolleybus/Trolleybus/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTrolleybus.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +} -- 2.25.1