PIbd-21_Anisin_R.S._DumpTru.../DumpTruck/DrawingObjects/DrawingTruck.cs

196 lines
7.4 KiB
C#
Raw Permalink 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 DumpTruck.Entities;
using DumpTruck.MovementStrategy;
namespace DumpTruck.DrawingObjects
{
public class DrawingTruck
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTruck? EntityTruck { 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>
protected readonly int _truckWidth = 160;
/// <summary>
/// Высота прорисовки грузовика
/// </summary>
protected readonly int _truckHeight = 90;
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _truckWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _truckHeight;
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawingTruck
/// </summary>
public IMoveableObject GetMoveableObject => new DrawningObjectTruck(this);
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _truckWidth || height < _truckHeight) return;
_pictureWidth = width;
_pictureHeight = height;
EntityTruck = new EntityTruck(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <param name="truckWidth">Ширина прорисовки грузовика</param>
/// <param name="truckWidth">Высота прорисовки грузовика</param>
protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight)
{
if (width < truckWidth || height < truckHeight) return;
_pictureWidth = width;
_pictureHeight = height;
_truckWidth = truckWidth;
_truckHeight = truckHeight;
EntityTruck = new EntityTruck(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 + _truckWidth > _pictureWidth) x = 0;
if (y < 0 || y + _truckHeight > _pictureHeight) y = 0;
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(Direction direction)
{
if (EntityTruck == null)
{
return false;
}
return direction switch
{
//влево
Direction.Left => _startPosX - EntityTruck.Step > 0,
//вверх
Direction.Up => _startPosY - EntityTruck.Step > 0,
// вправо
Direction.Right => _startPosX + _truckWidth + EntityTruck.Step < _pictureWidth,
//вниз
Direction.Down => _startPosY + _truckHeight + EntityTruck.Step < _pictureHeight,
_ => false,
};
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || EntityTruck == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityTruck.Step > 0)
{
_startPosX -= (int)EntityTruck.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - EntityTruck.Step > 0)
{
_startPosY -= (int)EntityTruck.Step;
}
break;
// вправо
case Direction.Right:
if (_startPosX + _truckWidth + EntityTruck.Step < _pictureWidth)
{
_startPosX += (int)EntityTruck.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _truckHeight + EntityTruck.Step < _pictureHeight)
{
_startPosY += (int)EntityTruck.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityTruck == null)
{
return;
}
Brush brush = new SolidBrush(EntityTruck.BodyColor);
g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10);
g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40);
g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40);
}
}
}