создание стратегии дохождения до границы, обновление формы и подключение нового функционала к элементам управления
This commit is contained in:
parent
5eaaca5971
commit
d25d9a4ea5
@ -1,6 +1,6 @@
|
||||
namespace ProjectElectricLocomotive
|
||||
{
|
||||
partial class FormElectricLocomotive
|
||||
partial class FormLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
@ -30,10 +30,13 @@
|
||||
{
|
||||
buttonUp = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateLocomotive = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonLeft = new Button();
|
||||
pictureBoxElectricLocomotive = new PictureBox();
|
||||
buttonCreateElectricLocomotive = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxElectricLocomotive).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -42,7 +45,7 @@
|
||||
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
|
||||
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonUp.Location = new Point(55, 459);
|
||||
buttonUp.Location = new Point(54, 514);
|
||||
buttonUp.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonUp.Name = "buttonUp";
|
||||
buttonUp.Size = new Size(34, 40);
|
||||
@ -56,7 +59,7 @@
|
||||
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
|
||||
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonRight.Location = new Point(96, 507);
|
||||
buttonRight.Location = new Point(95, 562);
|
||||
buttonRight.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonRight.Name = "buttonRight";
|
||||
buttonRight.Size = new Size(34, 40);
|
||||
@ -65,24 +68,24 @@
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += buttonRight_Click;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateLocomotive
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(14, 559);
|
||||
buttonCreate.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(117, 40);
|
||||
buttonCreate.TabIndex = 2;
|
||||
buttonCreate.Text = "создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreateLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonCreateLocomotive.Location = new Point(816, 562);
|
||||
buttonCreateLocomotive.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonCreateLocomotive.Name = "buttonCreateLocomotive";
|
||||
buttonCreateLocomotive.Size = new Size(182, 40);
|
||||
buttonCreateLocomotive.TabIndex = 2;
|
||||
buttonCreateLocomotive.Text = "создать локомотив";
|
||||
buttonCreateLocomotive.UseVisualStyleBackColor = true;
|
||||
buttonCreateLocomotive.Click += buttonCreateLocomotive_Click;
|
||||
//
|
||||
// buttonDown
|
||||
//
|
||||
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
|
||||
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonDown.Location = new Point(55, 507);
|
||||
buttonDown.Location = new Point(54, 562);
|
||||
buttonDown.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonDown.Name = "buttonDown";
|
||||
buttonDown.Size = new Size(34, 40);
|
||||
@ -96,7 +99,7 @@
|
||||
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
|
||||
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonLeft.Location = new Point(14, 507);
|
||||
buttonLeft.Location = new Point(13, 562);
|
||||
buttonLeft.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonLeft.Name = "buttonLeft";
|
||||
buttonLeft.Size = new Size(34, 40);
|
||||
@ -116,20 +119,58 @@
|
||||
pictureBoxElectricLocomotive.TabStop = false;
|
||||
pictureBoxElectricLocomotive.SizeChanged += pictureBoxElectricLocomotive_SizeChanged;
|
||||
//
|
||||
// FormElectricLocomotive
|
||||
// buttonCreateElectricLocomotive
|
||||
//
|
||||
buttonCreateElectricLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonCreateElectricLocomotive.Location = new Point(816, 514);
|
||||
buttonCreateElectricLocomotive.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive";
|
||||
buttonCreateElectricLocomotive.Size = new Size(182, 40);
|
||||
buttonCreateElectricLocomotive.TabIndex = 6;
|
||||
buttonCreateElectricLocomotive.Text = "создать электропоезд";
|
||||
buttonCreateElectricLocomotive.UseVisualStyleBackColor = true;
|
||||
buttonCreateElectricLocomotive.Click += buttonCreateElectricLocomotive_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "идти к центру экрана", "идти к краю экрана" });
|
||||
comboBoxStrategy.Location = new Point(816, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(182, 28);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStep.Location = new Point(915, 47);
|
||||
buttonStep.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(83, 33);
|
||||
buttonStep.TabIndex = 8;
|
||||
buttonStep.Text = "шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// FormLocomotive
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(1010, 615);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateElectricLocomotive);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateLocomotive);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(pictureBoxElectricLocomotive);
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
MinimumSize = new Size(100, 100);
|
||||
Name = "FormElectricLocomotive";
|
||||
MinimumSize = new Size(500, 300);
|
||||
Name = "FormLocomotive";
|
||||
Text = "ElectricLocomotive";
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxElectricLocomotive).EndInit();
|
||||
ResumeLayout(false);
|
||||
@ -140,9 +181,12 @@
|
||||
|
||||
private Button buttonUp;
|
||||
private Button buttonRight;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateLocomotive;
|
||||
private Button buttonDown;
|
||||
private Button buttonLeft;
|
||||
private PictureBox pictureBoxElectricLocomotive;
|
||||
private Button buttonCreateElectricLocomotive;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
namespace ProjectElectricLocomotive
|
||||
{
|
||||
public partial class FormElectricLocomotive : Form
|
||||
public partial class FormLocomotive : Form
|
||||
{
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
private void buttonCreateLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
ButtonCreateElectricLocomotive_Click(sender, e);
|
||||
ButtonCreateLocomotive_Click(sender, e);
|
||||
}
|
||||
|
||||
private void buttonUp_Click(object sender, EventArgs e)
|
||||
@ -31,5 +31,15 @@ namespace ProjectElectricLocomotive
|
||||
{
|
||||
PictureBoxElectricLocomotive_SizeChanged(sender, e);
|
||||
}
|
||||
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
ButtonStep_Click(sender, e);
|
||||
}
|
||||
|
||||
private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
ButtonCreateElectricLocomotive_Click(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using ProjectElectricLocomotive.DrawningObjects;
|
||||
using ProjectElectricLocomotive.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -10,16 +11,20 @@ namespace ProjectElectricLocomotive
|
||||
/// <summary>
|
||||
/// Форма работы с объектом "электровоз"
|
||||
/// </summary>
|
||||
public partial class FormElectricLocomotive
|
||||
public partial class FormLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// <summary>
|
||||
private DrawningLocomotive? _drawningElectricLocomotive;
|
||||
private DrawningLocomotive? _drawningLocomotive;
|
||||
/// <summary>
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
/// <summary>
|
||||
/// Инициализация формы
|
||||
/// </summary>
|
||||
public FormElectricLocomotive()
|
||||
public FormLocomotive()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
@ -28,46 +33,80 @@ namespace ProjectElectricLocomotive
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxElectricLocomotive.Width,
|
||||
pictureBoxElectricLocomotive.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningElectricLocomotive.DrawTransport(gr);
|
||||
_drawningLocomotive.DrawTransport(gr);
|
||||
pictureBoxElectricLocomotive.Image = bmp;
|
||||
}
|
||||
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
|
||||
/// Обработка нажатия кнопки "Создать электровоз"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateElectricLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningElectricLocomotive = new DrawningElectricLocomotive();
|
||||
_drawningElectricLocomotive.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)),
|
||||
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
|
||||
_drawningElectricLocomotive.SetPosition(random.Next(10, 100),
|
||||
random.Next(10, 100));
|
||||
_drawningLocomotive = new DrawningElectricLocomotive(
|
||||
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)),
|
||||
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
|
||||
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать локомотив"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_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)),
|
||||
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
|
||||
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение размеров формы
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void PictureBoxElectricLocomotive_SizeChanged(object sender, EventArgs e)
|
||||
{
|
||||
Size size = ((PictureBox)sender)?.Size ?? Size.Empty;
|
||||
|
||||
if (_abstractStrategy != null)
|
||||
{
|
||||
_abstractStrategy.SetFieldSize(size);
|
||||
}
|
||||
if (_drawningLocomotive != null)
|
||||
{
|
||||
_drawningLocomotive.SetPictureSize(size);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение положения автомобиля
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -75,29 +114,63 @@ namespace ProjectElectricLocomotive
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Up);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Down);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Left);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Right);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void PictureBoxElectricLocomotive_SizeChanged(object sender, EventArgs e)
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Шаг"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Size size = ((PictureBox)sender)?.Size ?? Size.Empty;
|
||||
_drawningElectricLocomotive.SetPictureSize(size);
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawningObjectLocomotive(_drawningLocomotive), pictureBoxElectricLocomotive.Width,
|
||||
pictureBoxElectricLocomotive.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
using ElectricLocomotive;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -90,8 +89,7 @@ namespace ProjectElectricLocomotive.MovementStrategy
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters =>
|
||||
_moveableObject?.GetObjectPosition;
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
@ -131,5 +129,11 @@ namespace ProjectElectricLocomotive.MovementStrategy
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetFieldSize(Size newSize)
|
||||
{
|
||||
FieldWidth = newSize.Width;
|
||||
FieldHeight = newSize.Height;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
45
ElectricLocomotive/MovementStrategy/MoveToBorder.cs
Normal file
45
ElectricLocomotive/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта к границе экрана экрана
|
||||
/// </summary>
|
||||
internal class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (MoveRight())
|
||||
{
|
||||
MoveLeft();
|
||||
return false;
|
||||
}
|
||||
if (MoveDown())
|
||||
{
|
||||
MoveUp();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MoveRight();
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ namespace ProjectElectricLocomotive
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new FormElectricLocomotive());
|
||||
Application.Run(new FormLocomotive());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user