196 lines
7.7 KiB
C#
196 lines
7.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Battleship
|
||
{
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningBattleship
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityBattleship? EntityBattleship { 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 _buttleshipWidth = 175;
|
||
/// <summary>
|
||
/// Высота прорисовки
|
||
/// </summary>
|
||
private readonly int _buttleshipHeight = 80;
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Цвет основы</param>
|
||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||
/// <param name="tower">Признак наличия орудийной башни</param>
|
||
/// <param name="section">Признак наличия отсека под ракеты</param>
|
||
/// <param name="width">Ширина картинки</param>
|
||
/// <param name="height">Высота картинки</param>
|
||
/// <returns>true - объект создан, false - проверка не пройдена,
|
||
///нельзя создать объект в этих размерах</retu rns>
|
||
|
||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool tower, bool section, int width, int height)
|
||
{
|
||
if (width < _buttleshipWidth || height < _buttleshipHeight)
|
||
{
|
||
return false;
|
||
}
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
EntityBattleship = new EntityBattleship();
|
||
EntityBattleship.Init(speed, weight, bodyColor, additionalColor,
|
||
tower, section);
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка позиции
|
||
/// </summary>
|
||
/// <param name="x">Координата X</param>
|
||
/// <param name="y">Координата Y</param>
|
||
public void SetPosition(int x, int y)
|
||
{
|
||
if (x >= 0 && x + _buttleshipWidth <= _pictureWidth && y >= 0 && y + _buttleshipHeight <= _pictureHeight)
|
||
{
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
public void MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityBattleship == null)
|
||
{
|
||
return;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX - EntityBattleship.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityBattleship.Step;
|
||
}
|
||
break;
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY - EntityBattleship.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityBattleship.Step;
|
||
}
|
||
break;
|
||
// вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX + _buttleshipWidth + EntityBattleship.Step < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityBattleship.Step;
|
||
}
|
||
break;
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY + _buttleshipHeight + EntityBattleship.Step < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityBattleship.Step;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityBattleship == null)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black, 2);
|
||
Brush additionalBrush = new
|
||
SolidBrush(EntityBattleship.AdditionalColor);
|
||
|
||
//основа
|
||
Brush mainBrush = new SolidBrush(EntityBattleship.BodyColor);
|
||
Point[] hull = new Point[]
|
||
{
|
||
new Point(_startPosX + 5, _startPosY + 0),
|
||
new Point(_startPosX + 120, _startPosY + 0),
|
||
new Point(_startPosX + 160, _startPosY + 35),
|
||
new Point(_startPosX + 120, _startPosY + 70),
|
||
new Point(_startPosX + 5, _startPosY + 70),
|
||
};
|
||
g.FillPolygon(mainBrush, hull);
|
||
g.DrawPolygon(pen, hull);
|
||
|
||
//блоки
|
||
Brush blockBrush = new
|
||
SolidBrush(Color.DimGray);
|
||
g.FillRectangle(blockBrush, _startPosX + 70, _startPosY + 15, 20, 40);
|
||
g.DrawRectangle(pen, _startPosX + 70, _startPosY + 15, 20, 40);
|
||
|
||
g.FillRectangle(additionalBrush, _startPosX + 40, _startPosY + 25, 30, 20);
|
||
g.DrawRectangle(pen, _startPosX + 40, _startPosY + 25, 30, 20);
|
||
|
||
g.FillEllipse(additionalBrush, _startPosX + 100, _startPosY + 20, 30, 30);
|
||
g.DrawEllipse(pen, _startPosX + 100, _startPosY + 20, 30, 30);
|
||
|
||
//для ускорения
|
||
Brush speedBrush = new
|
||
SolidBrush(Color.Gold);
|
||
g.FillRectangle(speedBrush, _startPosX + 0, _startPosY + 10, 5, 20);
|
||
g.DrawRectangle(pen, _startPosX + 0, _startPosY + 10, 5, 20);
|
||
g.FillRectangle(speedBrush, _startPosX + 0, _startPosY + 40, 5, 20);
|
||
g.DrawRectangle(pen, _startPosX + 0, _startPosY + 40, 5, 20);
|
||
|
||
//орудийная башня
|
||
if (EntityBattleship.Tower)
|
||
{
|
||
Brush baseBrush = new SolidBrush(Color.White);
|
||
g.FillRectangle(baseBrush, _startPosX + 108, _startPosY + 28, 15, 15);
|
||
g.DrawRectangle(pen, _startPosX + 108, _startPosY + 28, 15, 15);
|
||
Brush gunBrush = new SolidBrush(Color.Black);
|
||
g.FillRectangle(gunBrush, _startPosX + 123, _startPosY + 32, 47, 6);
|
||
g.DrawRectangle(pen, _startPosX + 123, _startPosY + 32, 55, 6);
|
||
}
|
||
//отсеки под ракеты
|
||
if (EntityBattleship.Section)
|
||
{
|
||
Brush sectionBrush = new
|
||
SolidBrush(Color.Gray);
|
||
g.FillRectangle(sectionBrush, _startPosX + 20, _startPosY + 70, 40, 10);
|
||
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 70, 40, 10);
|
||
g.FillRectangle(sectionBrush, _startPosX + 75, _startPosY + 70, 40, 10);
|
||
g.DrawRectangle(pen, _startPosX + 75, _startPosY + 70, 40, 10);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|