124 lines
4.2 KiB
C#
Raw Normal View History

2023-09-26 19:32:10 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Pkcs;
using System.Text;
using System.Threading.Tasks;
using Lab.Entities;
namespace Lab.DrawningObjects
{
public class DrawTanker
{
public BaseCar? GasolineTanker { get; protected set; }
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _carWidth = 100;
protected readonly int _carHeight = 80;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _carWidth;
public int GetHeight => _carHeight;
public bool CanMove(Direction direction)
{
if (GasolineTanker == null)
return false;
return direction switch
{
Direction.Left => _startPosX - GasolineTanker.Step > 0,
Direction.Up => _startPosY - GasolineTanker.Step > 0,
Direction.Right => _startPosX + _carWidth + GasolineTanker.Step < _pictureWidth,
Direction.Down => _startPosY + _carHeight + GasolineTanker.Step < _pictureHeight,
_ => false
};
}
// Конструктор класса
public DrawTanker(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureHeight = height;
_pictureWidth = width;
GasolineTanker = new BaseCar(speed, weight, bodyColor);
}
public DrawTanker(int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight)
{
_pictureHeight = height;
_pictureWidth = width;
_carHeight = carHeight;
_carWidth = carWidth;
GasolineTanker = new BaseCar(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || GasolineTanker == null)
return;
switch (direction)
{
case Direction.Left:
{
if (_startPosX - GasolineTanker.Step > 0)
{
_startPosX -= (int)GasolineTanker.Step;
}
}
break;
case Direction.Up:
{
if (_startPosY - GasolineTanker.Step > 0)
{
_startPosY -= (int)GasolineTanker.Step;
}
}
break;
case Direction.Right:
{
if (_startPosX + _carWidth + GasolineTanker.Step < _pictureWidth)
{
_startPosX += (int)GasolineTanker.Step;
}
}
break;
case Direction.Down:
{
if (_startPosY + GasolineTanker.Step + _carHeight < _pictureHeight)
{
_startPosY += (int)GasolineTanker.Step;
}
}
break;
}
}
public virtual void DrawTransport(Graphics g)
{
if (GasolineTanker == null)
return;
Pen pen = new(GasolineTanker.BodyColor, 2);
Brush brush = new SolidBrush(GasolineTanker.BodyColor);
// Отрисовка корпуса
g.FillRectangle(brush, 10 + _startPosX, 40 + _startPosY, 90, 20);
g.FillRectangle(brush, 80 + _startPosX, 10 + _startPosY, 20, 40);
// Отрисовка колесиков
g.FillEllipse(brush, 10 + _startPosX, 60 + _startPosY, 20, 20);
g.FillEllipse(brush, 30 + _startPosX, 60 + _startPosY, 20, 20);
g.FillEllipse(brush, 80 + _startPosX, 60 + _startPosY, 20, 20);
}
}
}