From c8eed1e7dfe1909f5301cca62951242b810b79f1 Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Fri, 8 Dec 2023 20:11:31 +0400 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lainer/Lainer1/EntitySuperLainer.cs | 16 ++++++ lainer/Lainer1/IMoveableObject.cs | 11 +++++ lainer/Lainer1/LainerForm.Designer.cs | 71 +++++++++++++++++++++------ lainer/Lainer1/LainerForm.cs | 70 +++++++++++++++++++++----- lainer/Lainer1/MoveToBorder.cs | 58 ++++++++++++++++++++++ 5 files changed, 197 insertions(+), 29 deletions(-) create mode 100644 lainer/Lainer1/EntitySuperLainer.cs create mode 100644 lainer/Lainer1/IMoveableObject.cs create mode 100644 lainer/Lainer1/MoveToBorder.cs diff --git a/lainer/Lainer1/EntitySuperLainer.cs b/lainer/Lainer1/EntitySuperLainer.cs new file mode 100644 index 0000000..73771d5 --- /dev/null +++ b/lainer/Lainer1/EntitySuperLainer.cs @@ -0,0 +1,16 @@ +namespace ProjectLainer.Entities +{ + public class EntitySuperLainer : EntityLainer + { + public Color AdditionalColor { get; private set; } + public bool Pools { get; private set; } + public bool Decks { get; private set; } + public EntitySuperLainer(int speed, double weight, Color bodyColor, Color + additionalColor, bool pools, bool decks) : base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + Pools = pools; + Decks = decks; + } + } +} diff --git a/lainer/Lainer1/IMoveableObject.cs b/lainer/Lainer1/IMoveableObject.cs new file mode 100644 index 0000000..04ccadf --- /dev/null +++ b/lainer/Lainer1/IMoveableObject.cs @@ -0,0 +1,11 @@ +//using ProjectLainer.Drawings; +namespace ProjectLainer.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/lainer/Lainer1/LainerForm.Designer.cs b/lainer/Lainer1/LainerForm.Designer.cs index aadae36..25b0db9 100644 --- a/lainer/Lainer1/LainerForm.Designer.cs +++ b/lainer/Lainer1/LainerForm.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBoxLainer = new PictureBox(); - buttonCreate = new Button(); + ButtonCreate = new Button(); buttonRight = new Button(); buttonLeft = new Button(); buttonDown = new Button(); buttonUp = new Button(); + ButtonCreateSuperLainer = new Button(); + comboBoxStrategy = new ComboBox(); + ButtonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLainer).BeginInit(); SuspendLayout(); // @@ -47,16 +50,16 @@ pictureBoxLainer.TabIndex = 0; pictureBoxLainer.TabStop = false; // - // buttonCreate + // ButtonCreate // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 356); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + ButtonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreate.Location = new Point(12, 356); + ButtonCreate.Name = "ButtonCreate"; + ButtonCreate.Size = new Size(94, 29); + ButtonCreate.TabIndex = 1; + ButtonCreate.Text = "создать"; + ButtonCreate.UseVisualStyleBackColor = true; + ButtonCreate.Click += ButtonCreate_Click; // // buttonRight // @@ -68,7 +71,7 @@ buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 2; buttonRight.UseVisualStyleBackColor = true; - buttonRight.Click += buttonMove_Click; + buttonRight.Click += ButtonMove_Click; // // buttonLeft // @@ -80,7 +83,7 @@ buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 3; buttonLeft.UseVisualStyleBackColor = true; - buttonLeft.Click += buttonMove_Click; + buttonLeft.Click += ButtonMove_Click; // // buttonDown // @@ -92,7 +95,7 @@ buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; - buttonDown.Click += buttonMove_Click; + buttonDown.Click += ButtonMove_Click; // // buttonUp // @@ -104,18 +107,50 @@ buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 5; buttonUp.UseVisualStyleBackColor = true; - buttonUp.Click += buttonMove_Click; + buttonUp.Click += ButtonMove_Click; + // + // ButtonCreateSuperLainer + // + ButtonCreateSuperLainer.Location = new Point(137, 356); + ButtonCreateSuperLainer.Name = "ButtonCreateSuperLainer"; + ButtonCreateSuperLainer.Size = new Size(192, 29); + ButtonCreateSuperLainer.TabIndex = 6; + ButtonCreateSuperLainer.Text = "создать супер лайнер"; + ButtonCreateSuperLainer.UseVisualStyleBackColor = true; + ButtonCreateSuperLainer.Click += ButtonCreateSuperLainer_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "центр", "край"}); + comboBoxStrategy.Location = new Point(566, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 7; + // + // ButtonStep + // + ButtonStep.Location = new Point(623, 66); + ButtonStep.Name = "ButtonStep"; + ButtonStep.Size = new Size(94, 29); + ButtonStep.TabIndex = 8; + ButtonStep.Text = "шаг"; + ButtonStep.UseVisualStyleBackColor = true; + ButtonStep.Click += ButtonStep_Click; // // LainerForm // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(729, 397); + Controls.Add(ButtonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(ButtonCreateSuperLainer); Controls.Add(buttonUp); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonRight); - Controls.Add(buttonCreate); + Controls.Add(ButtonCreate); Controls.Add(pictureBoxLainer); Name = "LainerForm"; Text = "LainerField"; @@ -124,13 +159,17 @@ PerformLayout(); } + #endregion private PictureBox pictureBoxLainer; - private Button buttonCreate; + private Button ButtonCreate; private Button buttonRight; private Button buttonLeft; private Button buttonDown; private Button buttonUp; + private Button ButtonCreateSuperLainer; + private ComboBox comboBoxStrategy; + private Button ButtonStep; } } \ No newline at end of file diff --git a/lainer/Lainer1/LainerForm.cs b/lainer/Lainer1/LainerForm.cs index debdbcd..fe7518d 100644 --- a/lainer/Lainer1/LainerForm.cs +++ b/lainer/Lainer1/LainerForm.cs @@ -1,16 +1,15 @@ +using ProjectLainer.DrawningObjects; +using ProjectLainer.MovementStrategy; namespace ProjectLainer { public partial class LainerForm : Form { - - private DrawningLainer? _drawningLainer; - + private DrawingLainer? _drawningLainer; + private AbstractStrategy? _abstractStrategy; public LainerForm() { InitializeComponent(); - } - private void Draw() { if (_drawningLainer == null) @@ -23,12 +22,10 @@ namespace ProjectLainer _drawningLainer.DrawTransport(gr); pictureBoxLainer.Image = bmp; } - - private void buttonCreate_Click(object sender, EventArgs e) + private void ButtonCreateSuperLainer_Click(object sender, EventArgs e) { Random random = new(); - _drawningLainer = new DrawningLainer(); - _drawningLainer.Init(random.Next(100, 300), + _drawningLainer = new DrawningSuperLainer(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), @@ -37,12 +34,23 @@ namespace ProjectLainer Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxLainer.Width, pictureBoxLainer.Height); - _drawningLainer.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + _drawningLainer.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } - - private void buttonMove_Click(object sender, EventArgs e) + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningLainer = new DrawingLainer(random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + pictureBoxLainer.Width, pictureBoxLainer.Height); + _drawningLainer.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningLainer == null) { @@ -66,5 +74,41 @@ namespace ProjectLainer } Draw(); } + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawningLainer == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectLainer(_drawningLainer), pictureBoxLainer.Width, + pictureBoxLainer.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/lainer/Lainer1/MoveToBorder.cs b/lainer/Lainer1/MoveToBorder.cs new file mode 100644 index 0000000..e94386d --- /dev/null +++ b/lainer/Lainer1/MoveToBorder.cs @@ -0,0 +1,58 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLainer.MovementStrategy +{ + public 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(); + } + } + } + } +}