From bb6cda5892a44a5e79f3d9d1417bba8738ec0eea Mon Sep 17 00:00:00 2001 From: tellsense Date: Tue, 26 Dec 2023 02:39:13 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20(2=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractStrategy.cs | 74 ++++++ .../DrawningElectricLocomotive.cs | 215 +++------------- .../DrawningLocomotive.cs | 232 ++++++++++++++++++ .../DrawningObjectLocomotive.cs | 34 +++ .../DrawningWheelsCircle.cs | 96 ++++++++ .../DrawningWheelsCross.cs | 94 +++++++ .../DrawningWheelsTriangle.cs | 94 +++++++ .../EntityElectricLocomotive.cs | 26 +- .../EntityLocomotive.cs | 28 +++ .../FormElectricLocomotive.Designer.cs | 142 ++++++++--- .../FormElectricLocomotive.cs | 102 ++++++-- .../IDrawningWheels.cs | 14 ++ .../IMoveableObject.cs | 21 ++ .../ProjectElectricLocomotive/MoveToCenter.cs | 56 +++++ .../MoveToRightEdge.cs | 29 +++ .../ObjectParameters.cs | 54 ++++ .../ProjectElectricLocomotive/Program.cs | 2 +- .../ProjectElectricLocomotive/Status.cs | 15 ++ 18 files changed, 1058 insertions(+), 270 deletions(-) create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningLocomotive.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningObjectLocomotive.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCircle.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCross.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsTriangle.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/EntityLocomotive.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/IDrawningWheels.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/IMoveableObject.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightEdge.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/ObjectParameters.cs create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/Status.cs diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs new file mode 100644 index 0000000..7f35b62 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/AbstractStrategy.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +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; } + 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(); + /// Направление + private bool MoveTo(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningElectricLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningElectricLocomotive.cs index dfa2b4b..d595c80 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningElectricLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningElectricLocomotive.cs @@ -1,104 +1,28 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectElectricLocomotive; +using ProjectElectricLocomotive.Entities; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; -namespace ProjectElectricLocomotive +namespace ProjectElectricLocomotive.DrawingObjects { - public class DrawningElectricLocomotive + public class DrawingElectricLocomotive : DrawingLocomotive { - public EntityElectricLocomotive? EntityElectricLocomotive { get; private set; } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private readonly int _locomWidth = 150; - private readonly int _locomHeight = 52; - private DrawningWheels drawningWheels; - /// Скорость - /// Вес - /// Цвет кузова - /// Дополнительный цвет - /// Признак наличия токоприемника - /// Признак наличия отсеков под электрические батареи - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pantograph, bool compartment, int wheels, int width, int height) + public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, + bool pantograph, bool compartment, int width, int height) : base(speed, weight, bodyColor, width, height, wheels, wheelsShapes) { - if (width < _locomWidth || height < _locomHeight) + if (EntityLocomotive != null) { - return false; - } - _pictureWidth = width; - _pictureHeight = height; - EntityElectricLocomotive = new EntityElectricLocomotive(); - EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, pantograph, compartment, wheels); - - drawningWheels = new DrawningWheels(); - drawningWheels.SetAmount(wheels); - return true; - - - } - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - if (x < 0 || x + _locomWidth > _pictureWidth) - { - x = _pictureWidth - _locomWidth; - } - if (y < 0 || y + _locomHeight > _pictureHeight) - { - y = _pictureHeight - _locomHeight; - } - _startPosX = x; - _startPosY = y; - } - /// Направление - public void MoveTransport(Direction direction) - { - if (EntityElectricLocomotive == null) - { - return; - } - switch (direction) - { - //влево - case Direction.Left: - if (_startPosX - EntityElectricLocomotive.Step > 0) - { - _startPosX -= (int)EntityElectricLocomotive.Step; - } - break; - //вверх - case Direction.Up: - if (_startPosY - EntityElectricLocomotive.Step > 0) - { - _startPosY -= (int)EntityElectricLocomotive.Step; - } - break; - //вправо - case Direction.Right: - if (_startPosX + EntityElectricLocomotive.Step + _locomWidth < _pictureWidth) - { - _startPosX += (int)EntityElectricLocomotive.Step; - } - break; - case Direction.Down: - if (_startPosY + EntityElectricLocomotive.Step + _locomHeight < _pictureHeight) - { - _startPosY += (int)EntityElectricLocomotive.Step; - } - break; + EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, pantograph, compartment); } } - /// - public void DrawTransport(Graphics g) + public override void DrawTransport(Graphics g) { - if (EntityElectricLocomotive == null) + if (EntityLocomotive is not EntityElectricLocomotive electricLocomotive) { return; } @@ -106,106 +30,23 @@ namespace ProjectElectricLocomotive Pen pen = new(Color.Black); Brush blackBrush = new SolidBrush(Color.Black); Brush windows = new SolidBrush(Color.LightBlue); - Brush bodyColor = new SolidBrush(EntityElectricLocomotive.BodyColor); - Brush additionalBrush = new SolidBrush(EntityElectricLocomotive.AdditionalColor); - - if (EntityElectricLocomotive.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); - } - - // Локомотив - g.FillPolygon(bodyColor, new Point[] - { - new Point(_startPosX, _startPosY + 40), - new Point(_startPosX, _startPosY + 30), - new Point(_startPosX + 20, _startPosY + 20), - new Point(_startPosX + 70, _startPosY + 20), - new Point(_startPosX +80, _startPosY + 20), - new Point(_startPosX +80, _startPosY + 40), - new Point(_startPosX +75, _startPosY + 45), - new Point(_startPosX +5, _startPosY + 45), - new Point(_startPosX, _startPosY + 40), - } - ); - - if (EntityElectricLocomotive.Compartment) - { - 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.DrawPolygon(pen, new Point[] - { - new Point(_startPosX, _startPosY + 40), - new Point(_startPosX, _startPosY + 30), - new Point(_startPosX + 20, _startPosY + 20), - new Point(_startPosX + 70, _startPosY + 20), - new Point(_startPosX + 80, _startPosY + 20), - new Point(_startPosX + 80, _startPosY + 40), - new Point(_startPosX + 75, _startPosY + 45), - new Point(_startPosX + 5, _startPosY + 45), - new Point(_startPosX, _startPosY + 40), - } - ); - - // Окна - g.FillEllipse(windows, _startPosX + 10, _startPosY + 24, 10, 10); - g.DrawEllipse(pen, _startPosX + 10, _startPosY + 25, 10, 10); - - g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5); - g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5); - - // Колёса - // Локомотив - g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 10, 10); - g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 10, 10); - - g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 10, 10); - g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 10, 10); - // Вагон + Brush additionalBrush = new SolidBrush(electricLocomotive.AdditionalColor); + Brush bodyColor = new SolidBrush(electricLocomotive.BodyColor); + g.DrawRectangle(pen, _startPosX + 40, _startPosY + 24, 25, 11); g.FillPolygon(additionalBrush, new Point[] - { - new Point(_startPosX + 90, _startPosY + 25), - new Point(_startPosX + 95, _startPosY + 20), - new Point(_startPosX + 145, _startPosY + 20), - new Point(_startPosX + 150, _startPosY + 25), - new Point(_startPosX + 150, _startPosY + 45), - new Point(_startPosX + 90, _startPosY + 45), - new Point(_startPosX + 90, _startPosY + 25), - } - ); + { + new Point(_startPosX + 61, _startPosY + 25), + new Point(_startPosX + 85, _startPosY + 25), + new Point(_startPosX + 85, _startPosY + 35), + new Point(_startPosX + 61, _startPosY + 35), + new Point(_startPosX + 61, _startPosY + 25), + } + ); - g.DrawPolygon(pen, new Point[] - { - new Point(_startPosX + 90, _startPosY + 25), - new Point(_startPosX + 95, _startPosY + 20), - new Point(_startPosX + 145, _startPosY + 20), - new Point(_startPosX + 150, _startPosY + 25), - new Point(_startPosX + 150, _startPosY + 45), - new Point(_startPosX + 90, _startPosY + 45), - new Point(_startPosX + 90, _startPosY + 25), - } - ); - //Окна - g.DrawLine(pen, _startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40); - g.FillRectangle(windows, _startPosX + 95, _startPosY + 30, 10, 5); - g.FillRectangle(windows, _startPosX + 115, _startPosY + 30, 10, 5); - g.FillRectangle(windows, _startPosX + 135, _startPosY + 30, 10, 5); - - //Колеса - drawningWheels.DrawWheels(g, _startPosX, _startPosY); + 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); } } -} +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningLocomotive.cs new file mode 100644 index 0000000..b1f2aa4 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningLocomotive.cs @@ -0,0 +1,232 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectElectricLocomotive.Entities; +using ProjectElectricLocomotive.Properties; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace ProjectElectricLocomotive.DrawingObjects +{ + public class DrawingLocomotive + { + public EntityLocomotive? EntityLocomotive { get; protected set; } + + protected int _pictureWidth; + protected int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected readonly int _locomWidth = 360; + protected readonly int _locomHeight = 100; + private IDrawningWheels drawningWheels; + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _locomWidth; + public int GetHeight => _locomHeight; + /// Скорость + /// Вес + /// Цвет кузова + /// Ширина картинки + /// Высота картинки + + public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth, int wheels, int wheelsShapes) + { + if (width < _locomWidth || heigth < _locomHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = heigth; + EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + switch (wheelsShapes) + { + case 1: + drawningWheels = new DrawningWheelsCircle(); + break; + case 2: + drawningWheels = new DrawningWheelsCross(); + break; + default: + drawningWheels = new DrawningWheelsTriangle(); + break; + } + drawningWheels.SetAmount(wheels); + } + /// Скорость + /// Вес + /// Цвет кузова + /// Ширина картинки + /// Высота картинки + /// Ширина картинки + /// Высота картинки + protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width, + int height, int locomWidth, int locomHeight) + { + if (width < _locomWidth || height < _locomHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _locomWidth = locomWidth; + _locomHeight = locomHeight; + EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + } + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (x < 0 || x + _locomWidth > _pictureWidth) + { + x = _pictureWidth - _locomWidth; + } + if (y < 0 || y + _locomHeight > _pictureHeight) + { + y = _pictureHeight - _locomHeight; + } + _startPosX = x; + _startPosY = y; + } + /// Направление + public void MoveTransport(Direction direction) + { + if (EntityLocomotive == null) + { + return; + } + switch (direction) + { + case Direction.Left: + if (_startPosX - EntityLocomotive.Step > 0) + { + _startPosX -= (int)EntityLocomotive.Step; + } + break; + case Direction.Up: + if (_startPosY - EntityLocomotive.Step > 0) + { + _startPosY -= (int)EntityLocomotive.Step; + } + break; + case Direction.Right: + if (_startPosX + EntityLocomotive.Step + _locomWidth < _pictureWidth) + { + _startPosX += (int)EntityLocomotive.Step; + } + break; + case Direction.Down: + if (_startPosY + EntityLocomotive.Step + _locomHeight < _pictureHeight) + { + _startPosY += (int)EntityLocomotive.Step; + } + break; + } + } + /// + public virtual void DrawTransport(Graphics g) + { + { + if (EntityLocomotive == null) return; + } + + Pen pen = new(Color.Black); + Brush blackBrush = new SolidBrush(Color.Black); + Brush windows = new SolidBrush(Color.LightBlue); + Brush bodyColor = new SolidBrush(EntityLocomotive.BodyColor); + // Локомотив + g.FillPolygon(bodyColor, new Point[] + { + new Point(_startPosX, _startPosY + 40), + new Point(_startPosX, _startPosY + 30), + new Point(_startPosX + 20, _startPosY + 20), + new Point(_startPosX + 70, _startPosY + 20), + new Point(_startPosX +80, _startPosY + 20), + new Point(_startPosX +80, _startPosY + 40), + new Point(_startPosX +75, _startPosY + 45), + new Point(_startPosX +5, _startPosY + 45), + new Point(_startPosX, _startPosY + 40), + } + ); + g.DrawPolygon(pen, new Point[] + { + new Point(_startPosX, _startPosY + 40), + new Point(_startPosX, _startPosY + 30), + new Point(_startPosX + 20, _startPosY + 20), + new Point(_startPosX + 70, _startPosY + 20), + new Point(_startPosX + 80, _startPosY + 20), + new Point(_startPosX + 80, _startPosY + 40), + new Point(_startPosX + 75, _startPosY + 45), + new Point(_startPosX + 5, _startPosY + 45), + new Point(_startPosX, _startPosY + 40), + } + ); + + // Окна + g.FillEllipse(windows, _startPosX + 10, _startPosY + 24, 10, 10); + g.DrawEllipse(pen, _startPosX + 10, _startPosY + 25, 10, 10); + + g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5); + g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5); + + // Колёса + // Локомотив + g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 10, 10); + g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 10, 10); + + g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 10, 10); + g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 10, 10); + // Вагон + g.FillPolygon(bodyColor, new Point[] + { + new Point(_startPosX + 90, _startPosY + 25), + new Point(_startPosX + 95, _startPosY + 20), + new Point(_startPosX + 145, _startPosY + 20), + new Point(_startPosX + 150, _startPosY + 25), + new Point(_startPosX + 150, _startPosY + 45), + new Point(_startPosX + 90, _startPosY + 45), + new Point(_startPosX + 90, _startPosY + 25), + } + ); + + g.DrawPolygon(pen, new Point[] + { + new Point(_startPosX + 90, _startPosY + 25), + new Point(_startPosX + 95, _startPosY + 20), + new Point(_startPosX + 145, _startPosY + 20), + new Point(_startPosX + 150, _startPosY + 25), + new Point(_startPosX + 150, _startPosY + 45), + new Point(_startPosX + 90, _startPosY + 45), + new Point(_startPosX + 90, _startPosY + 25), + } + ); + //Окна + g.DrawLine(pen, _startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40); + g.FillRectangle(windows, _startPosX + 95, _startPosY + 30, 10, 5); + g.FillRectangle(windows, _startPosX + 115, _startPosY + 30, 10, 5); + g.FillRectangle(windows, _startPosX + 135, _startPosY + 30, 10, 5); + + ////Колеса + drawningWheels.DrawWheels(g, _startPosX, _startPosY); + } + /// Направление + 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, + }; + } + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningObjectLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningObjectLocomotive.cs new file mode 100644 index 0000000..9896eb0 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningObjectLocomotive.cs @@ -0,0 +1,34 @@ +using ProjectElectricLocomotive.DrawingObjects; +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); + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCircle.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCircle.cs new file mode 100644 index 0000000..c860942 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCircle.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive +{ + internal class DrawningWheelsCircle : IDrawningWheels + { + private Wheels amount; + public void SetAmount(int a) + { + if (a == 2) + { + amount = Wheels.Two; + } + else if (a == 3) + { + amount = Wheels.Three; + } + else if (a == 4) + { + amount = Wheels.Four; + } + } + public void DrawWheels(Graphics g, int _startPosX, int _startPosY) + { + Brush enginesColor = new SolidBrush(Color.Black); + + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 51, _startPosY + 30), + new Point(_startPosX + 75, _startPosY + 30), + new Point(_startPosX + 75, _startPosY + 40), + new Point(_startPosX + 53, _startPosY + 40), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 28, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 52, _startPosY + 80), + new Point(_startPosX + 75, _startPosY + 80), + new Point(_startPosX + 75, _startPosY + 90), + new Point(_startPosX + 50, _startPosY + 90), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 78, 13, 13); + + if (amount == Wheels.Three) + { + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 48, _startPosY + 18), + new Point(_startPosX + 75, _startPosY + 18), + new Point(_startPosX + 75, _startPosY + 28), + new Point(_startPosX + 50, _startPosY + 28), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 16, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 49, _startPosY + 92), + new Point(_startPosX + 75, _startPosY + 92), + new Point(_startPosX + 75, _startPosY + 102), + new Point(_startPosX + 47, _startPosY + 102), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 90, 13, 13); + } + if (amount == Wheels.Four) + { + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 46, _startPosY + 6), + new Point(_startPosX + 75, _startPosY + 6), + new Point(_startPosX + 75, _startPosY + 16), + new Point(_startPosX + 48, _startPosY + 16), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 4, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 47, _startPosY + 104), + new Point(_startPosX + 75, _startPosY + 104), + new Point(_startPosX + 75, _startPosY + 114), + new Point(_startPosX + 45, _startPosY + 114), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 102, 13, 13); + } + } + + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCross.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCross.cs new file mode 100644 index 0000000..be30093 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsCross.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive +{ + internal class DrawningWheelsCross : IDrawningWheels + { + private Wheels amount; + public void SetAmount(int a) + { + if (a == 2) + { + amount = Wheels.Two; + } + else if (a == 3) + { + amount = Wheels.Three; + } + else if (a == 4) + { + amount = Wheels.Four; + } + } + public void DrawWheels(Graphics g, int _startPosX, int _startPosY) + { + Brush enginesColor = new SolidBrush(Color.Black); + + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 51, _startPosY + 30), + new Point(_startPosX + 75, _startPosY + 30), + new Point(_startPosX + 75, _startPosY + 40), + new Point(_startPosX + 53, _startPosY + 40), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 28, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 52, _startPosY + 80), + new Point(_startPosX + 75, _startPosY + 80), + new Point(_startPosX + 75, _startPosY + 90), + new Point(_startPosX + 50, _startPosY + 90), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 78, 13, 13); + + if (amount == Wheels.Three) + { + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 48, _startPosY + 18), + new Point(_startPosX + 75, _startPosY + 18), + new Point(_startPosX + 75, _startPosY + 28), + new Point(_startPosX + 50, _startPosY + 28), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 16, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 49, _startPosY + 92), + new Point(_startPosX + 75, _startPosY + 92), + new Point(_startPosX + 75, _startPosY + 102), + new Point(_startPosX + 47, _startPosY + 102), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 90, 13, 13); + } + if (amount == Wheels.Four) + { + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 46, _startPosY + 6), + new Point(_startPosX + 75, _startPosY + 6), + new Point(_startPosX + 75, _startPosY + 16), + new Point(_startPosX + 48, _startPosY + 16), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 4, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 47, _startPosY + 104), + new Point(_startPosX + 75, _startPosY + 104), + new Point(_startPosX + 75, _startPosY + 114), + new Point(_startPosX + 45, _startPosY + 114), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 102, 13, 13); + } + } + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsTriangle.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsTriangle.cs new file mode 100644 index 0000000..f876473 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawningWheelsTriangle.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive +{ + internal class DrawningWheelsTriangle : IDrawningWheels + { + private Wheels amount; + public void SetAmount(int a) + { + if (a == 2) + { + amount = Wheels.Two; + } + else if (a == 3) + { + amount = Wheels.Three; + } + else if (a == 4) + { + amount = Wheels.Four; + } + } + public void DrawWheels(Graphics g, int _startPosX, int _startPosY) + { + Brush enginesColor = new SolidBrush(Color.Black); + + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 51, _startPosY + 30), + new Point(_startPosX + 75, _startPosY + 30), + new Point(_startPosX + 75, _startPosY + 40), + new Point(_startPosX + 53, _startPosY + 40), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 28, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 52, _startPosY + 80), + new Point(_startPosX + 75, _startPosY + 80), + new Point(_startPosX + 75, _startPosY + 90), + new Point(_startPosX + 50, _startPosY + 90), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 78, 13, 13); + + if (amount == Wheels.Three) + { + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 48, _startPosY + 18), + new Point(_startPosX + 75, _startPosY + 18), + new Point(_startPosX + 75, _startPosY + 28), + new Point(_startPosX + 50, _startPosY + 28), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 16, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 49, _startPosY + 92), + new Point(_startPosX + 75, _startPosY + 92), + new Point(_startPosX + 75, _startPosY + 102), + new Point(_startPosX + 47, _startPosY + 102), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 90, 13, 13); + } + if (amount == Wheels.Four) + { + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 46, _startPosY + 6), + new Point(_startPosX + 75, _startPosY + 6), + new Point(_startPosX + 75, _startPosY + 16), + new Point(_startPosX + 48, _startPosY + 16), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 4, 13, 13); + g.FillPolygon(enginesColor, new Point[] + { + new Point(_startPosX + 47, _startPosY + 104), + new Point(_startPosX + 75, _startPosY + 104), + new Point(_startPosX + 75, _startPosY + 114), + new Point(_startPosX + 45, _startPosY + 114), + } + ); + g.FillEllipse(enginesColor, _startPosX + 71, _startPosY + 102, 13, 13); + } + } + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/EntityElectricLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/EntityElectricLocomotive.cs index 3e11d39..ef80c33 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/EntityElectricLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/EntityElectricLocomotive.cs @@ -1,36 +1,28 @@ -using System; +using ProjectElectricLocomotive.Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectElectricLocomotive +namespace ProjectElectricLocomotive.Entities { - public class EntityElectricLocomotive + public class EntityElectricLocomotive : EntityLocomotive { - public int Speed { get; private set; } - public double Weight { get; private set; } - public Color BodyColor { get; private set; } public Color AdditionalColor { get; private set; } public bool Pantograph { get; private set; } public bool Compartment { get; private set; } - public int Wheels { get; private set; } - public double Step => (double)Speed * 100 / Weight; - /// Скорость - /// Вес локомотива - /// Основной цвет + + /// Дополнительный цвет /// Признак наличия токоприемника /// Признак наличия отсеков под электрические батареи - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pantograph, bool compartment, int wheels) + public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, bool pantograph, + bool compartment) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Pantograph = pantograph; Compartment = compartment; - Wheels = wheels; } } -} +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/EntityLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/EntityLocomotive.cs new file mode 100644 index 0000000..70c5232 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/EntityLocomotive.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Entities +{ + public class EntityLocomotive + { + public int Speed { get; private set; } + public double Weight { get; private set; } + public Color BodyColor { get; private set; } + public int Wheels { get; private set; } + public double Step => (double)Speed * 100 / Weight; + + + /// Скорость + /// Вес локомотива + /// Основной цвет + public EntityLocomotive(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs index 97b60c5..d3d77e5 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.Designer.cs @@ -1,6 +1,12 @@ -namespace ProjectElectricLocomotive +using System; +using static System.Net.Mime.MediaTypeNames; +using System.Drawing.Printing; +using System.Windows.Forms; +using System.Xml.Linq; + +namespace ProjectElectricLocomotive { - partial class FormElectricLocomotive + partial class ElectricLocomotive { /// /// Required designer variable. @@ -29,11 +35,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 +53,131 @@ 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; + // + // 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 +188,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; } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs index 37cfe54..76e50f8 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormElectricLocomotive.cs @@ -1,45 +1,61 @@ +using ProjectElectricLocomotive; +using ProjectElectricLocomotive.DrawingObjects; +using ProjectElectricLocomotive.MovementStrategy; +using System; + namespace ProjectElectricLocomotive + { - 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; } - /// - /// - 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)), - random.Next(1, 5), - pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height) ; ; - _drawningElectricLocomotive.SetPosition(random.Next(1, 100), random.Next(1, 100)); + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), + pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); + _drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } - /// - /// + + 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)), rnd.Next(1, 5), rnd.Next(1, 5) + + 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; } @@ -47,19 +63,55 @@ namespace ProjectElectricLocomotive 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; + } + } } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/IDrawningWheels.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/IDrawningWheels.cs new file mode 100644 index 0000000..af72a09 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/IDrawningWheels.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive +{ + internal interface IDrawningWheels + { + public void SetAmount(int a); + public void DrawWheels(Graphics g, int _startPosX, int _startPosY); + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/IMoveableObject.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/IMoveableObject.cs new file mode 100644 index 0000000..ab05c84 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/IMoveableObject.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectElectricLocomotive.DrawingObjects; + +namespace ProjectElectricLocomotive.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + + int GetStep { get; } + /// + bool CheckCanMove(Direction direction); + /// Направление + void MoveObject(Direction direction); + + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs new file mode 100644 index 0000000..7ced34e --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToCenter.cs @@ -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(); + } + } + } + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightEdge.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightEdge.cs new file mode 100644 index 0000000..65988c0 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/MoveToRightEdge.cs @@ -0,0 +1,29 @@ +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(); + if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown(); + + + } + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ObjectParameters.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/ObjectParameters.cs new file mode 100644 index 0000000..598c13d --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ObjectParameters.cs @@ -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; + /// + /// Левая граница + /// + public int LeftBorder => _x; + /// + /// Верхняя граница + /// + public int TopBorder => _y; + /// + /// Правая граница + /// + public int RightBorder => _x + _width; + /// + /// Нижняя граница + /// + public int DownBorder => _y + _height; + /// + /// Середина объекта + /// + public int ObjectMiddleHorizontal => _x + _width / 2; + /// + /// Середина объекта + /// + public int ObjectMiddleVertical => _y + _height / 2; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs index ddb490c..6d29b90 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs @@ -11,7 +11,7 @@ namespace ProjectElectricLocomotive // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormElectricLocomotive()); + Application.Run(new ElectricLocomotive()); } } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/Status.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/Status.cs new file mode 100644 index 0000000..525c648 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/Status.cs @@ -0,0 +1,15 @@ +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 + } +}