Сделано

This commit is contained in:
Yunusov_Niyaz 2023-10-12 18:32:33 +04:00
parent 8d300a2aec
commit a1a6743ce1
15 changed files with 646 additions and 192 deletions

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTrolleybus.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

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trolleybus
namespace ProjectTrolleybus
{
public enum DirectionType
{

View File

@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTrolleybus.Entities;
namespace ProjectTrolleybus.DrawingObjects
{
public class DrawingBus
{
public EntityBus? EntityBus { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _busWidth = 170;
protected readonly int _busHeight = 124;
public DrawingBus(int speed, double weight, Color bodyColor, int
width, int height)
{
if (width < _busWidth || height < _busHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
EntityBus = new EntityBus(speed, weight, bodyColor);
}
protected DrawingBus(int speed, double weight, Color bodyColor, int
width, int height, int busWidth, int busHeight)
{
if (width < _busWidth || height < _busHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
_busWidth = busWidth;
_busHeight = busHeight;
EntityBus = new EntityBus(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || y < 0 || x + _busWidth >= _pictureWidth || y + _busHeight >= _pictureHeight)
x = y = 10;
_startPosX = x;
_startPosY = y;
}
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _busWidth;
public int GetHeight => _busHeight;
public bool CanMove(DirectionType direction)
{
if (EntityBus == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityBus.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityBus.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityBus.Step + _busWidth < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityBus.Step + _busHeight < _pictureHeight,
_ => false,
};
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityBus == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
_startPosX -= (int)EntityBus.Step;
break;
//вверх
case DirectionType.Up:
_startPosY -= (int)EntityBus.Step;
break;
// вправо
case DirectionType.Right:
_startPosX += (int)EntityBus.Step;
break;
//вниз
case DirectionType.Down:
_startPosY += (int)EntityBus.Step;
break;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityBus == null)
{
return;
}
Pen pen = new(Color.Black);
//кузов
Brush br = new SolidBrush(EntityBus.BodyColor);
g.FillRectangle(br, _startPosX + 6, _startPosY + 31, 164, 79);
//задние фары
Brush brRed = new SolidBrush(Color.Red);
g.FillRectangle(brRed, _startPosX + 5, _startPosY + 85, 10, 20);
//передние фары
Brush brYellow = new SolidBrush(Color.Yellow);
g.FillRectangle(brYellow, _startPosX + 160, _startPosY + 85, 10, 20);
//стекла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 35, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 95, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 120, _startPosY + 40, 20, 40);
//дверь
Brush brDoor = new SolidBrush(EntityBus.BodyColor);
g.FillRectangle(brDoor, _startPosX + 60, _startPosY + 50, 30, 60);
//колеса
Brush brblack = new SolidBrush(Color.Black);
g.FillEllipse(brblack, _startPosX + 25, _startPosY + 95, 30, 30);
g.FillEllipse(brblack, _startPosX + 120, _startPosY + 95, 30, 30);
//границы троллейбуса
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 30, 165, 80);
g.DrawEllipse(pen, _startPosX + 25, _startPosY + 95, 30, 30);
g.DrawEllipse(pen, _startPosX + 120, _startPosY + 95, 30, 30);
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 85, 10, 20);
g.DrawRectangle(pen, _startPosX + 160, _startPosY + 85, 10, 20);
g.DrawRectangle(pen, _startPosX + 60, _startPosY + 50, 30, 60);
g.DrawRectangle(pen, _startPosX + 150, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 35, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 95, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 120, _startPosY + 40, 20, 40);
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTrolleybus.DrawingObjects;
namespace ProjectTrolleybus.MovementStrategy
{
public class DrawingObjectBus : IMoveableObject
{
private readonly DrawingBus? _drawningCar = null;
public DrawingObjectBus(DrawingBus drawningCar)
{
_drawningCar = drawningCar;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningCar == null || _drawningCar.EntityBus ==
null)
{
return null;
}
return new ObjectParameters(_drawningCar.GetPosX,
_drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight);
}
}
public int GetStep => (int)(_drawningCar?.EntityBus?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningCar?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningCar?.MoveTransport(direction);
}
}

View File

@ -1,163 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using ProjectTrolleybus.DrawingObjects;
using ProjectTrolleybus.Entities;
namespace Trolleybus
namespace ProjectTrolleybus
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawingTrolleybus
public class DrawingTrolleybus : DrawingBus
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTrolleybus? EntityTrolleybus { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
private int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int _startPosY;
/// <summary>
/// Ширина прорисовки автомобиля
/// </summary>
private readonly int _trolleybusWidth = 170;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _trolleybusHeight = 124;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool roga, bool battery, int width, int height)
public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, bool roga, bool battery, int width, int height)
: base(speed, weight, bodyColor, width, height, 170, 124)
{
_pictureWidth = width;
_pictureHeight = height;
if (_trolleybusWidth > _pictureWidth || _trolleybusHeight > _pictureHeight)
return false;
EntityTrolleybus = new EntityTrolleybus();
EntityTrolleybus.Init(speed, weight, bodyColor, additionalColor, roga, battery);
return true;
}
public void SetPosition(int x, int y)
if (EntityBus != null)
{
if (EntityTrolleybus == null)
{
return;
}
if (x < 0 || y < 0 || x + _trolleybusWidth >= _pictureWidth || y + _trolleybusHeight >= _pictureHeight)
x = y = 10;
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntityTrolleybus == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntityTrolleybus.Step > 0)
{
_startPosX -= (int)EntityTrolleybus.Step;
}
break;
case DirectionType.Up:
if (_startPosY - EntityTrolleybus.Step > 0)
{
_startPosY -= (int)EntityTrolleybus.Step;
}
break;
case DirectionType.Right:
if (_startPosX + EntityTrolleybus.Step + _trolleybusWidth < _pictureWidth)
{
_startPosX += (int)EntityTrolleybus.Step;
}
break;
case DirectionType.Down:
if (_startPosY + EntityTrolleybus.Step + _trolleybusHeight < _pictureHeight)
{
_startPosY += (int)EntityTrolleybus.Step;
}
break;
EntityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, roga, battery);
}
}
public void DrawTransport(Graphics g)
public override void DrawTransport(Graphics g)
{
if (EntityTrolleybus == null)
if (EntityBus is not EntityTrolleybus trolleybus)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new
SolidBrush(EntityTrolleybus.AdditionalColor);
//кузов
Brush br = new SolidBrush(EntityTrolleybus.BodyColor);
g.FillRectangle(br, _startPosX + 6, _startPosY + 31, 164, 79);
SolidBrush(trolleybus.AdditionalColor);
base.DrawTransport(g);
//"рога"
if (EntityTrolleybus.Roga)
if (trolleybus.Roga)
{
g.DrawLine(new Pen(Color.Black, 3), _startPosX + 120, _startPosY + 30, _startPosX + 20, _startPosY + 3);
g.DrawLine(new Pen(Color.Black, 3), _startPosX + 140, _startPosY + 30, _startPosX + 40, _startPosY + 3);
g.DrawLine(new Pen(Color.Black, 1), _startPosX + 40, _startPosY + 30, _startPosX + 20, _startPosY + 3);
g.DrawLine(new Pen(Color.Black, 1), _startPosX + 60, _startPosY + 30, _startPosX + 40, _startPosY + 3);
}
//задние фары
Brush brRed = new SolidBrush(Color.Red);
g.FillRectangle(brRed, _startPosX + 5, _startPosY + 85, 10, 20);
//передние фары
Brush brYellow = new SolidBrush(Color.Yellow);
g.FillRectangle(brYellow, _startPosX + 160, _startPosY + 85, 10, 20);
//стекла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 35, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 95, _startPosY + 40, 20, 40);
g.FillEllipse(brBlue, _startPosX + 120, _startPosY + 40, 20, 40);
//дверь
Brush brDoor = new SolidBrush(EntityTrolleybus.BodyColor);
g.FillRectangle(brDoor, _startPosX + 60, _startPosY + 50, 30, 60);
//колеса
Brush brblack = new SolidBrush(Color.Black);
g.FillEllipse(brblack, _startPosX + 25, _startPosY + 95, 30, 30);
g.FillEllipse(brblack, _startPosX + 120, _startPosY + 95, 30, 30);
//Батарея
if(EntityTrolleybus.Battery)
if(trolleybus.Battery)
{
Brush brBattery = new SolidBrush(EntityTrolleybus.AdditionalColor);
Brush brBattery = new SolidBrush(trolleybus.AdditionalColor);
g.FillRectangle(brBattery, _startPosX + 95, _startPosY + 85, 20, 25);
g.DrawLine(new Pen(Color.Yellow, 2), _startPosX + 112, _startPosY + 90, _startPosX + 97, _startPosY + 100);
g.DrawLine(new Pen(Color.Yellow, 2), _startPosX + 97, _startPosY + 100, _startPosX + 112, _startPosY + 100);
g.DrawLine(new Pen(Color.Yellow, 2), _startPosX + 112, _startPosY + 100, _startPosX + 97, _startPosY + 110);
g.DrawRectangle(pen, _startPosX + 95, _startPosY + 85, 20, 25);
}
//границы троллейбуса
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 30, 165, 80);
g.DrawEllipse(pen, _startPosX + 25, _startPosY + 95, 30, 30);
g.DrawEllipse(pen, _startPosX + 120, _startPosY + 95, 30, 30);
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 85, 10, 20);
g.DrawRectangle(pen, _startPosX + 160, _startPosY + 85, 10, 20);
g.DrawRectangle(pen, _startPosX + 60, _startPosY + 50, 30, 60);
g.DrawRectangle(pen, _startPosX + 150, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 35, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 95, _startPosY + 40, 20, 40);
g.DrawEllipse(pen, _startPosX + 120, _startPosY + 40, 20, 40);
}
}
}

View File

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

View File

@ -4,44 +4,20 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trolleybus
namespace ProjectTrolleybus.Entities
{
public class EntityTrolleybus
public class EntityTrolleybus : EntityBus
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличия рогов
/// </summary>
public bool Roga { get; private set; }
/// <summary>
/// Признак (опция) наличия батареи
/// </summary>
public bool Battery { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color
additionalColor, bool roga, bool battery)
: base (speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Roga = roga;
Battery = battery;

View File

@ -1,4 +1,4 @@
namespace Trolleybus
namespace ProjectTrolleybus
{
partial class FormTrolleybus
{
@ -29,14 +29,19 @@
private void InitializeComponent()
{
pictureBoxTrolleybus = new PictureBox();
buttonCreate = new Button();
buttonCreateTrolleybus = new Button();
buttonRight = new Button();
buttonDown = new Button();
buttonLeft = new Button();
buttonUp = new Button();
buttonCreateBus = new Button();
buttonStep = new Button();
comboBoxStrategy = new ComboBox();
((System.ComponentModel.ISupportInitialize)pictureBoxTrolleybus).BeginInit();
SuspendLayout();
//
// pictureBoxTrolleybus
//
pictureBoxTrolleybus.Dock = DockStyle.Fill;
pictureBoxTrolleybus.Location = new Point(0, 0);
pictureBoxTrolleybus.Name = "pictureBoxTrolleybus";
@ -44,18 +49,22 @@
pictureBoxTrolleybus.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxTrolleybus.TabIndex = 0;
pictureBoxTrolleybus.TabStop = false;
// buttonCreate
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 426);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
//
// buttonCreateTrolleybus
//
buttonCreateTrolleybus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTrolleybus.Location = new Point(12, 411);
buttonCreateTrolleybus.Name = "buttonCreateTrolleybus";
buttonCreateTrolleybus.Size = new Size(84, 38);
buttonCreateTrolleybus.TabIndex = 1;
buttonCreateTrolleybus.Text = "Создать троллейбус";
buttonCreateTrolleybus.UseVisualStyleBackColor = true;
buttonCreateTrolleybus.Click += ButtonCreateTrolleybus_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.Right;
buttonRight.BackgroundImage = Trolleybus.Properties.Resources.Right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(842, 419);
buttonRight.Name = "buttonRight";
@ -63,9 +72,11 @@
buttonRight.TabIndex = 2;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonDown
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.Down;
buttonDown.BackgroundImage = Trolleybus.Properties.Resources.Down;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(806, 419);
buttonDown.Name = "buttonDown";
@ -73,9 +84,11 @@
buttonDown.TabIndex = 3;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.Left;
buttonLeft.BackgroundImage = Trolleybus.Properties.Resources.Left;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(770, 419);
buttonLeft.Name = "buttonLeft";
@ -83,9 +96,11 @@
buttonLeft.TabIndex = 4;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.Up;
buttonUp.BackgroundImage = Trolleybus.Properties.Resources.Up;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(806, 383);
buttonUp.Name = "buttonUp";
@ -93,15 +108,50 @@
buttonUp.TabIndex = 5;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonCreateBus
//
buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateBus.Location = new Point(102, 411);
buttonCreateBus.Name = "buttonCreateBus";
buttonCreateBus.Size = new Size(78, 38);
buttonCreateBus.TabIndex = 6;
buttonCreateBus.Text = "Создать автобус";
buttonCreateBus.UseVisualStyleBackColor = true;
buttonCreateBus.Click += ButtonCreateBus_Click;
//
// buttonStep
//
buttonStep.Location = new Point(797, 41);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(75, 23);
buttonStep.TabIndex = 7;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += ButtonStep_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "Движение в центр", "Движение в правый угол" });
comboBoxStrategy.Location = new Point(751, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 8;
//
// FormTrolleybus
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonStep);
Controls.Add(buttonCreateBus);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateTrolleybus);
Controls.Add(pictureBoxTrolleybus);
Name = "FormTrolleybus";
StartPosition = FormStartPosition.CenterScreen;
@ -114,10 +164,13 @@
#endregion
private PictureBox pictureBoxTrolleybus;
private Button buttonCreate;
private Button buttonCreateTrolleybus;
private Button buttonRight;
private Button buttonDown;
private Button buttonLeft;
private Button buttonUp;
private Button buttonCreateBus;
private Button buttonStep;
private ComboBox comboBoxStrategy;
}
}

View File

@ -1,40 +1,52 @@
namespace Trolleybus
using ProjectTrolleybus.MovementStrategy;
using ProjectTrolleybus.DrawingObjects;
namespace ProjectTrolleybus
{
public partial class FormTrolleybus : Form
{
private DrawingTrolleybus? _drawningTrolleybus;
private DrawingBus? _drawingBus;
private AbstractStrategy? _abstractStrategy;
public FormTrolleybus()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningTrolleybus == null)
if (_drawingBus == null)
{
return;
}
Bitmap bmp = new Bitmap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrolleybus.DrawTransport(gr);
_drawingBus.DrawTransport(gr);
pictureBoxTrolleybus.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
private void ButtonCreateTrolleybus_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawningTrolleybus = new DrawingTrolleybus();
_drawningTrolleybus.Init(random.Next(100, 300), random.Next(1000, 3000),
_drawingBus = new DrawingTrolleybus(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)), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
{
_drawningTrolleybus.SetPosition(random.Next(10, 100), random.Next(10, 100));
_drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonCreateBus_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawingBus = new DrawingBus(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
_drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTrolleybus == null)
if (_drawingBus == null)
{
return;
}
@ -42,19 +54,55 @@ namespace Trolleybus
switch (name)
{
case "buttonUp":
_drawningTrolleybus.MoveTransport(DirectionType.Up);
_drawingBus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningTrolleybus.MoveTransport(DirectionType.Down);
_drawingBus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningTrolleybus.MoveTransport(DirectionType.Left);
_drawingBus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningTrolleybus.MoveTransport(DirectionType.Right);
_drawingBus.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawingBus == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawingObjectBus(_drawingBus), pictureBoxTrolleybus.Width,
pictureBoxTrolleybus.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,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using ProjectTrolleybus.Drawings;
namespace ProjectTrolleybus.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(DirectionType direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTrolleybus.MovementStrategy
{
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder + GetStep() >= FieldWidth &&
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())
{
MoveRight();
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTrolleybus.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,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTrolleybus.MovementStrategy
{
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder => _x;
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder => _y;
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -1,4 +1,4 @@
namespace Trolleybus
namespace ProjectTrolleybus
{
internal static class Program
{

View File

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