This commit is contained in:
gg12 darfren 2023-10-03 19:33:45 +04:00
parent 00e62db1cf
commit 077319d7ec
13 changed files with 576 additions and 74 deletions

View File

@ -0,0 +1,84 @@
using Monorail.DrawningObjects;
namespace Monorail.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(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;
}
}
}

View File

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Monorail.Entities;
namespace Monorail.DrawningObjects
{
public class DrawningAdvancedMonorail : DrawningMonorail
{
public DrawningAdvancedMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int width, int height, int addWheelsNumb,
Color secondCabineColor, bool secondCabine, bool magniteRail) : base(speed, weight, bodyColor, wheelColor, tireColor, width, height)
{
if(EntityMonorail != null)
{
EntityMonorail = new EntityAdvancedMonorail(speed, weight, bodyColor, wheelColor, tireColor,
addWheelsNumb, secondCabineColor, secondCabine, magniteRail);
}
}
public override void DrawTransport(Graphics g)
{
base.DrawTransport(g);
int dif = _monoRailWidth / 10;
_monoRailWidth -= dif;
Pen windowPen = new(Color.Blue);
Brush wheelBrush = new SolidBrush(EntityMonorail.WheelColor);
Brush windowBrush = new SolidBrush(Color.White);
if (EntityMonorail == null)
return;
if(EntityMonorail is not EntityAdvancedMonorail advancedMonorail)
{
return;
}
Pen additionalPen = new(advancedMonorail.TireColor);
Brush additionalBrush = new SolidBrush(advancedMonorail.WheelColor);
//3 колеса
if (advancedMonorail.AddWheelsNumb >= 3)
{
g.FillEllipse(additionalBrush, _startPosX + _monoRailWidth / 10 * 6, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
g.DrawEllipse(additionalPen, _startPosX + _monoRailWidth / 10 * 6, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
}
//4 колеса
if (advancedMonorail.AddWheelsNumb >= 4)
{
g.FillEllipse(additionalBrush, _startPosX + _monoRailWidth / 10 * 3, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
g.DrawEllipse(additionalPen, _startPosX + _monoRailWidth / 10 * 3, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
}
if (advancedMonorail.SecondCabine)
{
Point[] pointsSecondCabine = { new Point(_startPosX + _monoRailWidth / 20 * 19, _startPosY + _monoRailHeight / 10),
new Point(_startPosX + _monoRailWidth + dif, _startPosY + _monoRailHeight / 5 * 2),
new Point(_startPosX + _monoRailWidth + dif, _startPosY + _monoRailHeight / 10 * 7),
new Point(_startPosX + _monoRailWidth / 20 * 19, _startPosY + _monoRailHeight / 10 * 7),};
g.FillPolygon(additionalBrush, pointsSecondCabine);
g.DrawPolygon(additionalPen, pointsSecondCabine);
Rectangle Rect = new();
Rect.X = _startPosX + _monoRailWidth / 20 * 19;
Rect.Y = _startPosY + _monoRailHeight / 25 * 4 + _monoRailHeight / 50 * 3;
Rect.Width = _monoRailWidth / 120 * 6;
Rect.Height = _monoRailHeight / 50 * 7;
g.DrawRectangle(windowPen, Rect);
g.FillRectangle(windowBrush, Rect);
}
_monoRailWidth += dif;
//магнитная линия
if (advancedMonorail.MagniteRail)
{
g.DrawLine(additionalPen, new Point(_startPosX, _startPosY + _monoRailHeight - 1), new Point(_startPosX + _monoRailWidth, _startPosY + _monoRailHeight - 1));
}
}
}
}

View File

@ -4,37 +4,57 @@ using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Monorail.Entities;
using Monorail.MovementStrategy;
namespace Monorail namespace Monorail.DrawningObjects
{ {
public class DrawningMonorail public class DrawningMonorail
{ {
public EntityMonorail? EntityMonorail { get; private set; } public EntityMonorail? EntityMonorail { get; protected set; }
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _monoRailWidth;
public int GetHeight => _monoRailHeight;
private int _pictureWidth; private int _pictureWidth;
private int _pictureHeight; private int _pictureHeight;
private int _startPosX = 0; protected int _startPosX = 0;
private int _startPosY = 0; protected int _startPosY = 0;
private int _monoRailWidth = 133; protected int _monoRailWidth = 133;
private int _monoRailHeight = 50; protected int _monoRailHeight = 50;
public bool Init(int speed, double weight, Color bodyColor, int width, int height, int wheelNumb, Color wheelColor, Color tireColor, bool secondCabine, bool magniteRail) protected int wheelSz;
public DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int width, int height)
{ {
if (width <= _monoRailWidth || height <= _monoRailHeight) if (width <= _monoRailWidth || height <= _monoRailHeight)
return false; return;
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
EntityMonorail = new EntityMonorail(); wheelSz = _monoRailHeight - _monoRailHeight * 7 / 10;
EntityMonorail.Init(speed, weight, bodyColor,wheelNumb,wheelColor, tireColor, secondCabine, magniteRail); EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor);
return true;
} }
protected DrawningMonorail(int speed, double weight, Color bodyColor, int width, int height, Color wheelColor, Color tireColor, int monoRailWidth, int monoRailHeight)
{
if (width <= _monoRailWidth || height <= _monoRailHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
_monoRailHeight = monoRailHeight;
_monoRailWidth = monoRailWidth;
wheelSz = _monoRailHeight - _monoRailHeight * 7 / 10;
EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor);
}
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
if (EntityMonorail == null) if (EntityMonorail == null)
@ -48,7 +68,19 @@ namespace Monorail
} }
} }
public void MoveTransport(DirectionType direction) public bool CanMove(DirectionType direction)
{
if (EntityMonorail == null)
return false;
return direction switch
{
DirectionType.Left => _startPosX - EntityMonorail.Step > 0,
DirectionType.Up => _startPosY - EntityMonorail.Step > 0,
DirectionType.Right => _startPosX + EntityMonorail.Step < _pictureWidth,
DirectionType.Down => _startPosY -+EntityMonorail.Step < _pictureHeight
};
}
/*public void MoveTransport(DirectionType direction)
{ {
if (EntityMonorail == null) if (EntityMonorail == null)
return; return;
@ -79,20 +111,39 @@ namespace Monorail
_startPosY = _pictureHeight - _monoRailHeight; _startPosY = _pictureHeight - _monoRailHeight;
break; break;
} }
} }*/
public void DrawPolyline(Pen pen, Graphics g, Point[] pointsArr) public void MoveTransport(DirectionType direction)
{ {
if (pointsArr.Length < 2) if (!CanMove(direction) || EntityMonorail == null)
return; return;
for (int i = 1; i < pointsArr.Length; i++) switch (direction)
g.DrawLine(pen, pointsArr[i - 1], pointsArr[i]); {
case DirectionType.Left:
if (_startPosX - EntityMonorail.Step >= 0)
_startPosX -= (int)EntityMonorail.Step;
break;
case DirectionType.Up:
if (_startPosY - EntityMonorail.Step >= 0)
_startPosY -= (int)EntityMonorail.Step;
break;
case DirectionType.Right:
if (_startPosX + EntityMonorail.Step + _monoRailWidth < _pictureWidth)
_startPosX += (int)EntityMonorail.Step;
break;
case DirectionType.Down:
if (_startPosY + EntityMonorail.Step + _monoRailHeight < _pictureHeight)
_startPosY += (int)EntityMonorail.Step;
break;
}
} }
public void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
if (EntityMonorail == null) if (EntityMonorail == null)
return; return;
int dif = _monoRailWidth / 10;
_monoRailWidth -= dif;
Pen pen = new (Color.Black); Pen pen = new (Color.Black);
Brush cartBrush = new SolidBrush(Color.Black); Brush cartBrush = new SolidBrush(Color.Black);
Brush bodyBrush = new SolidBrush(EntityMonorail.BodyColor); Brush bodyBrush = new SolidBrush(EntityMonorail.BodyColor);
@ -100,12 +151,10 @@ namespace Monorail
Brush wheelBrush = new SolidBrush(EntityMonorail.WheelColor); Brush wheelBrush = new SolidBrush(EntityMonorail.WheelColor);
Brush windowBrush = new SolidBrush(Color.White); Brush windowBrush = new SolidBrush(Color.White);
Pen tirePen = new Pen(EntityMonorail.TireColor); Pen tirePen = new Pen(EntityMonorail.TireColor);
int wheelSz = _monoRailHeight - _monoRailHeight * 7 / 10;
if (_monoRailWidth - _monoRailWidth / 20 * 17 < wheelSz) if (_monoRailWidth - _monoRailWidth / 20 * 17 < wheelSz)
wheelSz = _monoRailWidth - _monoRailWidth / 20 * 17; wheelSz = _monoRailWidth - _monoRailWidth / 20 * 17;
int dif = _monoRailWidth / 10;
_monoRailWidth -= dif;
//нижняя часть локомотива //нижняя часть локомотива
Point[] pointsArrLow = { new Point(_startPosX + _monoRailWidth / 10 * 4, _startPosY + _monoRailHeight / 5 * 2), Point[] pointsArrLow = { new Point(_startPosX + _monoRailWidth / 10 * 4, _startPosY + _monoRailHeight / 5 * 2),
new Point(_startPosX + _monoRailWidth / 10, _startPosY + _monoRailHeight / 5 * 2), new Point(_startPosX + _monoRailWidth / 10, _startPosY + _monoRailHeight / 5 * 2),
@ -137,7 +186,7 @@ namespace Monorail
new Point(_startPosX + _monoRailWidth / 10 * 5, _startPosY + _monoRailHeight / 5 * 3), new Point(_startPosX + _monoRailWidth / 10 * 5, _startPosY + _monoRailHeight / 5 * 3),
new Point(_startPosX + _monoRailWidth / 10 * 4, _startPosY + _monoRailHeight / 5 * 3) }; new Point(_startPosX + _monoRailWidth / 10 * 4, _startPosY + _monoRailHeight / 5 * 3) };
g.DrawPolygon(pen, pointsArrDoor); g.DrawPolygon(pen, pointsArrDoor);
g.FillPolygon(wheelBrush, pointsArrDoor); g.FillPolygon(cartBrush, pointsArrDoor);
//передняя часть тележки //передняя часть тележки
@ -185,16 +234,12 @@ namespace Monorail
g.DrawRectangle(windowPen, rightRect); g.DrawRectangle(windowPen, rightRect);
g.FillRectangle(windowBrush, rightRect); g.FillRectangle(windowBrush, rightRect);
//2 колеса g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
if (EntityMonorail.WheelsNumb >= 2)
{
g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10 , _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
g.DrawEllipse(tirePen, _startPosX + _monoRailWidth / 10, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz); g.DrawEllipse(tirePen, _startPosX + _monoRailWidth / 10, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10 * 8, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz); g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10 * 8, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
g.DrawEllipse(tirePen, _startPosX + _monoRailWidth / 10 * 8, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz); g.DrawEllipse(tirePen, _startPosX + _monoRailWidth / 10 * 8, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
}
//3 колеса /*//3 колеса
if (EntityMonorail.WheelsNumb >= 3) if (EntityMonorail.WheelsNumb >= 3)
{ {
g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10 * 6, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz); g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10 * 6, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
@ -207,31 +252,9 @@ namespace Monorail
{ {
g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10 * 3, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz); g.FillEllipse(wheelBrush, _startPosX + _monoRailWidth / 10 * 3, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
g.DrawEllipse(tirePen, _startPosX + _monoRailWidth / 10 * 3, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz); g.DrawEllipse(tirePen, _startPosX + _monoRailWidth / 10 * 3, _startPosY + _monoRailHeight / 10 * 7, wheelSz, wheelSz);
} }*/
//вторая кабина
if (EntityMonorail.SecondCabine)
{
Point[] pointsSecondCabine = { new Point(_startPosX + _monoRailWidth / 20 * 19, _startPosY + _monoRailHeight / 10),
new Point(_startPosX + _monoRailWidth + dif, _startPosY + _monoRailHeight / 5 * 2),
new Point(_startPosX + _monoRailWidth + dif, _startPosY + _monoRailHeight / 10 * 7),
new Point(_startPosX + _monoRailWidth / 20 * 19, _startPosY + _monoRailHeight / 10 * 7),};
g.FillPolygon(bodyBrush, pointsSecondCabine);
g.DrawPolygon(pen, pointsSecondCabine);
Rectangle Rect = new();
Rect.X = _startPosX + _monoRailWidth / 20 * 19;
Rect.Y = _startPosY + _monoRailHeight / 25 * 4 + _monoRailHeight / 50 * 3;
Rect.Width = _monoRailWidth / 120 * 6;
Rect.Height = _monoRailHeight / 50 * 7;
g.DrawRectangle(windowPen, Rect);
g.FillRectangle(windowBrush, Rect);
}
_monoRailWidth += dif; _monoRailWidth += dif;
//магнитная линия
if (EntityMonorail.MagniteRail)
{
g.DrawLine(pen, new Point(_startPosX, _startPosY + _monoRailHeight - 1), new Point(_startPosX + _monoRailWidth, _startPosY + _monoRailHeight - 1));
}
} }
} }

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Monorail.DrawningObjects;
namespace Monorail.MovementStrategy
{
public class DrawningObjectMonorail : IMoveableObject
{
private readonly DrawningMonorail? _drawningMonorail = null;
public DrawningObjectMonorail(DrawningMonorail drawningMonorail)
{
_drawningMonorail = drawningMonorail;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningMonorail == null || _drawningMonorail.EntityMonorail == null)
{
return null;
}
return new ObjectParameters(_drawningMonorail.GetPosX,
_drawningMonorail.GetPosY, _drawningMonorail.GetWidth, _drawningMonorail.GetHeight);
}
}
public int GetStep => (int)(_drawningMonorail?.EntityMonorail?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningMonorail?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningMonorail?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.Entities
{
public class EntityAdvancedMonorail : EntityMonorail
{
public Color additionalColor { get; private set; }
public int AddWheelsNumb { get; private set; }
public bool SecondCabine { get; private set; }
public bool MagniteRail { get; private set; }
public EntityAdvancedMonorail(int speed, double weight, Color bodyColor, Color wheelColor,
Color tireColor, int addWheelsNumb, Color secondCabineColor, bool secondCabine, bool magniteRail) : base(speed, weight, bodyColor, wheelColor, tireColor)
{
additionalColor = secondCabineColor;
AddWheelsNumb = addWheelsNumb;
SecondCabine = secondCabine;
MagniteRail = magniteRail;
}
}
}

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Monorail namespace Monorail.Entities
{ {
public class EntityMonorail public class EntityMonorail
{ {
@ -13,30 +13,20 @@ namespace Monorail
public double Weight { get; private set; } public double Weight { get; private set; }
public Color BodyColor { get; private set; } public Color BodyColor { get; private set; }
public int WheelsNumb { get; private set; }
public Color WheelColor { get; private set; } public Color WheelColor { get; private set; }
public Color TireColor { get; private set; } public Color TireColor { get; private set; }
public double Step => (double)Speed * 100 / Weight; public double Step => (double)Speed * 100 / Weight;
public bool SecondCabine { get; private set; }
public bool MagniteRail { get; private set; } public EntityMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor)
public void Init(int speed, double weight,Color bodyColor, int wheelsNumb, Color wheelColor, Color tireColor, bool secondCabine, bool magniteRail)
{ {
Speed = speed; Speed = speed;
Weight = weight; Weight = weight;
BodyColor = bodyColor; BodyColor = bodyColor;
if (!(wheelsNumb >= 2 && wheelsNumb <= 4))
wheelsNumb = 2;
WheelsNumb = wheelsNumb;
WheelColor = wheelColor; WheelColor = wheelColor;
TireColor = tireColor; TireColor = tireColor;
SecondCabine = secondCabine;
MagniteRail = magniteRail;
} }
} }
} }

View File

@ -0,0 +1,16 @@
using Monorail.DrawningObjects;
namespace Monorail.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

View File

@ -36,6 +36,9 @@ namespace Monorail
buttonRight = new System.Windows.Forms.Button(); buttonRight = new System.Windows.Forms.Button();
buttonUp = new System.Windows.Forms.Button(); buttonUp = new System.Windows.Forms.Button();
buttonDown = new System.Windows.Forms.Button(); buttonDown = new System.Windows.Forms.Button();
comboBoxStrategy = new System.Windows.Forms.ComboBox();
buttonStep = new System.Windows.Forms.Button();
buttonCreateAdvanced = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)pictureBoxMonorail).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxMonorail).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -52,9 +55,9 @@ namespace Monorail
// buttonCreate // buttonCreate
// //
buttonCreate.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; buttonCreate.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
buttonCreate.Location = new System.Drawing.Point(0, 424); buttonCreate.Location = new System.Drawing.Point(198, 401);
buttonCreate.Name = "buttonCreate"; buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new System.Drawing.Size(94, 29); buttonCreate.Size = new System.Drawing.Size(180, 40);
buttonCreate.TabIndex = 1; buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true; buttonCreate.UseVisualStyleBackColor = true;
@ -108,11 +111,44 @@ namespace Monorail
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMoveClick; buttonDown.Click += ButtonMoveClick;
// //
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "Довести до центра", "Довести до края" });
comboBoxStrategy.Location = new System.Drawing.Point(719, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new System.Drawing.Size(151, 28);
comboBoxStrategy.TabIndex = 6;
//
// buttonStep
//
buttonStep.Location = new System.Drawing.Point(768, 46);
buttonStep.Name = "buttonStep";
buttonStep.Size = new System.Drawing.Size(94, 29);
buttonStep.TabIndex = 7;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// buttonCreateAdvanced
//
buttonCreateAdvanced.Location = new System.Drawing.Point(12, 401);
buttonCreateAdvanced.Name = "buttonCreateAdvanced";
buttonCreateAdvanced.Size = new System.Drawing.Size(180, 40);
buttonCreateAdvanced.TabIndex = 8;
buttonCreateAdvanced.Text = "Создать продвинутый";
buttonCreateAdvanced.UseVisualStyleBackColor = true;
buttonCreateAdvanced.Click += buttonCreateAdvanced_Click;
//
// MonorailForm // MonorailForm
// //
AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(882, 453); ClientSize = new System.Drawing.Size(882, 453);
Controls.Add(buttonCreateAdvanced);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonRight); Controls.Add(buttonRight);
@ -134,6 +170,9 @@ namespace Monorail
private System.Windows.Forms.Button buttonRight; private System.Windows.Forms.Button buttonRight;
private System.Windows.Forms.Button buttonUp; private System.Windows.Forms.Button buttonUp;
private System.Windows.Forms.Button buttonDown; private System.Windows.Forms.Button buttonDown;
public System.Windows.Forms.ComboBox comboBoxStrategy;
private System.Windows.Forms.Button buttonStep;
private System.Windows.Forms.Button buttonCreateAdvanced;
} }
} }

View File

@ -7,6 +7,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Monorail.DrawningObjects;
using Monorail.MovementStrategy;
namespace Monorail namespace Monorail
{ {
@ -14,6 +16,8 @@ namespace Monorail
{ {
private DrawningMonorail? _drawningMonorail; private DrawningMonorail? _drawningMonorail;
private AbstractStrategy? _abstractStrategy;
public MonorailForm() public MonorailForm()
{ {
InitializeComponent(); InitializeComponent();
@ -33,12 +37,11 @@ namespace Monorail
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreate_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawningMonorail = new DrawningMonorail(); _drawningMonorail = new DrawningMonorail(random.Next(100, 300), random.Next(1000, 3000),
_drawningMonorail.Init(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxMonorail.Width, pictureBoxMonorail.Height, 4,
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)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), random.Next(0,2) > 0, random.Next(0,2) > 0); Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxMonorail.Width, pictureBoxMonorail.Height);
Draw(); Draw();
} }
@ -65,5 +68,53 @@ namespace Monorail
} }
Draw(); Draw();
} }
private void buttonCreateAdvanced_Click(object sender, EventArgs e)
{
Random random = new();
_drawningMonorail = new DrawningAdvancedMonorail(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)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxMonorail.Width, pictureBoxMonorail.Height, random.Next(3, 5),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), random.Next(0,2) > 0,
random.Next(0, 2) > 0);
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawningMonorail == null)
return;
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToEdge(),
_ => null,
} ;
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectMonorail(_drawningMonorail), pictureBoxMonorail.Width,
pictureBoxMonorail.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
} }
} }

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.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();
}
}
}
}
}

View File

@ -0,0 +1,48 @@
using Monorail.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
public class MoveToEdge : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder < FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder < FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - (FieldWidth-1);
if (Math.Abs(diffX) > GetStep())
{
if (diffX < 0)
{
MoveRight();
}
}
var diffY = objParams.DownBorder - (FieldHeight-1);
if (Math.Abs(diffY) > GetStep())
{
if (diffY < 0)
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,32 @@
namespace Monorail.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;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
public enum Status
{
NotInit = 1,
InProgress = 2,
Finish = 3
}
}