ISEbd-22. Baygulov A.A. Lab work 02 #3
78
ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs
Normal file
78
ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ElectricLocomotive;
|
||||
using ProjectElectricLocomotive.DrawingObjects;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
private IMoveableObject? _moveableObject;
|
||||
|
||||
private Status _state = Status.NotInit;
|
||||
protected int FieldWidth { get; private set; }
|
||||
protected int FieldHeight { get; private set; }
|
||||
public Status GetStatus() { return _state; }
|
||||
/// <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 = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = Status.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
protected bool MoveLeft() => MoveTo(Direction.Left);
|
||||
protected bool MoveRight() => MoveTo(Direction.Right);
|
||||
protected bool MoveUp() => MoveTo(Direction.Up);
|
||||
protected bool MoveDown() => MoveTo(Direction.Down);
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
/// <param name="directionType">Направление</param>
|
||||
private bool MoveTo(Direction directionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject?.CheckCanMove(directionType) ?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,12 +3,12 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectElectricLocomotive.DrawningObjects;
|
||||
using ElectricLocomotive;
|
||||
using ProjectElectricLocomotive.Entities;
|
||||
|
||||
namespace ProjectElectricLocomotive.DrawingObjects
|
||||
{
|
||||
public class DrawingElectricLocomotive : DrawningLocomotive
|
||||
public class DrawingElectricLocomotive : DrawingLocomotive
|
||||
{
|
||||
public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
bool pantograph, bool compartment, int width, int height) : base(speed, weight, bodyColor, width, height, 80, 52)
|
||||
@ -28,29 +28,22 @@ namespace ProjectElectricLocomotive.DrawingObjects
|
||||
Pen pen = new(Color.Black);
|
||||
Brush blackBrush = new SolidBrush(Color.Black);
|
||||
Brush windows = new SolidBrush(Color.LightBlue);
|
||||
Brush bodyColor = new SolidBrush(electricLocomotive.BodyColor);
|
||||
Brush additionalBrush = new SolidBrush(electricLocomotive.AdditionalColor);
|
||||
|
||||
if (electricLocomotive.Pantograph)
|
||||
{
|
||||
g.FillRectangle(blackBrush, _startPosX + 30, _startPosY + 15, 20, 5);
|
||||
g.DrawLine(pen, _startPosX + 30, _startPosY + 15, _startPosX + 50, _startPosY + 2);
|
||||
g.DrawLine(pen, _startPosX + 40, _startPosY + 15, _startPosX + 60, _startPosY + 2);
|
||||
}
|
||||
|
||||
if (electricLocomotive.Compartment)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX + 40, _startPosY + 24, 25, 11);
|
||||
g.FillPolygon(additionalBrush, new Point[]
|
||||
{
|
||||
Brush bodyColor = new SolidBrush(electricLocomotive.BodyColor);
|
||||
g.DrawRectangle(pen, _startPosX + 40, _startPosY + 24, 25, 11);
|
||||
g.FillPolygon(additionalBrush, new Point[]
|
||||
{
|
||||
new Point(_startPosX + 41, _startPosY + 25),
|
||||
new Point(_startPosX + 65, _startPosY + 25),
|
||||
new Point(_startPosX + 65, _startPosY + 35),
|
||||
new Point(_startPosX + 41, _startPosY + 35),
|
||||
new Point(_startPosX + 41, _startPosY + 25),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
g.FillRectangle(blackBrush, _startPosX + 30, _startPosY + 15, 20, 5);
|
||||
g.DrawLine(pen, _startPosX + 30, _startPosY + 15, _startPosX + 50, _startPosY + 2);
|
||||
g.DrawLine(pen, _startPosX + 40, _startPosY + 15, _startPosX + 60, _startPosY + 2);
|
||||
base.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ using ProjectElectricLocomotive.Entities;
|
||||
using ProjectElectricLocomotive.Properties;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace ProjectElectricLocomotive.DrawningObjects
|
||||
namespace ProjectElectricLocomotive.DrawingObjects
|
||||
{
|
||||
public class DrawningLocomotive
|
||||
public class DrawingLocomotive
|
||||
{
|
||||
public EntityLocomotive? EntityLocomotive { get; protected set; }
|
||||
|
||||
@ -20,13 +20,17 @@ namespace ProjectElectricLocomotive.DrawningObjects
|
||||
protected int _startPosY;
|
||||
protected readonly int _locomWidth = 80;
|
||||
protected readonly int _locomHeight = 52;
|
||||
public int GetPosX => _startPosX;
|
||||
public int GetPosY => _startPosY;
|
||||
public int GetWidth => _locomWidth;
|
||||
public int GetHeight => _locomHeight;
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Цвет кузова</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="heigth">Высота картинки</param>
|
||||
|
||||
public DrawningLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
|
||||
public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
|
||||
{
|
||||
if (width < _locomWidth || heigth < _locomHeight)
|
||||
{
|
||||
@ -43,7 +47,7 @@ namespace ProjectElectricLocomotive.DrawningObjects
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <param name="locomWidth">Ширина картинки</param>
|
||||
/// <param name="locomHeight">Высота картинки</param>
|
||||
protected DrawningLocomotive(int speed, double weight, Color bodyColor, int width,
|
||||
protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width,
|
||||
int height, int locomWidth, int locomHeight)
|
||||
{
|
||||
if (width < _locomWidth || height < _locomHeight)
|
||||
@ -157,5 +161,24 @@ namespace ProjectElectricLocomotive.DrawningObjects
|
||||
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 10, 10);
|
||||
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 10, 10);
|
||||
}
|
||||
/// <param name="direction">Направление</param>
|
||||
public bool CanMove(Direction direction)
|
||||
{
|
||||
if (EntityLocomotive == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
Direction.Left => _startPosX - EntityLocomotive.Step > 0,
|
||||
//вверх
|
||||
Direction.Up => _startPosY - EntityLocomotive.Step > 0,
|
||||
// вправо
|
||||
Direction.Right => _startPosX + EntityLocomotive.Step < _pictureWidth,
|
||||
//вниз
|
||||
Direction.Down => _startPosY + EntityLocomotive.Step < _pictureHeight,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
using ProjectElectricLocomotive.DrawingObjects;
|
||||
using ElectricLocomotive;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public class DrawingObjectLocomotive : IMoveableObject
|
||||
{
|
||||
private readonly DrawingLocomotive? _drawningLocomotive = null;
|
||||
|
||||
public DrawingObjectLocomotive(DrawingLocomotive drawningLocomotive)
|
||||
{
|
||||
_drawningLocomotive = drawningLocomotive;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawningLocomotive == null || _drawningLocomotive.EntityLocomotive == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningLocomotive.GetPosX,
|
||||
_drawningLocomotive.GetPosY, _drawningLocomotive.GetWidth, _drawningLocomotive.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawningLocomotive?.EntityLocomotive?.Step ?? 0);
|
||||
public bool CheckCanMove(Direction direction) => _drawningLocomotive?.CanMove(direction) ?? false;
|
||||
public void MoveObject(Direction direction) => _drawningLocomotive?.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
namespace ElectricLocomotive
|
||||
using System;
|
||||
|
||||
namespace ElectricLocomotive
|
||||
{
|
||||
partial class FormElectricLocomotive
|
||||
partial class ElectricLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
@ -29,11 +31,14 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBoxElectricLocomotive = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateElectricLocomotive = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonDown = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonCreateLocomotive = new Button();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxElectricLocomotive).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -44,81 +49,132 @@
|
||||
pictureBoxElectricLocomotive.Enabled = false;
|
||||
pictureBoxElectricLocomotive.Location = new Point(0, 0);
|
||||
pictureBoxElectricLocomotive.Name = "pictureBoxElectricLocomotive";
|
||||
pictureBoxElectricLocomotive.Size = new Size(1052, 553);
|
||||
pictureBoxElectricLocomotive.Size = new Size(847, 441);
|
||||
pictureBoxElectricLocomotive.SizeMode = PictureBoxSizeMode.AutoSize;
|
||||
pictureBoxElectricLocomotive.TabIndex = 0;
|
||||
pictureBoxElectricLocomotive.TabStop = false;
|
||||
pictureBoxElectricLocomotive.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateElectricLocomotive
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(0, 490);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(172, 63);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreateElectricLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateElectricLocomotive.Font = new Font("Calibri", 9.75F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
buttonCreateElectricLocomotive.Location = new Point(11, 365);
|
||||
buttonCreateElectricLocomotive.Margin = new Padding(2);
|
||||
buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive";
|
||||
buttonCreateElectricLocomotive.Size = new Size(134, 64);
|
||||
buttonCreateElectricLocomotive.TabIndex = 1;
|
||||
buttonCreateElectricLocomotive.Text = "Создать электролокомотив";
|
||||
buttonCreateElectricLocomotive.UseVisualStyleBackColor = true;
|
||||
buttonCreateElectricLocomotive.Click += buttonCreateElectricLocomotive_Click;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonLeft.BackgroundImage = Properties.Resources.free_icon_left_arrow_line_symbol_54321;
|
||||
buttonLeft.BackgroundImage = ProjectElectricLocomotive.Properties.Resources.free_icon_left_arrow_line_symbol_54321;
|
||||
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonLeft.Location = new Point(938, 511);
|
||||
buttonLeft.Location = new Point(733, 399);
|
||||
buttonLeft.Margin = new Padding(2);
|
||||
buttonLeft.Name = "buttonLeft";
|
||||
buttonLeft.Size = new Size(30, 30);
|
||||
buttonLeft.TabIndex = 2;
|
||||
buttonLeft.UseVisualStyleBackColor = true;
|
||||
buttonLeft.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonRight
|
||||
//
|
||||
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonRight.BackgroundImage = Properties.Resources.free_icon_right_arrow_angle_54833;
|
||||
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonRight.Location = new Point(1010, 511);
|
||||
buttonRight.Name = "buttonRight";
|
||||
buttonRight.Size = new Size(30, 30);
|
||||
buttonRight.TabIndex = 3;
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonUp
|
||||
//
|
||||
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonUp.BackgroundImage = Properties.Resources.free_icon_up_arrow_angle_54817;
|
||||
buttonUp.BackgroundImage = ProjectElectricLocomotive.Properties.Resources.free_icon_up_arrow_angle_54817;
|
||||
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonUp.Location = new Point(974, 475);
|
||||
buttonUp.Location = new Point(767, 365);
|
||||
buttonUp.Margin = new Padding(2);
|
||||
buttonUp.Name = "buttonUp";
|
||||
buttonUp.Size = new Size(30, 30);
|
||||
buttonUp.TabIndex = 4;
|
||||
buttonUp.TabIndex = 3;
|
||||
buttonUp.UseVisualStyleBackColor = true;
|
||||
buttonUp.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonRight
|
||||
//
|
||||
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonRight.BackgroundImage = ProjectElectricLocomotive.Properties.Resources.free_icon_right_arrow_angle_54833;
|
||||
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonRight.Location = new Point(802, 399);
|
||||
buttonRight.Margin = new Padding(2);
|
||||
buttonRight.Name = "buttonRight";
|
||||
buttonRight.Size = new Size(30, 30);
|
||||
buttonRight.TabIndex = 4;
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonDown
|
||||
//
|
||||
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonDown.BackgroundImage = Properties.Resources.free_icon_down_arrow_54785;
|
||||
buttonDown.BackgroundImage = ProjectElectricLocomotive.Properties.Resources.free_icon_down_arrow_54785;
|
||||
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
buttonDown.Location = new Point(974, 511);
|
||||
buttonDown.Location = new Point(767, 399);
|
||||
buttonDown.Margin = new Padding(2);
|
||||
buttonDown.Name = "buttonDown";
|
||||
buttonDown.Size = new Size(30, 30);
|
||||
buttonDown.TabIndex = 5;
|
||||
buttonDown.UseVisualStyleBackColor = true;
|
||||
buttonDown.Click += buttonMove_Click;
|
||||
//
|
||||
// FormElectricLocomotive
|
||||
// comboBoxStrategy
|
||||
//
|
||||
ClientSize = new Size(1052, 553);
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToRightEdge" });
|
||||
comboBoxStrategy.Location = new Point(681, 7);
|
||||
comboBoxStrategy.Margin = new Padding(2);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(151, 23);
|
||||
comboBoxStrategy.TabIndex = 6;
|
||||
comboBoxStrategy.SelectedIndexChanged += comboBoxStrategy_SelectedIndexChanged;
|
||||
//
|
||||
// buttonCreateLocomotive
|
||||
//
|
||||
buttonCreateLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateLocomotive.Font = new Font("Calibri", 9.75F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
buttonCreateLocomotive.Location = new Point(149, 365);
|
||||
buttonCreateLocomotive.Margin = new Padding(2);
|
||||
buttonCreateLocomotive.Name = "buttonCreateLocomotive";
|
||||
buttonCreateLocomotive.Size = new Size(102, 65);
|
||||
buttonCreateLocomotive.TabIndex = 7;
|
||||
buttonCreateLocomotive.Text = "Создать локомотив";
|
||||
buttonCreateLocomotive.UseVisualStyleBackColor = true;
|
||||
buttonCreateLocomotive.Click += buttonCreateLocomotive_Click;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStep.Font = new Font("Calibri", 9.75F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
buttonStep.Location = new Point(744, 34);
|
||||
buttonStep.Margin = new Padding(2);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(88, 34);
|
||||
buttonStep.TabIndex = 8;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// ElectricLocomotive
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(847, 441);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(buttonCreateLocomotive);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateElectricLocomotive);
|
||||
Controls.Add(pictureBoxElectricLocomotive);
|
||||
Name = "FormElectricLocomotive";
|
||||
Margin = new Padding(2);
|
||||
Name = "ElectricLocomotive";
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
Text = "ElectricLocomotive";
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxElectricLocomotive).EndInit();
|
||||
@ -129,10 +185,13 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxElectricLocomotive;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateElectricLocomotive;
|
||||
private Button buttonLeft;
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonRight;
|
||||
private Button buttonDown;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonCreateLocomotive;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,44 +1,61 @@
|
||||
|
||||
using ProjectElectricLocomotive;
|
||||
using ProjectElectricLocomotive.DrawingObjects;
|
||||
using ProjectElectricLocomotive.MovementStrategy;
|
||||
using System;
|
||||
|
||||
namespace ElectricLocomotive
|
||||
|
||||
{
|
||||
public partial class FormElectricLocomotive : Form
|
||||
public partial class ElectricLocomotive : Form
|
||||
{
|
||||
private DrawningElectricLocomotive? _drawningElectricLocomotive;
|
||||
public FormElectricLocomotive()
|
||||
|
||||
private DrawingLocomotive? _drawningLocomotive;
|
||||
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
|
||||
public ElectricLocomotive()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxElectricLocomotive.Width,
|
||||
pictureBoxElectricLocomotive.Height);
|
||||
Bitmap bmp = new(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningElectricLocomotive.DrawTransport(gr);
|
||||
_drawningLocomotive.DrawTransport(gr);
|
||||
pictureBoxElectricLocomotive.Image = bmp;
|
||||
}
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
|
||||
private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningElectricLocomotive = new DrawningElectricLocomotive();
|
||||
_drawningElectricLocomotive.Init(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Random random = new Random();
|
||||
_drawningLocomotive = new DrawingElectricLocomotive(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)), Convert.ToBoolean(random.Next(0, 2)),
|
||||
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
|
||||
_drawningElectricLocomotive.SetPosition(random.Next(1, 100), random.Next(1, 100));
|
||||
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
|
||||
private void buttonCreateLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
_drawningLocomotive = new DrawingLocomotive(rnd.Next(100, 300), rnd.Next(1000, 3000),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
|
||||
_drawningLocomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -46,19 +63,65 @@ namespace ElectricLocomotive
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
_drawningElectricLocomotive.MoveTransport(Direction.Up);
|
||||
_drawningLocomotive.MoveTransport(Direction.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawningElectricLocomotive.MoveTransport(Direction.Down);
|
||||
_drawningLocomotive.MoveTransport(Direction.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawningElectricLocomotive.MoveTransport(Direction.Left);
|
||||
_drawningLocomotive.MoveTransport(Direction.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawningElectricLocomotive.MoveTransport(Direction.Right);
|
||||
_drawningLocomotive.MoveTransport(Direction.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToRightEdge(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawingObjectLocomotive(_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;
|
||||
}
|
||||
}
|
||||
|
||||
private void comboBoxStrategy_SelectedIndexChanged(object sender, EventArgs e)
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void pictureBoxElectricLocomotive_Click(object sender, EventArgs e)
|
||||
eegov
commented
Пустых методов быть не должно Пустых методов быть не должно
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,64 @@
|
||||
<root>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
|
22
ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs
Normal file
22
ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ElectricLocomotive;
|
||||
using ProjectElectricLocomotive.DrawingObjects;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
int GetStep { get; }
|
||||
/// <param name="direction"></param>
|
||||
bool CheckCanMove(Direction direction);
|
||||
/// <param name="direction">Направление</param>
|
||||
void MoveObject(Direction direction);
|
||||
|
||||
}
|
||||
}
|
56
ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs
Normal file
56
ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
28
ElectricLocomotive/ElectricLocomotive/MoveToRightEdge.cs
Normal file
28
ElectricLocomotive/ElectricLocomotive/MoveToRightEdge.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public class MoveToRightEdge : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null) return false;
|
||||
|
||||
return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep();
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null) return;
|
||||
if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
54
ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs
Normal file
54
ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public class ObjectParameters
|
||||
{
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ namespace ElectricLocomotive
|
||||
// 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 ElectricLocomotive());
|
||||
}
|
||||
}
|
||||
}
|
16
ElectricLocomotive/ElectricLocomotive/Status.cs
Normal file
16
ElectricLocomotive/ElectricLocomotive/Status.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Пустых методов быть не должно