From 6c4691699b6863b562c036f4715849765ed5e854 Mon Sep 17 00:00:00 2001 From: gettterot Date: Wed, 6 Mar 2024 07:52:17 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=80=D0=BE=D0=B4=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=D1=81?= =?UTF-8?q?=D0=BA=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20entity=20liner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => Drawnings}/DirectionType.cs | 2 +- .../DrawningCommonLiner.cs} | 130 ++++++++---------- .../ProjectLiner/Drawnings/DrawningLiner.cs | 72 ++++++++++ .../Entities/EntityCommonLiner.cs | 50 +++++++ .../{ => Entities}/EntityLiner.cs | 31 +---- .../ProjectLiner/FormLiner.Designer.cs | 20 ++- ProjectLiner/ProjectLiner/FormLiner.cs | 73 +++++++--- 7 files changed, 258 insertions(+), 120 deletions(-) rename ProjectLiner/ProjectLiner/{ => Drawnings}/DirectionType.cs (91%) rename ProjectLiner/ProjectLiner/{DrawningLiner.cs => Drawnings/DrawningCommonLiner.cs} (60%) create mode 100644 ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs create mode 100644 ProjectLiner/ProjectLiner/Entities/EntityCommonLiner.cs rename ProjectLiner/ProjectLiner/{ => Entities}/EntityLiner.cs (58%) diff --git a/ProjectLiner/ProjectLiner/DirectionType.cs b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs similarity index 91% rename from ProjectLiner/ProjectLiner/DirectionType.cs rename to ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs index 12c5fb2..cc78a5a 100644 --- a/ProjectLiner/ProjectLiner/DirectionType.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs @@ -1,4 +1,4 @@ -namespace ProjectLiner; +namespace ProjectLiner.Drawnings; /// /// Направление перемещения diff --git a/ProjectLiner/ProjectLiner/DrawningLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs similarity index 60% rename from ProjectLiner/ProjectLiner/DrawningLiner.cs rename to ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs index 35d42b6..2bc1885 100644 --- a/ProjectLiner/ProjectLiner/DrawningLiner.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs @@ -1,14 +1,17 @@ -namespace ProjectLiner; +using ProjectLiner.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; -/// -/// Класс, отвечающий за прорисовку и перемещение объекта-сущности -/// -public class DrawningLiner +namespace ProjectLiner.Drawnings; +public class DrawningCommonLiner { /// /// Класс-сущность /// - public EntityLiner? EntityLiner { get; private set; } + public EntityCommonLiner? EntityCommonLiner { get; protected set; } /// /// Ширина окна @@ -23,12 +26,12 @@ public class DrawningLiner /// /// Левая координата прорисовки лайнера /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя кооридната прорисовки лайнера /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки лайнера @@ -38,27 +41,44 @@ public class DrawningLiner /// /// Высота прорисовки лайнера /// - private readonly int _drawningLinerHeight = 125; + private readonly int _drawningLinerHeight = 90; /// - /// Инициализация свойств + /// Пустой конструктор /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия якоря - /// Признак наличия шлюпок - /// Признак наличия трубы - public void Init(EntityLiner liner) + + public DrawningCommonLiner() { - EntityLiner = liner; _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + + public DrawningCommonLiner(int speed, double weight, Color bodycolor) : this() + { + EntityCommonLiner = new EntityCommonLiner(speed, weight, bodycolor); + } + + /// + /// конструктор для наследников + /// + /// Высота + /// Длина + + protected DrawningCommonLiner(int drawningLinerWight, int drawningLinerHeight) : this() + { + _drawningLinerHeight = drawningLinerHeight; + _drawningLinerWidth = drawningLinerWight; + } + /// /// Установка границ поля /// @@ -73,7 +93,7 @@ public class DrawningLiner _pictureHeight = height; if (_startPosX != null && _startPosY != null) { - if (_startPosX.Value < 0) + if (_startPosX.Value < 0) _startPosX = 0; if (_startPosY.Value < 0) _startPosY = 0; @@ -129,7 +149,7 @@ public class DrawningLiner /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntityLiner == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityCommonLiner == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -138,30 +158,30 @@ public class DrawningLiner { //влево case DirectionType.Left: - if (_startPosX.Value - EntityLiner.Step > 0) + if (_startPosX.Value - EntityCommonLiner.Step > 0) { - _startPosX -= (int)EntityLiner.Step; + _startPosX -= (int)EntityCommonLiner.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntityLiner.Step > 0) + if (_startPosY.Value - EntityCommonLiner.Step > 0) { - _startPosY -= (int)EntityLiner.Step; + _startPosY -= (int)EntityCommonLiner.Step; } return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityLiner.Step < _pictureWidth-_drawningLinerWidth) + if (_startPosX.Value + EntityCommonLiner.Step < _pictureWidth - _drawningLinerWidth) { - _startPosX += (int)EntityLiner.Step; + _startPosX += (int)EntityCommonLiner.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityLiner.Step < _pictureHeight-_drawningLinerHeight) + if (_startPosY.Value + EntityCommonLiner.Step < _pictureHeight - _drawningLinerHeight) { - _startPosY += (int)EntityLiner.Step; + _startPosY += (int)EntityCommonLiner.Step; } return true; default: @@ -173,61 +193,33 @@ public class DrawningLiner /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityLiner == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityCommonLiner == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityLiner.AdditionalColor); - Brush br = new SolidBrush(EntityLiner.BodyColor); + Brush br = new SolidBrush(EntityCommonLiner.BodyColor); - // якорь - if (EntityLiner.Anchor) - { - g.DrawLine(pen, _startPosX.Value + 45, _startPosY.Value + 85, _startPosX.Value + 45, _startPosY.Value + 115); - g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 95, _startPosX.Value + 60, _startPosY.Value + 95); - g.DrawLine(pen, _startPosX.Value + 25, _startPosY.Value + 100, _startPosX.Value + 45, _startPosY.Value + 115); - g.DrawLine(pen, _startPosX.Value + 45, _startPosY.Value + 115, _startPosX.Value + 65, _startPosY.Value + 100); - } // кузов лайнера - Point point1 = new Point(_startPosX.Value, _startPosY.Value + 80); - Point point2 = new Point(_startPosX.Value + 50, _startPosY.Value + 130); - Point point3 = new Point(_startPosX.Value + 135, _startPosY.Value + 130); - Point point4 = new Point(_startPosX.Value + 155, _startPosY.Value + 80); + Point point1 = new Point(_startPosX.Value, _startPosY.Value + 40); + Point point2 = new Point(_startPosX.Value + 50, _startPosY.Value + 90); + Point point3 = new Point(_startPosX.Value + 135, _startPosY.Value + 90); + Point point4 = new Point(_startPosX.Value + 155, _startPosY.Value + 40); Point[] curvePoints = { point1, point2, point3, point4 }; g.FillPolygon(br, curvePoints); // борт лайнера - Point point5 = new Point(_startPosX.Value+40, _startPosY.Value + 40); - Point point6 = new Point(_startPosX.Value + 140, _startPosY.Value + 40); - Point point7 = new Point(_startPosX.Value + 140, _startPosY.Value + 80); - Point point8 = new Point(_startPosX.Value + 40, _startPosY.Value + 80); + Point point5 = new Point(_startPosX.Value + 40, _startPosY.Value); + Point point6 = new Point(_startPosX.Value + 140, _startPosY.Value); + Point point7 = new Point(_startPosX.Value + 140, _startPosY.Value + 40); + Point point8 = new Point(_startPosX.Value + 40, _startPosY.Value + 40); Point[] curvePoints2 = { point5, point6, point7, point8 }; - g.FillPolygon(additionalBrush, curvePoints2); + g.FillPolygon(br, curvePoints2); - // шлюпки - if (EntityLiner.Boats) - { - g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 85, 30, 15); - g.FillEllipse(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 85, 30, 15); - g.FillEllipse(additionalBrush, _startPosX.Value + 110, _startPosY.Value + 85, 30, 15); - } - - // труба - if (EntityLiner.Pipe) - { - - Point point9 = new Point(_startPosX.Value + 90, _startPosY.Value + 10); - Point point10 = new Point(_startPosX.Value + 90, _startPosY.Value + 40); - Point point11 = new Point(_startPosX.Value + 115, _startPosY.Value + 40); - Point point12 = new Point(_startPosX.Value + 115, _startPosY.Value + 10); - Point[] curvePoints3 = { point9, point10, point11, point12 }; - g.FillPolygon(br, curvePoints3); - } } } diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs new file mode 100644 index 0000000..a2ce841 --- /dev/null +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs @@ -0,0 +1,72 @@ +using ProjectLiner.Entities; + +namespace ProjectLiner.Drawnings; + +/// +/// Класс, отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawningLiner : DrawningCommonLiner +{ + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия якоря + /// Признак наличия шлюпок + /// Признак наличия трубы + public DrawningLiner(int speed, double weight, Color bodycolor, Color additionalColor, bool anchor, bool boats, bool pipe) : base(150, 125) + { + EntityCommonLiner = new EntityLiner(speed, weight, bodycolor, additionalColor, anchor, boats, pipe); + + } + + public override void DrawTransport(Graphics g) + { + if (EntityCommonLiner == null || EntityCommonLiner is not EntityLiner liner || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(liner.AdditionalColor); + Brush br = new SolidBrush(liner.BodyColor); + + _startPosY += 40; + base.DrawTransport(g); + _startPosY -= 40; + + + // якорь + if (liner.Anchor) + { + g.DrawLine(pen, _startPosX.Value + 45, _startPosY.Value + 85, _startPosX.Value + 45, _startPosY.Value + 115); + g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 95, _startPosX.Value + 60, _startPosY.Value + 95); + g.DrawLine(pen, _startPosX.Value + 25, _startPosY.Value + 100, _startPosX.Value + 45, _startPosY.Value + 115); + g.DrawLine(pen, _startPosX.Value + 45, _startPosY.Value + 115, _startPosX.Value + 65, _startPosY.Value + 100); + } + + // шлюпки + if (liner.Boats) + { + g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 85, 30, 15); + g.FillEllipse(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 85, 30, 15); + g.FillEllipse(additionalBrush, _startPosX.Value + 110, _startPosY.Value + 85, 30, 15); + } + + // труба + if (liner.Pipe) + { + + Point point9 = new Point(_startPosX.Value + 90, _startPosY.Value + 10); + Point point10 = new Point(_startPosX.Value + 90, _startPosY.Value + 40); + Point point11 = new Point(_startPosX.Value + 115, _startPosY.Value + 40); + Point point12 = new Point(_startPosX.Value + 115, _startPosY.Value + 10); + Point[] curvePoints3 = { point9, point10, point11, point12 }; + g.FillPolygon(br, curvePoints3); + } + } + +} diff --git a/ProjectLiner/ProjectLiner/Entities/EntityCommonLiner.cs b/ProjectLiner/ProjectLiner/Entities/EntityCommonLiner.cs new file mode 100644 index 0000000..bb99401 --- /dev/null +++ b/ProjectLiner/ProjectLiner/Entities/EntityCommonLiner.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +/// +/// Класс-сущность обычного "Лайнера" +/// + +namespace ProjectLiner.Entities +{ + public class EntityCommonLiner + { + /// + /// Скорость + /// + public int Speed { get; private set; } + + /// + /// Вес + /// + public double Weight { get; private set; } + + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + + /// + /// Шаг перемещения автомобиля + /// + public double Step => Speed * 100 / Weight; + + /// + /// Конструктор лайнера + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + + public EntityCommonLiner(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + + } + } +} diff --git a/ProjectLiner/ProjectLiner/EntityLiner.cs b/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs similarity index 58% rename from ProjectLiner/ProjectLiner/EntityLiner.cs rename to ProjectLiner/ProjectLiner/Entities/EntityLiner.cs index 66d27c1..18a65d3 100644 --- a/ProjectLiner/ProjectLiner/EntityLiner.cs +++ b/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs @@ -1,24 +1,10 @@ -namespace ProjectLiner; +namespace ProjectLiner.Entities; /// /// Класс-сущность "Лайнер" /// -public class EntityLiner +public class EntityLiner: EntityCommonLiner { - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } - - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } /// /// Дополнительный цвет (для опциональных элементов) @@ -40,26 +26,17 @@ public class EntityLiner /// public bool Pipe { get; private set; } - /// - /// Шаг перемещения автомобиля - /// - public double Step => Speed * 100 / Weight; /// /// Инициализация полей объекта-класса спортивного автомобиля /// - /// Скорость - /// Вес автомобиля - /// Основной цвет /// Дополнительный цвет /// Признак наличия якоря /// Признак наличия шлюпок /// Признак наличия трубы - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool anchor, bool boats, bool pipe) + public EntityLiner(int speed, double weight, Color bodyColor, Color additionalColor, bool anchor, bool boats, bool pipe) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; + new EntityCommonLiner(speed, weight, bodyColor); AdditionalColor = additionalColor; Anchor = anchor; Boats = boats; diff --git a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs index 94eb653..2806d52 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs @@ -34,6 +34,7 @@ buttonUp = new Button(); buttonLeft = new Button(); buttonRight = new Button(); + buttonCreateCommonLiner = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit(); SuspendLayout(); // @@ -50,11 +51,11 @@ // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 520); + buttonCreate.Location = new Point(12, 544); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(150, 82); + buttonCreate.Size = new Size(203, 58); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать Лайнер"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += buttonCreate_Click; // @@ -106,11 +107,23 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // + // buttonCreateCommonLiner + // + buttonCreateCommonLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateCommonLiner.Location = new Point(221, 544); + buttonCreateCommonLiner.Name = "buttonCreateCommonLiner"; + buttonCreateCommonLiner.Size = new Size(240, 58); + buttonCreateCommonLiner.TabIndex = 6; + buttonCreateCommonLiner.Text = "Создать Обычный Лайнер"; + buttonCreateCommonLiner.UseVisualStyleBackColor = true; + buttonCreateCommonLiner.Click += buttonCreateCommonLiner_Click; + // // FormLiner // AutoScaleDimensions = new SizeF(11F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(948, 614); + Controls.Add(buttonCreateCommonLiner); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonUp); @@ -131,5 +144,6 @@ private Button buttonUp; private Button buttonLeft; private Button buttonRight; + private Button buttonCreateCommonLiner; } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormLiner.cs b/ProjectLiner/ProjectLiner/FormLiner.cs index c72187d..8597fce 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.cs @@ -7,12 +7,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using ProjectLiner.Drawnings; namespace ProjectLiner { public partial class FormLiner : Form { - private DrawningLiner? _drawningLiner; + private DrawningCommonLiner? _drawningCommonLiner; public FormLiner() { InitializeComponent(); @@ -23,35 +24,66 @@ namespace ProjectLiner /// private void Draw() { - if (_drawningLiner == null) + if (_drawningCommonLiner == null) { return; } Bitmap bmp = new(pictureBoxLiner.Width, pictureBoxLiner.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningLiner.DrawTransport(gr); + _drawningCommonLiner.DrawTransport(gr); pictureBoxLiner.Image = bmp; } /// - /// Обработка нажатия кнопки "Создать" + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawningCommonLiner): + _drawningCommonLiner = new DrawningCommonLiner(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(DrawningLiner): + _drawningCommonLiner = new DrawningLiner(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)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + + } + + _drawningCommonLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height); + _drawningCommonLiner.SetPosition(random.Next(10, 100), random.Next(10, 100)); + + Draw(); + } + + /// + /// Обработка нажатия кнопки "Создать Лайнер" /// /// /// private void buttonCreate_Click(object sender, EventArgs e) { - Random random = new(); - EntityLiner liner = new EntityLiner(); - liner.Init(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)), Convert.ToBoolean(random.Next(0, 2))); - _drawningLiner = new DrawningLiner(); - _drawningLiner.Init(liner); - _drawningLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height); - _drawningLiner.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); + CreateObject(nameof(DrawningLiner)); + } + + /// + /// Обработка нажатия кнопки "Создать Обычный Лайнер" + /// + /// + /// + + private void buttonCreateCommonLiner_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningCommonLiner)); } /// @@ -61,7 +93,7 @@ namespace ProjectLiner /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningLiner == null) + if (_drawningCommonLiner == null) { return; } @@ -71,16 +103,16 @@ namespace ProjectLiner switch (name) { case "buttonUp": - result = _drawningLiner.MoveTransport(DirectionType.Up); + result = _drawningCommonLiner.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawningLiner.MoveTransport(DirectionType.Down); + result = _drawningCommonLiner.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawningLiner.MoveTransport(DirectionType.Left); + result = _drawningCommonLiner.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawningLiner.MoveTransport(DirectionType.Right); + result = _drawningCommonLiner.MoveTransport(DirectionType.Right); break; } @@ -89,5 +121,6 @@ namespace ProjectLiner Draw(); } } + } } -- 2.25.1 From ecfe3f3131795c7eb4b6f126e3e20de3b27862a8 Mon Sep 17 00:00:00 2001 From: gettterot Date: Wed, 6 Mar 2024 09:38:21 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5?= =?UTF-8?q?=D1=89=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=20=D1=86=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D1=80=D1=83=20=D0=B8=20=D0=BA=20=D0=BA=D1=80=D0=B0=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectLiner/Drawnings/DirectionType.cs | 5 + .../Drawnings/DrawningCommonLiner.cs | 20 +++ .../ProjectLiner/Drawnings/DrawningLiner.cs | 4 +- .../ProjectLiner/FormLiner.Designer.cs | 26 ++++ ProjectLiner/ProjectLiner/FormLiner.cs | 49 +++++- .../MovementStrategy/AbstractStrategy.cs | 145 ++++++++++++++++++ .../MovementStrategy/IMoveableObjectcs.cs | 30 ++++ .../MovementStrategy/MoveToBorder.cs | 58 +++++++ .../MovementStrategy/MoveToCenter.cs | 60 ++++++++ .../MovementStrategy/MoveableLiner.cs | 69 +++++++++ .../MovementStrategy/MovementDirection.cs | 30 ++++ .../MovementStrategy/ObjectParameters.cs | 78 ++++++++++ .../MovementStrategy/StrategyStatus.cs | 27 ++++ 13 files changed, 598 insertions(+), 3 deletions(-) create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObjectcs.cs create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs create mode 100644 ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs diff --git a/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs index cc78a5a..bca182c 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs @@ -5,6 +5,11 @@ /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs index 2bc1885..f3e6fe6 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs @@ -43,6 +43,26 @@ public class DrawningCommonLiner /// private readonly int _drawningLinerHeight = 90; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningLinerWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningLinerHeight; + /// /// Пустой конструктор /// diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs index a2ce841..46a4021 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs @@ -12,7 +12,7 @@ public class DrawningLiner : DrawningCommonLiner /// /// Скорость /// Вес - /// Основной цвет + /// Основной цвет /// Дополнительный цвет /// Признак наличия якоря /// Признак наличия шлюпок @@ -65,7 +65,7 @@ public class DrawningLiner : DrawningCommonLiner Point point11 = new Point(_startPosX.Value + 115, _startPosY.Value + 40); Point point12 = new Point(_startPosX.Value + 115, _startPosY.Value + 10); Point[] curvePoints3 = { point9, point10, point11, point12 }; - g.FillPolygon(br, curvePoints3); + g.FillPolygon(additionalBrush, curvePoints3); } } diff --git a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs index 2806d52..c21ee63 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs @@ -35,6 +35,8 @@ buttonLeft = new Button(); buttonRight = new Button(); buttonCreateCommonLiner = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit(); SuspendLayout(); // @@ -118,11 +120,33 @@ buttonCreateCommonLiner.UseVisualStyleBackColor = true; buttonCreateCommonLiner.Click += buttonCreateCommonLiner_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(742, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(194, 33); + comboBoxStrategy.TabIndex = 11; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(816, 51); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(120, 37); + buttonStrategyStep.TabIndex = 12; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormLiner // AutoScaleDimensions = new SizeF(11F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(948, 614); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateCommonLiner); Controls.Add(buttonRight); Controls.Add(buttonLeft); @@ -145,5 +169,7 @@ private Button buttonLeft; private Button buttonRight; private Button buttonCreateCommonLiner; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormLiner.cs b/ProjectLiner/ProjectLiner/FormLiner.cs index 8597fce..aa13498 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.cs @@ -8,15 +8,19 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectLiner.Drawnings; +using ProjectLiner.MovementStrategy; namespace ProjectLiner { public partial class FormLiner : Form { private DrawningCommonLiner? _drawningCommonLiner; + + private AbstractStrategy? _strategy; public FormLiner() { InitializeComponent(); + _strategy = null; } /// @@ -61,7 +65,8 @@ namespace ProjectLiner _drawningCommonLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height); _drawningCommonLiner.SetPosition(random.Next(10, 100), random.Next(10, 100)); - + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -122,5 +127,47 @@ namespace ProjectLiner } } + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningCommonLiner == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableLiner(_drawningCommonLiner), pictureBoxLiner.Width, pictureBoxLiner.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs b/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..ca37c66 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.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 (IsTargetDestination()) + { + _state = StrategyStatus.Finish; + return; + } + + MoveToTarget(); + } + + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(MovementDirection.Right); + + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(MovementDirection.Up); + + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + 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 IsTargetDestination(); + + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObjectcs.cs b/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObjectcs.cs new file mode 100644 index 0000000..a5ace3f --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObjectcs.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..37eced3 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.MovementStrategy; + +/// +/// Стратегия перемещения объекта к правой нижней границы +/// +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? 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() + { + 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/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..afe820c --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.MovementStrategy; + +/// +/// Стратегия перемещения объекта в центр экрана +/// +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + 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/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs new file mode 100644 index 0000000..c87517a --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs @@ -0,0 +1,69 @@ +using ProjectLiner.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.MovementStrategy; + +/// +/// Класс-реализация IMoveableObject с использованием DrawningCommonLiner +/// +public class MoveableLiner : IMoveableObject +{ + /// + /// Поле-объект класса DrawningCommonLiner или его наследника + /// + private readonly DrawningCommonLiner? _CommonLiner = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningCommonLiner + public MoveableLiner(DrawningCommonLiner CommonLiner) + { + _CommonLiner = CommonLiner; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_CommonLiner == null || _CommonLiner.EntityCommonLiner == null || !_CommonLiner.GetPosX.HasValue || !_CommonLiner.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_CommonLiner.GetPosX.Value, _CommonLiner.GetPosY.Value, _CommonLiner.GetWidth, _CommonLiner.GetHeight); + } + } + + public int GetStep => (int)(_CommonLiner?.EntityCommonLiner?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_CommonLiner == null || _CommonLiner.EntityCommonLiner == null) + { + return false; + } + + return _CommonLiner.MoveTransport(GetDirectionType(direction)); + } + + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + 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, + }; + } +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..3981ef6 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs @@ -0,0 +1,30 @@ +namespace ProjectLiner.MovementStrategy; + + + +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} + diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs b/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..251aa9a --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.MovementStrategy; + +/// +/// Параметры-координаты объекта +/// +public class ObjectParameters +{ + /// + /// Координата X + /// + private readonly int _x; + + /// + /// Координата Y + /// + 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; + } +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs b/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..b221f83 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.MovementStrategy; +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +} \ No newline at end of file -- 2.25.1 From f1f6698e1fee6a2d006a12400df5b08df8cb7ae5 Mon Sep 17 00:00:00 2001 From: gettterot Date: Wed, 6 Mar 2024 09:45:25 +0400 Subject: [PATCH 3/3] =?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=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F=20=D0=B2=20DrawningCommonLiner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs index f3e6fe6..b47fb35 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs @@ -6,6 +6,9 @@ using System.Text; using System.Threading.Tasks; namespace ProjectLiner.Drawnings; +/// +/// Класс, отвечающий за прорисовку и перемещение обычного объекта-сущности +/// public class DrawningCommonLiner { /// @@ -36,7 +39,7 @@ public class DrawningCommonLiner /// /// Ширина прорисовки лайнера /// - private readonly int _drawningLinerWidth = 150; + private readonly int _drawningLinerWidth = 155; /// /// Высота прорисовки лайнера -- 2.25.1