2024-05-08 00:49:29 +04:00

219 lines
7.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.Entities;
namespace Tank.Drowings;
public class DrawningMachine
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityMachine? EntityMachine { 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>
private readonly int _drawningTankWidth = 218;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _drawningTankHeight = 105;
/// <summary>
/// Координата X
/// </summary>
public int? GetPosX => _startPosX;
/// <summary>
/// Координата Y
/// </summary>
public int? GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _drawningTankWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _drawningTankHeight;
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningMachine()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningMachine(int speed, double weight, Color bodyColor) : this()
{
EntityMachine = new EntityMachine(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="_drawningTankWidth">Ширина прорисовки автомобиля</param>
/// <param name="_drawningTankHeight">Высота прорисовки автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
protected DrawningMachine(int drawningTankWidth, int drawningTankHeight) : this()
{
_drawningTankWidth = drawningTankWidth;
_drawningTankHeight = drawningTankHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя
public bool SetPictureSize(int width, int height)
{
if (_drawningTankWidth <= width && _drawningTankHeight <= height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
if (_startPosX + _drawningTankWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningTankWidth;
}
if (_startPosY + _drawningTankHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningTankHeight;
}
}
return true;
}
return false;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
if (x < 0) x = 0;
else if (x + _drawningTankWidth > _pictureWidth) x = _pictureWidth.Value - _drawningTankWidth;
if (y < 0) y = 0;
else if (y + _drawningTankHeight > _pictureHeight) y = _pictureHeight.Value - _drawningTankHeight;
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityMachine == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityMachine.Step > 0)
_startPosX -= (int)EntityMachine.Step;
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityMachine.Step > 0)
_startPosY -= (int)EntityMachine.Step;
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningTankWidth + EntityMachine.Step < _pictureWidth)
_startPosX += (int)EntityMachine.Step;
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningTankHeight + EntityMachine.Step < _pictureHeight)
_startPosY += (int)EntityMachine.Step;
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityMachine == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityMachine.BodyColor);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 45, 150, 60);
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 60, 33, 33);
g.DrawEllipse(pen, _startPosX.Value + 110, _startPosY.Value + 60, 33, 33);
g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 80, 16, 16);
g.DrawEllipse(pen, _startPosX.Value + 55, _startPosY.Value + 83, 16, 16);
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 83, 16, 16);
g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 80, 16, 16);
g.FillEllipse(bodyBrush, _startPosX.Value + 45, _startPosY.Value + 55, 15, 15);
g.FillEllipse(bodyBrush, _startPosX.Value + 65, _startPosY.Value + 55, 15, 15);
g.FillEllipse(bodyBrush, _startPosX.Value + 85, _startPosY.Value + 55, 15, 15);
g.FillRectangle(bodyBrush, _startPosX.Value + 30, _startPosY.Value + 10, 100, 30);
g.FillRectangle(bodyBrush, _startPosX.Value + 5, _startPosY.Value + 40, 140, 25);
}
internal void SetPictureSize(object width, object height)
{
throw new NotImplementedException();
}
}