PIbd-14_Pruidze_I.K_Simple_.../ProjectCruiser/DrawningSamples/DrawningBase.cs

174 lines
5.7 KiB
C#
Raw Permalink Normal View History

2024-06-12 19:21:59 +04:00
using ProjectCruiser.Entities;
namespace ProjectCruiser.DrawningSamples;
2024-06-12 14:29:35 +04:00
public class DrawningBase
{
// Класс-сущность
public EntityBase? EntityTransport { get; protected set; }
2024-06-12 14:29:35 +04:00
private int? _pictureWidth; // Ширина окна
private int? _pictureHeight; // Высота окна
protected int? _startPosX; // < protected
protected int? _startPosY; // < protected
2024-06-12 14:29:35 +04:00
2024-06-12 19:21:59 +04:00
private readonly int _drawningWidth = 302; // Ширина прорисовки автомобиля
2024-06-12 14:29:35 +04:00
private readonly int _drawningHeight = 42; // Высота прорисовки автомобиля
// Инициализация свойств (теперь через конструктор)
2024-06-12 19:21:59 +04:00
public DrawningBase(int speed, double weight, Color bodyColor) : this()
2024-06-12 14:29:35 +04:00
{
EntityTransport = new EntityBase(speed, weight, bodyColor);
}
2024-06-12 14:29:35 +04:00
private DrawningBase()
{
2024-06-12 14:29:35 +04:00
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
// protected >
protected DrawningBase(int drawningCarWidth, int drawningCarHeight) : this()
{
_drawningWidth = drawningCarWidth;
_pictureHeight = drawningCarHeight;
}
2024-06-12 14:29:35 +04:00
public int getHeight() // для создания объекта в нижнем левом углу (*)
{
return _drawningHeight;
}
// Установка границ поля
public bool SetPictureSize(int w, int h)
{
2024-06-12 19:21:59 +04:00
if (w < _drawningWidth || h < _drawningHeight)
// canvas always bigger then obj to fit it
2024-06-12 14:29:35 +04:00
{
return false;
}
_pictureWidth = w;
_pictureHeight = h;
if (_startPosX != null || _startPosY != null)
{
SetPosition(_startPosX.Value, _startPosY.Value);
}
return true;
}
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
if (x > _pictureWidth - _drawningWidth) _startPosX =
_pictureWidth - _drawningWidth;
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y > _pictureHeight - _drawningHeight) _startPosY =
_pictureHeight.Value - _drawningHeight;
else if (y < 0) _startPosY = 0;
else _startPosY = y;
}
public bool MoveTransport(DirectionType direction)
{
2024-06-12 19:21:59 +04:00
if (EntityTransport == null || !_startPosX.HasValue || !_startPosY.HasValue)
2024-06-12 14:29:35 +04:00
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityTransport.Step > 0)
2024-06-12 14:29:35 +04:00
{
_startPosX -= (int)EntityTransport.Step;
2024-06-12 14:29:35 +04:00
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityTransport.Step > 0)
2024-06-12 14:29:35 +04:00
{
_startPosY -= (int)EntityTransport.Step;
2024-06-12 14:29:35 +04:00
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningWidth + EntityTransport.Step
2024-06-12 14:29:35 +04:00
< _pictureWidth.Value)
{
_startPosX += (int)EntityTransport.Step;
2024-06-12 14:29:35 +04:00
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningHeight + EntityTransport.Step
2024-06-12 14:29:35 +04:00
< _pictureHeight.Value)
{
_startPosY += (int)EntityTransport.Step;
2024-06-12 14:29:35 +04:00
}
return true;
default:
return false;
// Перемещение объекта (удалось перемещение - true \ false)
}
}
public virtual void DrawTransport(Graphics g) // < virtual
2024-06-12 14:29:35 +04:00
{
if (EntityTransport == null || !_startPosX.HasValue ||
2024-06-12 14:29:35 +04:00
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black, 2);
Brush mainBrush = new SolidBrush(EntityTransport.MainColor);
2024-06-12 14:29:35 +04:00
//границы cruiser
Point point0 = new Point(_startPosX.Value + 2, _startPosY.Value + 7);
Point point1 = new Point(_startPosX.Value + 2, _startPosY.Value + 30);
Point point2 = new Point(_startPosX.Value + 184, _startPosY.Value + 42);
Point point3 = new Point(_startPosX.Value + 260, _startPosY.Value + 34);
Point point4 = new Point(_startPosX.Value + 300, _startPosY.Value + 22);
Point point5 = new Point(_startPosX.Value + 260, _startPosY.Value + 10);
Point point6 = new Point(_startPosX.Value + 184, _startPosY.Value + 2);
Point[] boarders = {
point0,
point1,
point2,
point3,
point4,
point5,
point6
};
g.DrawPolygon(pen, boarders);
g.FillPolygon(mainBrush, boarders);
// салон на верхней палубе
int y_h = EntityTransport.values[1];
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24);
g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12);
g.FillRectangle(mainBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12);
g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20);
g.FillRectangle(mainBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20);
2024-06-12 14:29:35 +04:00
}
public int? GetPosX => _startPosX;
public int? GetPosY => _startPosY;
public int GetWidth => _drawningWidth;
public int GetHeight => _drawningHeight;
2024-06-12 14:29:35 +04:00
}