Laba02_new_branch
This commit is contained in:
parent
b9ea3213ee
commit
8a64878aad
87
ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs
Normal file
87
ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ElectricLocomotive;
|
||||
using ElectricLocomotive.MovementStrategy;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Абстрактный класс стратегии
|
||||
/// </summary>
|
||||
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; }
|
||||
|
||||
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(DirectionType.Left);
|
||||
|
||||
protected bool MoveRight() => MoveTo(DirectionType.Right);
|
||||
|
||||
protected bool MoveUp() => MoveTo(DirectionType.Up);
|
||||
|
||||
protected bool MoveDown() => MoveTo(DirectionType.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();
|
||||
|
||||
private bool MoveTo(DirectionType directionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject?.CheckCanMove(directionType) ?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,11 +9,8 @@ namespace ElectricLocomotive
|
||||
public enum DirectionType
|
||||
{
|
||||
Up = 1,
|
||||
|
||||
Down = 2,
|
||||
|
||||
Left = 3,
|
||||
|
||||
Right = 4
|
||||
}
|
||||
}
|
||||
|
@ -1,174 +1,35 @@
|
||||
using System;
|
||||
using ElectricLocomotive.DrawningObject;
|
||||
using ElectricLocomotive.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms.VisualStyles;
|
||||
|
||||
namespace ElectricLocomotive
|
||||
{
|
||||
public class DrawningElectricLocomotive
|
||||
public class DrawningElectricLocomotive : DrawningLocomotive
|
||||
{
|
||||
public EntityElectricLocomotive? _ElectricLocomotive { get; private set; }
|
||||
|
||||
private int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
|
||||
private int _startPosX;
|
||||
|
||||
private int _startPosY;
|
||||
|
||||
private readonly int _LocomotiveWidth = 100;
|
||||
|
||||
private readonly int _LocomoriveHeight =50;
|
||||
|
||||
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns, int width, int height)
|
||||
public DrawningElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, bool Horns, int width, int height)
|
||||
: base (speed, weight, bodyColor, additionalColor, width , height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if ((_pictureHeight < _LocomoriveHeight) || (_pictureWidth < _LocomotiveWidth))
|
||||
if (EntityLocomotive != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_ElectricLocomotive = new EntityElectricLocomotive();
|
||||
_ElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x + _LocomotiveWidth > _pictureWidth || y + _LocomoriveHeight > _pictureHeight)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
else
|
||||
{
|
||||
_startPosX = x;
|
||||
|
||||
_startPosY = y;
|
||||
EntityLocomotive = new EntityElectroLocomotive(speed, weight, bodyColor,
|
||||
additionalColor, Horns);
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction)
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (_ElectricLocomotive == null)
|
||||
if (EntityLocomotive is not EntityElectroLocomotive electroLocomotive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - _ElectricLocomotive.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)_ElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - _ElectricLocomotive.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)_ElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
|
||||
// вправо
|
||||
case DirectionType.Right: // TODO: Продумать логику
|
||||
if (_startPosX+ _LocomotiveWidth + (int)_ElectricLocomotive.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)_ElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
|
||||
//вниз
|
||||
case DirectionType.Down: // TODO: Продумать логику
|
||||
if (_startPosY + _LocomoriveHeight + (int)_ElectricLocomotive.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)_ElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (_ElectricLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(_ElectricLocomotive.AdditionalColor);
|
||||
Brush brush = new SolidBrush(_ElectricLocomotive.BodyColor);
|
||||
|
||||
///ВЛ60к-1595
|
||||
|
||||
///ходовая
|
||||
g.FillRectangle(brush, new Rectangle(_startPosX + 20, _startPosY + 45, 15, 5));
|
||||
g.FillRectangle(brush, new Rectangle(_startPosX + 65, _startPosY + 45, 15, 5));
|
||||
|
||||
g.FillEllipse(brush, _startPosX + 15, _startPosY + 45, 10, 10);
|
||||
g.FillEllipse(brush, _startPosX + 30, _startPosY + 45, 10, 10);
|
||||
g.FillEllipse(brush, _startPosX + 60, _startPosY + 45, 10, 10);
|
||||
g.FillEllipse(brush, _startPosX + 75, _startPosY + 45, 10, 10);
|
||||
PointF[] chassis =
|
||||
{
|
||||
new Point(_startPosX+10, _startPosY+50),
|
||||
new Point(_startPosX, _startPosY+50),
|
||||
new Point(_startPosX+10, _startPosY +40),
|
||||
new Point(_startPosX + 90, _startPosY+40),
|
||||
new Point(_startPosX+100, _startPosY+50),
|
||||
new Point(_startPosX+90,_startPosY+ 50),
|
||||
new Point(_startPosX+85, _startPosY+45),
|
||||
new Point(_startPosX+15, _startPosY+45),
|
||||
};
|
||||
g.FillPolygon(brush, chassis);
|
||||
|
||||
///обводка ходовой
|
||||
Point[] chassisCont =
|
||||
{
|
||||
new Point(_startPosX+10, _startPosY+50),
|
||||
new Point(_startPosX, _startPosY+50),
|
||||
new Point(_startPosX+10, _startPosY +40),
|
||||
new Point(_startPosX + 90, _startPosY+40),
|
||||
new Point(_startPosX+100, _startPosY+50),
|
||||
new Point(_startPosX+90,_startPosY+ 50),
|
||||
new Point(_startPosX+85, _startPosY+45),
|
||||
new Point(_startPosX+15, _startPosY+45),
|
||||
};
|
||||
g.DrawPolygon(pen, chassisCont);
|
||||
|
||||
///кабина
|
||||
Point[] cabin =
|
||||
{
|
||||
new Point(_startPosX+20, _startPosY+10),
|
||||
new Point(_startPosX+90,_startPosY+10),
|
||||
new Point(_startPosX+90,_startPosY + 40),
|
||||
new Point(_startPosX+10,_startPosY+40),
|
||||
new Point(_startPosX+10,_startPosY+25)
|
||||
};
|
||||
g.DrawPolygon(pen, cabin);
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 25, _startPosX + 90, _startPosY + 25);
|
||||
|
||||
///окна
|
||||
g.FillRectangle(brush, _startPosX + 20, _startPosY + 15, 10, 10);
|
||||
g.FillRectangle(brush, _startPosX + 35, _startPosY + 15, 10, 10);
|
||||
|
||||
g.FillRectangle(brush, _startPosX + 75, _startPosY + 15, 10, 10);
|
||||
|
||||
///дверь
|
||||
g.FillRectangle(brush, _startPosX + 50, _startPosY + 15, 10, 25);
|
||||
|
||||
///рога
|
||||
if (_ElectricLocomotive.Horns == true)
|
||||
Pen pen = new(electroLocomotive.BodyColor) ;
|
||||
Brush brush = new SolidBrush(electroLocomotive.AdditionalColor);
|
||||
// обвесы
|
||||
if (electroLocomotive.Horns)
|
||||
{
|
||||
g.FillRectangle(brush, _startPosX + 25, _startPosY + 5, 15, 5);
|
||||
|
||||
@ -197,9 +58,8 @@ namespace ElectricLocomotive
|
||||
};
|
||||
g.DrawPolygon(pen, horns2);
|
||||
}
|
||||
///Батарея
|
||||
g.FillRectangle(brush, _startPosX + 45, _startPosY + 5, 15, 5);
|
||||
|
||||
base.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
210
ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs
Normal file
210
ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs
Normal file
@ -0,0 +1,210 @@
|
||||
using ElectricLocomotive.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive.DrawningObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningLocomotive
|
||||
{
|
||||
public EntityLocomotive? EntityLocomotive { get; protected set; }
|
||||
|
||||
protected int _pictureWidth;
|
||||
|
||||
protected int _pictureHeight;
|
||||
|
||||
protected int _startPosX;
|
||||
|
||||
protected int _startPosY;
|
||||
|
||||
protected readonly int _locoWidth = 95;
|
||||
|
||||
protected readonly int _locoHeight = 50;
|
||||
public int GetPosX => _startPosX;
|
||||
public int GetPosY => _startPosY;
|
||||
public int GetWidth => _locoWidth;
|
||||
public int GetHeight => _locoHeight;
|
||||
|
||||
public DrawningLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, int width, int heigth)
|
||||
{
|
||||
if (width < _locoWidth || heigth < _locoHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = heigth;
|
||||
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor, additionalColor);
|
||||
}
|
||||
|
||||
protected DrawningLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, int width,
|
||||
int height, int locoWidth, int locoHeight)
|
||||
{
|
||||
if (width < _locoWidth || height < _locoHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_locoWidth = locoWidth;
|
||||
_locoHeight = locoHeight;
|
||||
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor, additionalColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0 || x + _locoWidth > _pictureWidth)
|
||||
{
|
||||
x = _pictureWidth - _locoWidth;
|
||||
}
|
||||
if (y < 0 || y + _locoHeight > _pictureHeight)
|
||||
{
|
||||
y = _pictureHeight - _locoHeight;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityLocomotive.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityLocomotive.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + EntityLocomotive.Step + _locoWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + EntityLocomotive.Step + _locoHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(EntityLocomotive.BodyColor);
|
||||
Brush brush = new SolidBrush(EntityLocomotive.AdditionalColor);
|
||||
///ВЛ60к-1595
|
||||
|
||||
///ходовая
|
||||
g.FillRectangle(brush, new Rectangle(_startPosX + 20, _startPosY + 45, 15, 5));
|
||||
g.FillRectangle(brush, new Rectangle(_startPosX + 65, _startPosY + 45, 15, 5));
|
||||
|
||||
g.FillEllipse(brush, _startPosX + 15, _startPosY + 45, 10, 10);
|
||||
g.FillEllipse(brush, _startPosX + 30, _startPosY + 45, 10, 10);
|
||||
g.FillEllipse(brush, _startPosX + 60, _startPosY + 45, 10, 10);
|
||||
g.FillEllipse(brush, _startPosX + 75, _startPosY + 45, 10, 10);
|
||||
PointF[] chassis =
|
||||
{
|
||||
new Point(_startPosX+10, _startPosY+50),
|
||||
new Point(_startPosX, _startPosY+50),
|
||||
new Point(_startPosX+10, _startPosY +40),
|
||||
new Point(_startPosX + 90, _startPosY+40),
|
||||
new Point(_startPosX+100, _startPosY+50),
|
||||
new Point(_startPosX+90,_startPosY+ 50),
|
||||
new Point(_startPosX+85, _startPosY+45),
|
||||
new Point(_startPosX+15, _startPosY+45),
|
||||
};
|
||||
g.FillPolygon(brush, chassis);
|
||||
|
||||
///обводка ходовой
|
||||
Point[] chassisCont =
|
||||
{
|
||||
new Point(_startPosX+10, _startPosY+50),
|
||||
new Point(_startPosX, _startPosY+50),
|
||||
new Point(_startPosX+10, _startPosY +40),
|
||||
new Point(_startPosX + 90, _startPosY+40),
|
||||
new Point(_startPosX+100, _startPosY+50),
|
||||
new Point(_startPosX+90,_startPosY+ 50),
|
||||
new Point(_startPosX+85, _startPosY+45),
|
||||
new Point(_startPosX+15, _startPosY+45),
|
||||
};
|
||||
g.DrawPolygon(pen, chassisCont);
|
||||
|
||||
///кабина
|
||||
Point[] cabin =
|
||||
{
|
||||
new Point(_startPosX+20, _startPosY+10),
|
||||
new Point(_startPosX+90,_startPosY+10),
|
||||
new Point(_startPosX+90,_startPosY + 40),
|
||||
new Point(_startPosX+10,_startPosY+40),
|
||||
new Point(_startPosX+10,_startPosY+25)
|
||||
};
|
||||
g.DrawPolygon(pen, cabin);
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 25, _startPosX + 90, _startPosY + 25);
|
||||
|
||||
///окна
|
||||
g.FillRectangle(brush, _startPosX + 20, _startPosY + 15, 10, 10);
|
||||
g.FillRectangle(brush, _startPosX + 35, _startPosY + 15, 10, 10);
|
||||
|
||||
g.FillRectangle(brush, _startPosX + 75, _startPosY + 15, 10, 10);
|
||||
|
||||
///дверь
|
||||
g.FillRectangle(brush, _startPosX + 50, _startPosY + 15, 10, 25);
|
||||
|
||||
///Батарея
|
||||
g.FillRectangle(brush, _startPosX + 45, _startPosY + 5, 15, 5);
|
||||
|
||||
}
|
||||
|
||||
public bool CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityLocomotive == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
DirectionType.Left => _startPosX - EntityLocomotive.Step > 0,
|
||||
//вверх
|
||||
DirectionType.Up => _startPosY - EntityLocomotive.Step > 0,
|
||||
// вправо
|
||||
DirectionType.Right => _startPosX + EntityLocomotive.Step < _pictureWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + EntityLocomotive.Step < _pictureHeight,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
using ElectricLocomotive.DrawningObject;
|
||||
using ElectricLocomotive.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive
|
||||
{
|
||||
public class DrawningObjectLocomotive : IMoveableObject
|
||||
{
|
||||
private readonly DrawningLocomotive? _drawningLocomotive = null;
|
||||
public DrawningObjectLocomotive(DrawningLocomotive 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(DirectionType direction) =>_drawningLocomotive?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionType direction) =>_drawningLocomotive?.MoveTransport(direction);
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,10 @@
|
||||
buttonRight = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateElectricLocomotive = new Button();
|
||||
buttonCreateLocomotive = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxElectroLocomotiv).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -48,6 +51,7 @@
|
||||
pictureBoxElectroLocomotiv.SizeMode = PictureBoxSizeMode.AutoSize;
|
||||
pictureBoxElectroLocomotiv.TabIndex = 5;
|
||||
pictureBoxElectroLocomotiv.TabStop = false;
|
||||
|
||||
//
|
||||
// buttonUp
|
||||
//
|
||||
@ -97,27 +101,66 @@
|
||||
buttonLeft.UseVisualStyleBackColor = true;
|
||||
buttonLeft.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateElectricLocomotive
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(13, 425);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(108, 34);
|
||||
buttonCreate.TabIndex = 6;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreateElectricLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateElectricLocomotive.Location = new Point(13, 425);
|
||||
buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive";
|
||||
buttonCreateElectricLocomotive.Size = new Size(208, 34);
|
||||
buttonCreateElectricLocomotive.TabIndex = 6;
|
||||
buttonCreateElectricLocomotive.Text = "Создать Электролокомотив";
|
||||
buttonCreateElectricLocomotive.UseVisualStyleBackColor = true;
|
||||
buttonCreateElectricLocomotive.Click += buttonCreateElectricLocomotive_Click;
|
||||
//
|
||||
// buttonCreateLocomotive
|
||||
//
|
||||
buttonCreateLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateLocomotive.Location = new Point(227, 425);
|
||||
buttonCreateLocomotive.Name = "buttonCreateLocomotive";
|
||||
buttonCreateLocomotive.Size = new Size(197, 34);
|
||||
buttonCreateLocomotive.TabIndex = 11;
|
||||
buttonCreateLocomotive.Text = "Создать локомотив";
|
||||
buttonCreateLocomotive.UseVisualStyleBackColor = true;
|
||||
buttonCreateLocomotive.Click += buttonCreateLocomotive_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToRightCorner" });
|
||||
comboBoxStrategy.Location = new Point(730, 11);
|
||||
comboBoxStrategy.Margin = new Padding(2);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(113, 23);
|
||||
comboBoxStrategy.TabIndex = 12;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStep.Font = new Font("Candara Light", 10F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
buttonStep.Location = new Point(741, 49);
|
||||
buttonStep.Margin = new Padding(2);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(88, 34);
|
||||
buttonStep.TabIndex = 13;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// ElectricLocomotive
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(854, 467);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateLocomotive);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateElectricLocomotive);
|
||||
Controls.Add(pictureBoxElectroLocomotiv);
|
||||
Name = "ElectricLocomotive";
|
||||
Text = "ElectricLocomotive";
|
||||
@ -133,6 +176,9 @@
|
||||
private Button buttonRight;
|
||||
private Button buttonDown;
|
||||
private Button buttonLeft;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateElectricLocomotive;
|
||||
private Button buttonCreateLocomotive;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,8 +1,18 @@
|
||||
using ElectricLocomotive.DrawningObject;
|
||||
using ElectricLocomotive.MovementStrategy;
|
||||
using ProjectElectricLocomotive;
|
||||
using ProjectElectricLocomotive.MovementStrategy;
|
||||
using System;
|
||||
|
||||
namespace ElectricLocomotive
|
||||
{
|
||||
public partial class ElectricLocomotive : Form
|
||||
{
|
||||
private DrawningElectricLocomotive? _drawningElectricLocomotive;
|
||||
|
||||
private DrawningLocomotive? _drawningLocomotive;
|
||||
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
|
||||
public ElectricLocomotive()
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -10,56 +20,100 @@ namespace ElectricLocomotive
|
||||
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new(pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningElectricLocomotive.DrawTransport(gr);
|
||||
_drawningLocomotive.DrawTransport(gr);
|
||||
pictureBoxElectroLocomotiv.Image = bmp;
|
||||
}
|
||||
|
||||
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 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)), pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
|
||||
|
||||
_drawningElectricLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
|
||||
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonCreateLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
_drawningLocomotive = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 3000),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.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;
|
||||
}
|
||||
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
|
||||
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 buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToRightCorner(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawningObjectLocomotive(_drawningLocomotive), pictureBoxElectroLocomotiv.Width,
|
||||
pictureBoxElectroLocomotiv.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -3,51 +3,18 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ElectricLocomotive.Entities;
|
||||
|
||||
namespace ElectricLocomotive
|
||||
namespace ElectricLocomotive.Entities
|
||||
{
|
||||
public class EntityElectricLocomotive
|
||||
public class EntityElectroLocomotive : EntityLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; private set; }
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; private set; }
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Дополнительный цвет (для опциональных элементов)
|
||||
/// </summary>
|
||||
public Color AdditionalColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия обвеса
|
||||
/// </summary>
|
||||
public bool Horns { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) roga
|
||||
/// </summary>
|
||||
|
||||
public double Step => (double)Speed * 100 / Weight; ///свойство с лямбда выражением
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса электролокоматива
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="Horns">Признак наличия рогов</param>
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns)
|
||||
public EntityElectroLocomotive(int Speed, double weight, Color bodyColor, Color additionalColor, bool horns) : base(Speed,weight,bodyColor, additionalColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Horns = horns;
|
||||
}
|
||||
}
|
||||
|
45
ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs
Normal file
45
ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive.Entities
|
||||
{
|
||||
public class EntityLocomotive
|
||||
{
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; private set; }
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; private set; }
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Дополнительный цвет (для опциональных элементов)
|
||||
/// </summary>
|
||||
public Color AdditionalColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия обвеса
|
||||
/// </summary>
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
/// <summary>
|
||||
/// Конструктор с параметрами
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public EntityLocomotive(int speed, double weight, Color bodyColor, Color additionalColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
}
|
||||
}
|
||||
}
|
18
ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs
Normal file
18
ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
int GetStep { get; }
|
||||
|
||||
bool CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
||||
}
|
57
ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs
Normal file
57
ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using ElectricLocomotive.MovementStrategy;
|
||||
using ProjectElectricLocomotive.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
30
ElectricLocomotive/ElectricLocomotive/MoveToRightCorner.cs
Normal file
30
ElectricLocomotive/ElectricLocomotive/MoveToRightCorner.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using ProjectElectricLocomotive.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public class MoveToRightCorner : 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();
|
||||
if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
27
ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs
Normal file
27
ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive
|
||||
{
|
||||
public class ObjectParameters
|
||||
{
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
public int RightBorder => _x + _width;
|
||||
public int DownBorder => _y + _height;
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
||||
}
|
15
ElectricLocomotive/ElectricLocomotive/Status.cs
Normal file
15
ElectricLocomotive/ElectricLocomotive/Status.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user