Готовая 2 лабораторная
This commit is contained in:
parent
2a0e8067f2
commit
5b001c5b2f
@ -1,156 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DumpTruck
|
|
||||||
{
|
|
||||||
public class DrawingDumpTruck
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Класс-сущность
|
|
||||||
/// </summary>
|
|
||||||
public EntityDumpTruck? EntityDumpTruck { 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 _truckWidth = 160;
|
|
||||||
/// <summary>
|
|
||||||
/// Высота прорисовки самосвала
|
|
||||||
/// </summary>
|
|
||||||
private readonly int _truckHeight = 90;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Инициализация свойств
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="speed">Скорость</param>
|
|
||||||
/// <param name="weight">Вес</param>
|
|
||||||
/// <param name="bodyColor">Основной цвет самосвала</param>
|
|
||||||
/// <param name="tent">Признак наличия тента</param>
|
|
||||||
/// <param name="dumpBox">Признак наличия кузова</param>
|
|
||||||
/// <param name="dumpBoxColor">Цвет кузова</param>
|
|
||||||
/// <param name="tentColor">Цвет тента</param>
|
|
||||||
/// <param name="width">Ширина картинки</param>
|
|
||||||
/// <param name="height">Высота картинки</param>
|
|
||||||
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
|
|
||||||
public bool Init(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor, int width, int height)
|
|
||||||
{
|
|
||||||
_pictureWidth = width;
|
|
||||||
_pictureHeight = height;
|
|
||||||
if (_pictureHeight < _truckHeight || _pictureWidth < _truckWidth) return false;
|
|
||||||
EntityDumpTruck = new EntityDumpTruck();
|
|
||||||
EntityDumpTruck.Init(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Установка позиции
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">Координата X</param>
|
|
||||||
/// <param name="y">Координата Y</param>
|
|
||||||
public void SetPosition(int x, int y)
|
|
||||||
{
|
|
||||||
if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; }
|
|
||||||
if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; }
|
|
||||||
_startPosX = x;
|
|
||||||
_startPosY = y;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Изменение направления перемещения
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="direction">Направление</param>
|
|
||||||
public void MoveTransport(Direction direction)
|
|
||||||
{
|
|
||||||
if (EntityDumpTruck == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
//влево
|
|
||||||
case Direction.Left:
|
|
||||||
if (_startPosX - EntityDumpTruck.Step > 0)
|
|
||||||
{
|
|
||||||
_startPosX -= (int)EntityDumpTruck.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
//вверх
|
|
||||||
case Direction.Up:
|
|
||||||
if (_startPosY - EntityDumpTruck.Step > 0)
|
|
||||||
{
|
|
||||||
_startPosY -= (int)EntityDumpTruck.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
// вправо
|
|
||||||
case Direction.Right:
|
|
||||||
if (_startPosX + _truckWidth + EntityDumpTruck.Step < _pictureWidth)
|
|
||||||
{
|
|
||||||
_startPosX += (int)EntityDumpTruck.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
//вниз
|
|
||||||
case Direction.Down:
|
|
||||||
if (_startPosY + _truckHeight + EntityDumpTruck.Step < _pictureHeight)
|
|
||||||
{
|
|
||||||
_startPosY += (int)EntityDumpTruck.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Прорисовка объекта
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="g"></param>
|
|
||||||
public void DrawTransport(Graphics g)
|
|
||||||
{
|
|
||||||
if (EntityDumpTruck == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Brush brush = new SolidBrush(EntityDumpTruck.BodyColor);
|
|
||||||
g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10);
|
|
||||||
g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40);
|
|
||||||
g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40);
|
|
||||||
g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40);
|
|
||||||
g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40);
|
|
||||||
if (EntityDumpTruck.DumpBox)
|
|
||||||
{
|
|
||||||
Brush brDumpBox = new SolidBrush(EntityDumpTruck.DumpBoxColor);
|
|
||||||
Point point1 = new Point(_startPosX + 20, _startPosY);
|
|
||||||
Point point2 = new Point(_startPosX + 120, _startPosY);
|
|
||||||
Point point3 = new Point(_startPosX + 100, _startPosY + 39);
|
|
||||||
Point point4 = new Point(_startPosX, _startPosY + 39);
|
|
||||||
Point[] dumpBoxPoints = { point1, point2, point3, point4};
|
|
||||||
g.FillPolygon(brDumpBox, dumpBoxPoints);
|
|
||||||
}
|
|
||||||
if (EntityDumpTruck.DumpBox && EntityDumpTruck.Tent)
|
|
||||||
{
|
|
||||||
Brush brTent = new SolidBrush(EntityDumpTruck.TentColor);
|
|
||||||
Point point1 = new Point(_startPosX + 15, _startPosY);
|
|
||||||
Point point2 = new Point(_startPosX + 120, _startPosY);
|
|
||||||
Point point3 = new Point(_startPosX + 115, _startPosY + 10);
|
|
||||||
Point point4 = new Point(_startPosX + 10, _startPosY + 10);
|
|
||||||
Point[] tentPoints = { point1, point2, point3, point4 };
|
|
||||||
g.FillPolygon(brTent, tentPoints);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
53
DumpTruck/DrawingObjects/DrawingDumpTruck.cs
Normal file
53
DumpTruck/DrawingObjects/DrawingDumpTruck.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DumpTruck.Entities;
|
||||||
|
|
||||||
|
|
||||||
|
namespace DumpTruck.DrawingObjects
|
||||||
|
{
|
||||||
|
public class DrawingDumpTruck : DrawingTruck
|
||||||
|
{
|
||||||
|
public DrawingDumpTruck(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor, int width, int height)
|
||||||
|
: base(speed, weight, bodyColor, width, height, 160, 90)
|
||||||
|
{
|
||||||
|
if (EntityTruck != null)
|
||||||
|
{
|
||||||
|
EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public override void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (EntityTruck is not EntityDumpTruck dumpTruck)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
base.DrawTransport(g);
|
||||||
|
|
||||||
|
if (dumpTruck.DumpBox)
|
||||||
|
{
|
||||||
|
Brush brDumpBox = new SolidBrush(dumpTruck.DumpBoxColor);
|
||||||
|
Point point1 = new Point(_startPosX + 20, _startPosY);
|
||||||
|
Point point2 = new Point(_startPosX + 120, _startPosY);
|
||||||
|
Point point3 = new Point(_startPosX + 100, _startPosY + 39);
|
||||||
|
Point point4 = new Point(_startPosX, _startPosY + 39);
|
||||||
|
Point[] dumpBoxPoints = { point1, point2, point3, point4 };
|
||||||
|
g.FillPolygon(brDumpBox, dumpBoxPoints);
|
||||||
|
}
|
||||||
|
if (dumpTruck.DumpBox && dumpTruck.Tent)
|
||||||
|
{
|
||||||
|
Brush brTent = new SolidBrush(dumpTruck.TentColor);
|
||||||
|
Point point1 = new Point(_startPosX + 15, _startPosY);
|
||||||
|
Point point2 = new Point(_startPosX + 120, _startPosY);
|
||||||
|
Point point3 = new Point(_startPosX + 115, _startPosY + 10);
|
||||||
|
Point point4 = new Point(_startPosX + 10, _startPosY + 10);
|
||||||
|
Point[] tentPoints = { point1, point2, point3, point4 };
|
||||||
|
g.FillPolygon(brTent, tentPoints);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
190
DumpTruck/DrawingObjects/DrawingTruck.cs
Normal file
190
DumpTruck/DrawingObjects/DrawingTruck.cs
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DumpTruck.Entities;
|
||||||
|
|
||||||
|
namespace DumpTruck.DrawingObjects
|
||||||
|
{
|
||||||
|
public class DrawingTruck
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-сущность
|
||||||
|
/// </summary>
|
||||||
|
public EntityTruck? EntityTruck { get; protected set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина окна
|
||||||
|
/// </summary>
|
||||||
|
private int _pictureWidth;
|
||||||
|
/// <summary>
|
||||||
|
/// Высота окна
|
||||||
|
/// </summary>
|
||||||
|
private int _pictureHeight;
|
||||||
|
/// <summary>
|
||||||
|
/// Левая координата прорисовки грузовика
|
||||||
|
/// </summary>
|
||||||
|
protected int _startPosX;
|
||||||
|
/// <summary>
|
||||||
|
/// Верхняя кооридната прорисовки грузовика
|
||||||
|
/// </summary>
|
||||||
|
protected int _startPosY;
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина прорисовки грузовика
|
||||||
|
/// </summary>
|
||||||
|
protected readonly int _truckWidth = 160;
|
||||||
|
/// <summary>
|
||||||
|
/// Высота прорисовки грузовика
|
||||||
|
/// </summary>
|
||||||
|
protected readonly int _truckHeight = 90;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Координата X объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetPosX => _startPosX;
|
||||||
|
/// <summary>
|
||||||
|
/// Координата Y объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetPosY => _startPosY;
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetWidth => _truckWidth;
|
||||||
|
/// <summary>
|
||||||
|
/// Высота объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetHeight => _truckHeight;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
/// <param name="width">Ширина картинки</param>
|
||||||
|
/// <param name="height">Высота картинки</param>
|
||||||
|
public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height)
|
||||||
|
{
|
||||||
|
if (width < _truckWidth || height < _truckHeight) return;
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
EntityTruck = new EntityTruck(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
/// <param name="width">Ширина картинки</param>
|
||||||
|
/// <param name="height">Высота картинки</param>
|
||||||
|
/// <param name="truckWidth">Ширина прорисовки грузовика</param>
|
||||||
|
/// <param name="truckWidth">Высота прорисовки грузовика</param>
|
||||||
|
protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight)
|
||||||
|
{
|
||||||
|
if (width < truckWidth || height < truckHeight) return;
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
_truckWidth = truckWidth;
|
||||||
|
_truckHeight = truckHeight;
|
||||||
|
EntityTruck = new EntityTruck(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Установка позиции
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">Координата X</param>
|
||||||
|
/// <param name="y">Координата Y</param>
|
||||||
|
public void SetPosition(int x, int y)
|
||||||
|
{
|
||||||
|
if (x < 0 || x + _truckWidth > _pictureWidth) x = 0;
|
||||||
|
if (y < 0 || y + _truckHeight > _pictureHeight) y = 0;
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Проверка, что объект может переместится по указанному направлению
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Направление</param>
|
||||||
|
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||||
|
public bool CanMove(Direction direction)
|
||||||
|
{
|
||||||
|
if (EntityTruck == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return direction switch
|
||||||
|
{
|
||||||
|
//влево
|
||||||
|
Direction.Left => _startPosX - EntityTruck.Step > 0,
|
||||||
|
//вверх
|
||||||
|
Direction.Up => _startPosY - EntityTruck.Step > 0,
|
||||||
|
// вправо
|
||||||
|
Direction.Right => _startPosX + _truckWidth + EntityTruck.Step < _pictureWidth,
|
||||||
|
//вниз
|
||||||
|
Direction.Down => _startPosY + _truckHeight + EntityTruck.Step < _pictureHeight,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Изменение направления перемещения
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Направление</param>
|
||||||
|
public void MoveTransport(Direction direction)
|
||||||
|
{
|
||||||
|
if (!CanMove(direction) || EntityTruck == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
//влево
|
||||||
|
case Direction.Left:
|
||||||
|
if (_startPosX - EntityTruck.Step > 0)
|
||||||
|
{
|
||||||
|
_startPosX -= (int)EntityTruck.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
//вверх
|
||||||
|
case Direction.Up:
|
||||||
|
if (_startPosY - EntityTruck.Step > 0)
|
||||||
|
{
|
||||||
|
_startPosY -= (int)EntityTruck.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// вправо
|
||||||
|
case Direction.Right:
|
||||||
|
if (_startPosX + _truckWidth + EntityTruck.Step < _pictureWidth)
|
||||||
|
{
|
||||||
|
_startPosX += (int)EntityTruck.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
//вниз
|
||||||
|
case Direction.Down:
|
||||||
|
if (_startPosY + _truckHeight + EntityTruck.Step < _pictureHeight)
|
||||||
|
{
|
||||||
|
_startPosY += (int)EntityTruck.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Прорисовка объекта
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
public virtual void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (EntityTruck == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Brush brush = new SolidBrush(EntityTruck.BodyColor);
|
||||||
|
g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10);
|
||||||
|
g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40);
|
||||||
|
g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40);
|
||||||
|
g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40);
|
||||||
|
g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
46
DumpTruck/Entities/EntityDumpTruck.cs
Normal file
46
DumpTruck/Entities/EntityDumpTruck.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.Entities
|
||||||
|
{
|
||||||
|
public class EntityDumpTruck : EntityTruck
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Признак (опция) наличия тента
|
||||||
|
/// </summary>
|
||||||
|
public bool Tent { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Признак (опция) наличия кузова
|
||||||
|
/// </summary>
|
||||||
|
public bool DumpBox { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Цвет кузова
|
||||||
|
/// </summary>
|
||||||
|
public Color DumpBoxColor { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Цвет тента
|
||||||
|
/// </summary>
|
||||||
|
public Color TentColor { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес самосвала</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет самосвала</param>
|
||||||
|
/// <param name="tent">Признак наличия тента</param>
|
||||||
|
/// <param name="dumpBox">Признак наличия кузова</param>
|
||||||
|
/// <param name="dumpBoxColor">Цвет кузова</param>
|
||||||
|
/// <param name="tentColor">Цвет тента</param>
|
||||||
|
|
||||||
|
public EntityDumpTruck(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor) : base(speed, weight, bodyColor)
|
||||||
|
{
|
||||||
|
Tent = tent;
|
||||||
|
DumpBox = dumpBox;
|
||||||
|
DumpBoxColor = dumpBoxColor;
|
||||||
|
TentColor = tentColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
41
DumpTruck/Entities/EntityTruck.cs
Normal file
41
DumpTruck/Entities/EntityTruck.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.Entities
|
||||||
|
{
|
||||||
|
public class EntityTruck
|
||||||
|
{
|
||||||
|
/// <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 double Step => (double)Speed * 100 / Weight;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес грузовика</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет грузовика</param>
|
||||||
|
public EntityTruck(int speed, double weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
Speed = speed;
|
||||||
|
Weight = weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,66 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.NetworkInformation;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DumpTruck
|
|
||||||
{
|
|
||||||
public class EntityDumpTruck
|
|
||||||
{
|
|
||||||
/// <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 bool Tent { get; private set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Признак (опция) наличия кузова
|
|
||||||
/// </summary>
|
|
||||||
public bool DumpBox { get; private set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Цвет кузова
|
|
||||||
/// </summary>
|
|
||||||
public Color DumpBoxColor { get; private set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Цвет тента
|
|
||||||
/// </summary>
|
|
||||||
public Color TentColor { get; private set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Шаг
|
|
||||||
/// </summary>
|
|
||||||
public double Step => (double)Speed * 100 / Weight;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Инициализация полей объекта-класса самосвала
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="speed">Скорость</param>
|
|
||||||
/// <param name="weight">Вес самосвала</param>
|
|
||||||
/// <param name="bodyColor">Основной цвет самосвала</param>
|
|
||||||
/// <param name="tent">Признак наличия тента</param>
|
|
||||||
/// <param name="dumpBox">Признак наличия кузова</param>
|
|
||||||
/// <param name="dumpBoxColor">Цвет кузова</param>
|
|
||||||
/// <param name="tentColor">Цвет тента</param>
|
|
||||||
public void Init(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor)
|
|
||||||
{
|
|
||||||
Speed = speed;
|
|
||||||
Weight = weight;
|
|
||||||
BodyColor = bodyColor;
|
|
||||||
Tent = tent;
|
|
||||||
DumpBox = dumpBox;
|
|
||||||
DumpBoxColor = dumpBoxColor;
|
|
||||||
TentColor = tentColor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
191
DumpTruck/FormDumpTruck.Designer.cs
generated
191
DumpTruck/FormDumpTruck.Designer.cs
generated
@ -28,107 +28,152 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
pictureBoxDumpTruck = new PictureBox();
|
this.pictureBoxDumpTruck = new System.Windows.Forms.PictureBox();
|
||||||
Create = new Button();
|
this.ButtonCreateDumpTruck = new System.Windows.Forms.Button();
|
||||||
buttonLeft = new Button();
|
this.buttonLeft = new System.Windows.Forms.Button();
|
||||||
buttonUp = new Button();
|
this.buttonUp = new System.Windows.Forms.Button();
|
||||||
buttonRight = new Button();
|
this.buttonRight = new System.Windows.Forms.Button();
|
||||||
buttonDown = new Button();
|
this.buttonDown = new System.Windows.Forms.Button();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit();
|
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
|
||||||
SuspendLayout();
|
this.ButtonCreateTruck = new System.Windows.Forms.Button();
|
||||||
|
this.ButtonStep = new System.Windows.Forms.Button();
|
||||||
|
((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).BeginInit();
|
||||||
|
this.SuspendLayout();
|
||||||
//
|
//
|
||||||
// pictureBoxDumpTruck
|
// pictureBoxDumpTruck
|
||||||
//
|
//
|
||||||
pictureBoxDumpTruck.Dock = DockStyle.Fill;
|
this.pictureBoxDumpTruck.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||||
pictureBoxDumpTruck.Location = new Point(0, 0);
|
this.pictureBoxDumpTruck.Location = new System.Drawing.Point(0, 0);
|
||||||
pictureBoxDumpTruck.Name = "pictureBoxDumpTruck";
|
this.pictureBoxDumpTruck.Name = "pictureBoxDumpTruck";
|
||||||
pictureBoxDumpTruck.Size = new Size(800, 450);
|
this.pictureBoxDumpTruck.Size = new System.Drawing.Size(800, 450);
|
||||||
pictureBoxDumpTruck.TabIndex = 0;
|
this.pictureBoxDumpTruck.TabIndex = 0;
|
||||||
pictureBoxDumpTruck.TabStop = false;
|
this.pictureBoxDumpTruck.TabStop = false;
|
||||||
//
|
//
|
||||||
// Create
|
// ButtonCreateDumpTruck
|
||||||
//
|
//
|
||||||
Create.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
this.ButtonCreateDumpTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||||
Create.Location = new Point(12, 415);
|
this.ButtonCreateDumpTruck.Location = new System.Drawing.Point(12, 408);
|
||||||
Create.Name = "Create";
|
this.ButtonCreateDumpTruck.Name = "ButtonCreateDumpTruck";
|
||||||
Create.Size = new Size(75, 23);
|
this.ButtonCreateDumpTruck.Size = new System.Drawing.Size(130, 30);
|
||||||
Create.TabIndex = 1;
|
this.ButtonCreateDumpTruck.TabIndex = 1;
|
||||||
Create.Text = "Создать";
|
this.ButtonCreateDumpTruck.Text = "Создать самосвал";
|
||||||
Create.UseVisualStyleBackColor = true;
|
this.ButtonCreateDumpTruck.UseVisualStyleBackColor = true;
|
||||||
Create.Click += Create_Click;
|
this.ButtonCreateDumpTruck.Click += new System.EventHandler(this.ButtonCreateDumpTruck_Click);
|
||||||
//
|
//
|
||||||
// buttonLeft
|
// buttonLeft
|
||||||
//
|
//
|
||||||
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
buttonLeft.BackgroundImage = Properties.Resources.left;
|
this.buttonLeft.BackgroundImage = global::DumpTruck.Properties.Resources.left;
|
||||||
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
|
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||||
buttonLeft.Location = new Point(686, 408);
|
this.buttonLeft.Location = new System.Drawing.Point(686, 408);
|
||||||
buttonLeft.Name = "buttonLeft";
|
this.buttonLeft.Name = "buttonLeft";
|
||||||
buttonLeft.Size = new Size(30, 30);
|
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
|
||||||
buttonLeft.TabIndex = 2;
|
this.buttonLeft.TabIndex = 2;
|
||||||
buttonLeft.UseVisualStyleBackColor = true;
|
this.buttonLeft.UseVisualStyleBackColor = true;
|
||||||
buttonLeft.Click += ButtonMove_Click;
|
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
|
||||||
//
|
//
|
||||||
// buttonUp
|
// buttonUp
|
||||||
//
|
//
|
||||||
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
buttonUp.BackgroundImage = Properties.Resources.up;
|
this.buttonUp.BackgroundImage = global::DumpTruck.Properties.Resources.up;
|
||||||
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
|
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||||
buttonUp.Location = new Point(722, 372);
|
this.buttonUp.Location = new System.Drawing.Point(722, 372);
|
||||||
buttonUp.Name = "buttonUp";
|
this.buttonUp.Name = "buttonUp";
|
||||||
buttonUp.Size = new Size(30, 30);
|
this.buttonUp.Size = new System.Drawing.Size(30, 30);
|
||||||
buttonUp.TabIndex = 3;
|
this.buttonUp.TabIndex = 3;
|
||||||
buttonUp.UseVisualStyleBackColor = true;
|
this.buttonUp.UseVisualStyleBackColor = true;
|
||||||
buttonUp.Click += ButtonMove_Click;
|
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
|
||||||
//
|
//
|
||||||
// buttonRight
|
// buttonRight
|
||||||
//
|
//
|
||||||
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
buttonRight.BackgroundImage = Properties.Resources.right;
|
this.buttonRight.BackgroundImage = global::DumpTruck.Properties.Resources.right;
|
||||||
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
|
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||||
buttonRight.Location = new Point(758, 408);
|
this.buttonRight.Location = new System.Drawing.Point(758, 408);
|
||||||
buttonRight.Name = "buttonRight";
|
this.buttonRight.Name = "buttonRight";
|
||||||
buttonRight.Size = new Size(30, 30);
|
this.buttonRight.Size = new System.Drawing.Size(30, 30);
|
||||||
buttonRight.TabIndex = 4;
|
this.buttonRight.TabIndex = 4;
|
||||||
buttonRight.UseVisualStyleBackColor = true;
|
this.buttonRight.UseVisualStyleBackColor = true;
|
||||||
buttonRight.Click += ButtonMove_Click;
|
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
|
||||||
//
|
//
|
||||||
// buttonDown
|
// buttonDown
|
||||||
//
|
//
|
||||||
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
buttonDown.BackgroundImage = Properties.Resources.down;
|
this.buttonDown.BackgroundImage = global::DumpTruck.Properties.Resources.down;
|
||||||
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
|
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||||
buttonDown.Location = new Point(722, 408);
|
this.buttonDown.Location = new System.Drawing.Point(722, 408);
|
||||||
buttonDown.Name = "buttonDown";
|
this.buttonDown.Name = "buttonDown";
|
||||||
buttonDown.Size = new Size(30, 30);
|
this.buttonDown.Size = new System.Drawing.Size(30, 30);
|
||||||
buttonDown.TabIndex = 5;
|
this.buttonDown.TabIndex = 5;
|
||||||
buttonDown.UseVisualStyleBackColor = true;
|
this.buttonDown.UseVisualStyleBackColor = true;
|
||||||
buttonDown.Click += ButtonMove_Click;
|
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
|
||||||
|
//
|
||||||
|
// comboBoxStrategy
|
||||||
|
//
|
||||||
|
this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
|
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||||
|
this.comboBoxStrategy.FormattingEnabled = true;
|
||||||
|
this.comboBoxStrategy.Items.AddRange(new object[] {
|
||||||
|
"0",
|
||||||
|
"1"});
|
||||||
|
this.comboBoxStrategy.Location = new System.Drawing.Point(667, 12);
|
||||||
|
this.comboBoxStrategy.Name = "comboBoxStrategy";
|
||||||
|
this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23);
|
||||||
|
this.comboBoxStrategy.TabIndex = 6;
|
||||||
|
//
|
||||||
|
// ButtonCreateTruck
|
||||||
|
//
|
||||||
|
this.ButtonCreateTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||||
|
this.ButtonCreateTruck.Location = new System.Drawing.Point(148, 408);
|
||||||
|
this.ButtonCreateTruck.Name = "ButtonCreateTruck";
|
||||||
|
this.ButtonCreateTruck.Size = new System.Drawing.Size(130, 30);
|
||||||
|
this.ButtonCreateTruck.TabIndex = 7;
|
||||||
|
this.ButtonCreateTruck.Text = "Создать грузовик";
|
||||||
|
this.ButtonCreateTruck.UseVisualStyleBackColor = true;
|
||||||
|
this.ButtonCreateTruck.Click += new System.EventHandler(this.ButtonCreateTruck_Click);
|
||||||
|
//
|
||||||
|
// ButtonStep
|
||||||
|
//
|
||||||
|
this.ButtonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
|
this.ButtonStep.Location = new System.Drawing.Point(713, 41);
|
||||||
|
this.ButtonStep.Name = "ButtonStep";
|
||||||
|
this.ButtonStep.Size = new System.Drawing.Size(75, 23);
|
||||||
|
this.ButtonStep.TabIndex = 8;
|
||||||
|
this.ButtonStep.Text = "Шаг";
|
||||||
|
this.ButtonStep.UseVisualStyleBackColor = true;
|
||||||
|
this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click);
|
||||||
//
|
//
|
||||||
// FormDumpTruck
|
// FormDumpTruck
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
ClientSize = new Size(800, 450);
|
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||||
Controls.Add(buttonDown);
|
this.Controls.Add(this.ButtonStep);
|
||||||
Controls.Add(buttonRight);
|
this.Controls.Add(this.ButtonCreateTruck);
|
||||||
Controls.Add(buttonUp);
|
this.Controls.Add(this.comboBoxStrategy);
|
||||||
Controls.Add(buttonLeft);
|
this.Controls.Add(this.buttonDown);
|
||||||
Controls.Add(Create);
|
this.Controls.Add(this.buttonRight);
|
||||||
Controls.Add(pictureBoxDumpTruck);
|
this.Controls.Add(this.buttonUp);
|
||||||
Name = "FormDumpTruck";
|
this.Controls.Add(this.buttonLeft);
|
||||||
Text = "FormDumpTruck";
|
this.Controls.Add(this.ButtonCreateDumpTruck);
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).EndInit();
|
this.Controls.Add(this.pictureBoxDumpTruck);
|
||||||
ResumeLayout(false);
|
this.Name = "FormDumpTruck";
|
||||||
|
this.Text = "FormDumpTruck";
|
||||||
|
((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).EndInit();
|
||||||
|
this.ResumeLayout(false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private PictureBox pictureBoxDumpTruck;
|
private PictureBox pictureBoxDumpTruck;
|
||||||
private Button Create;
|
private Button ButtonCreateDumpTruck;
|
||||||
private Button buttonLeft;
|
private Button buttonLeft;
|
||||||
private Button buttonUp;
|
private Button buttonUp;
|
||||||
private Button buttonRight;
|
private Button buttonRight;
|
||||||
private Button buttonDown;
|
private Button buttonDown;
|
||||||
|
private ComboBox comboBoxStrategy;
|
||||||
|
private Button ButtonCreateTruck;
|
||||||
|
private Button ButtonStep;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,6 @@
|
|||||||
|
using DumpTruck.DrawingObjects;
|
||||||
|
using DumpTruck.MovementStrategy;
|
||||||
|
|
||||||
namespace DumpTruck
|
namespace DumpTruck
|
||||||
{
|
{
|
||||||
public partial class FormDumpTruck : Form
|
public partial class FormDumpTruck : Form
|
||||||
@ -7,39 +10,26 @@ namespace DumpTruck
|
|||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
private DrawingDumpTruck? _drawingDumpTruck;
|
private DrawingTruck? _drawingTruck;
|
||||||
|
|
||||||
|
private AbstractStrategy? _abstractStrategy;
|
||||||
|
|
||||||
private void Draw()
|
private void Draw()
|
||||||
{
|
{
|
||||||
if (_drawingDumpTruck == null)
|
if (_drawingTruck == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Bitmap bmp = new(pictureBoxDumpTruck.Width,
|
Bitmap bmp = new(pictureBoxDumpTruck.Width,
|
||||||
pictureBoxDumpTruck.Height);
|
pictureBoxDumpTruck.Height);
|
||||||
Graphics gr = Graphics.FromImage(bmp);
|
Graphics gr = Graphics.FromImage(bmp);
|
||||||
_drawingDumpTruck.DrawTransport(gr);
|
_drawingTruck.DrawTransport(gr);
|
||||||
pictureBoxDumpTruck.Image = bmp;
|
pictureBoxDumpTruck.Image = bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void Create_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
Random random = new();
|
|
||||||
_drawingDumpTruck = new DrawingDumpTruck();
|
|
||||||
_drawingDumpTruck.Init(random.Next(100, 300), random.Next(1000, 3000),
|
|
||||||
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)),
|
|
||||||
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)),
|
|
||||||
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
|
||||||
_drawingDumpTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ButtonMove_Click(object sender, EventArgs e)
|
private void ButtonMove_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (_drawingDumpTruck == null)
|
if (_drawingTruck == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -47,19 +37,78 @@ namespace DumpTruck
|
|||||||
switch (name)
|
switch (name)
|
||||||
{
|
{
|
||||||
case "buttonUp":
|
case "buttonUp":
|
||||||
_drawingDumpTruck.MoveTransport(Direction.Up);
|
_drawingTruck.MoveTransport(Direction.Up);
|
||||||
break;
|
break;
|
||||||
case "buttonDown":
|
case "buttonDown":
|
||||||
_drawingDumpTruck.MoveTransport(Direction.Down);
|
_drawingTruck.MoveTransport(Direction.Down);
|
||||||
break;
|
break;
|
||||||
case "buttonLeft":
|
case "buttonLeft":
|
||||||
_drawingDumpTruck.MoveTransport(Direction.Left);
|
_drawingTruck.MoveTransport(Direction.Left);
|
||||||
break;
|
break;
|
||||||
case "buttonRight":
|
case "buttonRight":
|
||||||
_drawingDumpTruck.MoveTransport(Direction.Right);
|
_drawingTruck.MoveTransport(Direction.Right);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Random random = new();
|
||||||
|
_drawingTruck = new DrawingDumpTruck(random.Next(100, 300), random.Next(1000, 3000),
|
||||||
|
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)),
|
||||||
|
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)),
|
||||||
|
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
||||||
|
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ButtonCreateTruck_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Random random = new();
|
||||||
|
_drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||||
|
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
|
||||||
|
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ButtonStep_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_drawingTruck == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (comboBoxStrategy.Enabled)
|
||||||
|
{
|
||||||
|
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||||
|
switch
|
||||||
|
{
|
||||||
|
0 => new MoveToCenter(),
|
||||||
|
1 => new MoveToBorder(),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
if (_abstractStrategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_abstractStrategy.SetData(new
|
||||||
|
DrawningObjectTruck(_drawingTruck), pictureBoxDumpTruck.Width,
|
||||||
|
pictureBoxDumpTruck.Height);
|
||||||
|
comboBoxStrategy.Enabled = false;
|
||||||
|
}
|
||||||
|
if (_abstractStrategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_abstractStrategy.MakeStep();
|
||||||
|
Draw();
|
||||||
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
_abstractStrategy = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
129
DumpTruck/MovementStrategy/AbstractStrategy.cs
Normal file
129
DumpTruck/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.MovementStrategy
|
||||||
|
{
|
||||||
|
public abstract class AbstractStrategy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещаемый объект
|
||||||
|
/// </summary>
|
||||||
|
private IMoveableObject? _moveableObject;
|
||||||
|
/// <summary>
|
||||||
|
/// Статус перемещения
|
||||||
|
/// </summary>
|
||||||
|
private Status _state = Status.NotInit;
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина поля
|
||||||
|
/// </summary>
|
||||||
|
protected int FieldWidth { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Высота поля
|
||||||
|
/// </summary>
|
||||||
|
protected int FieldHeight { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Статус перемещения
|
||||||
|
/// </summary>
|
||||||
|
public Status GetStatus() { return _state; }
|
||||||
|
/// <summary>
|
||||||
|
/// Установка данных
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="moveableObject">Перемещаемый объект</param>
|
||||||
|
/// <param name="width">Ширина поля</param>
|
||||||
|
/// <param name="height">Высота поля</param>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Шаг перемещения
|
||||||
|
/// </summary>
|
||||||
|
public void MakeStep()
|
||||||
|
{
|
||||||
|
if (_state != Status.InProgress)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (IsTargetDestinaion())
|
||||||
|
{
|
||||||
|
_state = Status.Finish;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MoveToTarget();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение влево
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveLeft() => MoveTo(Direction.Left);
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение вправо
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveRight() => MoveTo(Direction.Right);
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение вверх
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveUp() => MoveTo(Direction.Up);
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение вниз
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveDown() => MoveTo(Direction.Down);
|
||||||
|
/// <summary>
|
||||||
|
/// Параметры объекта
|
||||||
|
/// </summary>
|
||||||
|
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||||
|
/// <summary>
|
||||||
|
/// Шаг объекта
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected int? GetStep()
|
||||||
|
{
|
||||||
|
if (_state != Status.InProgress)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _moveableObject?.GetStep;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение к цели
|
||||||
|
/// </summary>
|
||||||
|
protected abstract void MoveToTarget();
|
||||||
|
/// <summary>
|
||||||
|
/// Достигнута ли цель
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected abstract bool IsTargetDestinaion();
|
||||||
|
/// <summary>
|
||||||
|
/// Попытка перемещения в требуемом направлении
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="directionType">Направление</param>
|
||||||
|
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
private bool MoveTo(Direction directionType)
|
||||||
|
{
|
||||||
|
if (_state != Status.InProgress)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_moveableObject?.CheckCanMove(directionType) ?? false)
|
||||||
|
{
|
||||||
|
_moveableObject.MoveObject(directionType);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
DumpTruck/MovementStrategy/DrawningObjectTruck.cs
Normal file
36
DumpTruck/MovementStrategy/DrawningObjectTruck.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DumpTruck.DrawingObjects;
|
||||||
|
|
||||||
|
namespace DumpTruck.MovementStrategy
|
||||||
|
{
|
||||||
|
public class DrawningObjectTruck : IMoveableObject
|
||||||
|
{
|
||||||
|
private readonly DrawingTruck? _drawingTruck = null;
|
||||||
|
public DrawningObjectTruck(DrawingTruck drawningCar)
|
||||||
|
{
|
||||||
|
_drawingTruck = drawningCar;
|
||||||
|
}
|
||||||
|
public ObjectParameters? GetObjectPosition
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_drawingTruck == null || _drawingTruck.EntityTruck ==
|
||||||
|
null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_drawingTruck.GetPosX,
|
||||||
|
_drawingTruck.GetPosY, _drawingTruck.GetWidth, _drawingTruck.GetHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int GetStep => (int)(_drawingTruck?.EntityTruck?.Step ?? 0);
|
||||||
|
public bool CheckCanMove(Direction direction) =>
|
||||||
|
_drawingTruck?.CanMove(direction) ?? false;
|
||||||
|
public void MoveObject(Direction direction) =>
|
||||||
|
_drawingTruck?.MoveTransport(direction);
|
||||||
|
}
|
||||||
|
}
|
31
DumpTruck/MovementStrategy/IMoveableObject.cs
Normal file
31
DumpTruck/MovementStrategy/IMoveableObject.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.MovementStrategy
|
||||||
|
{
|
||||||
|
public interface IMoveableObject
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Получение координаты X объекта
|
||||||
|
/// </summary>
|
||||||
|
ObjectParameters? GetObjectPosition { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// Шаг объекта
|
||||||
|
/// </summary>
|
||||||
|
int GetStep { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// Проверка, можно ли переместиться по нужному направлению
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool CheckCanMove(Direction direction);
|
||||||
|
/// <summary>
|
||||||
|
/// Изменение направления пермещения объекта
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Направление</param>
|
||||||
|
void MoveObject(Direction direction);
|
||||||
|
}
|
||||||
|
}
|
49
DumpTruck/MovementStrategy/MoveToBorder.cs
Normal file
49
DumpTruck/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.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.ObjectMiddleHorizontal - FieldWidth;
|
||||||
|
if (Math.Abs(diffX) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffX < 0)
|
||||||
|
{
|
||||||
|
MoveRight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var diffY = objParams.ObjectMiddleVertical - FieldHeight;
|
||||||
|
if (Math.Abs(diffY) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffY < 0)
|
||||||
|
{
|
||||||
|
MoveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
56
DumpTruck/MovementStrategy/MoveToCenter.cs
Normal file
56
DumpTruck/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
54
DumpTruck/MovementStrategy/ObjectParameters.cs
Normal file
54
DumpTruck/MovementStrategy/ObjectParameters.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
DumpTruck/MovementStrategy/Status.cs
Normal file
15
DumpTruck/MovementStrategy/Status.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DumpTruck.MovementStrategy
|
||||||
|
{
|
||||||
|
public enum Status
|
||||||
|
{
|
||||||
|
NotInit,
|
||||||
|
InProgress,
|
||||||
|
Finish
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user