ISEBD-11 Ivanova.A.A. LabWork02 Simple #2
@ -1,271 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectLinkor;
|
||||
|
||||
/// <summary>
|
||||
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningLinkor
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityLinkor? EntityLinkor { 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 _drawningLinkorWidth = 150;
|
||||
|
||||
/// <summary>
|
||||
/// Высота прорисовки линкора
|
||||
/// </summary>
|
||||
private readonly int _drawningLinkorHeight = 80;
|
||||
|
||||
/// <summary>
|
||||
/// Иницилизация полей объекта-класса линкора
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weigth">Вес линкора</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="gunTurret">Признак наличия орудийной башни</param>
|
||||
/// <param name="compartment">Признак наличия отсека под ракеты</param>
|
||||
/// <param name="linkorMotor">Признак наличия</param>
|
||||
public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool gunTurret, bool compartment, bool linkorMotor)
|
||||
{
|
||||
EntityLinkor = new EntityLinkor();
|
||||
EntityLinkor.Init(speed, weigth, bodyColor, additionalColor, gunTurret, compartment, linkorMotor);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
// TODO проверка, что объект "влезает" в размеры поля
|
||||
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже устоновлена
|
||||
if (_drawningLinkorWidth <= width && _drawningLinkorHeight <= height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
|
||||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||||
{
|
||||
if (_startPosX + _drawningLinkorWidth > _pictureWidth )
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningLinkorWidth;
|
||||
}
|
||||
|
||||
if (_startPosY + _drawningLinkorWidth <= _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningLinkorHeight;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Кордината X</param>
|
||||
/// <param name="y">Кордината Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
||||
// то надо изменить координаты, чтобы он оставался в этих границах
|
||||
|
||||
if (x < 0)
|
||||
{
|
||||
x = 0;
|
||||
}
|
||||
|
||||
else if (x + _drawningLinkorWidth > _pictureWidth)
|
||||
{
|
||||
x = _pictureWidth.Value - _drawningLinkorWidth;
|
||||
}
|
||||
|
||||
if (y < 0)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
else if (y + _drawningLinkorHeight > _pictureHeight)
|
||||
{
|
||||
y = _pictureHeight.Value - _drawningLinkorHeight;
|
||||
}
|
||||
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityLinkor == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityLinkor.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityLinkor.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityLinkor.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityLinkor.Step;
|
||||
}
|
||||
return true;
|
||||
//вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value+_drawningLinkorWidth + EntityLinkor.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityLinkor.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY.Value+_drawningLinkorHeight + EntityLinkor.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityLinkor.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLinkor == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntityLinkor.AdditionalColor);
|
||||
Brush bodybrush = new SolidBrush(EntityLinkor.BodyColor);
|
||||
|
||||
|
||||
//Заливка лодки
|
||||
Point point1 = new Point(_startPosX.Value + 5, _startPosY.Value + 5);
|
||||
Point point2 = new Point(_startPosX.Value + 110, _startPosY.Value + 5);
|
||||
Point point3 = new Point(_startPosX.Value + 150, _startPosY.Value + 42);
|
||||
Point point4 = new Point(_startPosX.Value + 110, _startPosY.Value + 80);
|
||||
Point point5 = new Point(_startPosX.Value + 5, _startPosY.Value + 80);
|
||||
Point point6 = new Point(_startPosX.Value + 5, _startPosY.Value + 5);
|
||||
|
||||
Point[] LinkorLine = { point1, point2, point3, point4, point5, point6 };
|
||||
g.FillPolygon(additionalBrush, LinkorLine);
|
||||
|
||||
//границы линкора
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 5, _startPosX.Value + 110, _startPosY.Value + 5);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 80, _startPosX.Value + 110, _startPosY.Value + 80);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 5, _startPosX.Value + 5, _startPosY.Value + 80);
|
||||
g.DrawLine(pen, _startPosX.Value + 110, _startPosY.Value + 5, _startPosX.Value + 150, _startPosY.Value + 42);
|
||||
g.DrawLine(pen, _startPosX.Value + 110, _startPosY.Value + 80, _startPosX.Value + 150, _startPosY.Value + 42);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 59, 15, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 64, 15, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 12, 15, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 17, 15, 5);
|
||||
//Парус
|
||||
g.DrawArc(pen, _startPosX.Value + 45, _startPosY.Value + 15, 35, 60, 280, 160);
|
||||
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 17, _startPosX.Value + 67, _startPosY.Value + 72);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 72);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 17);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 45);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 35);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 55);
|
||||
|
||||
|
||||
//заливка ракет
|
||||
Brush br = new SolidBrush(Color.Black);
|
||||
g.FillRectangle(bodybrush, _startPosX.Value + 25, _startPosY.Value + 59, 15, 15);
|
||||
g.FillRectangle(bodybrush, _startPosX.Value + 10, _startPosY.Value + 64, 15, 5);
|
||||
g.FillRectangle(bodybrush, _startPosX.Value + 25, _startPosY.Value + 12, 15, 15);
|
||||
g.FillRectangle(bodybrush, _startPosX.Value + 10, _startPosY.Value + 17, 15, 5);
|
||||
|
||||
//Орудийная башня
|
||||
if (EntityLinkor.GunTurret)
|
||||
{
|
||||
Brush bri = new SolidBrush(Color.Gray);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 27, 30, 30);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 32, 20, 20);
|
||||
g.FillEllipse(bri, _startPosX.Value + 90, _startPosY.Value + 27, 30, 30);
|
||||
g.FillEllipse(br, _startPosX.Value + 95, _startPosY.Value + 32, 20, 20);
|
||||
}
|
||||
|
||||
//Дополнительная ракета
|
||||
if (EntityLinkor.Сompartment)
|
||||
{
|
||||
Brush brfara = new SolidBrush(Color.Blue);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 35, 15, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 40, 15, 5);
|
||||
g.FillRectangle(brfara, _startPosX.Value + 25, _startPosY.Value + 35, 15, 15);
|
||||
g.FillRectangle(brfara, _startPosX.Value + 10, _startPosY.Value + 40, 15, 5);
|
||||
|
||||
}
|
||||
|
||||
//Мотор
|
||||
if (EntityLinkor.LinkorMotor)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 59, 5, 15);
|
||||
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 59, 5, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 10, 5, 15);
|
||||
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 10, 5, 15);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
namespace ProjectLinkor;
|
||||
namespace ProjectLinkor.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1,
|
||||
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
280
ProjectSportCar/ProjectSportCar/Drawnings/DrawingWarship.cs
Normal file
280
ProjectSportCar/ProjectSportCar/Drawnings/DrawingWarship.cs
Normal file
@ -0,0 +1,280 @@
|
||||
using ProjectLinkor.Entities;
|
||||
using static System.Windows.Forms.LinkLabel;
|
||||
|
||||
namespace ProjectLinkor.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawingWarship
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityWarship? EntityWarship { 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>
|
||||
private readonly int _drawingWarshipWidth = 148;
|
||||
|
||||
/// <summary>
|
||||
/// Высота прорисовки военного корабля
|
||||
/// </summary>
|
||||
private readonly int _drawingWarshipHeight = 75;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Х объекта
|
||||
/// </summary>
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawingWarshipWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawingWarshipHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private DrawingWarship()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public DrawingWarship(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
EntityWarship = new EntityWarship(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор для наследников
|
||||
/// </summary>
|
||||
/// <param name="drawingWarshipWidth">Ширина прорисовки военного корабля</param>
|
||||
/// <param name="drawingWarshipHeight">Высота прорисовки военного корабля</param>
|
||||
protected DrawingWarship(int drawingWarshipWidth, int drawingWarshipHeight) : this()
|
||||
{
|
||||
_drawingWarshipWidth = drawingWarshipWidth;
|
||||
_drawingWarshipHeight = drawingWarshipHeight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||||
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
//TODO проверка, что объект "влезает" в размеры поля
|
||||
//если влезает, сохраняем границы и корректируем позицию объекта, если она уже была установлена
|
||||
|
||||
if (_drawingWarshipWidth > width || _drawingWarshipHeight > height)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
|
||||
if (_startPosX.HasValue || _startPosY.HasValue)
|
||||
{
|
||||
|
||||
if (_startPosX + _drawingWarshipWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawingWarshipWidth;
|
||||
}
|
||||
else if (_startPosX < 0) _startPosX = 0;
|
||||
if (_startPosY + _drawingWarshipHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawingWarshipHeight;
|
||||
}
|
||||
else if (_startPosY < 0) _startPosY = 0;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
||||
// то надо изменить координаты, чтобы он оставался в этих границах
|
||||
|
||||
if (x + _drawingWarshipWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawingWarshipWidth;
|
||||
}
|
||||
else if (x < 0) _startPosX = 0;
|
||||
else _startPosX = x;
|
||||
|
||||
if (y + _drawingWarshipHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawingWarshipHeight;
|
||||
}
|
||||
else if (y < 0) _startPosY = 0;
|
||||
else _startPosY = y;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
||||
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityWarship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityWarship.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityWarship.Step;
|
||||
}
|
||||
return true;
|
||||
|
||||
//вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + EntityWarship.Step + _drawingWarshipWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityWarship.Step;
|
||||
}
|
||||
return true;
|
||||
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityWarship.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityWarship.Step;
|
||||
}
|
||||
return true;
|
||||
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY.Value + EntityWarship.Step + _drawingWarshipHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityWarship.Step;
|
||||
}
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityWarship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
|
||||
Brush main = new SolidBrush(EntityWarship.BodyColor);
|
||||
//Заливка линкора
|
||||
Point point1 = new Point(_startPosX.Value + 5, _startPosY.Value + 5);
|
||||
Point point2 = new Point(_startPosX.Value + 110, _startPosY.Value + 5);
|
||||
Point point3 = new Point(_startPosX.Value + 150, _startPosY.Value + 42);
|
||||
Point point4 = new Point(_startPosX.Value + 110, _startPosY.Value + 80);
|
||||
Point point5 = new Point(_startPosX.Value + 5, _startPosY.Value + 80);
|
||||
Point point6 = new Point(_startPosX.Value + 5, _startPosY.Value + 5);
|
||||
|
||||
Point[] LinkorLine = { point1, point2, point3, point4, point5, point6 };
|
||||
g.FillPolygon(main, LinkorLine);
|
||||
|
||||
//границы линкора
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 5, _startPosX.Value + 110, _startPosY.Value + 5);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 80, _startPosX.Value + 110, _startPosY.Value + 80);
|
||||
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 5, _startPosX.Value + 5, _startPosY.Value + 80);
|
||||
g.DrawLine(pen, _startPosX.Value + 110, _startPosY.Value + 5, _startPosX.Value + 150, _startPosY.Value + 42);
|
||||
g.DrawLine(pen, _startPosX.Value + 110, _startPosY.Value + 80, _startPosX.Value + 150, _startPosY.Value + 42);
|
||||
|
||||
|
||||
//ракета
|
||||
Brush brfara = new SolidBrush(Color.Blue);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 35, 15, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 40, 15, 5);
|
||||
g.FillRectangle(brfara, _startPosX.Value + 25, _startPosY.Value + 35, 15, 15);
|
||||
g.FillRectangle(brfara, _startPosX.Value + 10, _startPosY.Value + 40, 15, 5);
|
||||
|
||||
//Парус
|
||||
g.DrawArc(pen, _startPosX.Value + 45, _startPosY.Value + 15, 35, 60, 280, 160);
|
||||
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 17, _startPosX.Value + 67, _startPosY.Value + 72);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 72);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 17);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 45);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 35);
|
||||
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 45, _startPosX.Value + 67, _startPosY.Value + 55);
|
||||
|
||||
//Моторы
|
||||
Brush br = new SolidBrush(Color.Black);
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 59, 5, 15);
|
||||
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 59, 5, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 10, 5, 15);
|
||||
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 10, 5, 15);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
65
ProjectSportCar/ProjectSportCar/Drawnings/DrawningLinkor.cs
Normal file
65
ProjectSportCar/ProjectSportCar/Drawnings/DrawningLinkor.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using ProjectLinkor.Entities;
|
||||
|
||||
namespace ProjectLinkor.Drawnings;
|
||||
/// <summary>
|
||||
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningLinkor : DrawingWarship
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weigth">Вес линкора</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="gunTurret">Признак наличия орудийной башни</param>
|
||||
/// <param name="compartment">Признак наличия отсека под ракеты</param>
|
||||
/// <param name="linkorMotor">Признак наличия</param>
|
||||
public DrawningLinkor(int speed, double weigth, Color bodyColor, Color additionalColor, bool gunTurret, bool compartment, bool linkorMotor) : base(148, 75)
|
||||
{
|
||||
EntityWarship = new EntityLinkor(speed, weigth, bodyColor, additionalColor, gunTurret, compartment, linkorMotor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityWarship == null || EntityWarship is not EntityLinkor linkor || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(linkor.AdditionalColor);
|
||||
|
||||
base.DrawTransport(g);
|
||||
|
||||
//Орудийная башня
|
||||
if (linkor.GunTurret)
|
||||
{
|
||||
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 27, 30, 30);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 32, 20, 20);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 90, _startPosY.Value + 27, 30, 30);
|
||||
g.FillEllipse(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 32, 20, 20);
|
||||
}
|
||||
|
||||
//Дополнительнst ракетs
|
||||
if (linkor.Сompartment)
|
||||
{
|
||||
//границы ракет
|
||||
g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 59, 15, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 64, 15, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 25, _startPosY.Value + 12, 15, 15);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 17, 15, 5);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 25, _startPosY.Value + 59, 15, 15);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 64, 15, 5);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 25, _startPosY.Value + 12, 15, 15);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 17, 15, 5);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,23 +1,20 @@
|
||||
namespace ProjectLinkor;
|
||||
namespace ProjectLinkor.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущность "Линкор"
|
||||
/// </summary>
|
||||
public class EntityLinkor
|
||||
public class EntityLinkor : EntityWarship
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weigth { get; private set; }
|
||||
|
||||
public double Weight { get; private set; }
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
|
||||
@ -42,12 +39,12 @@ public class EntityLinkor
|
||||
public bool LinkorMotor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг перемещения линкора
|
||||
/// Перемещение линкора
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weigth;
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
/// <summary>
|
||||
/// Иницилизация полей объекта-класса линкора
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="speed">Скорость</param>
|
||||
@ -57,10 +54,10 @@ public class EntityLinkor
|
||||
/// <param name="gunTurret">Признак наличия орудийной башни</param>
|
||||
/// <param name="compartment">Признак наличия отсека под ракеты</param>
|
||||
/// <param name="linkorMotor">Признак наличия</param>
|
||||
public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool gunTurret, bool compartment, bool linkorMotor)
|
||||
public EntityLinkor(int speed, double weigth, Color bodyColor, Color additionalColor, bool gunTurret, bool compartment, bool linkorMotor) : base(speed, weigth, bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weigth = weigth;
|
||||
Weight = weigth;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
GunTurret = gunTurret;
|
37
ProjectSportCar/ProjectSportCar/Entities/EntityWarship.cs
Normal file
37
ProjectSportCar/ProjectSportCar/Entities/EntityWarship.cs
Normal file
@ -0,0 +1,37 @@
|
||||
namespace ProjectLinkor.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущность "Военный корабль"
|
||||
/// </summary>
|
||||
public class EntityWarship
|
||||
{
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; private set; }
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; private set; }
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Перемещение военного корабля
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор сущности
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public EntityWarship(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -34,6 +34,9 @@
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonWarship = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxLinkor).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -42,18 +45,18 @@
|
||||
pictureBoxLinkor.Dock = DockStyle.Fill;
|
||||
pictureBoxLinkor.Location = new Point(0, 0);
|
||||
pictureBoxLinkor.Name = "pictureBoxLinkor";
|
||||
pictureBoxLinkor.Size = new Size(857, 474);
|
||||
pictureBoxLinkor.Size = new Size(931, 524);
|
||||
pictureBoxLinkor.TabIndex = 0;
|
||||
pictureBoxLinkor.TabStop = false;
|
||||
//
|
||||
// buttonCreate
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 433);
|
||||
buttonCreate.Location = new Point(12, 483);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(94, 29);
|
||||
buttonCreate.Size = new Size(191, 29);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.Text = "Создать Линкор";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += ButtonCreate_Click;
|
||||
//
|
||||
@ -62,7 +65,7 @@
|
||||
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonLeft.BackgroundImage = Properties.Resources.str_Left;
|
||||
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonLeft.Location = new Point(732, 427);
|
||||
buttonLeft.Location = new Point(806, 477);
|
||||
buttonLeft.Name = "buttonLeft";
|
||||
buttonLeft.Size = new Size(35, 35);
|
||||
buttonLeft.TabIndex = 2;
|
||||
@ -74,7 +77,7 @@
|
||||
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonRight.BackgroundImage = Properties.Resources.str_Right;
|
||||
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonRight.Location = new Point(814, 427);
|
||||
buttonRight.Location = new Point(888, 477);
|
||||
buttonRight.Name = "buttonRight";
|
||||
buttonRight.Size = new Size(35, 35);
|
||||
buttonRight.TabIndex = 3;
|
||||
@ -86,7 +89,7 @@
|
||||
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonUp.BackgroundImage = Properties.Resources.str_Up;
|
||||
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonUp.Location = new Point(773, 387);
|
||||
buttonUp.Location = new Point(847, 437);
|
||||
buttonUp.Name = "buttonUp";
|
||||
buttonUp.Size = new Size(35, 35);
|
||||
buttonUp.TabIndex = 4;
|
||||
@ -98,18 +101,52 @@
|
||||
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonDown.BackgroundImage = Properties.Resources.str_Down;
|
||||
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonDown.Location = new Point(773, 427);
|
||||
buttonDown.Location = new Point(847, 477);
|
||||
buttonDown.Name = "buttonDown";
|
||||
buttonDown.Size = new Size(35, 35);
|
||||
buttonDown.TabIndex = 5;
|
||||
buttonDown.UseVisualStyleBackColor = true;
|
||||
buttonDown.Click += ButtonMove_Click;
|
||||
//
|
||||
// buttonWarship
|
||||
//
|
||||
buttonWarship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonWarship.Location = new Point(209, 483);
|
||||
buttonWarship.Name = "buttonWarship";
|
||||
buttonWarship.Size = new Size(216, 29);
|
||||
buttonWarship.TabIndex = 6;
|
||||
buttonWarship.Text = "Создать Военный корабль";
|
||||
buttonWarship.UseVisualStyleBackColor = true;
|
||||
buttonWarship.Click += ButtonWarship_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||
comboBoxStrategy.Location = new Point(768, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(151, 28);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Location = new Point(825, 55);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(94, 29);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "Шаг";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += ButtonStrategyStep_Click;
|
||||
//
|
||||
// FormLinkor
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(857, 474);
|
||||
ClientSize = new Size(931, 524);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonWarship);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
@ -130,5 +167,8 @@
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonDown;
|
||||
private Button buttonWarship;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,81 +1,151 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using ProjectLinkor.Drawnings;
|
||||
using ProjectLinkor.MovementStrategy;
|
||||
|
||||
namespace ProjectLinkor
|
||||
namespace ProjectLinkor;
|
||||
|
||||
public partial class FormLinkor : Form
|
||||
{
|
||||
public partial class FormLinkor : Form
|
||||
private DrawingWarship? _drawningWarship;
|
||||
|
||||
/// <summary>
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _strategy;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор формы
|
||||
/// </summary>
|
||||
public FormLinkor()
|
||||
{
|
||||
private DrawningLinkor? _drawningLinkor;
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
|
||||
public FormLinkor()
|
||||
private void Draw()
|
||||
{
|
||||
|
||||
if (_drawningWarship == null)
|
||||
{
|
||||
InitializeComponent();
|
||||
return;
|
||||
}
|
||||
|
||||
private void Draw()
|
||||
Bitmap bmp = new(pictureBoxLinkor.Width, pictureBoxLinkor.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningWarship.DrawTransport(gr);
|
||||
pictureBoxLinkor.Image = bmp;
|
||||
}
|
||||
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
|
||||
if (_drawningLinkor == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new(pictureBoxLinkor.Width, pictureBoxLinkor.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningLinkor.DrawTransport(gr);
|
||||
pictureBoxLinkor.Image = bmp;
|
||||
}
|
||||
|
||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningLinkor = new DrawningLinkor();
|
||||
_drawningLinkor.Init(random.Next(100, 300), random.Next(1000, 3000),
|
||||
case nameof(DrawingWarship):
|
||||
_drawningWarship = new DrawingWarship(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
||||
break;
|
||||
case nameof(DrawningLinkor):
|
||||
_drawningWarship = new DrawningLinkor(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
||||
_drawningLinkor.SetPictureSize(pictureBoxLinkor.Width, pictureBoxLinkor.Height);
|
||||
_drawningLinkor.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
_drawningWarship.SetPictureSize(pictureBoxLinkor.Width, pictureBoxLinkor.Height);
|
||||
_drawningWarship.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxLinkor.Width, pictureBoxLinkor.Height);
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
|
||||
Draw();
|
||||
/// <summary>
|
||||
/// обработка нажатия кнопки "создать линкор"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLinkor));
|
||||
|
||||
/// <summary>
|
||||
/// обработка нажатия кнопки "создать военный корабль"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonWarship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingWarship));
|
||||
|
||||
/// <summary>
|
||||
/// кнопки вверх/вниз/влево/вправо
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningWarship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
{
|
||||
if (_drawningLinkor == null)
|
||||
case "buttonUp":
|
||||
result = _drawningWarship.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningWarship.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningWarship.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningWarship.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningWarship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStrategy.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableWarship(_drawningWarship),
|
||||
pictureBoxLinkor.Width, pictureBoxLinkor.Height);
|
||||
}
|
||||
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningLinkor.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningLinkor.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningLinkor.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningLinkor.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
comboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,140 @@
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObjectcs? _moveableObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private StrategyStatus _state = StrategyStatus.NotIInit;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set;}
|
||||
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
|
||||
/// <summary>
|
||||
/// Установка данных
|
||||
/// </summary>
|
||||
/// <param name="moveableObjectcs">Перемещаемый объект</param>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
public void SetData(IMoveableObjectcs moveableObjectcs, int width, int height)
|
||||
{
|
||||
if (moveableObjectcs == null)
|
||||
{
|
||||
_state = StrategyStatus.NotIInit;
|
||||
return;
|
||||
}
|
||||
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObjects = moveableObjectcs;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObjects?.GetObjectPosition;
|
||||
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObjects?.GetStep;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestination();
|
||||
|
||||
/// <summary>
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// </summary>
|
||||
/// <param name="movementDirection"></param>
|
||||
/// <returns></returns>
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _moveableObjects?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
public interface IMoveableObjectcs
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение кординаты объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Попытка переместить объект в указанном направлении
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return objParams.RightBorder - GetStep() <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder - GetStep() <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int diffX = objParams.RightBorder - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
|
||||
int diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return objParams.ObjectMiddleHorizontall - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontall + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int diffX = objParams.ObjectMiddleHorizontall - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
|
||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
using ProjectLinkor.Drawnings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-реализация IMoveableObjectcs с использованием DrawingWarship
|
||||
/// </summary>
|
||||
public class MoveableWarship : IMoveableObjectcs
|
||||
{
|
||||
/// <summary>
|
||||
/// Gле-объект класса DrawingWarship или его наследника
|
||||
/// </summary>
|
||||
private DrawingWarship? _warship = null;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="warship"></param>
|
||||
public MoveableWarship(DrawingWarship warship)
|
||||
{
|
||||
_warship = warship;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_warship == null || _warship.EntityWarship == null || !_warship.GetPosX.HasValue || !_warship.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_warship.GetPosX.Value, _warship.GetPosY.Value, _warship.GetWidth, _warship.GetHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetStep => (int)(_warship?.EntityWarship?.Step ?? 0);
|
||||
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_warship == null || _warship.EntityWarship == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _warship.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конвертация из MovementDirection в DirectionType
|
||||
/// </summary>
|
||||
/// <param name="direction">MovementDirection</param>
|
||||
/// <returns>DirectionType</returns>
|
||||
private static DirectionType GetDirectionType(MovementDirection direction)
|
||||
{
|
||||
return direction switch
|
||||
{
|
||||
MovementDirection.Left => DirectionType.Left,
|
||||
MovementDirection.Right => DirectionType.Right,
|
||||
MovementDirection.Up => DirectionType.Up,
|
||||
MovementDirection.Down => DirectionType.Down,
|
||||
_ => DirectionType.Unknow,
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
public enum MovementDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
public class ObjectParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// Координата Х
|
||||
/// </summary>
|
||||
private readonly int _x;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Y
|
||||
/// </summary>
|
||||
private readonly int _y;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
private readonly int _width;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
private readonly int _height;
|
||||
|
||||
/// <summary>
|
||||
/// Левая граница
|
||||
/// </summary>
|
||||
public int LeftBorder => _x;
|
||||
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
public int TopBorder => _y;
|
||||
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
public int RightBorder => _x + _width;
|
||||
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
public int DownBorder => _y + _height;
|
||||
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontall => _x + _width / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="x">Координата Х</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
/// <param name="width">Ширина объекта</param>
|
||||
/// <param name="height">Высота объекта</param>
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
namespace ProjectLinkor.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Статус выполнения операции перемещения
|
||||
/// </summary>
|
||||
public enum StrategyStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Все готово к началу
|
||||
/// </summary>
|
||||
NotIInit,
|
||||
|
||||
/// <summary>
|
||||
/// Выполняется
|
||||
/// </summary>
|
||||
InProgress,
|
||||
|
||||
/// <summary>
|
||||
/// Завершено
|
||||
/// </summary>
|
||||
Finish
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user