PIbd-21_Putintsev_D.M._Road.../RoadTrain/DrawningRoadTrain.cs

186 lines
6.3 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoadTrain.Entities;
using RoadTrain.MovementStrategy;
namespace RoadTrain.DrawningObjects
{
public class DrawningRoadTrain
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityRoadTrain? EntityRoadTrain { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
public int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
public int _pictureHeight;
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
protected int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
protected int _startPosY;
/// <summary>
/// Ширина прорисовки автомобиля
/// </summary>
protected readonly int _trainWidth = 70;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
protected readonly int _trainHeight = 30;
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _trainWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _trainHeight;
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(DirectionType direction)
{
if (EntityRoadTrain == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityRoadTrain.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityRoadTrain.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityRoadTrain.Step + _trainWidth < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityRoadTrain.Step + _trainHeight < _pictureHeight
};
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public DrawningRoadTrain(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _trainWidth) { return; }
if (height < _trainHeight) { return; }
_pictureWidth = width;
_pictureHeight = height;
EntityRoadTrain = new EntityRoadTrain(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 = 0;
}
else if (x + _trainWidth > _pictureWidth)
{
x = _pictureWidth - _trainWidth;
}
if (y < 0)
{
y = 0;
}
else if (y + _trainHeight > _pictureHeight)
{
y = _pictureHeight - _trainHeight;
}
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityRoadTrain == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
_startPosX -= (int)EntityRoadTrain.Step;
break;
//вверх
case DirectionType.Up:
_startPosY -= (int)EntityRoadTrain.Step;
break;
// вправо
case DirectionType.Right:
_startPosX += (int)EntityRoadTrain.Step;
break;
//вниз
case DirectionType.Down:
_startPosY += (int)EntityRoadTrain.Step;
break;
}
}
public IMoveableObject GetMoveableObject => new
DrawningObjectTrain(this);
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityRoadTrain == null)
{
return;
}
Brush br = new SolidBrush(EntityRoadTrain.BodyColor);
Pen pen = new(Color.Black);
g.DrawLine(pen, _startPosX + 20, _startPosY + 20, _startPosX + 70, _startPosY + 20);
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 20, 10, 10);
g.DrawEllipse(pen, _startPosX + 30, _startPosY + 20, 10, 10);
g.DrawEllipse(pen, _startPosX + 60, _startPosY + 20, 10, 10);
g.DrawRectangle(pen, _startPosX + 60, _startPosY, 10, 20);
g.FillEllipse(br, _startPosX + 20, _startPosY + 20, 10, 10);
g.FillEllipse(br, _startPosX + 30, _startPosY + 20, 10, 10);
g.FillEllipse(br, _startPosX + 60, _startPosY + 20, 10, 10);
g.FillRectangle(br, _startPosX + 60, _startPosY, 10, 20);
}
public void SetBodyColor(Color color)
{
EntityRoadTrain.SetBodyColor(color);
}
}
}