From 51a21db2aa10e55c395fdd5a686f9841ba3fca2d Mon Sep 17 00:00:00 2001 From: MayDayR Date: Tue, 14 Nov 2023 13:11:54 +0300 Subject: [PATCH] PIbd22_Kamcharova_K.A._lab2 --- DoubleDeckerBus/AbstractStrategy.cs | 75 ++++++++ .../{Direction.cs => DirectionType.cs} | 4 +- DoubleDeckerBus/DoubleDeckerBus.cs | 29 --- DoubleDeckerBus/DrawingBus.cs | 161 ++++++++-------- DoubleDeckerBus/DrawingDoubleDeckerbus.cs | 61 +++++++ DoubleDeckerBus/DrawingObjectBus.cs | 35 ++++ DoubleDeckerBus/EntityBus.cs | 22 +++ DoubleDeckerBus/EntityDoubleDeckerBus.cs | 22 +++ .../FormDoubleDeckerBus.Designer.cs | 172 +++++++++++------- DoubleDeckerBus/FormDoubleDeckerBus.cs | 79 ++++++-- DoubleDeckerBus/FormDoubleDeckerBus.resx | 62 ++++++- DoubleDeckerBus/IMoveableObject.cs | 18 ++ DoubleDeckerBus/MoveToBorder.cs | 56 ++++++ DoubleDeckerBus/MoveToCenter.cs | 56 ++++++ DoubleDeckerBus/ObjectParameters.cs | 29 +++ DoubleDeckerBus/Program.cs | 2 +- DoubleDeckerBus/Status.cs | 15 ++ Temp.txt | 1 - 18 files changed, 698 insertions(+), 201 deletions(-) create mode 100644 DoubleDeckerBus/AbstractStrategy.cs rename DoubleDeckerBus/{Direction.cs => DirectionType.cs} (78%) delete mode 100644 DoubleDeckerBus/DoubleDeckerBus.cs create mode 100644 DoubleDeckerBus/DrawingDoubleDeckerbus.cs create mode 100644 DoubleDeckerBus/DrawingObjectBus.cs create mode 100644 DoubleDeckerBus/EntityBus.cs create mode 100644 DoubleDeckerBus/EntityDoubleDeckerBus.cs create mode 100644 DoubleDeckerBus/IMoveableObject.cs create mode 100644 DoubleDeckerBus/MoveToBorder.cs create mode 100644 DoubleDeckerBus/MoveToCenter.cs create mode 100644 DoubleDeckerBus/ObjectParameters.cs create mode 100644 DoubleDeckerBus/Status.cs delete mode 100644 Temp.txt diff --git a/DoubleDeckerBus/AbstractStrategy.cs b/DoubleDeckerBus/AbstractStrategy.cs new file mode 100644 index 0000000..817dbde --- /dev/null +++ b/DoubleDeckerBus/AbstractStrategy.cs @@ -0,0 +1,75 @@ +using DoubleDeckerbus.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +using DoubleDeckerbus.DrawingObjects; + +namespace DoubleDeckerbus.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/DoubleDeckerBus/Direction.cs b/DoubleDeckerBus/DirectionType.cs similarity index 78% rename from DoubleDeckerBus/Direction.cs rename to DoubleDeckerBus/DirectionType.cs index f5516f2..52f8626 100644 --- a/DoubleDeckerBus/Direction.cs +++ b/DoubleDeckerBus/DirectionType.cs @@ -4,9 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace DoubleDeckerBus +namespace DoubleDeckerbus { - internal enum Direction + public enum DirectionType { Up = 1, Down = 2, diff --git a/DoubleDeckerBus/DoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus.cs deleted file mode 100644 index 2f954c2..0000000 --- a/DoubleDeckerBus/DoubleDeckerBus.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DoubleDeckerBus -{ - public class EntityDoubleDeckerBus - { - 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 Stairs { get; private set; } - public bool SecondFloor { get; private set; } - public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool secondFloor, bool stairs) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Stairs = secondFloor; - SecondFloor = stairs; - } - } -} \ No newline at end of file diff --git a/DoubleDeckerBus/DrawingBus.cs b/DoubleDeckerBus/DrawingBus.cs index 1af8b0f..cfbbe7a 100644 --- a/DoubleDeckerBus/DrawingBus.cs +++ b/DoubleDeckerBus/DrawingBus.cs @@ -4,138 +4,127 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace DoubleDeckerBus +using DoubleDeckerbus.Entities; + +namespace DoubleDeckerbus.DrawingObjects { - internal class DrawingBus + public class DrawingBus { - public EntityDoubleDeckerBus? EntityDoubleDeckerBus { get; private set; } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private readonly int _busWidth = 180; - private readonly int _busHeight = 120; - public bool Init(EntityDoubleDeckerBus bus, int width, int height) + public EntityBus? EntityBus { get; protected set; } + public int _pictureWidth; + public int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected readonly int _busWidth = 175; + protected readonly int _busHeight = 115; + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _busWidth; + public int GetHeight => _busHeight; + public DrawingBus(int speed, double weight, Color bodyColor, int width, int height) { + if (width < _busWidth || height < _busHeight) + { + return; + } _pictureWidth = width; _pictureHeight = height; - EntityDoubleDeckerBus = bus; - if (_pictureWidth < _busWidth || _pictureHeight < _busHeight) + 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 false; + return; } - return true; + _pictureWidth = width; + _pictureHeight = height; + _busWidth = busWidth; + _busHeight = busHeight; + EntityBus = new EntityBus(speed, weight, bodyColor); } public void SetPosition(int x, int y) { + if (x > _pictureWidth || y > _pictureHeight) + { + return; + } _startPosX = x; _startPosY = y; - if (_startPosX + _busWidth > _pictureWidth) { - _startPosX = _pictureWidth - _busWidth; - } - if (_startPosX < 0) - { - _startPosX = 0; - } - if (_startPosY + _busHeight > _pictureHeight) - { - _startPosY = _pictureHeight - _busHeight; - } - if (_startPosY < 0) - { - _startPosY = 0; - } } - public void MoveTransport(Direction direction) - { - if (EntityDoubleDeckerBus == null) + public void MoveTransport(DirectionType direction) { + if (!CanMove(direction) || EntityBus == null) + { return; } switch (direction) { - case Direction.Left: - if (_startPosX - EntityDoubleDeckerBus.Step > 0) + case DirectionType.Left: + if (_startPosX - EntityBus.Step > 0) { - _startPosX -= (int)EntityDoubleDeckerBus.Step; + _startPosX -= (int)EntityBus.Step; } break; - case Direction.Up: - if (_startPosY - EntityDoubleDeckerBus.Step > 0) + case DirectionType.Up: + if (_startPosY - EntityBus.Step > 0) { - _startPosY -= (int)EntityDoubleDeckerBus.Step; + _startPosY -= (int)EntityBus.Step; } break; - case Direction.Right: - if (_startPosX + EntityDoubleDeckerBus.Step + _busWidth < _pictureWidth) + case DirectionType.Right: + if (_startPosX + _busWidth + EntityBus.Step < _pictureWidth) { - _startPosX += (int)EntityDoubleDeckerBus.Step; + _startPosX += (int)EntityBus.Step; } break; - case Direction.Down: - if (_startPosY + EntityDoubleDeckerBus.Step + _busHeight < _pictureHeight) + case DirectionType.Down: + if (_startPosY + _busHeight + EntityBus.Step < _pictureHeight) { - _startPosY += (int)EntityDoubleDeckerBus.Step; + _startPosY += (int)EntityBus.Step; } break; } } - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityDoubleDeckerBus == null) - { - return; - } Pen pen = new(Color.Black); - Brush bodyColor = new - SolidBrush(EntityDoubleDeckerBus.BodyColor); - g.FillRectangle(bodyColor, _startPosX + 147, _startPosY + 52, 21, 20); - g.FillRectangle(bodyColor, _startPosX + 147, _startPosY + 71, 30, 27); - g.FillRectangle(bodyColor, _startPosX + 10, _startPosY + 52, 137, 46); + Brush additionalBrush = new + SolidBrush(EntityBus.BodyColor); + g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 52, 21, 20); //маленький + g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 71, 30, 27); //2 + g.FillRectangle(additionalBrush, _startPosX + 10, _startPosY + 52, 137, 46); //большой прямоугольник 2 + Brush brBlue = new SolidBrush(Color.LightBlue); g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 123, _startPosY + 55, 15, 15); + g.DrawLine(pen, _startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98); g.DrawLine(pen, _startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98); - //ВТОРОЙ ЭТАЖ - if (EntityDoubleDeckerBus.SecondFloor) - { - Brush additionalColor = new - SolidBrush(EntityDoubleDeckerBus.AdditionalColor); - - g.FillRectangle(additionalColor, _startPosX + 7, _startPosY + 12, 172, 40); - - g.DrawLine(pen, _startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36); - - g.FillRectangle(brBlue, _startPosX + 15, _startPosY + 15, 15, 15); - g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 15, 15, 15); - g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 15, 15, 15); - g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 15, 15, 15); - g.FillRectangle(brBlue, _startPosX + 123, _startPosY + 15, 15, 15); - g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 15, 15, 15); - } - //ЛЕСТНИЦА - if (EntityDoubleDeckerBus.Stairs) - { - g.DrawLine(pen, _startPosX + 10, _startPosY + 55, _startPosX + 34, _startPosY + 55); - g.DrawLine(pen, _startPosX + 10, _startPosY + 58, _startPosX + 34, _startPosY + 58); - g.DrawLine(pen, _startPosX + 10, _startPosY + 64, _startPosX + 34, _startPosY + 64); - g.DrawLine(pen, _startPosX + 10, _startPosY + 72, _startPosX + 34, _startPosY + 72); - g.DrawLine(pen, _startPosX + 10, _startPosY + 80, _startPosX + 34, _startPosY + 80); - g.DrawLine(pen, _startPosX + 10, _startPosY + 88, _startPosX + 34, _startPosY + 88); - g.DrawLine(pen, _startPosX + 10, _startPosY + 94, _startPosX + 34, _startPosY + 94); - } - // колёса Brush gr = new SolidBrush(Color.Gray); - g.FillEllipse(bodyColor, _startPosX + 23, _startPosY + 88, 25, 25); - g.FillEllipse(bodyColor, _startPosX + 123, _startPosY + 88, 25, 25); - + g.FillEllipse(additionalBrush, _startPosX + 23, _startPosY + 88, 25, 25); + g.FillEllipse(additionalBrush, _startPosX + 123, _startPosY + 88, 25, 25); g.FillEllipse(gr, _startPosX + 25, _startPosY + 90, 21, 21); g.FillEllipse(gr, _startPosX + 125, _startPosY + 90, 21, 21); } + 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 + _busWidth + EntityBus.Step < _pictureWidth, + DirectionType.Down => _startPosY + _busHeight + EntityBus.Step < _pictureHeight, + _ => false, + }; + } } } \ No newline at end of file diff --git a/DoubleDeckerBus/DrawingDoubleDeckerbus.cs b/DoubleDeckerBus/DrawingDoubleDeckerbus.cs new file mode 100644 index 0000000..3429e68 --- /dev/null +++ b/DoubleDeckerBus/DrawingDoubleDeckerbus.cs @@ -0,0 +1,61 @@ +using DoubleDeckerbus.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using DoubleDeckerbus.Entities; + +namespace DoubleDeckerbus +{ + public class DrawingDoubleDeckerbus : DrawingBus + { + public DrawingDoubleDeckerbus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondfloor, bool stairs, int width, int height) : base(speed, weight, bodyColor, width, height, 180, 110) + { + if (EntityBus != null) + { + EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondfloor, stairs); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityBus is not EntityDoubleDeckerBus doubleDeckerBus) + { + return; + } + Pen pen = new(Color.Black); + Pen additionalPen = new(doubleDeckerBus.AdditionalColor); + Brush additionalBrush = new SolidBrush(doubleDeckerBus.AdditionalColor); + base.DrawTransport(g); + + if (doubleDeckerBus.Secondfloor) + { + Brush additionalBrush2 = new SolidBrush(doubleDeckerBus.AdditionalColor); + Brush brBlue = new SolidBrush(Color.LightBlue); + + g.FillRectangle(additionalBrush2, _startPosX + 7, _startPosY + 12, 172, 40); + + g.DrawLine(pen, _startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36); + + g.FillRectangle(brBlue, _startPosX + 15, _startPosY + 15, 15, 15); + g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 15, 15, 15); + g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 15, 15, 15); + g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 15, 15, 15); + g.FillRectangle(brBlue, _startPosX + 123, _startPosY + 15, 15, 15); + g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 15, 15, 15); + } + + if (doubleDeckerBus.Stairs) + { + g.DrawLine(pen, _startPosX + 10, _startPosY + 55, _startPosX + 34, _startPosY + 55); + g.DrawLine(pen, _startPosX + 10, _startPosY + 58, _startPosX + 34, _startPosY + 58); + g.DrawLine(pen, _startPosX + 10, _startPosY + 64, _startPosX + 34, _startPosY + 64); + g.DrawLine(pen, _startPosX + 10, _startPosY + 72, _startPosX + 34, _startPosY + 72); + g.DrawLine(pen, _startPosX + 10, _startPosY + 80, _startPosX + 34, _startPosY + 80); + g.DrawLine(pen, _startPosX + 10, _startPosY + 88, _startPosX + 34, _startPosY + 88); + g.DrawLine(pen, _startPosX + 10, _startPosY + 94, _startPosX + 34, _startPosY + 94); + } + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/DrawingObjectBus.cs b/DoubleDeckerBus/DrawingObjectBus.cs new file mode 100644 index 0000000..2f081f9 --- /dev/null +++ b/DoubleDeckerBus/DrawingObjectBus.cs @@ -0,0 +1,35 @@ +using DoubleDeckerbus.MovementStrategy; +using DoubleDeckerbus; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading.Tasks; + +using DoubleDeckerbus.DrawingObjects; +namespace DoubleDeckerbus.MovementStrategy +{ + public class DrawingObjectBus : IMoveableObject + { + private readonly DrawingBus? _drawingBus = null; + public DrawingObjectBus(DrawingBus drawingBus) + { + _drawingBus = drawingBus; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawingBus == null || _drawingBus.EntityBus == null) + { + return null; + } + return new ObjectParameters(_drawingBus.GetPosX,_drawingBus.GetPosY, _drawingBus.GetWidth, _drawingBus.GetHeight); + } + } + public int GetStep => (int)(_drawingBus?.EntityBus?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => _drawingBus?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawingBus?.MoveTransport(direction); + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/EntityBus.cs b/DoubleDeckerBus/EntityBus.cs new file mode 100644 index 0000000..b19e3e9 --- /dev/null +++ b/DoubleDeckerBus/EntityBus.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerbus.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; + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/EntityDoubleDeckerBus.cs new file mode 100644 index 0000000..fca0681 --- /dev/null +++ b/DoubleDeckerBus/EntityDoubleDeckerBus.cs @@ -0,0 +1,22 @@ +using DoubleDeckerbus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerbus.Entities +{ + public class EntityDoubleDeckerBus : EntityBus + { + public Color AdditionalColor { get; private set; } + public bool Secondfloor { get; private set; } + public bool Stairs { get; private set; } + public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondfloor, bool stairs) : base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + Secondfloor = secondfloor; + Stairs = stairs; + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs b/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs index c037403..a8d0b5e 100644 --- a/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs @@ -1,4 +1,4 @@ -namespace DoubleDeckerBus +namespace DoubleDeckerbus { partial class FormDoubleDeckerBus { @@ -28,111 +28,153 @@ /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormDoubleDeckerBus)); - this.pictureBoxBus = new System.Windows.Forms.PictureBox(); - this.buttonCreate = new System.Windows.Forms.Button(); + this.pictureBoxDoubleDeckerbus = new System.Windows.Forms.PictureBox(); + this.DoubleDeckerbus = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBus)).BeginInit(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.buttonStep = new System.Windows.Forms.Button(); + this.buttonCreateBus = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDoubleDeckerbus)).BeginInit(); this.SuspendLayout(); // - // pictureBoxBus + // pictureBoxDoubleDeckerbus // - this.pictureBoxBus.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxBus.Location = new System.Drawing.Point(0, 0); - this.pictureBoxBus.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.pictureBoxBus.Name = "pictureBoxBus"; - this.pictureBoxBus.Size = new System.Drawing.Size(1010, 615); - this.pictureBoxBus.TabIndex = 1; - this.pictureBoxBus.TabStop = false; + this.pictureBoxDoubleDeckerbus.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxDoubleDeckerbus.Location = new System.Drawing.Point(0, 0); + this.pictureBoxDoubleDeckerbus.Name = "pictureBoxDoubleDeckerbus"; + this.pictureBoxDoubleDeckerbus.Size = new System.Drawing.Size(882, 453); + this.pictureBoxDoubleDeckerbus.TabIndex = 5; + this.pictureBoxDoubleDeckerbus.TabStop = false; // - // buttonCreate + // DoubleDeckerbus // - this.buttonCreate.Location = new System.Drawing.Point(14, 541); - this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(95, 57); - this.buttonCreate.TabIndex = 2; - this.buttonCreate.Text = "Создать"; - this.buttonCreate.UseVisualStyleBackColor = true; - this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + this.DoubleDeckerbus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.DoubleDeckerbus.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.DoubleDeckerbus.Location = new System.Drawing.Point(11, 365); + this.DoubleDeckerbus.Name = "DoubleDeckerbus"; + this.DoubleDeckerbus.Size = new System.Drawing.Size(120, 73); + this.DoubleDeckerbus.TabIndex = 6; + this.DoubleDeckerbus.Text = "Создать Двухэтажный автобус"; + this.DoubleDeckerbus.UseVisualStyleBackColor = true; + this.DoubleDeckerbus.Click += new System.EventHandler(this.buttonCreateDoubleDeckerbus_Click); // // buttonUp // - this.buttonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonUp.BackgroundImage"))); + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(896, 487); - this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonUp.Location = new System.Drawing.Point(811, 377); this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(46, 53); - this.buttonUp.TabIndex = 3; + this.buttonUp.Size = new System.Drawing.Size(30, 29); + this.buttonUp.TabIndex = 7; this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonDown - // - this.buttonDown.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonDown.BackgroundImage"))); - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(896, 548); - this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(46, 53); - this.buttonDown.TabIndex = 4; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click); // // buttonLeft // - this.buttonLeft.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonLeft.BackgroundImage"))); + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(843, 547); - this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonLeft.Location = new System.Drawing.Point(776, 413); this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(46, 53); - this.buttonLeft.TabIndex = 5; + this.buttonLeft.Size = new System.Drawing.Size(30, 29); + this.buttonLeft.TabIndex = 8; this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowDown; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonDown.Location = new System.Drawing.Point(811, 413); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 29); + this.buttonDown.TabIndex = 9; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); // // buttonRight // - this.buttonRight.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonRight.BackgroundImage"))); + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(949, 547); - this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonRight.Location = new System.Drawing.Point(848, 413); this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(46, 53); - this.buttonRight.TabIndex = 6; + this.buttonRight.Size = new System.Drawing.Size(30, 29); + this.buttonRight.TabIndex = 10; this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); // - // DDBus + // comboBoxStrategy + // + this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "В центр", + "В правый нижний угол"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(719, 12); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28); + this.comboBoxStrategy.TabIndex = 11; + // + // buttonStep + // + this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonStep.Location = new System.Drawing.Point(747, 45); + this.buttonStep.Name = "buttonStep"; + this.buttonStep.Size = new System.Drawing.Size(94, 29); + this.buttonStep.TabIndex = 12; + this.buttonStep.Text = "Шаг"; + this.buttonStep.UseVisualStyleBackColor = true; + this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click); + // + // buttonCreateBus + // + this.buttonCreateBus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateBus.Location = new System.Drawing.Point(138, 388); + this.buttonCreateBus.Name = "buttonCreateBus"; + this.buttonCreateBus.Size = new System.Drawing.Size(120, 51); + this.buttonCreateBus.TabIndex = 13; + this.buttonCreateBus.Text = "Создать автобус"; + this.buttonCreateBus.UseVisualStyleBackColor = true; + this.buttonCreateBus.Click += new System.EventHandler(this.buttonCreateBus_Click); + // + // FormDoubleDeckerbus // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1010, 615); + this.ClientSize = new System.Drawing.Size(882, 453); + this.Controls.Add(this.buttonCreateBus); + this.Controls.Add(this.buttonStep); + this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonCreate); - this.Controls.Add(this.pictureBoxBus); - this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.Name = "DDBus"; - this.Text = "Bus"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBus)).EndInit(); + this.Controls.Add(this.DoubleDeckerbus); + this.Controls.Add(this.pictureBoxDoubleDeckerbus); + this.Name = "FormDoubleDeckerbus"; + this.Text = "DoubleDeckerbus"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDoubleDeckerbus)).EndInit(); this.ResumeLayout(false); } #endregion - private PictureBox pictureBoxBus; - private Button buttonCreate; + private PictureBox pictureBoxDoubleDeckerbus; + private Button DoubleDeckerbus; private Button buttonUp; - private Button buttonDown; private Button buttonLeft; + private Button buttonDown; private Button buttonRight; + private ComboBox comboBoxStrategy; + private Button buttonStep; + private Button buttonCreateBus; } } \ No newline at end of file diff --git a/DoubleDeckerBus/FormDoubleDeckerBus.cs b/DoubleDeckerBus/FormDoubleDeckerBus.cs index c83a5b1..2589a18 100644 --- a/DoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/DoubleDeckerBus/FormDoubleDeckerBus.cs @@ -1,8 +1,12 @@ -namespace DoubleDeckerBus +using DoubleDeckerbus.DrawingObjects; +using DoubleDeckerbus.MovementStrategy; + +namespace DoubleDeckerbus { public partial class FormDoubleDeckerBus : Form { private DrawingBus? _drawingBus; + private AbstractStrategy? _abstractStrategy; public FormDoubleDeckerBus() { InitializeComponent(); @@ -13,29 +17,38 @@ namespace DoubleDeckerBus { return; } - Bitmap bmp = new(pictureBoxBus.Width, pictureBoxBus.Height); + Bitmap bmp = new(pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); Graphics gr = Graphics.FromImage(bmp); _drawingBus.DrawTransport(gr); - pictureBoxBus.Image = bmp; + pictureBoxDoubleDeckerbus.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateDoubleDeckerbus_Click(object sender, EventArgs e) { Random random = new(); - _drawingBus = new DrawingBus(); - EntityDoubleDeckerBus bus = new EntityDoubleDeckerBus(); - bus.Init(random.Next(100, 300), + _drawingBus = new DrawingDoubleDeckerbus(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))); - _drawingBus.Init(bus, pictureBoxBus.Width, pictureBoxBus.Height); - _drawingBus.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + true, + Convert.ToBoolean(random.Next(0, 2)), + pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); + _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } - private void ButtonMove_Click(object sender, EventArgs e) + private void buttonCreateBus_Click(object sender, EventArgs e) + { + Random random = new(); + _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)), + pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); + _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void buttonMove_Click(object sender, EventArgs e) { if (_drawingBus == null) { @@ -45,19 +58,53 @@ namespace DoubleDeckerBus switch (name) { case "buttonUp": - _drawingBus.MoveTransport(Direction.Up); + _drawingBus.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawingBus.MoveTransport(Direction.Down); + _drawingBus.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawingBus.MoveTransport(Direction.Left); + _drawingBus.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawingBus.MoveTransport(Direction.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), pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.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/DoubleDeckerBus/FormDoubleDeckerBus.resx b/DoubleDeckerBus/FormDoubleDeckerBus.resx index 7bd5202..2f7d102 100644 --- a/DoubleDeckerBus/FormDoubleDeckerBus.resx +++ b/DoubleDeckerBus/FormDoubleDeckerBus.resx @@ -1,4 +1,64 @@ - + + + diff --git a/DoubleDeckerBus/IMoveableObject.cs b/DoubleDeckerBus/IMoveableObject.cs new file mode 100644 index 0000000..263b464 --- /dev/null +++ b/DoubleDeckerBus/IMoveableObject.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using DoubleDeckerbus.DrawingObjects; + +namespace DoubleDeckerbus.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + int GetStep { get; } + bool CheckCanMove(DirectionType direction); + void MoveObject(DirectionType direction); + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/MoveToBorder.cs b/DoubleDeckerBus/MoveToBorder.cs new file mode 100644 index 0000000..2134eed --- /dev/null +++ b/DoubleDeckerBus/MoveToBorder.cs @@ -0,0 +1,56 @@ +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 IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/MoveToCenter.cs b/DoubleDeckerBus/MoveToCenter.cs new file mode 100644 index 0000000..5846414 --- /dev/null +++ b/DoubleDeckerBus/MoveToCenter.cs @@ -0,0 +1,56 @@ +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 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(); + } + } + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/ObjectParameters.cs b/DoubleDeckerBus/ObjectParameters.cs new file mode 100644 index 0000000..1aaace0 --- /dev/null +++ b/DoubleDeckerBus/ObjectParameters.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerbus.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; + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/Program.cs b/DoubleDeckerBus/Program.cs index 0ebcfa7..7ac98e0 100644 --- a/DoubleDeckerBus/Program.cs +++ b/DoubleDeckerBus/Program.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace DoubleDeckerBus +namespace DoubleDeckerbus { internal static class Program { diff --git a/DoubleDeckerBus/Status.cs b/DoubleDeckerBus/Status.cs new file mode 100644 index 0000000..2f4640b --- /dev/null +++ b/DoubleDeckerBus/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerbus.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +} diff --git a/Temp.txt b/Temp.txt deleted file mode 100644 index 5f28270..0000000 --- a/Temp.txt +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file -- 2.25.1