Части кода.

This commit is contained in:
user 2024-04-16 11:19:21 +04:00
parent 46e0f1a276
commit dc49392ffd
5 changed files with 208 additions and 11 deletions

View File

@ -1,6 +1,4 @@

namespace HoistingCrane
namespace HoistingCrane.Drawing
{
public enum DirectionType
{

View File

@ -0,0 +1,180 @@
using HoistingCrane.Entities;
namespace HoistingCrane.Drawing;
public class DrawingCar
{
/// Класс-сущность
public EntityCrane? EntityCrane { get; private set; }
/// Ширина окна
private int? _pictureWidth;
/// Высота окна
private int? _pictureHeight;
/// Левая координата прорисовки автомобиля
private int? _startPosX;
/// Верхняя кооридната прорисовки автомобиля
private int? _startPosY;
/// Ширина прорисовки автомобиля
private readonly int _drawningCraneWidth = 110;
/// Высота прорисовки автомобиля
private readonly int _drawningCraneHeight = 56;
/// Инициализация свойств
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public void Init(int speed, double weight, Color bodyColor)
{
EntityCrane = new EntityCrane(speed, weight, bodyColor);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// Установка границ поля
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
// TODO проверка, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
if (width < _drawningCraneWidth || height < _drawningCraneHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawningCraneWidth > _pictureWidth) _startPosX = _pictureWidth - _drawningCraneWidth;
if (_startPosY + _drawningCraneHeight > _pictureHeight) _startPosY = _pictureHeight - _drawningCraneHeight;
if (_startPosX < 0) _startPosX = 0;
if (_startPosY < 0) _startPosY = 0;
}
return true;
}
/// Установка позиции
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах
_startPosX = x;
_startPosY = y;
if (_startPosX + _drawningCraneWidth > _pictureWidth) _startPosX = _pictureWidth - _drawningCraneWidth;
if (_startPosY + _drawningCraneHeight > _pictureHeight) _startPosY = _pictureHeight - _drawningCraneHeight;
if (_startPosX < 0) _startPosX = 0;
if (_startPosY < 0) _startPosY = 0;
}
/// Изменение направления перемещения
/// <param name="direction">Направление</param>
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityCrane == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityCrane.Step > 0)
{
_startPosX -= (int)EntityCrane.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityCrane.Step > 0)
{
_startPosY -= (int)EntityCrane.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityCrane.Step < _pictureWidth - _drawningCraneWidth)
{
_startPosX += (int)EntityCrane.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityCrane.Step < _pictureHeight - _drawningCraneHeight)
{
_startPosY += (int)EntityCrane.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityCrane == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush BodyBrush = new SolidBrush(EntityCrane.BodyColor);
//Уменьшаем все числа на 8
//корпус
g.FillRectangle(BodyBrush, _startPosX.Value + 2, _startPosY.Value + 22, 54, 4);
g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 22, 54, 4);
g.FillRectangle(BodyBrush, _startPosX.Value + 14, _startPosY.Value + 8, -4, 7);
g.DrawRectangle(pen, _startPosX.Value + 14, _startPosY.Value + 8, -4, 6);
g.FillRectangle(BodyBrush, _startPosX.Value + 29, _startPosY.Value , -3, 15);
g.DrawRectangle(pen, _startPosX.Value + 29, _startPosY.Value + 0, -3, 14);
//гусеницы
g.DrawLine(pen, _startPosX.Value , _startPosY.Value + 34, _startPosX.Value + 2, _startPosY.Value + 34);
g.DrawLine(pen, _startPosX.Value - 1, _startPosY.Value + 35, _startPosX.Value - 1, _startPosY.Value + 45);
g.DrawLine(pen, _startPosX.Value , _startPosY.Value + 46, _startPosX.Value + 4, _startPosY.Value + 46);
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 47, _startPosX.Value + 61, _startPosY.Value + 47);
g.DrawLine(pen, _startPosX.Value + 62, _startPosY.Value + 46, _startPosX.Value + 65, _startPosY.Value + 46);
g.DrawLine(pen, _startPosX.Value + 66, _startPosY.Value + 45, _startPosX.Value + 66, _startPosY.Value + 35);
g.DrawLine(pen, _startPosX.Value + 63, _startPosY.Value + 34, _startPosX.Value + 65, _startPosY.Value + 34);
//колеса
g.FillEllipse(BodyBrush, _startPosX.Value + 2, _startPosY.Value + 36, 1, 1);
g.DrawEllipse(pen, _startPosX.Value + 2, _startPosY.Value + 36, 1, 1);
g.FillEllipse(BodyBrush, _startPosX.Value + 55, _startPosY.Value + 36, 1, 1);
g.DrawEllipse(pen, _startPosX.Value + 55, _startPosY.Value + 36, 1, 1);
g.FillEllipse(BodyBrush, _startPosX.Value + 17, _startPosY.Value + 40, -2, -2);
g.DrawEllipse(pen, _startPosX.Value + 17, _startPosY.Value + 40, -2, -2);
g.FillEllipse(BodyBrush, _startPosX.Value + 30, _startPosY.Value + 40, -2, -2);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 40, -2, -2);
g.FillEllipse(BodyBrush, _startPosX.Value + 42, _startPosY.Value + 40, -2, -2);
g.DrawEllipse(pen, _startPosX.Value + 42, _startPosY.Value + 40, -2, -2);
g.FillEllipse(BodyBrush, _startPosX.Value + 25, _startPosY.Value + 36, -4, -4);
g.DrawEllipse(pen, _startPosX.Value + 25, _startPosY.Value + 36, -4, -4);
g.FillEllipse(BodyBrush, _startPosX.Value + 37, _startPosY.Value + 46, -4, -4);
g.DrawEllipse(pen, _startPosX.Value + 37, _startPosY.Value + 36, -4, -4);
//стекло
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX.Value + 44, _startPosY.Value + 6, 13, 8);
g.DrawRectangle(pen, _startPosX.Value + 44, _startPosY.Value + 6, 12, 8);
}
}

View File

@ -1,6 +1,4 @@

namespace HoistingCrane;
namespace HoistingCrane.Drawing;
public class DrawingHoistingCrane
{

View File

@ -0,0 +1,24 @@
namespace HoistingCrane.Entities;
///Класс-сущность "Автомобиль"
public class EntityCrane
{
/// Скорость
public int Speed { get; private set; }
/// Вес
public double Weight { get; private set; }
/// Основной цвет
public Color BodyColor { get; private set; }
/// Шаг перемещения автомобиля
public double Step => Speed * 100 / Weight;
/// Конструктор сущности
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityCrane(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -1,6 +1,5 @@

namespace HoistingCrane
namespace HoistingCrane.Entities
///Класс-сущность "Спортивный автомобиль"
{
public class EntityHoistingCrane
{
@ -16,8 +15,6 @@ namespace HoistingCrane
public bool Crane { get; private set; }
/// Признак (опция) наличия противовеса
public bool Counterweight { get; private set; }
/// Шаг перемещения автомобиля
public double Step => Speed * 100 / Weight;
/// Инициализация полей объекта-класса спортивного автомобиля
/// <param name="speed">Скорость</param>