Marselchi 1a22fe73af Lab1
2023-09-25 09:27:40 +04:00

171 lines
6.8 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;
namespace Liner
{
public class DrawingLiner
{
private Brush poolBrush = new SolidBrush(Color.Blue);
/// <summary>
/// Класс-сущность
/// </summary>
public EntityLiner? EntityLiner { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки лайнера
/// </summary>
private int _startPosX;
/// <summary>
/// Верхняя координата прорисовки лайнера
/// </summary>
private int _startPosY;
/// <summary>
/// Ширина прорисовки лайнера
/// </summary>
private readonly int _linerWidth = 100;
/// <summary>
/// Высота прорисовки лайнера
/// </summary>
private readonly int _linerHeight = 80;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лайнера</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="swimmingPool">Признак наличия бассейна</param>
/// <param name="deck">Признак наличия доп палубы</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена,
/// нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor,Color bottomColor, bool swimmingPool, bool deck, int width, int height)
{
if(width < _linerWidth || height < _linerHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityLiner = new EntityLiner();
EntityLiner.Init(speed, weight, bodyColor,bottomColor, swimmingPool,deck);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if(x > _pictureWidth || y > _pictureHeight || x < 0 || y < 0)
{
_startPosX = 0;
_startPosY = 0;
}
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (EntityLiner == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityLiner.Step > 0)
{
_startPosX -= (int)EntityLiner.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityLiner.Step > 0)
{
_startPosY -= (int)EntityLiner.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + EntityLiner.Step < _pictureWidth - _linerWidth)
{
_startPosX += (int)EntityLiner.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + EntityLiner.Step < _pictureHeight - _linerHeight)
{
_startPosY += (int)EntityLiner.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
// высота палубы - 20 пикселей
public void DrawTransport(Graphics g)
{
if (EntityLiner == null)
{
return;
}
Pen pen = new(Color.Black);
Brush bottomBrush = new SolidBrush(EntityLiner.BottomColor);
Brush deckBrush = new SolidBrush(EntityLiner.BodyColor);
// палуба
Point[] firstDeck = {new Point(_startPosX + 30, _startPosY), new Point(_startPosX + 30 + 60, _startPosY),
new Point(_startPosX + 30 + 60, _startPosY + 20), new Point(_startPosX + 30, _startPosY + 20)};
int addY = 20;
// нижний корпус
Point[] bottom = {new Point(_startPosX, _startPosY), new Point(_startPosX + 100, _startPosY),
new Point(_startPosX+80, _startPosY + 40), new Point(_startPosX + 20, _startPosY + 40)};
// бассейн
Point[] pool = {new Point(_startPosX + 10, _startPosY), new Point(_startPosX + 30, _startPosY),
new Point(_startPosX+30, _startPosY + 10), new Point(_startPosX + 10, _startPosY + 10)};
g.FillPolygon(deckBrush, firstDeck);
g.DrawPolygon(pen, firstDeck);//рисуем палубу
Point[] currentDeck = new Point[4]; //буффер для прорисовки доп палуб
firstDeck.CopyTo(currentDeck, 0);
if(EntityLiner.Deck) //рисуем вторую палубу
{
for(int i = 0; i < 4; i++)
{
currentDeck[i].Y += addY;
}
g.FillPolygon(deckBrush, currentDeck);
g.DrawPolygon(pen, currentDeck);
addY += 20;
}
for(int i = 0; i < 4; i++) //сдвигаем элементы вниз на кол-во палуб
{
bottom[i].Y += addY;
pool[i].Y += addY;
}
g.FillPolygon(bottomBrush, bottom);
g.DrawPolygon(pen, bottom);//рисуем нижний корпус
if (EntityLiner.SwimmingPool) { g.FillPolygon(poolBrush, pool); }//рисуем бассейн
g.Dispose();
}
}
}