Лабораторная работа №2
This commit is contained in:
parent
c853fe4b1f
commit
d1cc8a693a
@ -1,22 +1,30 @@
|
|||||||
namespace ProjectLiner;
|
namespace ProjectLiner.Drawnings;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Направление передвижения
|
/// Направление передвижения
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public enum DirectionType
|
public enum DirectionType
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Неизвестное направление
|
||||||
|
/// </summary>
|
||||||
|
Unknow = -1,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вверх
|
/// Вверх
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Up = 1,
|
Up = 1,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вниз
|
/// Вниз
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Down = 2,
|
Down = 2,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Влево
|
/// Влево
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Left = 3,
|
Left = 3,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вправо
|
/// Вправо
|
||||||
/// </summary>
|
/// </summary>
|
69
ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs
Normal file
69
ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
using ProjectLiner.Entities;
|
||||||
|
|
||||||
|
namespace ProjectLiner.Drawnings;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-прорисовка сущности "Лайнер"
|
||||||
|
/// </summary>
|
||||||
|
public class DrawningLiner : DrawningSmallLiner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||||
|
/// <param name="secondDeck">Наличие второй палубы</param>
|
||||||
|
/// <param name="pool">Наличие басейна</param>
|
||||||
|
public DrawningLiner(int speed, double weight, Color bodyColor, Color additionalColor, bool secondDeck, bool pool) : base(150, 65)
|
||||||
|
{
|
||||||
|
EntitySmallLiner = new EntityLiner(speed, weight, bodyColor, additionalColor, secondDeck, pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (EntitySmallLiner == null || EntitySmallLiner is not EntityLiner Liner || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pen pen = new(Color.Black, 2);
|
||||||
|
Brush br = new SolidBrush(Liner.BodyColor);
|
||||||
|
Brush additionalBrush = new SolidBrush(Liner.AdditionalColor);
|
||||||
|
|
||||||
|
// Вторая палуба
|
||||||
|
if (Liner.SecondDeck)
|
||||||
|
{
|
||||||
|
g.FillPolygon(additionalBrush, new[]
|
||||||
|
{
|
||||||
|
new Point(_startPosX.Value + 20, _startPosY.Value + 15),
|
||||||
|
new Point(_startPosX.Value + 30, _startPosY.Value + 0),
|
||||||
|
new Point(_startPosX.Value + 95, _startPosY.Value + 0),
|
||||||
|
new Point(_startPosX.Value + 100, _startPosY.Value + 15)
|
||||||
|
});
|
||||||
|
g.DrawPolygon(pen, new[]
|
||||||
|
{
|
||||||
|
new Point(_startPosX.Value + 20, _startPosY.Value + 15),
|
||||||
|
new Point(_startPosX.Value + 30, _startPosY.Value + 0),
|
||||||
|
new Point(_startPosX.Value + 95, _startPosY.Value + 0),
|
||||||
|
new Point(_startPosX.Value + 100, _startPosY.Value + 15)
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Бассейн
|
||||||
|
if (Liner.Pool)
|
||||||
|
{
|
||||||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 110, _startPosY.Value + 25, 30, 5);
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + 25, 30, 5);
|
||||||
|
g.DrawArc(pen, _startPosX.Value + 135, _startPosY.Value + 20, 8, 8, 180, 180);
|
||||||
|
g.DrawLine(pen, _startPosX.Value + 143, _startPosY.Value + 25, _startPosX.Value + 143, _startPosY.Value + 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Основные элементы
|
||||||
|
_startPosY += 15;
|
||||||
|
base.DrawTransport(g);
|
||||||
|
_startPosY -= 15;
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +1,13 @@
|
|||||||
namespace ProjectLiner;
|
using ProjectLiner.Entities;
|
||||||
|
|
||||||
/// <summary>
|
namespace ProjectLiner.Drawnings;
|
||||||
/// Класс-прорисовка сущности "Лайнер"
|
|
||||||
/// </summary>
|
public class DrawningSmallLiner
|
||||||
public class DrawningLiner
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность "Лайнер"
|
/// Класс-сущность "Маленький Лайнер"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public EntityLiner? EntityLiner { get; private set; }
|
public EntitySmallLiner? EntitySmallLiner { get; protected set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина окна
|
/// Ширина окна
|
||||||
@ -23,12 +22,12 @@ public class DrawningLiner
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Левая координата прорисовки сущности "Лайнер"
|
/// Левая координата прорисовки сущности "Лайнер"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int? _startPosX;
|
protected int? _startPosX;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Верхнаяя координата прорисовки сущности "Лайнер"
|
/// Верхнаяя координата прорисовки сущности "Лайнер"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int? _startPosY;
|
protected int? _startPosY;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина прорисовки сущности "Лайнер"
|
/// Ширина прорисовки сущности "Лайнер"
|
||||||
@ -36,29 +35,64 @@ public class DrawningLiner
|
|||||||
private readonly int _drawningLinerWidth = 150;
|
private readonly int _drawningLinerWidth = 150;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Высота сущности "Лайнер"
|
/// Высота прорисовки сущности "Лайнер"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _drawningLinerHeight = 65;
|
private readonly int _drawningLinerHeight = 50;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Инициализация свойств
|
/// Координата X объекта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="speed">Скорость</param>
|
public int? GetPosX => _startPosX;
|
||||||
/// <param name="weight">Вес</param>
|
|
||||||
/// <param name="bodyColor">Основной цвет</param>
|
/// <summary>
|
||||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
/// Координата Y объекта
|
||||||
/// <param name="secondDeck">Наличие второй палубы</param>
|
/// </summary>
|
||||||
/// <param name="pool">Наличие басейна</param>
|
public int? GetPosY => _startPosY;
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool secondDeck, bool pool)
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetWidth => _drawningLinerWidth;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Высота объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetHeight => _drawningLinerHeight;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Пустой конструктор
|
||||||
|
/// </summary>
|
||||||
|
private DrawningSmallLiner()
|
||||||
{
|
{
|
||||||
EntityLiner = new EntityLiner();
|
|
||||||
EntityLiner.Init(speed, weight, bodyColor, additionalColor, secondDeck, pool);
|
|
||||||
_pictureWidth = null;
|
_pictureWidth = null;
|
||||||
_pictureHeight = null;
|
_pictureHeight = null;
|
||||||
_startPosX = null;
|
_startPosX = null;
|
||||||
_startPosY = null;
|
_startPosY = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
public DrawningSmallLiner(int speed, double weight, Color bodyColor) : this()
|
||||||
|
{
|
||||||
|
EntitySmallLiner = new EntitySmallLiner(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор для наследников
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="drawningLinerWidth">Ширина прорисовки сущности "Маленький Лайнер"</param>
|
||||||
|
/// <param name="drawningLinerHeight">Высота прорисовки сущности "Маленький Лайнер"</param>
|
||||||
|
protected DrawningSmallLiner(int drawningLinerWidth, int drawningLinerHeight) : this()
|
||||||
|
{
|
||||||
|
_drawningLinerWidth = drawningLinerWidth;
|
||||||
|
_drawningLinerHeight = drawningLinerHeight;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Установка границ поля
|
/// Установка границ поля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -73,8 +107,6 @@ public class DrawningLiner
|
|||||||
}
|
}
|
||||||
_pictureWidth = width;
|
_pictureWidth = width;
|
||||||
_pictureHeight = height;
|
_pictureHeight = height;
|
||||||
if (_startPosX.HasValue && _startPosX + _drawningLinerWidth > width) _startPosX = _pictureWidth - _drawningLinerWidth;
|
|
||||||
if (_startPosY.HasValue && _startPosY + _drawningLinerHeight > height) _startPosY = _pictureHeight - _drawningLinerHeight;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +148,7 @@ public class DrawningLiner
|
|||||||
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
||||||
public bool MoveTransport(DirectionType direction)
|
public bool MoveTransport(DirectionType direction)
|
||||||
{
|
{
|
||||||
if (EntityLiner == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
if (EntitySmallLiner == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -124,34 +156,31 @@ public class DrawningLiner
|
|||||||
{
|
{
|
||||||
// Влево
|
// Влево
|
||||||
case DirectionType.Left:
|
case DirectionType.Left:
|
||||||
if (_startPosX.Value - EntityLiner.Step > 0)
|
if (_startPosX.Value - EntitySmallLiner.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosX -= (int)EntityLiner.Step;
|
_startPosX -= (int)EntitySmallLiner.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Вверх
|
// Вверх
|
||||||
case DirectionType.Up:
|
case DirectionType.Up:
|
||||||
if (_startPosY.Value - EntityLiner.Step > 0)
|
if (_startPosY.Value - EntitySmallLiner.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosY -= (int)EntityLiner.Step;
|
_startPosY -= (int)EntitySmallLiner.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Вправо
|
// Вправо
|
||||||
case DirectionType.Right:
|
case DirectionType.Right:
|
||||||
|
|
||||||
if (_startPosX.Value + _drawningLinerWidth + EntityLiner.Step < _pictureWidth.Value)
|
if (_startPosX.Value + _drawningLinerWidth + EntitySmallLiner.Step < _pictureWidth.Value)
|
||||||
{
|
{
|
||||||
_startPosX += (int)EntityLiner.Step;
|
_startPosX += (int)EntitySmallLiner.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Вниз
|
// Вниз
|
||||||
case DirectionType.Down:
|
case DirectionType.Down:
|
||||||
if (_startPosY.Value + _drawningLinerHeight + EntityLiner.Step < _pictureHeight.Value)
|
if (_startPosY.Value + _drawningLinerHeight + EntitySmallLiner.Step < _pictureHeight.Value)
|
||||||
{
|
{
|
||||||
_startPosY += (int)EntityLiner.Step;
|
_startPosY += (int)EntitySmallLiner.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
@ -163,79 +192,50 @@ public class DrawningLiner
|
|||||||
/// Прорисовка объекта
|
/// Прорисовка объекта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="g"></param>
|
/// <param name="g"></param>
|
||||||
public void DrawTransport(Graphics g)
|
public virtual void DrawTransport(Graphics g)
|
||||||
{
|
{
|
||||||
if (EntityLiner == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
if (EntitySmallLiner == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Pen pen = new(Color.Black, 2);
|
Pen pen = new(Color.Black, 2);
|
||||||
Brush br = new SolidBrush(EntityLiner.BodyColor);
|
Brush br = new SolidBrush(EntitySmallLiner.BodyColor);
|
||||||
Brush additionalBrush = new SolidBrush(EntityLiner.AdditionalColor);
|
|
||||||
|
|
||||||
// Вторая палуба
|
|
||||||
if (EntityLiner.SecondDeck)
|
|
||||||
{
|
|
||||||
g.FillPolygon(additionalBrush, new[]
|
|
||||||
{
|
|
||||||
new Point(_startPosX.Value + 20, _startPosY.Value + 15),
|
|
||||||
new Point(_startPosX.Value + 30, _startPosY.Value + 0),
|
|
||||||
new Point(_startPosX.Value + 95, _startPosY.Value + 0),
|
|
||||||
new Point(_startPosX.Value + 100, _startPosY.Value + 15)
|
|
||||||
});
|
|
||||||
g.DrawPolygon(pen, new[]
|
|
||||||
{
|
|
||||||
new Point(_startPosX.Value + 20, _startPosY.Value + 15),
|
|
||||||
new Point(_startPosX.Value + 30, _startPosY.Value + 0),
|
|
||||||
new Point(_startPosX.Value + 95, _startPosY.Value + 0),
|
|
||||||
new Point(_startPosX.Value + 100, _startPosY.Value + 15)
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Бассейн
|
|
||||||
if (EntityLiner.Pool)
|
|
||||||
{
|
|
||||||
g.FillRectangle(additionalBrush, _startPosX.Value + 110, _startPosY.Value + 25, 30, 5);
|
|
||||||
g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + 25, 30, 5);
|
|
||||||
g.DrawArc(pen, _startPosX.Value + 135, _startPosY.Value + 20, 8, 8, 180, 180);
|
|
||||||
g.DrawLine(pen, _startPosX.Value + 143, _startPosY.Value + 25, _startPosX.Value + 143, _startPosY.Value + 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Корпус и палуба Лайнера
|
// Корпус и палуба Лайнера
|
||||||
g.FillPolygon(br, new[]
|
g.FillPolygon(br, new[]
|
||||||
{
|
{
|
||||||
new Point(_startPosX.Value + 0, _startPosY.Value + 30),
|
new Point(_startPosX.Value + 0, _startPosY.Value + 15),
|
||||||
new Point(_startPosX.Value + 150, _startPosY.Value + 30),
|
new Point(_startPosX.Value + 150, _startPosY.Value + 15),
|
||||||
new Point(_startPosX.Value + 120, _startPosY.Value + 65),
|
new Point(_startPosX.Value + 120, _startPosY.Value + 50),
|
||||||
new Point(_startPosX.Value + 30, _startPosY.Value + 65)
|
new Point(_startPosX.Value + 30, _startPosY.Value + 50)
|
||||||
});
|
});
|
||||||
g.FillPolygon(br, new[]
|
g.FillPolygon(br, new[]
|
||||||
{
|
{
|
||||||
new Point(_startPosX.Value + 5, _startPosY.Value + 30),
|
new Point(_startPosX.Value + 5, _startPosY.Value + 15),
|
||||||
new Point(_startPosX.Value + 15, _startPosY.Value + 15),
|
new Point(_startPosX.Value + 15, _startPosY.Value + 0),
|
||||||
new Point(_startPosX.Value + 100, _startPosY.Value + 15),
|
new Point(_startPosX.Value + 100, _startPosY.Value + 0),
|
||||||
new Point(_startPosX.Value + 105, _startPosY.Value + 30)
|
new Point(_startPosX.Value + 105, _startPosY.Value + 15)
|
||||||
|
});
|
||||||
|
|
||||||
|
g.DrawPolygon(pen, new[]
|
||||||
|
{
|
||||||
|
new Point(_startPosX.Value + 0, _startPosY.Value + 15),
|
||||||
|
new Point(_startPosX.Value + 150, _startPosY.Value + 15),
|
||||||
|
new Point(_startPosX.Value + 120, _startPosY.Value + 50),
|
||||||
|
new Point(_startPosX.Value + 30, _startPosY.Value + 50)
|
||||||
});
|
});
|
||||||
g.DrawPolygon(pen, new[]
|
g.DrawPolygon(pen, new[]
|
||||||
{
|
{
|
||||||
new Point(_startPosX.Value + 0, _startPosY.Value + 30),
|
new Point(_startPosX.Value + 5, _startPosY.Value + 15),
|
||||||
new Point(_startPosX.Value + 150, _startPosY.Value + 30),
|
new Point(_startPosX.Value + 15, _startPosY.Value + 0),
|
||||||
new Point(_startPosX.Value + 120, _startPosY.Value + 65),
|
new Point(_startPosX.Value + 100, _startPosY.Value + 0),
|
||||||
new Point(_startPosX.Value + 30, _startPosY.Value + 65)
|
new Point(_startPosX.Value + 105, _startPosY.Value + 15)
|
||||||
});
|
|
||||||
g.DrawPolygon(pen, new[]
|
|
||||||
{
|
|
||||||
new Point(_startPosX.Value + 5, _startPosY.Value + 30),
|
|
||||||
new Point(_startPosX.Value + 15, _startPosY.Value + 15),
|
|
||||||
new Point(_startPosX.Value + 100, _startPosY.Value + 15),
|
|
||||||
new Point(_startPosX.Value + 105, _startPosY.Value + 30)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Якорь
|
// Якорь
|
||||||
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 33, _startPosX.Value + 30, _startPosY.Value + 45);
|
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 18, _startPosX.Value + 30, _startPosY.Value + 30);
|
||||||
g.DrawLine(pen, _startPosX.Value + 27, _startPosY.Value + 36, _startPosX.Value + 33, _startPosY.Value + 36);
|
g.DrawLine(pen, _startPosX.Value + 27, _startPosY.Value + 21, _startPosX.Value + 33, _startPosY.Value + 21);
|
||||||
g.DrawLine(pen, _startPosX.Value + 25, _startPosY.Value + 40, _startPosX.Value + 30, _startPosY.Value + 45);
|
g.DrawLine(pen, _startPosX.Value + 25, _startPosY.Value + 25, _startPosX.Value + 30, _startPosY.Value + 30);
|
||||||
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 45, _startPosX.Value + 35, _startPosY.Value + 40);
|
g.DrawLine(pen, _startPosX.Value + 30, _startPosY.Value + 30, _startPosX.Value + 35, _startPosY.Value + 25);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,25 +1,10 @@
|
|||||||
namespace ProjectLiner;
|
namespace ProjectLiner.Entities;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность "Лайнер"
|
/// Класс-сущность "Лайнер"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class EntityLiner
|
public class EntityLiner : EntitySmallLiner
|
||||||
{
|
{
|
||||||
/// <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>
|
||||||
/// Дополнительный цвет
|
/// Дополнительный цвет
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -36,12 +21,7 @@ public class EntityLiner
|
|||||||
public bool Pool { get; private set; }
|
public bool Pool { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Шаг перемещения сущности "Лайнер"
|
/// Конструктор
|
||||||
/// </summary>
|
|
||||||
public double Step => Speed * 100 / Weight;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Инициализация полей объекта класса "Лайнер"
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="speed">Скорость</param>
|
/// <param name="speed">Скорость</param>
|
||||||
/// <param name="weight">Вес</param>
|
/// <param name="weight">Вес</param>
|
||||||
@ -49,11 +29,8 @@ public class EntityLiner
|
|||||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||||
/// <param name="secondDeck">Наличие второй палубы</param>
|
/// <param name="secondDeck">Наличие второй палубы</param>
|
||||||
/// <param name="pool">Наличие бассейна</param>
|
/// <param name="pool">Наличие бассейна</param>
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool secondDeck, bool pool)
|
public EntityLiner(int speed, double weight, Color bodyColor, Color additionalColor, bool secondDeck, bool pool) : base(speed, weight, bodyColor)
|
||||||
{
|
{
|
||||||
Speed = speed;
|
|
||||||
Weight = weight;
|
|
||||||
BodyColor = bodyColor;
|
|
||||||
AdditionalColor = additionalColor;
|
AdditionalColor = additionalColor;
|
||||||
SecondDeck = secondDeck;
|
SecondDeck = secondDeck;
|
||||||
Pool = pool;
|
Pool = pool;
|
40
ProjectLiner/ProjectLiner/Entities/EntitySmallLiner.cs
Normal file
40
ProjectLiner/ProjectLiner/Entities/EntitySmallLiner.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
namespace ProjectLiner.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-сущность "Маленький Лайнер"
|
||||||
|
/// </summary>
|
||||||
|
public class EntitySmallLiner
|
||||||
|
{
|
||||||
|
/// <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 EntitySmallLiner(int speed, double weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
Speed = speed;
|
||||||
|
Weight = weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
}
|
||||||
|
}
|
64
ProjectLiner/ProjectLiner/FormLiner.Designer.cs
generated
64
ProjectLiner/ProjectLiner/FormLiner.Designer.cs
generated
@ -29,11 +29,14 @@
|
|||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
pictureBoxLiner = new PictureBox();
|
pictureBoxLiner = new PictureBox();
|
||||||
buttonCreate = new Button();
|
buttonCreateSmallLiner = new Button();
|
||||||
buttonRight = new Button();
|
buttonRight = new Button();
|
||||||
buttonDown = new Button();
|
buttonDown = new Button();
|
||||||
buttonLeft = new Button();
|
buttonLeft = new Button();
|
||||||
buttonUp = new Button();
|
buttonUp = new Button();
|
||||||
|
buttonCreateLiner = new Button();
|
||||||
|
comboBoxStrategy = new ComboBox();
|
||||||
|
buttonStrategyStep = new Button();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -46,16 +49,16 @@
|
|||||||
pictureBoxLiner.TabIndex = 0;
|
pictureBoxLiner.TabIndex = 0;
|
||||||
pictureBoxLiner.TabStop = false;
|
pictureBoxLiner.TabStop = false;
|
||||||
//
|
//
|
||||||
// buttonCreate
|
// buttonCreateSmallLiner
|
||||||
//
|
//
|
||||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
buttonCreateSmallLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
buttonCreate.Location = new Point(12, 426);
|
buttonCreateSmallLiner.Location = new Point(12, 426);
|
||||||
buttonCreate.Name = "buttonCreate";
|
buttonCreateSmallLiner.Name = "buttonCreateSmallLiner";
|
||||||
buttonCreate.Size = new Size(75, 23);
|
buttonCreateSmallLiner.Size = new Size(154, 23);
|
||||||
buttonCreate.TabIndex = 1;
|
buttonCreateSmallLiner.TabIndex = 1;
|
||||||
buttonCreate.Text = "Создать";
|
buttonCreateSmallLiner.Text = "Создать малый лайнер";
|
||||||
buttonCreate.UseVisualStyleBackColor = true;
|
buttonCreateSmallLiner.UseVisualStyleBackColor = true;
|
||||||
buttonCreate.Click += ButtonCreate_Click;
|
buttonCreateSmallLiner.Click += ButtonCreateSmallLiner_Click;
|
||||||
//
|
//
|
||||||
// buttonRight
|
// buttonRight
|
||||||
//
|
//
|
||||||
@ -105,16 +108,50 @@
|
|||||||
buttonUp.UseVisualStyleBackColor = true;
|
buttonUp.UseVisualStyleBackColor = true;
|
||||||
buttonUp.Click += ButtonMove_Click;
|
buttonUp.Click += ButtonMove_Click;
|
||||||
//
|
//
|
||||||
|
// buttonCreateLiner
|
||||||
|
//
|
||||||
|
buttonCreateLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
|
buttonCreateLiner.Location = new Point(172, 426);
|
||||||
|
buttonCreateLiner.Name = "buttonCreateLiner";
|
||||||
|
buttonCreateLiner.Size = new Size(154, 23);
|
||||||
|
buttonCreateLiner.TabIndex = 6;
|
||||||
|
buttonCreateLiner.Text = "Создать лайнер";
|
||||||
|
buttonCreateLiner.UseVisualStyleBackColor = true;
|
||||||
|
buttonCreateLiner.Click += ButtonCreateLiner_Click;
|
||||||
|
//
|
||||||
|
// comboBoxStrategy
|
||||||
|
//
|
||||||
|
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||||
|
comboBoxStrategy.FormattingEnabled = true;
|
||||||
|
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||||
|
comboBoxStrategy.Location = new Point(751, 12);
|
||||||
|
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||||
|
comboBoxStrategy.Size = new Size(121, 23);
|
||||||
|
comboBoxStrategy.TabIndex = 7;
|
||||||
|
//
|
||||||
|
// buttonStrategyStep
|
||||||
|
//
|
||||||
|
buttonStrategyStep.Location = new Point(797, 41);
|
||||||
|
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||||
|
buttonStrategyStep.Size = new Size(75, 23);
|
||||||
|
buttonStrategyStep.TabIndex = 8;
|
||||||
|
buttonStrategyStep.Text = "Шаг";
|
||||||
|
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||||
|
buttonStrategyStep.Click += ButtonStrategyStep_Click;
|
||||||
|
//
|
||||||
// FormLiner
|
// FormLiner
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(884, 461);
|
ClientSize = new Size(884, 461);
|
||||||
|
Controls.Add(buttonStrategyStep);
|
||||||
|
Controls.Add(comboBoxStrategy);
|
||||||
|
Controls.Add(buttonCreateLiner);
|
||||||
Controls.Add(buttonUp);
|
Controls.Add(buttonUp);
|
||||||
Controls.Add(buttonLeft);
|
Controls.Add(buttonLeft);
|
||||||
Controls.Add(buttonDown);
|
Controls.Add(buttonDown);
|
||||||
Controls.Add(buttonRight);
|
Controls.Add(buttonRight);
|
||||||
Controls.Add(buttonCreate);
|
Controls.Add(buttonCreateSmallLiner);
|
||||||
Controls.Add(pictureBoxLiner);
|
Controls.Add(pictureBoxLiner);
|
||||||
Name = "FormLiner";
|
Name = "FormLiner";
|
||||||
StartPosition = FormStartPosition.CenterScreen;
|
StartPosition = FormStartPosition.CenterScreen;
|
||||||
@ -126,10 +163,13 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private PictureBox pictureBoxLiner;
|
private PictureBox pictureBoxLiner;
|
||||||
private Button buttonCreate;
|
private Button buttonCreateSmallLiner;
|
||||||
private Button buttonRight;
|
private Button buttonRight;
|
||||||
private Button buttonDown;
|
private Button buttonDown;
|
||||||
private Button buttonLeft;
|
private Button buttonLeft;
|
||||||
private Button buttonUp;
|
private Button buttonUp;
|
||||||
|
private Button buttonCreateLiner;
|
||||||
|
private ComboBox comboBoxStrategy;
|
||||||
|
private Button buttonStrategyStep;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,7 @@
|
|||||||
namespace ProjectLiner;
|
using ProjectLiner.Drawnings;
|
||||||
|
using ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
namespace ProjectLiner;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Форма работы с объектом "Лайнер"
|
/// Форма работы с объектом "Лайнер"
|
||||||
@ -8,7 +11,12 @@ public partial class FormLiner : Form
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Поле-объект для прорисовки объекта
|
/// Поле-объект для прорисовки объекта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private DrawningLiner? _drawningLiner;
|
private DrawningSmallLiner? _drawningSmallLiner;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения
|
||||||
|
/// </summary>
|
||||||
|
private AbstractStrategy? _strategy;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор формы
|
/// Конструктор формы
|
||||||
@ -16,6 +24,7 @@ public partial class FormLiner : Form
|
|||||||
public FormLiner()
|
public FormLiner()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
_strategy = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -23,35 +32,62 @@ public partial class FormLiner : Form
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void Draw()
|
private void Draw()
|
||||||
{
|
{
|
||||||
if (_drawningLiner == null)
|
if (_drawningSmallLiner == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Bitmap bmp = new(pictureBoxLiner.Width,
|
Bitmap bmp = new(pictureBoxLiner.Width,
|
||||||
pictureBoxLiner.Height);
|
pictureBoxLiner.Height);
|
||||||
Graphics gr = Graphics.FromImage(bmp);
|
Graphics gr = Graphics.FromImage(bmp);
|
||||||
_drawningLiner.DrawTransport(gr);
|
_drawningSmallLiner.DrawTransport(gr);
|
||||||
pictureBoxLiner.Image = bmp;
|
pictureBoxLiner.Image = bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Обработка нажатия кнопки "Создать"
|
/// Создание объекта класса-перемещения
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">Тип создаваемого объекта</param>
|
||||||
|
private void CreateObject(string type)
|
||||||
|
{
|
||||||
|
Random random = new();
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case nameof(DrawningSmallLiner):
|
||||||
|
_drawningSmallLiner = new DrawningSmallLiner(
|
||||||
|
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(DrawningLiner):
|
||||||
|
_drawningSmallLiner = new DrawningLiner(
|
||||||
|
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)));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_drawningSmallLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||||
|
_drawningSmallLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||||
|
_strategy = null;
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Обработка нажатия кнопки "Создать лайнер"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
private void ButtonCreateLiner_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLiner));
|
||||||
{
|
|
||||||
Random random = new();
|
|
||||||
_drawningLiner = new DrawningLiner();
|
|
||||||
_drawningLiner.Init(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)));
|
|
||||||
_drawningLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height);
|
|
||||||
_drawningLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
|
||||||
|
|
||||||
Draw();
|
/// <summary>
|
||||||
}
|
/// Обработка нажатия кнопки "Создать малый лайнер"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonCreateSmallLiner_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSmallLiner));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Обработка нажатия кнопок навигации движения
|
/// Обработка нажатия кнопок навигации движения
|
||||||
@ -60,7 +96,7 @@ public partial class FormLiner : Form
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonMove_Click(object sender, EventArgs e)
|
private void ButtonMove_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (_drawningLiner == null)
|
if (_drawningSmallLiner == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -69,16 +105,16 @@ public partial class FormLiner : Form
|
|||||||
switch (name)
|
switch (name)
|
||||||
{
|
{
|
||||||
case "buttonUp":
|
case "buttonUp":
|
||||||
result = _drawningLiner.MoveTransport(DirectionType.Up);
|
result = _drawningSmallLiner.MoveTransport(DirectionType.Up);
|
||||||
break;
|
break;
|
||||||
case "buttonDown":
|
case "buttonDown":
|
||||||
result = _drawningLiner.MoveTransport(DirectionType.Down);
|
result = _drawningSmallLiner.MoveTransport(DirectionType.Down);
|
||||||
break;
|
break;
|
||||||
case "buttonLeft":
|
case "buttonLeft":
|
||||||
result = _drawningLiner.MoveTransport(DirectionType.Left);
|
result = _drawningSmallLiner.MoveTransport(DirectionType.Left);
|
||||||
break;
|
break;
|
||||||
case "buttonRight":
|
case "buttonRight":
|
||||||
result = _drawningLiner.MoveTransport(DirectionType.Right);
|
result = _drawningSmallLiner.MoveTransport(DirectionType.Right);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (result)
|
if (result)
|
||||||
@ -86,4 +122,50 @@ public partial class FormLiner : Form
|
|||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Обработка нажатия кнопки "Шаг"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonStrategyStep_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_drawningSmallLiner == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comboBoxStrategy.Enabled)
|
||||||
|
{
|
||||||
|
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||||
|
{
|
||||||
|
0 => new MoveToCenter(),
|
||||||
|
1 => new MoveToBorder(),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_strategy.SetData(new MoveableLiner(_drawningSmallLiner),
|
||||||
|
pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
comboBoxStrategy.Enabled = false;
|
||||||
|
_strategy.MakeStep();
|
||||||
|
Draw();
|
||||||
|
|
||||||
|
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
_strategy = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
136
ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs
Normal file
136
ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-стратегия перемещения объекта
|
||||||
|
/// </summary>
|
||||||
|
public abstract class AbstractStrategy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещаемый объект
|
||||||
|
/// </summary>
|
||||||
|
private IMoveableObject? _moveableObject;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Статус перемещения
|
||||||
|
/// </summary>
|
||||||
|
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина поля
|
||||||
|
/// </summary>
|
||||||
|
protected int FieldWidth { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Высота поля
|
||||||
|
/// </summary>
|
||||||
|
protected int FieldHeight { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Статус перемещения
|
||||||
|
/// </summary>
|
||||||
|
public StrategyStatus GetStatus() { return _state; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Установка данных
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="moveableObject">Перемещаемый объект</param>
|
||||||
|
/// <param name="width">Ширина поля</param>
|
||||||
|
/// <param name="height">Высота поля</param>
|
||||||
|
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||||
|
{
|
||||||
|
if (moveableObject == null)
|
||||||
|
{
|
||||||
|
_state = StrategyStatus.NotInit;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_state = StrategyStatus.InProgress;
|
||||||
|
_moveableObject = moveableObject;
|
||||||
|
FieldWidth = width;
|
||||||
|
FieldHeight = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Шаг перемещения
|
||||||
|
/// </summary>
|
||||||
|
public void MakeStep()
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsTargetDestinaion())
|
||||||
|
{
|
||||||
|
_state = StrategyStatus.Finish;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MoveToTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение влево
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение вправо
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение вверх
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение вниз
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Параметры объекта
|
||||||
|
/// </summary>
|
||||||
|
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Шаг объекта
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected int? GetStep()
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _moveableObject?.GetStep;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение к цели
|
||||||
|
/// </summary>
|
||||||
|
protected abstract void MoveToTarget();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Достигнута ли цель
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected abstract bool IsTargetDestinaion();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Попытка перемещения в требуемом направлении
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="movementDirection">Направление</param>
|
||||||
|
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||||
|
private bool MoveTo(MovementDirection movementDirection)
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Интерфейс для работы с перемещаемым объектом
|
||||||
|
/// </summary>
|
||||||
|
public interface IMoveableObject
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Получение координаты объекта
|
||||||
|
/// </summary>
|
||||||
|
ObjectParameters? GetObjectPosition { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Шаг объекта
|
||||||
|
/// </summary>
|
||||||
|
int GetStep { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Попытка переместить объект в указанном направлении
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Направление</param>
|
||||||
|
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
|
||||||
|
bool TryMoveObject(MovementDirection direction);
|
||||||
|
}
|
38
ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs
Normal file
38
ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения объекта в угол экрана
|
||||||
|
/// </summary>
|
||||||
|
public class MoveToBorder : AbstractStrategy
|
||||||
|
{
|
||||||
|
protected override bool IsTargetDestinaion()
|
||||||
|
{
|
||||||
|
ObjectParameters? objParams = GetObjectParameters;
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return objParams.RightBorder + GetStep() >= FieldWidth && 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
53
ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs
Normal file
53
ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения объекта в центр экрана
|
||||||
|
/// </summary>
|
||||||
|
public class MoveToCenter : AbstractStrategy
|
||||||
|
{
|
||||||
|
protected override bool IsTargetDestinaion()
|
||||||
|
{
|
||||||
|
ObjectParameters? objParams = GetObjectParameters;
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2
|
||||||
|
&& objParams.ObjectMiddleHorizontal + 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.ObjectMiddleHorizontal - 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs
Normal file
79
ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using ProjectLiner.Drawnings;
|
||||||
|
|
||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-реализация IMoveableObject с использованием drawningSmallLiner
|
||||||
|
/// </summary>
|
||||||
|
public class MoveableLiner : IMoveableObject
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Поле-объект класса DrawningSmallLiner или его наследника
|
||||||
|
/// </summary>
|
||||||
|
private readonly DrawningSmallLiner? _liner = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="car">Объект класса DrawningSmallLiner</param>
|
||||||
|
public MoveableLiner(DrawningSmallLiner liner)
|
||||||
|
{
|
||||||
|
_liner = liner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectParameters? GetObjectPosition
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_liner == null || _liner.EntitySmallLiner == null ||
|
||||||
|
!_liner.GetPosX.HasValue || !_liner.GetPosY.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_liner.GetPosX.Value,
|
||||||
|
_liner.GetPosY.Value, _liner.GetWidth, _liner.GetHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetStep => (int)(_liner?.EntitySmallLiner?.Step ?? 0);
|
||||||
|
|
||||||
|
public bool TryMoveObject(MovementDirection direction)
|
||||||
|
{
|
||||||
|
if (_liner == null || _liner.EntitySmallLiner == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return _liner.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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public MoveableLiner(DrawningSmallLiner drawningSmallLiner)
|
||||||
|
{
|
||||||
|
_drawningSmallLiner = drawningSmallLiner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectParameters? GetObjectPosition => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public int GetStep => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public bool TryMoveObject(MovementDirection direction)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}*/
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
public enum MovementDirection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Вверх
|
||||||
|
/// </summary>
|
||||||
|
Up = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Вниз
|
||||||
|
/// </summary>
|
||||||
|
Down = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Влево
|
||||||
|
/// </summary>
|
||||||
|
Left = 3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Вправо
|
||||||
|
/// </summary>
|
||||||
|
Right = 4
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
public class ObjectParameters
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Координата X
|
||||||
|
/// </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 ObjectMiddleHorizontal => _x + _width / 2;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Середина объекта
|
||||||
|
/// </summary>
|
||||||
|
public int ObjectMiddleVertical => _y + _height / 2;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">Координата 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;
|
||||||
|
}
|
||||||
|
}
|
20
ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs
Normal file
20
ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
namespace ProjectLiner.MovementStrategy;
|
||||||
|
|
||||||
|
public enum StrategyStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Все готово к началу
|
||||||
|
/// </summary>
|
||||||
|
NotInit,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Выполняется
|
||||||
|
/// </summary>
|
||||||
|
InProgress,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Завершено
|
||||||
|
/// </summary>
|
||||||
|
Finish
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user