PIbd-21_MalafeevL.S._Cruise.../Cruiser/Drawing/DrawingCruiser.cs

212 lines
8.1 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 Cruiser.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.Drawing
{
public class DrawingCruiser
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityCruiser? EntityCruiser { get; set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки Крейсера
/// </summary>
protected static int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки Крейсера
/// </summary>
protected static int _startPosY;
/// <summary>
/// Ширина прорисовки Крейсера
/// </summary>
private readonly int _cruiserWidth = 150;
/// <summary>
/// Высота прорисовки Крейсера
/// </summary>
private readonly int _cruiserHeight = 60;
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _cruiserWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _cruiserHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="secColor">Элементов цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawingCruiser(int speed, double weight, Color bodyColor, Color secColor, int width, int height)
{
if (width < _cruiserWidth || height < _cruiserHeight)
{
_pictureHeight = _cruiserHeight + 100;
_pictureWidth = _cruiserWidth + 100;
}
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor, secColor);
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="secColor">Элементов цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <param name="cruiserWidth">Ширина прорисовки крейсера</param>
/// <param name="cruiserHeight">Высота прорисовки крейсера</param>
public DrawingCruiser(int speed, double weight, Color bodyColor, Color secColor, int width, int height, int cruiserWidth, int cruiserHeight)
{
_pictureWidth = width;
_pictureHeight = height;
_cruiserHeight = cruiserHeight;
_cruiserWidth = cruiserWidth;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor, secColor);
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0 || y < 0)
{
return;
}
if (x > _pictureWidth || y > _pictureHeight)
{
return;
}
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(Direction direction)
{
if (EntityCruiser == null)
{
return false;
}
return direction switch
{
//влево
Direction.Left => _startPosX - EntityCruiser.Step > 0,
//вверх
Direction.Up => _startPosY - EntityCruiser.Step > 0,
// вправо
Direction.Right => _startPosX + EntityCruiser.Step + _cruiserWidth < _pictureWidth,
//вниз
Direction.Down => _startPosY + EntityCruiser.Step + _cruiserHeight < _pictureHeight,
};
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || EntityCruiser == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
_startPosX -= (int)EntityCruiser.Step;
break;
//вверх
case Direction.Up:
_startPosY -= (int)EntityCruiser.Step;
break;
// вправо
case Direction.Right:
_startPosX += (int)EntityCruiser.Step;
break;
//вниз
case Direction.Down:
_startPosY += (int)EntityCruiser.Step;
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityCruiser == null)
{
return;
}
// палуба
Point[] Paluba = new Point[5]
{
new Point(_startPosX + 10,_startPosY),
new Point(_startPosX + 110,_startPosY),
new Point(_startPosX + 160,_startPosY + 30),
new Point(_startPosX + 110,_startPosY + 60),
new Point(_startPosX + 10,_startPosY + 60)
};
Brush brush = new SolidBrush(EntityCruiser.BodyColor);
g.FillPolygon(brush, Paluba);
// элементы
Point[] Elements = new Point[8]
{
new Point(_startPosX + 50,_startPosY + 20),
new Point(_startPosX + 70,_startPosY + 20),
new Point(_startPosX + 70,_startPosY + 10),
new Point(_startPosX + 90,_startPosY + 10),
new Point(_startPosX + 90,_startPosY + 50),
new Point(_startPosX + 70,_startPosY + 50),
new Point(_startPosX + 70,_startPosY + 40),
new Point(_startPosX + 50,_startPosY + 40),
};
Brush brushElem = new SolidBrush(EntityCruiser.SecondColor);
g.FillPolygon(brushElem, Elements);
g.FillEllipse(brushElem, _startPosX + 100, _startPosY + 20, 20, 20);
// турбины
Brush Turbins = new SolidBrush(Color.Black);
g.FillRectangle(Turbins, _startPosX, _startPosY + 10, 10, 20);
g.FillRectangle(Turbins, _startPosX, _startPosY + 35, 10, 20);
}
}
}