Lab2
This commit is contained in:
parent
8a3e76d5fb
commit
ad55a673aa
134
Liner/Liner/AbstractStrategy.cs
Normal file
134
Liner/Liner/AbstractStrategy.cs
Normal file
@ -0,0 +1,134 @@
|
||||
using Liner.MovementStrategy;
|
||||
using Liner;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObject? _moveableObject;
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private Status _state = Status.NotInit;
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set; }
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
public Status 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 = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = Status.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(DirectionType.Left);
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(DirectionType.Right);
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(DirectionType.Up);
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(DirectionType.Down);
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
/// <summary>
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// </summary>
|
||||
/// <param name="directionType">Направление</param>
|
||||
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||
private bool MoveTo(DirectionType directionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject?.CheckCanMove(directionType) ?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
70
Liner/Liner/DrawingAdditionalDeck.cs
Normal file
70
Liner/Liner/DrawingAdditionalDeck.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Liner.Entities;
|
||||
|
||||
namespace Liner.DrawingObjects
|
||||
{
|
||||
public class DrawingAdditionalDeck : DrawingLiner
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Цвет кузова</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="doubleDeck">Признак наличия второй палубы</param>
|
||||
/// <param name="tripleDeck">Признак наличия третьей палубы</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
|
||||
public DrawingAdditionalDeck(int speed, double weight, Color bodyColor, Color additionalColor, bool doubleDeck, bool tripleDeck, int width, int height) : base(speed, weight, bodyColor, width, height, 150, 95)
|
||||
{
|
||||
if (EntityLiner != null)
|
||||
{
|
||||
EntityLiner = new EntityAdditionalDeck(speed, weight, bodyColor, additionalColor, doubleDeck, tripleDeck);
|
||||
}
|
||||
|
||||
}
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLiner is not EntityAdditionalDeck additionalDeck)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Brush additionalBrush = new SolidBrush(additionalDeck.AdditionalColor);
|
||||
base.DrawTransport(g);
|
||||
// 2 Палуба
|
||||
if (additionalDeck.DoubleDeck)
|
||||
{
|
||||
Point point1 = new Point(_startPosX + 30, _startPosY + 35);
|
||||
Point point2 = new Point(_startPosX + 110, _startPosY + 35);
|
||||
Point point3 = new Point(_startPosX + 110, _startPosY + 60);
|
||||
Point point4 = new Point(_startPosX + 30, _startPosY + 60);
|
||||
Point[] curvePoints = { point1, point2, point3, point4 };
|
||||
g.FillPolygon(additionalBrush, curvePoints);
|
||||
|
||||
}
|
||||
// 3 Палуба
|
||||
if (additionalDeck.TripleDeck)
|
||||
{
|
||||
Point point1 = new Point(_startPosX + 50, _startPosY + 35);
|
||||
Point point2 = new Point(_startPosX + 50, _startPosY + 10);
|
||||
Point point3 = new Point(_startPosX + 90, _startPosY + 10);
|
||||
Point point4 = new Point(_startPosX + 90, _startPosY + 35);
|
||||
Point point5 = new Point(_startPosX + 110, _startPosY + 35);
|
||||
Point point6 = new Point(_startPosX + 110, _startPosY + 60);
|
||||
Point point7 = new Point(_startPosX + 30, _startPosY + 60);
|
||||
Point point8 = new Point(_startPosX + 30, _startPosY + 35);
|
||||
Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7, point8 };
|
||||
g.FillPolygon(additionalBrush, curvePoints);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,16 +4,19 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Liner.Entities;
|
||||
|
||||
namespace Liner
|
||||
namespace Liner.DrawingObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawingLiner
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityLiner? EntityLiner { get; private set; }
|
||||
public EntityLiner? EntityLiner { get; protected set; }
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
@ -25,42 +28,75 @@ namespace Liner
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки лайнера
|
||||
/// </summary>
|
||||
private int _startPosX;
|
||||
protected int _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки лайнера
|
||||
/// </summary>
|
||||
private int _startPosY;
|
||||
protected int _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки лайнера
|
||||
/// </summary>
|
||||
private readonly int _linerWidth = 150;
|
||||
protected readonly int _linerWidth = 150;
|
||||
/// <summary>
|
||||
/// Высота прорисовки лайнера
|
||||
/// </summary>
|
||||
private readonly int _linerHeight = 95;
|
||||
protected readonly int _linerHeight = 95;
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _linerWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _linerHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Цвет кузова</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="doubleDeck">Признак наличия второй палубы</param>
|
||||
/// <param name="tripleDeck">Признак наличия третьей палубы</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool doubleDeck, bool tripleDeck, int width, int height)
|
||||
public DrawingLiner(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if (width < _linerWidth || height < _linerHeight)
|
||||
{
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityLiner = new EntityLiner();
|
||||
EntityLiner.Init(speed, weight, bodyColor, additionalColor, doubleDeck, tripleDeck);
|
||||
return true;
|
||||
EntityLiner = new EntityLiner(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <param name="linerWidth">Ширина прорисовки лайнера</param>
|
||||
/// <param name="linerHeight">Высота прорисовки лайнера</param>
|
||||
protected DrawingLiner(int speed, double weight, Color bodyColor, int width, int height, int linerWidth, int linerHeight)
|
||||
{
|
||||
if (width < _linerWidth || height < _linerHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_linerWidth = linerWidth;
|
||||
_linerHeight = linerHeight;
|
||||
EntityLiner = new EntityLiner(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
@ -78,7 +114,7 @@ namespace Liner
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityLiner == null)
|
||||
if (!CanMove(direction) || EntityLiner == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -105,7 +141,7 @@ namespace Liner
|
||||
_startPosX += (int)EntityLiner.Step;
|
||||
}
|
||||
break;
|
||||
// вниз
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _linerHeight + EntityLiner.Step < _pictureHeight)
|
||||
{
|
||||
@ -118,7 +154,7 @@ namespace Liner
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLiner == null)
|
||||
{
|
||||
@ -139,33 +175,31 @@ namespace Liner
|
||||
g.FillEllipse(brBlue, _startPosX + 60, _startPosY + 70, 10, 10);
|
||||
g.FillEllipse(brBlue, _startPosX + 80, _startPosY + 70, 10, 10);
|
||||
g.FillEllipse(brBlue, _startPosX + 100, _startPosY + 70, 10, 10);
|
||||
Brush additionalBrush = new SolidBrush(EntityLiner.AdditionalColor);
|
||||
// 2 Палуба
|
||||
if (EntityLiner.DoubleDeck)
|
||||
{
|
||||
Point point5 = new Point(_startPosX + 30, _startPosY + 35);
|
||||
Point point6 = new Point(_startPosX + 110, _startPosY + 35);
|
||||
Point point7 = new Point(_startPosX + 110, _startPosY + 60);
|
||||
Point point8 = new Point(_startPosX + 30, _startPosY + 60);
|
||||
Point[] curvePoints2 = { point5, point6, point7, point8 };
|
||||
g.FillPolygon(additionalBrush, curvePoints2);
|
||||
}
|
||||
|
||||
}
|
||||
// 3 Палуба
|
||||
if (EntityLiner.TripleDeck)
|
||||
/// <summary>
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||
public bool CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityLiner == null)
|
||||
{
|
||||
Point point5 = new Point(_startPosX + 50, _startPosY + 35);
|
||||
Point point6 = new Point(_startPosX + 50, _startPosY + 10);
|
||||
Point point7 = new Point(_startPosX + 90, _startPosY + 10);
|
||||
Point point8 = new Point(_startPosX + 90, _startPosY + 35);
|
||||
Point point9 = new Point(_startPosX + 110, _startPosY + 35);
|
||||
Point point10 = new Point(_startPosX + 110, _startPosY + 60);
|
||||
Point point11 = new Point(_startPosX + 30, _startPosY + 60);
|
||||
Point point12 = new Point(_startPosX + 30, _startPosY + 35);
|
||||
Point[] curvePoints2 = { point5, point6, point7, point8, point9, point10, point11, point12 };
|
||||
g.FillPolygon(additionalBrush, curvePoints2);
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
DirectionType.Left => _startPosX - EntityLiner.Step > 0,
|
||||
//вверх
|
||||
DirectionType.Up => _startPosY - EntityLiner.Step > 0,
|
||||
// вправо
|
||||
DirectionType.Right => _startPosX + _linerWidth + EntityLiner.Step < _pictureWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + _linerHeight + EntityLiner.Step < _pictureHeight,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
34
Liner/Liner/DrawingObjectLiner.cs
Normal file
34
Liner/Liner/DrawingObjectLiner.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Liner.MovementStrategy;
|
||||
using Liner;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Liner.DrawingObjects;
|
||||
|
||||
namespace Liner.MovementStrategy
|
||||
{
|
||||
public class DrawingObjectLiner : IMoveableObject
|
||||
{
|
||||
private readonly DrawingLiner? _drawingLiner = null;
|
||||
public DrawingObjectLiner(DrawingLiner drawingLiner)
|
||||
{
|
||||
_drawingLiner = drawingLiner;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawingLiner == null || _drawingLiner.EntityLiner == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingLiner.GetPosX, _drawingLiner.GetPosY, _drawingLiner.GetWidth, _drawingLiner.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawingLiner?.EntityLiner?.Step ?? 0);
|
||||
public bool CheckCanMove(DirectionType direction) => _drawingLiner?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionType direction) => _drawingLiner?.MoveTransport(direction);
|
||||
}
|
||||
}
|
43
Liner/Liner/EntityAdditionalDeck.cs
Normal file
43
Liner/Liner/EntityAdditionalDeck.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность "Лайнер"
|
||||
/// </summary>
|
||||
public class EntityAdditionalDeck : EntityLiner
|
||||
{
|
||||
/// <summary>
|
||||
/// Дополнительный цвет (для опциональных элементов)
|
||||
/// </summary>
|
||||
public Color AdditionalColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия второй палубы
|
||||
/// </summary>
|
||||
public bool DoubleDeck { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия третьей палубы
|
||||
/// </summary>
|
||||
public bool TripleDeck { get; private set; }
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса лайнер
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес лайнера</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="doubleDeck">Признак наличия второй палубы</param>
|
||||
/// <param name="tripleDeck">Признак наличия третьей палубы</param>
|
||||
public EntityAdditionalDeck(int speed, double weight, Color bodyColor, Color additionalColor, bool doubleDeck, bool tripleDeck) : base(speed, weight, bodyColor)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
DoubleDeck = doubleDeck;
|
||||
TripleDeck = tripleDeck;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,11 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner
|
||||
namespace Liner.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность "Лайнер"
|
||||
/// </summary>
|
||||
public class EntityLiner
|
||||
{
|
||||
/// <summary>
|
||||
@ -21,39 +24,20 @@ namespace Liner
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Дополнительный цвет (для опциональных элементов)
|
||||
/// </summary>
|
||||
public Color AdditionalColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия второй палубы
|
||||
/// </summary>
|
||||
public bool DoubleDeck { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия третьей палубы
|
||||
/// </summary>
|
||||
public bool TripleDeck { get; private set; }
|
||||
/// <summary>
|
||||
/// Шаг перемещения лайнера
|
||||
/// </summary>
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса лайнера
|
||||
/// Конструктор с параметрами
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес лайнера</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="doubleDeck">Признак наличия второй палубы</param>
|
||||
/// <param name="tripleDeck">Признак наличия третьей палубы</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool doubleDeck, bool tripleDeck)
|
||||
public EntityLiner(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
DoubleDeck = doubleDeck;
|
||||
TripleDeck = tripleDeck;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
66
Liner/Liner/FormLiner.Designer.cs
generated
66
Liner/Liner/FormLiner.Designer.cs
generated
@ -33,11 +33,14 @@ namespace Liner
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBoxLiner = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateAdditionalDeck = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonCreateLiner = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -50,16 +53,16 @@ namespace Liner
|
||||
pictureBoxLiner.TabIndex = 5;
|
||||
pictureBoxLiner.TabStop = false;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateAdditionalDeck
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 409);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(90, 30);
|
||||
buttonCreate.TabIndex = 6;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += ButtonCreate_Click;
|
||||
buttonCreateAdditionalDeck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateAdditionalDeck.Location = new Point(12, 389);
|
||||
buttonCreateAdditionalDeck.Name = "buttonCreateAdditionalDeck";
|
||||
buttonCreateAdditionalDeck.Size = new Size(120, 50);
|
||||
buttonCreateAdditionalDeck.TabIndex = 6;
|
||||
buttonCreateAdditionalDeck.Text = "Создать усложнённый лайнер";
|
||||
buttonCreateAdditionalDeck.UseVisualStyleBackColor = true;
|
||||
buttonCreateAdditionalDeck.Click += ButtonCreateAdditionalDeck_Click;
|
||||
//
|
||||
// buttonUp
|
||||
//
|
||||
@ -109,16 +112,52 @@ namespace Liner
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += ButtonMove_Click;
|
||||
//
|
||||
// buttonCreateLiner
|
||||
//
|
||||
buttonCreateLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateLiner.Location = new Point(138, 389);
|
||||
buttonCreateLiner.Name = "buttonCreateLiner";
|
||||
buttonCreateLiner.Size = new Size(120, 50);
|
||||
buttonCreateLiner.TabIndex = 11;
|
||||
buttonCreateLiner.Text = "Создать простой лайнер";
|
||||
buttonCreateLiner.UseVisualStyleBackColor = true;
|
||||
buttonCreateLiner.Click += ButtonCreateLiner_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "В центр", "В правый нижний угол" });
|
||||
comboBoxStrategy.Location = new Point(719, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(151, 28);
|
||||
comboBoxStrategy.TabIndex = 12;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStep.Location = new Point(748, 46);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(94, 29);
|
||||
buttonStep.TabIndex = 13;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += ButtonStep_Click;
|
||||
//
|
||||
// FormLiner
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(882, 453);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateLiner);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateAdditionalDeck);
|
||||
Controls.Add(pictureBoxLiner);
|
||||
Name = "FormLiner";
|
||||
Text = "Лайнер";
|
||||
@ -129,10 +168,13 @@ namespace Liner
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxLiner;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateAdditionalDeck;
|
||||
private Button buttonUp;
|
||||
private Button buttonLeft;
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonCreateLiner;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using Liner;
|
||||
using Liner.DrawingObjects;
|
||||
using Liner.MovementStrategy;
|
||||
using Liner;
|
||||
|
||||
namespace Liner
|
||||
{
|
||||
@ -11,13 +13,17 @@ namespace Liner
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawingLiner? _drawingLiner;
|
||||
/// <summary>
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
public FormLiner()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки лайнера
|
||||
/// </summary>>
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawingLiner == null)
|
||||
@ -30,15 +36,14 @@ namespace Liner
|
||||
pictureBoxLiner.Image = bmp;
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать лайнер"
|
||||
/// Обработка нажатия кнопки "Создать усложненный лайнер"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||
private void ButtonCreateAdditionalDeck_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingLiner = new DrawingLiner();
|
||||
_drawingLiner.Init(random.Next(100, 300),
|
||||
_drawingLiner = new DrawingAdditionalDeck(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)),
|
||||
@ -49,6 +54,22 @@ namespace Liner
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать простой лайнер"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateLiner_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingLiner = new DrawingLiner(random.Next(100, 300),
|
||||
random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
||||
random.Next(0, 256)),
|
||||
pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||
_drawingLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопок движения
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
@ -77,5 +98,44 @@ namespace Liner
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Шаг"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingLiner == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawingObjectLiner(_drawingLiner), pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
37
Liner/Liner/IMoveableObject.cs
Normal file
37
Liner/Liner/IMoveableObject.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Liner.MovementStrategy;
|
||||
using Liner;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Liner.DrawingObjects;
|
||||
|
||||
namespace Liner.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты X объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
/// <summary>
|
||||
/// Проверка, можно ли переместиться по нужному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction"></param>
|
||||
/// <returns></returns>
|
||||
bool CheckCanMove(DirectionType direction);
|
||||
/// <summary>
|
||||
/// Изменение направления пермещения объекта
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
||||
}
|
57
Liner/Liner/MoveToBorder.cs
Normal file
57
Liner/Liner/MoveToBorder.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Liner.MovementStrategy
|
||||
{
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder <= FieldWidth &&
|
||||
objParams.RightBorder + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder <= FieldHeight &&
|
||||
objParams.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
60
Liner/Liner/MoveToCenter.cs
Normal file
60
Liner/Liner/MoveToCenter.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
58
Liner/Liner/ObjectParameters.cs
Normal file
58
Liner/Liner/ObjectParameters.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
public class ObjectParameters
|
||||
{
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
Liner/Liner/Status.cs
Normal file
15
Liner/Liner/Status.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user