PIbd-22. Stroev V.M. Lab Work 02 #3

Closed
StroevVladimir wants to merge 3 commits from Lab02 into Lab1
15 changed files with 712 additions and 272 deletions
Showing only changes of commit 39ab63f97c - Show all commits

View File

@ -1,204 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
public class DrawningMonorail
{
public Entity? Entity { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _relWidth = 150;
private readonly int _relHeight = 46;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor,
bool monorails, bool secondCabin, int width, int height)
{
if (0 + _relWidth >= width || 0 + _relHeight >= height)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
Entity = new Entity();
Entity.Init(speed, weight, bodyColor, additionalColor, monorails, secondCabin);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x > _pictureWidth - _relWidth)
{
x = 0;
}
if (y < 0 || y > _pictureHeight - _relHeight)
{
y = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (Entity == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - Entity.Step > 0)
{
_startPosX -= (int)Entity.Step;
}
break;
case DirectionType.Right:
if (_startPosX + (int)Entity.Step < _pictureWidth - _relWidth)
{
_startPosX += (int)Entity.Step;
}
break;
case DirectionType.Up:
if (_startPosY - Entity.Step > 0)
{
_startPosY -= (int)Entity.Step;
}
break;
case DirectionType.Down:
if (_startPosY + Entity.Step < _pictureHeight - _relHeight)
{
_startPosY += (int)Entity.Step;
}
break;
}
}
public void DrawMonorail(Graphics g)
{
if (Entity == null)
{
return;
}
_relWidth = 150;
Pen pen = new(Color.Black, 2);
Brush additionalBrush = new SolidBrush(Entity.AdditionalColor);
Brush bodyBrush = new SolidBrush(Entity.BodyColor);
//колёса
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 50, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 80, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 110, _startPosY + 35, 15, 15);
g.DrawRectangle(pen, _startPosX + 22, _startPosY + 36, 40, 8);
g.DrawRectangle(pen, _startPosX + 82, _startPosY + 36, 40, 8);
g.DrawEllipse(pen, _startPosX + 3, _startPosY + 37, 30, 8);
g.DrawEllipse(pen, _startPosX + 110, _startPosY + 37, 29, 8);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX + 22, _startPosY + 36, 40, 8);
g.FillRectangle(brBlack, _startPosX + 82, _startPosY + 36, 40, 8);
g.FillEllipse(brBlack, _startPosX + 3, _startPosY + 37, 30, 8);
g.FillEllipse(brBlack, _startPosX + 110, _startPosY + 37, 29, 8);
Brush brWhite = new SolidBrush(Color.White);
g.FillEllipse(brWhite, _startPosX + 20, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 50, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 80, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 110, _startPosY + 35, 15, 15);
//Кабина
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 20, 120, 16);
g.FillRectangle(bodyBrush, _startPosX + 10, _startPosY + 20, 120, 16);
Point p1 = new Point(_startPosX + 10, _startPosY + 20);
Point p2 = new Point(_startPosX + 130, _startPosY + 20);
Point p3 = new Point(_startPosX + 130, _startPosY + 5);
Point p4 = new Point(_startPosX + 13, _startPosY + 5);
g.DrawPolygon(pen, new Point[] { p1, p2, p3, p4 });
g.FillPolygon(bodyBrush, new Point[] { p1, p2, p3, p4 });
//дверь
g.FillRectangle(brWhite, _startPosX + 49, _startPosY + 9, 12, 22);
g.DrawRectangle(pen, _startPosX + 49, _startPosY + 9, 12, 22);
//Окна и прочее
Pen penBlue = new Pen(Color.Cyan, 2);
g.DrawRectangle(penBlue, _startPosX + 20, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 35, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 117, _startPosY + 8, 10, 10);
g.DrawRectangle(pen, _startPosX + 132, _startPosY + 10, 2, 22);
//Магнитная рельса
if (Entity.Monorails == true)
{
g.DrawRectangle(pen, _startPosX, _startPosY + 50, 140, 10);
g.FillRectangle(brBlack, _startPosX, _startPosY + 50, 140, 10);
g.FillRectangle(brWhite, _startPosX + 5, _startPosY + 53, 130, 5);
}
if (Entity.SecondCabin == true)
{
_relWidth = 290;
//низ
g.DrawEllipse(pen, _startPosX + 160, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 190, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 220, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 250, _startPosY + 35, 15, 15);
g.DrawRectangle(pen, _startPosX + 162, _startPosY + 36, 40, 8);
g.DrawRectangle(pen, _startPosX + 222, _startPosY + 36, 40, 8);
g.DrawEllipse(pen, _startPosX + 143, _startPosY + 37, 30, 8);
g.DrawEllipse(pen, _startPosX + 250, _startPosY + 37, 29, 8);
g.FillRectangle(brBlack, _startPosX + 162, _startPosY + 36, 40, 8);
g.FillRectangle(brBlack, _startPosX + 222, _startPosY + 36, 40, 8);
g.FillEllipse(brBlack, _startPosX + 143, _startPosY + 37, 30, 8);
g.FillEllipse(brBlack, _startPosX + 250, _startPosY + 37, 29, 8);
g.FillEllipse(brWhite, _startPosX + 160, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 190, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 220, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 250, _startPosY + 35, 15, 15);
if (Entity.Monorails == true)
{
g.DrawRectangle(pen, _startPosX + 140, _startPosY + 50, 145, 10);
g.FillRectangle(brBlack, _startPosX + 140, _startPosY + 50, 145, 10);
g.FillRectangle(brWhite, _startPosX + 135, _startPosY + 53, 145, 5);
}
//Кабина
g.DrawRectangle(pen, _startPosX + 150, _startPosY + 20, 120, 16);
g.FillRectangle(additionalBrush, _startPosX + 150, _startPosY + 20, 120, 16);
Point p5 = new Point(_startPosX + 150, _startPosY + 20);
Point p6 = new Point(_startPosX + 270, _startPosY + 20);
Point p7 = new Point(_startPosX + 267, _startPosY + 5);
Point p8 = new Point(_startPosX + 150, _startPosY + 5);
g.DrawPolygon(pen, new Point[] { p5, p6, p7, p8 });
g.FillPolygon(additionalBrush, new Point[] { p5, p6, p7, p8 });
//дверь
g.FillRectangle(brWhite, _startPosX + 189, _startPosY + 9, 12, 22);
g.DrawRectangle(pen, _startPosX + 189, _startPosY + 9, 12, 22);
//Окна и прочее
g.DrawRectangle(penBlue, _startPosX + 160, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 175, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 257, _startPosY + 8, 10, 10);
g.DrawRectangle(pen, _startPosX + 272, _startPosY + 10, 2, 22);
}
}
}
}

View File

@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Monorail.Entities;
namespace Monorail.DrawningObjects
{
public class DrawningMonorail
{
public EntityMonorail? EntityMonorail { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _relWidth = 150;
protected readonly int _relHeight = 46;
public DrawningMonorail(int speed, double weight, Color bodyColor, int width, int height)
{
if (_relWidth >= width || _relHeight >= height)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityMonorail = new EntityMonorail(speed, weight, bodyColor);
}
protected DrawningMonorail(int speed, double weight, Color bodyColor, int width,
int height, int carWidth, int carHeight)
{
if (_relWidth >= width || _relHeight >= height)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_relWidth = carWidth;
_relHeight = carHeight;
EntityMonorail = new EntityMonorail(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x > _pictureWidth - _relWidth)
{
x = 0;
}
if (y < 0 || y > _pictureHeight - _relHeight)
{
y = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityMonorail == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
_startPosX -= (int)EntityMonorail.Step;
break;
case DirectionType.Right:
_startPosX += (int)EntityMonorail.Step;
break;
case DirectionType.Up:
_startPosY -= (int)EntityMonorail.Step;
break;
case DirectionType.Down:
_startPosY += (int)EntityMonorail.Step;
break;
}
}
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _relWidth;
public int GetHeight => _relHeight;
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 - _relWidth,
DirectionType.Down => _startPosY + EntityMonorail.Step < _pictureHeight - _relHeight
};
}
public virtual void DrawTransport(Graphics g)
{
if (EntityMonorail == null)
{
return;
}
_relWidth = 150;
Pen pen = new(Color.Black, 2);
Brush bodyBrush = new SolidBrush(EntityMonorail.BodyColor);
//колёса
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 50, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 80, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 110, _startPosY + 35, 15, 15);
g.DrawRectangle(pen, _startPosX + 22, _startPosY + 36, 40, 8);
g.DrawRectangle(pen, _startPosX + 82, _startPosY + 36, 40, 8);
g.DrawEllipse(pen, _startPosX + 3, _startPosY + 37, 30, 8);
g.DrawEllipse(pen, _startPosX + 110, _startPosY + 37, 29, 8);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX + 22, _startPosY + 36, 40, 8);
g.FillRectangle(brBlack, _startPosX + 82, _startPosY + 36, 40, 8);
g.FillEllipse(brBlack, _startPosX + 3, _startPosY + 37, 30, 8);
g.FillEllipse(brBlack, _startPosX + 110, _startPosY + 37, 29, 8);
Brush brWhite = new SolidBrush(Color.White);
g.FillEllipse(brWhite, _startPosX + 20, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 50, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 80, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 110, _startPosY + 35, 15, 15);
//Кабина
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 20, 120, 16);
g.FillRectangle(bodyBrush, _startPosX + 10, _startPosY + 20, 120, 16);
Point p1 = new Point(_startPosX + 10, _startPosY + 20);
Point p2 = new Point(_startPosX + 130, _startPosY + 20);
Point p3 = new Point(_startPosX + 130, _startPosY + 5);
Point p4 = new Point(_startPosX + 13, _startPosY + 5);
g.DrawPolygon(pen, new Point[] { p1, p2, p3, p4 });
g.FillPolygon(bodyBrush, new Point[] { p1, p2, p3, p4 });
//дверь
g.FillRectangle(brWhite, _startPosX + 49, _startPosY + 9, 12, 22);
g.DrawRectangle(pen, _startPosX + 49, _startPosY + 9, 12, 22);
//Окна и прочее
Pen penBlue = new Pen(Color.Cyan, 2);
g.DrawRectangle(penBlue, _startPosX + 20, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 35, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 117, _startPosY + 8, 10, 10);
g.DrawRectangle(pen, _startPosX + 132, _startPosY + 10, 2, 22);
}
}
}

View File

@ -0,0 +1,100 @@
using Monorail.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.DrawningObjects
{
public class DrawningSecondMonorail : DrawningMonorail
{
public DrawningSecondMonorail(int speed, double weight, Color bodyColor, Color
additionalColor, bool monorails, bool secondCabin, int width, int height) :
base(speed, weight, bodyColor, width, height, 110, 60)
{
if (EntityMonorail != null)
{
EntityMonorail = new EntitySecondMonorail(speed, weight, bodyColor,
additionalColor, monorails, secondCabin);
}
}
public override void DrawTransport(Graphics g)
{
base.DrawTransport(g);
if (EntityMonorail is not EntitySecondMonorail secondMonorail)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(secondMonorail.AdditionalColor);
Brush brBlack = new SolidBrush(Color.Black);
Brush brWhite = new SolidBrush(Color.White);
Pen penBlue = new Pen(Color.Cyan, 2);
if (secondMonorail.Monorails)
{
g.DrawRectangle(pen, _startPosX, _startPosY + 50, 140, 10);
g.FillRectangle(brBlack, _startPosX, _startPosY + 50, 140, 10);
g.FillRectangle(brWhite, _startPosX + 5, _startPosY + 53, 130, 5);
}
if (secondMonorail.SecondCabin)
{
_relWidth = 290;
//низ
g.DrawEllipse(pen, _startPosX + 160, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 190, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 220, _startPosY + 35, 15, 15);
g.DrawEllipse(pen, _startPosX + 250, _startPosY + 35, 15, 15);
g.DrawRectangle(pen, _startPosX + 162, _startPosY + 36, 40, 8);
g.DrawRectangle(pen, _startPosX + 222, _startPosY + 36, 40, 8);
g.DrawEllipse(pen, _startPosX + 143, _startPosY + 37, 30, 8);
g.DrawEllipse(pen, _startPosX + 250, _startPosY + 37, 29, 8);
g.FillRectangle(brBlack, _startPosX + 162, _startPosY + 36, 40, 8);
g.FillRectangle(brBlack, _startPosX + 222, _startPosY + 36, 40, 8);
g.FillEllipse(brBlack, _startPosX + 143, _startPosY + 37, 30, 8);
g.FillEllipse(brBlack, _startPosX + 250, _startPosY + 37, 29, 8);
g.FillEllipse(brWhite, _startPosX + 160, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 190, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 220, _startPosY + 35, 15, 15);
g.FillEllipse(brWhite, _startPosX + 250, _startPosY + 35, 15, 15);
if (secondMonorail.Monorails)
{
g.DrawRectangle(pen, _startPosX + 140, _startPosY + 50, 145, 10);
g.FillRectangle(brBlack, _startPosX + 140, _startPosY + 50, 145, 10);
g.FillRectangle(brWhite, _startPosX + 135, _startPosY + 53, 145, 5);
}
//Кабина
g.DrawRectangle(pen, _startPosX + 150, _startPosY + 20, 120, 16);
g.FillRectangle(additionalBrush, _startPosX + 150, _startPosY + 20, 120, 16);
Point p5 = new Point(_startPosX + 150, _startPosY + 20);
Point p6 = new Point(_startPosX + 270, _startPosY + 20);
Point p7 = new Point(_startPosX + 267, _startPosY + 5);
Point p8 = new Point(_startPosX + 150, _startPosY + 5);
g.DrawPolygon(pen, new Point[] { p5, p6, p7, p8 });
g.FillPolygon(additionalBrush, new Point[] { p5, p6, p7, p8 });
//дверь
g.FillRectangle(brWhite, _startPosX + 189, _startPosY + 9, 12, 22);
g.DrawRectangle(pen, _startPosX + 189, _startPosY + 9, 12, 22);
//Окна и прочее
g.DrawRectangle(penBlue, _startPosX + 160, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 175, _startPosY + 8, 10, 10);
g.DrawRectangle(penBlue, _startPosX + 257, _startPosY + 8, 10, 10);
g.DrawRectangle(pen, _startPosX + 272, _startPosY + 10, 2, 22);
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.Entities
{
public class EntityMonorail
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 130 / Weight;
public EntityMonorail(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.Entities
{
public class EntitySecondMonorail : EntityMonorail
{
public Color AdditionalColor { get; private set; }
public bool Monorails { get; private set; }
public bool SecondCabin { get; private set; }
public EntitySecondMonorail(int speed, double weight, Color bodyColor,
Color additionalColor, bool monorails, bool secondCabin) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Monorails = monorails;
SecondCabin = secondCabin;
}
}
}

View File

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
public class Entity
{
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 Monorails { get; private set; }
public bool SecondCabin { get; private set; }
public double Step => (double)Speed * 130 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
bool monorails, bool secondCabin)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Monorails = monorails;
SecondCabin = secondCabin;
}
}
}

View File

@ -28,24 +28,27 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBox1 = new PictureBox(); pictureBoxMonorail = new PictureBox();
buttonUp = new Button(); buttonUp = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonCreate = new Button(); ButtonCreateMonorail = new Button();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); ButtonCreateSecondMonorail = new Button();
buttonStep = new Button();
comboBoxStrategy = new ComboBox();
((System.ComponentModel.ISupportInitialize)pictureBoxMonorail).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// pictureBox1 // pictureBoxMonorail
// //
pictureBox1.Dock = DockStyle.Fill; pictureBoxMonorail.Dock = DockStyle.Fill;
pictureBox1.Location = new Point(0, 0); pictureBoxMonorail.Location = new Point(0, 0);
pictureBox1.Name = "pictureBox1"; pictureBoxMonorail.Name = "pictureBoxMonorail";
pictureBox1.Size = new Size(882, 453); pictureBoxMonorail.Size = new Size(882, 453);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxMonorail.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.TabIndex = 1; pictureBoxMonorail.TabIndex = 1;
pictureBox1.TabStop = false; pictureBoxMonorail.TabStop = false;
// //
// buttonUp // buttonUp
// //
@ -99,17 +102,49 @@
buttonRight.UseVisualStyleBackColor = false; buttonRight.UseVisualStyleBackColor = false;
buttonRight.Click += buttonMove_Click; buttonRight.Click += buttonMove_Click;
// //
// buttonCreate // ButtonCreateMonorail
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; ButtonCreateMonorail.BackColor = SystemColors.Info;
buttonCreate.BackColor = SystemColors.Info; ButtonCreateMonorail.Location = new Point(9, 400);
buttonCreate.Location = new Point(12, 412); ButtonCreateMonorail.Name = "ButtonCreateMonorail";
buttonCreate.Name = "buttonCreate"; ButtonCreateMonorail.Size = new Size(175, 41);
buttonCreate.Size = new Size(77, 29); ButtonCreateMonorail.TabIndex = 22;
buttonCreate.TabIndex = 7; ButtonCreateMonorail.Text = "Создать Монорельс";
buttonCreate.Text = "Создать"; ButtonCreateMonorail.UseVisualStyleBackColor = false;
buttonCreate.UseVisualStyleBackColor = false; ButtonCreateMonorail.Click += ButtonCreateMonorail_Click;
buttonCreate.Click += buttonCreate_Click; //
// ButtonCreateSecondMonorail
//
ButtonCreateSecondMonorail.BackColor = SystemColors.Info;
ButtonCreateSecondMonorail.Location = new Point(202, 400);
ButtonCreateSecondMonorail.Name = "ButtonCreateSecondMonorail";
ButtonCreateSecondMonorail.Size = new Size(239, 41);
ButtonCreateSecondMonorail.TabIndex = 21;
ButtonCreateSecondMonorail.Text = "Создать два Монорельса";
ButtonCreateSecondMonorail.UseVisualStyleBackColor = false;
ButtonCreateSecondMonorail.Click += ButtonCreateSecondMonorail_Click;
//
// buttonStep
//
buttonStep.BackColor = SystemColors.Info;
buttonStep.Location = new Point(782, 43);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(94, 29);
buttonStep.TabIndex = 20;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = false;
buttonStep.Click += ButtonStep_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.BackColor = SystemColors.Info;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "0", "1" });
comboBoxStrategy.Location = new Point(725, 9);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 19;
// //
// MonoRail // MonoRail
// //
@ -117,27 +152,33 @@
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ControlDarkDark; BackColor = SystemColors.ControlDarkDark;
ClientSize = new Size(882, 453); ClientSize = new Size(882, 453);
Controls.Add(ButtonCreateMonorail);
Controls.Add(ButtonCreateSecondMonorail);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonCreate);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(pictureBox1); Controls.Add(pictureBoxMonorail);
Name = "MonoRail"; Name = "MonoRail";
StartPosition = FormStartPosition.CenterScreen; StartPosition = FormStartPosition.CenterScreen;
Text = "Monorail"; Text = "Monorail";
((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxMonorail).EndInit();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }
#endregion #endregion
private PictureBox pictureBox1; private PictureBox pictureBoxMonorail;
private Button buttonUp; private Button buttonUp;
private Button buttonLeft; private Button buttonLeft;
private Button buttonDown; private Button buttonDown;
private Button buttonRight; private Button buttonRight;
private Button buttonCreate; private Button ButtonCreateMonorail;
private Button ButtonCreateSecondMonorail;
private Button buttonStep;
private ComboBox comboBoxStrategy;
} }
} }

View File

@ -1,9 +1,13 @@
using Monorail.DrawningObjects;
using Monorail.MovementStrategy;
namespace Monorail namespace Monorail
{ {
public partial class MonoRail : Form public partial class MonoRail : Form
{ {
private DrawningMonorail? _drawningMonorail; private DrawningMonorail? _drawningMonorail;
private AbstractStrategy? _abstractStrategy;
public MonoRail() public MonoRail()
{ {
@ -15,24 +19,36 @@ namespace Monorail
{ {
return; return;
} }
Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height); Bitmap bmp = new(pictureBoxMonorail.Width,
pictureBoxMonorail.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningMonorail.DrawMonorail(gr); _drawningMonorail.DrawTransport(gr);
pictureBox1.Image = bmp; pictureBoxMonorail.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e) private void ButtonCreateSecondMonorail_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawningMonorail = new DrawningMonorail();
_drawningMonorail.Init(random.Next(500, 800), random.Next(1000, 3000), _drawningMonorail = new DrawningSecondMonorail(random.Next(100, 300),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), 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),
Convert.ToBoolean(random.Next(0, 2)), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
pictureBox1.Width, pictureBox1.Height); random.Next(0, 256)),
_drawningMonorail.SetPosition(random.Next(10, 100), true, true,
random.Next(30, 100)); pictureBoxMonorail.Width, pictureBoxMonorail.Height);
_drawningMonorail.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonCreateMonorail_Click(object sender, EventArgs e)
{
Random random = new();
_drawningMonorail = new DrawningMonorail(random.Next(100, 300),
random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)), pictureBoxMonorail.Width, pictureBoxMonorail.Height);
_drawningMonorail.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
} }
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
@ -59,5 +75,41 @@ namespace Monorail
} }
Draw(); 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 MoveToBorder(),
_ => 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,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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,36 @@
using Monorail.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.MovementStrategy
{
public class MoveToBorder : 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;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,56 @@
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,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}