PIbd-14 ChertovA.A. LabWork2 Simple #2
@ -4,10 +4,11 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRoadTrain;
|
||||
namespace ProjectRoadTrain.Drawnings;
|
||||
|
||||
public enum DirectionType
|
||||
{
|
||||
Unknow = -1,
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
Left = 3,
|
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectRoadTrain.Entities;
|
||||
|
||||
namespace ProjectRoadTrain.Drawnings;
|
||||
|
||||
public class DrawningRoadTrain : DrawningTrain
|
||||
{
|
||||
|
||||
|
||||
public DrawningRoadTrain(int speed, double weight, Color bodycolor, Color bodytankcolor, bool watertank, bool cleanbrush) : base(230, 115)
|
||||
{
|
||||
EntityTrain = new EntityRoadTrain(speed, weight, bodycolor, bodytankcolor, watertank, cleanbrush);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTrain == null || EntityTrain is not EntityRoadTrain roadTrain || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush bodytankcolor = new SolidBrush(roadTrain.BodyTankColor);
|
||||
Brush blackcolor = new SolidBrush(Color.Black);
|
||||
Brush bodycolor = new SolidBrush(roadTrain.BodyColor);
|
||||
|
||||
if (roadTrain.WaterTank)
|
||||
{
|
||||
g.FillEllipse(bodytankcolor, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50);
|
||||
}
|
||||
if (roadTrain.CleanBrush)
|
||||
{
|
||||
g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 70, 100, 2);
|
||||
g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 75, 100, 2);
|
||||
g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 65, 100, 2);
|
||||
}
|
||||
|
||||
base.DrawTransport(g);
|
||||
}
|
||||
}
|
@ -1,40 +1,62 @@
|
||||
using System;
|
||||
using ProjectRoadTrain.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRoadTrain;
|
||||
namespace ProjectRoadTrain.Drawnings;
|
||||
|
||||
public class DrawningRoadTrain
|
||||
public class DrawningTrain
|
||||
|
||||
{
|
||||
public EntityRoadTrain? EntityRoadTrain { get; private set; }
|
||||
public EntityTrain? EntityTrain { get; protected set; }
|
||||
|
||||
private int? _pictureWidth;
|
||||
|
||||
private int? _pictureHight;
|
||||
|
||||
private int? _startPosX;
|
||||
protected int? _startPosX;
|
||||
|
||||
private int? _startPosY;
|
||||
protected int? _startPosY;
|
||||
|
||||
private readonly int _drawningRoadWidth = 230;
|
||||
private readonly int _drawningTrainWidth = 170;
|
||||
|
||||
private readonly int _drawningRoadHeight = 115;
|
||||
private readonly int _drawningTrainHeight = 117;
|
||||
|
||||
public void Init(int speed, double weight, Color bodycolor, Color bodytankcolor, bool watertank, bool cleanbrush)
|
||||
public int? GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawningTrainWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawningTrainHeight;
|
||||
|
||||
|
||||
private DrawningTrain()
|
||||
{
|
||||
EntityRoadTrain = new EntityRoadTrain();
|
||||
EntityRoadTrain.Init(speed, weight, bodycolor, bodytankcolor, watertank, cleanbrush);
|
||||
_pictureWidth = null;
|
||||
_pictureHight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
public DrawningTrain(int speed, double weight, Color bodycolor) : this()
|
||||
{
|
||||
EntityTrain = new EntityTrain(speed, weight, bodycolor);
|
||||
}
|
||||
protected DrawningTrain(int drawningRoadWidth, int drawningRoadHeight) : this()
|
||||
{
|
||||
_drawningTrainWidth = drawningRoadWidth;
|
||||
_drawningTrainHeight = drawningRoadHeight;
|
||||
}
|
||||
public bool SetPictureSize(int width, int hight)
|
||||
{
|
||||
if (width >= _drawningRoadWidth && hight >= _drawningRoadHeight)
|
||||
if (width >= _drawningTrainWidth && hight >= _drawningTrainHeight)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHight = hight;
|
||||
@ -59,18 +81,18 @@ public class DrawningRoadTrain
|
||||
{
|
||||
x = 0;
|
||||
}
|
||||
else if (x > _pictureWidth.Value - _drawningRoadWidth)
|
||||
else if (x > _pictureWidth.Value - _drawningTrainWidth)
|
||||
{
|
||||
x = _pictureWidth.Value - _drawningRoadWidth;
|
||||
x = _pictureWidth.Value - _drawningTrainWidth;
|
||||
}
|
||||
|
||||
if (y < 0)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
else if (y > _pictureHight.Value - _drawningRoadHeight)
|
||||
else if (y > _pictureHight.Value - _drawningTrainHeight)
|
||||
{
|
||||
y = _pictureHight.Value - _drawningRoadHeight;
|
||||
y = _pictureHight.Value - _drawningTrainHeight;
|
||||
}
|
||||
|
||||
_startPosX = x;
|
||||
@ -79,12 +101,12 @@ public class DrawningRoadTrain
|
||||
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityRoadTrain == null || !_startPosX.HasValue ||
|
||||
if (EntityTrain == null || !_startPosX.HasValue ||
|
||||
!_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_startPosX.Value < 0 || _startPosY.Value < 0 || _startPosX > _pictureWidth - _drawningRoadWidth || _startPosY > _pictureHight - _drawningRoadHeight)
|
||||
if (_startPosX.Value < 0 || _startPosY.Value < 0 || _startPosX > _pictureWidth - _drawningTrainWidth || _startPosY > _pictureHight - _drawningTrainHeight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -92,32 +114,32 @@ public class DrawningRoadTrain
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityRoadTrain.Step > 0)
|
||||
if (_startPosX.Value - EntityTrain.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityRoadTrain.Step;
|
||||
_startPosX -= (int)EntityTrain.Step;
|
||||
}
|
||||
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityRoadTrain.Step > 0)
|
||||
if (_startPosY.Value - EntityTrain.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityRoadTrain.Step;
|
||||
_startPosY -= (int)EntityTrain.Step;
|
||||
}
|
||||
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + EntityRoadTrain.Step < _pictureWidth - _drawningRoadWidth)
|
||||
if (_startPosX.Value + EntityTrain.Step < _pictureWidth - _drawningTrainWidth)
|
||||
{
|
||||
_startPosX += (int)EntityRoadTrain.Step;
|
||||
_startPosX += (int)EntityTrain.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY.Value + EntityRoadTrain.Step < _pictureHight - _drawningRoadHeight)
|
||||
if (_startPosY.Value + EntityTrain.Step < _pictureHight - _drawningTrainHeight)
|
||||
{
|
||||
_startPosY += (int)EntityRoadTrain.Step;
|
||||
_startPosY += (int)EntityTrain.Step;
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -127,28 +149,20 @@ public class DrawningRoadTrain
|
||||
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityRoadTrain == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityTrain == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush bodytankcolor = new SolidBrush(EntityRoadTrain.BodyTankColor);
|
||||
Brush blackcolor = new SolidBrush(Color.Black);
|
||||
Brush bodycolor = new SolidBrush(EntityRoadTrain.BodyColor);
|
||||
Brush bodycolor = new SolidBrush(EntityTrain.BodyColor);
|
||||
|
||||
if (EntityRoadTrain.WaterTank || EntityRoadTrain.CleanBrush)
|
||||
{
|
||||
g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 70, 100, 2);
|
||||
g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 75, 100, 2);
|
||||
g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 65, 100, 2);
|
||||
g.FillEllipse(bodytankcolor, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50);
|
||||
}
|
||||
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 60, 170, 20);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 120, _startPosY.Value, 50, 60);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50);
|
||||
// 120 высота
|
||||
// 270 ширина
|
||||
g.FillRectangle(blackcolor, _startPosX.Value, _startPosY.Value + 60, 170, 20);
|
||||
@ -158,3 +172,5 @@ public class DrawningRoadTrain
|
||||
g.FillEllipse(blackcolor, _startPosX.Value, _startPosY.Value + 77, 48, 40);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,18 @@
|
||||
namespace ProjectRoadTrain.Entities;
|
||||
|
||||
internal class EntityRoadTrain : EntityTrain
|
||||
{
|
||||
public Color BodyTankColor { get; private set; }
|
||||
public bool WaterTank { get; private set; }
|
||||
public bool CleanBrush { get; private set; }
|
||||
public double Step => Speed * 50 / Weight;
|
||||
|
||||
public EntityRoadTrain(int speed, double weight, Color bodycolor, Color bodytankcolor,
|
||||
bool watertank, bool cleanbrush) : base(speed, weight, bodycolor)
|
||||
{
|
||||
BodyTankColor = bodytankcolor;
|
||||
WaterTank = watertank;
|
||||
CleanBrush = cleanbrush;
|
||||
}
|
||||
|
||||
}
|
22
ProjectRoadTrain/ProjectRoadTrain/Entities/EntityTrain.cs
Normal file
22
ProjectRoadTrain/ProjectRoadTrain/Entities/EntityTrain.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRoadTrain.Entities;
|
||||
|
||||
public class EntityTrain
|
||||
{
|
||||
public int Speed { get; private set; }
|
||||
public double Weight { get; private set; }
|
||||
public Color BodyColor { get; private set; }
|
||||
public double Step => Speed * 50 / Weight;
|
||||
|
||||
public EntityTrain(int speed, double weight, Color bodycolor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodycolor;
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
namespace ProjectRoadTrain;
|
||||
|
||||
public class EntityRoadTrain
|
||||
{
|
||||
public int Speed { get; private set; }
|
||||
public double Weight { get; private set; }
|
||||
public Color BodyColor { get; private set; }
|
||||
public Color BodyTankColor { get; private set; }
|
||||
public bool WaterTank { get; private set; }
|
||||
public bool CleanBrush { get; private set; }
|
||||
public double Step => Speed * 50 / Weight;
|
||||
|
||||
public void Init(int speed, double weight, Color bodycolor, Color bodytankcolor,
|
||||
bool watertank, bool cleanbrush)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodycolor;
|
||||
BodyTankColor = bodytankcolor;
|
||||
WaterTank = watertank;
|
||||
CleanBrush = cleanbrush;
|
||||
}
|
||||
|
||||
}
|
@ -34,6 +34,9 @@
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonCreateTrain = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxRoadTrain).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -51,9 +54,9 @@
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 495);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(94, 29);
|
||||
buttonCreate.Size = new Size(184, 29);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "создать";
|
||||
buttonCreate.Text = "создать моющий камаз";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
//
|
||||
@ -105,11 +108,45 @@
|
||||
buttonUp.UseVisualStyleBackColor = true;
|
||||
buttonUp.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreateTrain
|
||||
//
|
||||
buttonCreateTrain.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateTrain.Location = new Point(215, 495);
|
||||
buttonCreateTrain.Name = "buttonCreateTrain";
|
||||
buttonCreateTrain.Size = new Size(122, 29);
|
||||
buttonCreateTrain.TabIndex = 6;
|
||||
buttonCreateTrain.Text = "создать камаз";
|
||||
buttonCreateTrain.UseVisualStyleBackColor = true;
|
||||
buttonCreateTrain.Click += buttonCreateTrain_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||
comboBoxStrategy.Location = new Point(755, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(151, 28);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Location = new Point(812, 46);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(94, 29);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "Шаг";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += buttonStrategyStep_Click;
|
||||
//
|
||||
// FormRoadTrain
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(923, 536);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateTrain);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
@ -130,5 +167,8 @@
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonCreateTrain;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,42 +1,72 @@
|
||||
namespace ProjectRoadTrain;
|
||||
using ProjectElectroTrans.MovementStrategy;
|
||||
using ProjectRoadTrain.Drawnings;
|
||||
using ProjectRoadTrain.Entities;
|
||||
using ProjectRoadTrain.MovementStrategy;
|
||||
using ProjectSportCar.MovementStrategy;
|
||||
|
||||
namespace ProjectRoadTrain;
|
||||
|
||||
public partial class FormRoadTrain : Form
|
||||
{
|
||||
private DrawningRoadTrain? _drawningRoadTrain;
|
||||
private DrawningTrain? _drawningTrain;
|
||||
private AbstractStrategy? _strategy;
|
||||
public FormRoadTrain()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
|
||||
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningRoadTrain == null)
|
||||
if (_drawningTrain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new (pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
|
||||
Bitmap bmp = new(pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningRoadTrain.DrawTransport(gr);
|
||||
_drawningTrain.DrawTransport(gr);
|
||||
pictureBoxRoadTrain.Image = bmp;
|
||||
}
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
case nameof(DrawningTrain):
|
||||
_drawningTrain = new DrawningTrain(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(DrawningRoadTrain):
|
||||
_drawningTrain = new DrawningRoadTrain(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;
|
||||
}
|
||||
|
||||
_drawningTrain.SetPictureSize(pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
|
||||
_drawningTrain.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningRoadTrain = new DrawningRoadTrain();
|
||||
_drawningRoadTrain.Init(random.Next(100, 300), random.Next(1200, 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)));
|
||||
_drawningRoadTrain.SetPictureSize(pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
|
||||
_drawningRoadTrain.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
CreateObject(nameof(DrawningRoadTrain));
|
||||
|
||||
}
|
||||
private void buttonCreateTrain_Click(object sender, EventArgs e)
|
||||
{
|
||||
CreateObject(nameof(DrawningTrain));
|
||||
}
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningRoadTrain == null)
|
||||
if (_drawningTrain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -46,16 +76,16 @@ public partial class FormRoadTrain : Form
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningRoadTrain.MoveTransport(DirectionType.Up);
|
||||
result = _drawningTrain.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningRoadTrain.MoveTransport(DirectionType.Down);
|
||||
result = _drawningTrain.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningRoadTrain.MoveTransport(DirectionType.Left);
|
||||
result = _drawningTrain.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningRoadTrain.MoveTransport(DirectionType.Right);
|
||||
result = _drawningTrain.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -64,4 +94,43 @@ public partial class FormRoadTrain : Form
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningTrain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableTrans(_drawningTrain), pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height);
|
||||
}
|
||||
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
comboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,127 @@
|
||||
|
||||
using ProjectRoadTrain.MovementStrategy;
|
||||
|
||||
namespace ProjectElectroTrans.MovementStrategy;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObject? _moveableObject;
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set; }
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
/// <summary>
|
||||
/// Установка данных
|
||||
/// </summary>
|
||||
/// <param name="moveableObject">Перемещаемый объект</param>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters =>
|
||||
_moveableObject?.GetObjectPosition;
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
/// <summary>
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// </summary>
|
||||
/// <param name="movementDirection">Направление</param>
|
||||
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using ProjectRoadTrain.MovementStrategy;
|
||||
namespace ProjectElectroTrans.MovementStrategy;
|
||||
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
/// <summary>
|
||||
/// Попытка переместить объект в указанном направлении
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
||||
|
@ -0,0 +1,54 @@
|
||||
using ProjectElectroTrans.MovementStrategy;
|
||||
|
||||
namespace ProjectSportCar.MovementStrategy;
|
||||
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
|
||||
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder - GetStep() <= FieldWidth
|
||||
&& objParams.RightBorder + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder - GetStep() <= FieldHeight
|
||||
&& objParams.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
|
||||
namespace ProjectElectroTrans.MovementStrategy;
|
||||
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
|
||||
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
using ProjectRoadTrain.Drawnings;
|
||||
using ProjectRoadTrain.Drawnings;
|
||||
using ProjectRoadTrain.MovementStrategy;
|
||||
|
||||
|
||||
namespace ProjectElectroTrans.MovementStrategy;
|
||||
public class MoveableTrans : IMoveableObject
|
||||
{
|
||||
private readonly DrawningTrain? _train = null;
|
||||
public MoveableTrans(DrawningTrain train)
|
||||
{
|
||||
_train = train;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_train == null || _train.EntityTrain == null ||
|
||||
!_train.GetPosX.HasValue || !_train.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_train.GetPosX.Value,
|
||||
_train.GetPosY.Value, _train.GetWidth, _train.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_train?.EntityTrain?.Step ?? 0);
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_train == null || _train.EntityTrain == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _train.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
/// <summary>
|
||||
/// Конвертация из MovementDirection в DirectionType
|
||||
/// </summary>
|
||||
/// <param name="direction">MovementDirection</param>
|
||||
/// <returns>DirectionType</returns>
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
namespace ProjectRoadTrain.MovementStrategy;
|
||||
|
||||
public enum MovementDirection
|
||||
{
|
||||
Up = 1, Down = 2, Left = 3, Right = 4
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
namespace ProjectElectroTrans.MovementStrategy;
|
||||
|
||||
public class ObjectParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// Координата X
|
||||
/// </summary>
|
||||
private readonly int _x;
|
||||
/// <summary>
|
||||
/// Координата Y
|
||||
/// </summary>
|
||||
private readonly int _y;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
private readonly int _width;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
private readonly int _height;
|
||||
/// <summary>
|
||||
/// Левая граница
|
||||
/// </summary>
|
||||
public int LeftBorder => _x;
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
public int TopBorder => _y;
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
public int RightBorder => _x + _width;
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
public int DownBorder => _y + _height;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
/// <param name="width">Ширина объекта</param>
|
||||
/// <param name="height">Высота объекта</param>
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRoadTrain.MovementStrategy;
|
||||
|
||||
public enum StrategyStatus
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Имя элемента проекта не соответствует указанному в задании