From 63173ed4781c5c28a5b2f6982532b9e55fe3d425 Mon Sep 17 00:00:00 2001 From: dlopatin Date: Mon, 15 Apr 2024 17:08:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormWarmlyLocomotive.Designer.cs | 44 ++++++- WarmlyLocomotive/FormWarmlyLocomotive.cs | 120 ++++++++++++++---- 2 files changed, 140 insertions(+), 24 deletions(-) diff --git a/WarmlyLocomotive/FormWarmlyLocomotive.Designer.cs b/WarmlyLocomotive/FormWarmlyLocomotive.Designer.cs index 4dd3069..b32457e 100644 --- a/WarmlyLocomotive/FormWarmlyLocomotive.Designer.cs +++ b/WarmlyLocomotive/FormWarmlyLocomotive.Designer.cs @@ -34,6 +34,9 @@ buttonDown = new Button(); buttonLeft = new Button(); buttonRight = new Button(); + createBaseButton = new Button(); + comboBoxStrategy = new ComboBox(); + strategyMoveButton = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyLocomotive).BeginInit(); SuspendLayout(); // @@ -52,9 +55,9 @@ createButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; createButton.Location = new Point(12, 426); createButton.Name = "createButton"; - createButton.Size = new Size(75, 23); + createButton.Size = new Size(153, 23); createButton.TabIndex = 1; - createButton.Text = "Создать"; + createButton.Text = "Создать тепловоз"; createButton.UseVisualStyleBackColor = true; createButton.Click += createButton_Click; // @@ -107,11 +110,45 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += moveButton_Click; // + // createBaseButton + // + createBaseButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + createBaseButton.Location = new Point(171, 426); + createBaseButton.Name = "createBaseButton"; + createBaseButton.Size = new Size(153, 23); + createBaseButton.TabIndex = 6; + createBaseButton.Text = "Создать локомотив"; + createBaseButton.UseVisualStyleBackColor = true; + createBaseButton.Click += createBaseButton_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(753, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // strategyMoveButton + // + strategyMoveButton.Location = new Point(797, 41); + strategyMoveButton.Name = "strategyMoveButton"; + strategyMoveButton.Size = new Size(75, 23); + strategyMoveButton.TabIndex = 8; + strategyMoveButton.Text = "Шаг"; + strategyMoveButton.UseVisualStyleBackColor = true; + strategyMoveButton.Click += strategyMoveButton_Click; + // // FormWarmlyLocomotive // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(strategyMoveButton); + Controls.Add(comboBoxStrategy); + Controls.Add(createBaseButton); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonDown); @@ -134,5 +171,8 @@ private Button buttonDown; private Button buttonLeft; private Button buttonRight; + private Button createBaseButton; + private ComboBox comboBoxStrategy; + private Button strategyMoveButton; } } \ No newline at end of file diff --git a/WarmlyLocomotive/FormWarmlyLocomotive.cs b/WarmlyLocomotive/FormWarmlyLocomotive.cs index ba05c40..ade9492 100644 --- a/WarmlyLocomotive/FormWarmlyLocomotive.cs +++ b/WarmlyLocomotive/FormWarmlyLocomotive.cs @@ -8,52 +8,92 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WarmlyLocomotive.Drawnings; +using WarmlyLocomotive.MovementStrategy; namespace WarmlyLocomotive { public partial class FormWarmlyLocomotive : Form { - private DrawningWarmlyLocomotive? _drawningWarmlyLocomotive; + private DrawningLocomotive? _drawningLocomotive; + + private AbstractStrategy _strategy; public FormWarmlyLocomotive() { InitializeComponent(); + _strategy = null; } private void Draw() { - if (_drawningWarmlyLocomotive == null) + if (_drawningLocomotive == null) { return; } Bitmap bmp = new(pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningWarmlyLocomotive.DrawTransport(gr); + _drawningLocomotive.DrawTransport(gr); pictureBoxWarmlyLocomotive.Image = bmp; } - - private void createButton_Click(object sender, EventArgs e){ + private void CreateObject(string type) + { Random random = new(); - _drawningWarmlyLocomotive = new DrawningWarmlyLocomotive(); - _drawningWarmlyLocomotive.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))); - _drawningWarmlyLocomotive.SetPictureSize( - pictureBoxWarmlyLocomotive.Width, - pictureBoxWarmlyLocomotive.Height); - _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); + switch (type) + { + case nameof(DrawningLocomotive): + { + _drawningLocomotive = new DrawningLocomotive( + 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(DrawningWarmlyLocomotive): + { + _drawningLocomotive = new DrawningWarmlyLocomotive( + 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))); + break; + } + default: + return; + } + _drawningLocomotive.SetPictureSize(pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height); + _drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } + + private void createButton_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarmlyLocomotive)); + private void createBaseButton_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive)); + //{ + //Random random = new(); + //_drawningWarmlyLocomotive = new DrawningWarmlyLocomotive(); + //_drawningWarmlyLocomotive.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))); + //_drawningWarmlyLocomotive.SetPictureSize( + // pictureBoxWarmlyLocomotive.Width, + // pictureBoxWarmlyLocomotive.Height); + //_drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); + //Draw(); + //} + private void moveButton_Click(object sender, EventArgs e) { - if (_drawningWarmlyLocomotive == null) + if (_drawningLocomotive == null) { return; } @@ -63,19 +103,19 @@ namespace WarmlyLocomotive { case "buttonUp": result = - _drawningWarmlyLocomotive.MoveTransport(DirectionType.Up); + _drawningLocomotive.MoveTransport(DirectionType.Up); break; case "buttonDown": result = - _drawningWarmlyLocomotive.MoveTransport(DirectionType.Down); + _drawningLocomotive.MoveTransport(DirectionType.Down); break; case "buttonLeft": result = - _drawningWarmlyLocomotive.MoveTransport(DirectionType.Left); + _drawningLocomotive.MoveTransport(DirectionType.Left); break; case "buttonRight": result = - _drawningWarmlyLocomotive.MoveTransport(DirectionType.Right); + _drawningLocomotive.MoveTransport(DirectionType.Right); break; } if (result) @@ -85,5 +125,41 @@ namespace WarmlyLocomotive } + private void strategyMoveButton_Click(object sender, EventArgs e) + { + if (_drawningLocomotive == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableLocomotive(_drawningLocomotive), + pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } + + } }