Compare commits
12 Commits
6b9a62231a
...
054f03921c
Author | SHA1 | Date | |
---|---|---|---|
054f03921c | |||
e8379ecea1 | |||
a52c951843 | |||
810b4f70a6 | |||
92af897804 | |||
ba3b017e15 | |||
080dd52d11 | |||
72c17b847d | |||
7a89f52f40 | |||
803810eb19 | |||
3113f2defd | |||
e5c15c66a1 |
135
Sailboat/Sailboat/AbstractStrategy.cs
Normal file
135
Sailboat/Sailboat/AbstractStrategy.cs
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Sailboat.DrawingObjects;
|
||||||
|
namespace Sailboat.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
231
Sailboat/Sailboat/DrawingBoat.cs
Normal file
231
Sailboat/Sailboat/DrawingBoat.cs
Normal file
@ -0,0 +1,231 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Sailboat.Entities;
|
||||||
|
|
||||||
|
namespace Sailboat.DrawingObjects
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||||
|
/// </summary>
|
||||||
|
public class DrawingBoat
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-сущность
|
||||||
|
/// </summary>
|
||||||
|
public EntityBoat? EntityBoat { 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 _boatWidth = 185;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Высота прорисовки лодки
|
||||||
|
/// </summary>
|
||||||
|
private readonly int _boatHeight = 160;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Координата X объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetPosX => _startPosX;
|
||||||
|
/// <summary>
|
||||||
|
/// Координата Y объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetPosY => _startPosY;
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetWidth => _boatWidth;
|
||||||
|
/// <summary>
|
||||||
|
/// Высота объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetHeight => _boatHeight;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
/// <param name="width">Ширина картинки</param>
|
||||||
|
/// <param name="height">Высота картинки</param>
|
||||||
|
public DrawingBoat(int speed, double weight, Color bodyColor, int width, int height)
|
||||||
|
{
|
||||||
|
if (width < _boatWidth || height < _boatHeight)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
EntityBoat = new EntityBoat(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="boatWidth">Ширина прорисовки лодки</param>
|
||||||
|
/// <param name="boatHeight">Высота прорисовки лодки</param>
|
||||||
|
protected DrawingBoat(int speed, double weight, Color bodyColor, int width, int height, int boatWidth, int boatHeight)
|
||||||
|
{
|
||||||
|
if (width < _boatWidth || height < _boatHeight)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
_boatWidth = boatWidth;
|
||||||
|
_boatHeight = boatHeight;
|
||||||
|
EntityBoat = new EntityBoat(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Установка позиции
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">Координата X</param>
|
||||||
|
/// <param name="y">Координата Y</param>
|
||||||
|
public void SetPosition(int x, int y)
|
||||||
|
{
|
||||||
|
if (x < 0 || x + _boatWidth > _pictureWidth)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
}
|
||||||
|
if (y < 0 || y + _boatHeight > _pictureHeight)
|
||||||
|
{
|
||||||
|
y = 0;
|
||||||
|
}
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Проверка, что объект может переместится по указанному направлению
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Направление</param>
|
||||||
|
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||||
|
public bool CanMove(DirectionType direction)
|
||||||
|
{
|
||||||
|
if (EntityBoat == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return direction switch
|
||||||
|
{
|
||||||
|
//влево
|
||||||
|
DirectionType.Left => _startPosX - EntityBoat.Step > 0,
|
||||||
|
//вверх
|
||||||
|
DirectionType.Up => _startPosY - EntityBoat.Step > 0,
|
||||||
|
// вправо
|
||||||
|
DirectionType.Right => _startPosX + EntityBoat.Step < _pictureWidth,
|
||||||
|
//вниз
|
||||||
|
DirectionType.Down => _startPosY + EntityBoat.Step < _pictureHeight,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Изменение направления перемещения
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Направление</param>
|
||||||
|
public void MoveTransport(DirectionType direction)
|
||||||
|
{
|
||||||
|
if (!CanMove(direction) || EntityBoat == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
//влево
|
||||||
|
case DirectionType.Left:
|
||||||
|
if (_startPosX - EntityBoat.Step > 0)
|
||||||
|
{
|
||||||
|
_startPosX -= (int)EntityBoat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
//вверх
|
||||||
|
case DirectionType.Up:
|
||||||
|
if (_startPosY - EntityBoat.Step > 0)
|
||||||
|
{
|
||||||
|
_startPosY -= (int)EntityBoat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
//вправо
|
||||||
|
case DirectionType.Right:
|
||||||
|
if (_startPosX + EntityBoat.Step + _boatWidth < _pictureWidth)
|
||||||
|
{
|
||||||
|
_startPosX += (int)EntityBoat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
//вниз
|
||||||
|
case DirectionType.Down:
|
||||||
|
if (_startPosY + EntityBoat.Step + _boatHeight < _pictureHeight)
|
||||||
|
{
|
||||||
|
_startPosY += (int)EntityBoat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Прорисовка объекта
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
public virtual void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (EntityBoat == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pen pen = new(Color.Black);
|
||||||
|
|
||||||
|
// Основной корпус парусника
|
||||||
|
Brush Brush = new
|
||||||
|
SolidBrush(EntityBoat.BodyColor);
|
||||||
|
|
||||||
|
Point[] hull = new Point[]
|
||||||
|
{
|
||||||
|
new Point(_startPosX + 5, _startPosY + 90),
|
||||||
|
new Point(_startPosX + 120, _startPosY + 90),
|
||||||
|
new Point(_startPosX + 180, _startPosY + 120),
|
||||||
|
new Point(_startPosX + 120, _startPosY + 150),
|
||||||
|
new Point(_startPosX + 5, _startPosY + 150)
|
||||||
|
};
|
||||||
|
g.FillPolygon(Brush, hull);
|
||||||
|
g.DrawPolygon(pen, hull);
|
||||||
|
|
||||||
|
Brush addBrush = new
|
||||||
|
SolidBrush(Color.Green);
|
||||||
|
|
||||||
|
g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 100, 40);
|
||||||
|
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 100, 40);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
40
Sailboat/Sailboat/DrawingObjectBoat.cs
Normal file
40
Sailboat/Sailboat/DrawingObjectBoat.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Sailboat.DrawingObjects;
|
||||||
|
|
||||||
|
namespace Sailboat.MovementStrategy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningBoat (паттерн Adapter)
|
||||||
|
/// </summary>
|
||||||
|
public class DrawingObjectBoat : IMoveableObject
|
||||||
|
{
|
||||||
|
private readonly DrawingBoat? _drawingBoat = null;
|
||||||
|
public DrawingObjectBoat(DrawingBoat drawingBoat)
|
||||||
|
{
|
||||||
|
_drawingBoat = drawingBoat;
|
||||||
|
}
|
||||||
|
public ObjectParameters? GetObjectPosition
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_drawingBoat == null || _drawingBoat.EntityBoat ==
|
||||||
|
null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_drawingBoat.GetPosX, _drawingBoat.GetPosY, _drawingBoat.GetWidth, _drawingBoat.GetHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int GetStep => (int)(_drawingBoat?.EntityBoat?.Step ?? 0);
|
||||||
|
public bool CheckCanMove(DirectionType direction) =>
|
||||||
|
_drawingBoat?.CanMove(direction) ?? false;
|
||||||
|
public void MoveObject(DirectionType direction) =>
|
||||||
|
_drawingBoat?.MoveTransport(direction);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,157 +1,51 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Sailboat
|
using Sailboat.Entities;
|
||||||
|
|
||||||
|
namespace Sailboat.DrawingObjects
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DrawingSailboat
|
public class DrawingSailboat : DrawingBoat
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность
|
/// Конструктор
|
||||||
/// </summary>
|
|
||||||
public EntitySailboat? EntitySailboat { 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 _boatWidth = 185;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Высота прорисовки лодки
|
|
||||||
/// </summary>
|
|
||||||
private readonly int _boatHeight = 160;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Инициализация свойств
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="speed">Скорость</param>
|
/// <param name="speed">Скорость</param>
|
||||||
/// <param name="weight">Вес</param>
|
/// <param name="weight">Вес</param>
|
||||||
/// <param name="bodyColor">Цвет кузова</param>
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||||
/// <param name="hullCooler">Признак наличия усиленного корпуса парусника</param>
|
/// <param name="hull">Признак наличия усиленного корпуса</param>
|
||||||
/// <param name="hull">Признак наличия основного корпуса парусника</param>
|
|
||||||
/// <param name="sail">Признак наличия паруса</param>
|
/// <param name="sail">Признак наличия паруса</param>
|
||||||
/// <param name="width">Ширина картинки</param>
|
/// <param name="width">Ширина картинки</param>
|
||||||
/// <param name="height">Высота картинки</param>
|
/// <param name="height">Высота картинки</param>
|
||||||
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
|
public DrawingSailboat(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height) :
|
||||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool hullCooler, bool hull, bool sail, int width, int height)
|
base(speed, weight, bodyColor, width, height, 200, 160)
|
||||||
{
|
{
|
||||||
// Проверки
|
if (EntityBoat != null)
|
||||||
if (width < _boatWidth || height < _boatHeight)
|
|
||||||
{
|
{
|
||||||
return false;
|
EntityBoat = new EntitySailboat(speed, weight, bodyColor,
|
||||||
|
additionalColor, hull, sail);
|
||||||
}
|
}
|
||||||
_pictureWidth = width;
|
|
||||||
_pictureHeight = height;
|
|
||||||
EntitySailboat = new EntitySailboat();
|
|
||||||
EntitySailboat.Init(speed, weight, bodyColor, additionalColor, hull, sail);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
public override void DrawTransport(Graphics g)
|
||||||
/// <summary>
|
|
||||||
/// Установка позиции
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">Координата X</param>
|
|
||||||
/// <param name="y">Координата Y</param>
|
|
||||||
public void SetPosition(int x, int y)
|
|
||||||
{
|
{
|
||||||
// Изменение x, y
|
if (EntityBoat is not EntitySailboat sailboat)
|
||||||
if (x < 0 || x + _boatWidth > _pictureWidth)
|
|
||||||
{
|
|
||||||
x = 0;
|
|
||||||
}
|
|
||||||
if (y < 0 || y + _boatHeight > _pictureHeight)
|
|
||||||
{
|
|
||||||
y = 0;
|
|
||||||
}
|
|
||||||
_startPosX = x;
|
|
||||||
_startPosY = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Изменение направления перемещения
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="direction">Направление</param>
|
|
||||||
public void MoveTransport(DirectionType direction)
|
|
||||||
{
|
|
||||||
if (EntitySailboat == null)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (direction)
|
Pen pen = new(Color.Black);
|
||||||
{
|
|
||||||
//влево
|
|
||||||
case DirectionType.Left:
|
|
||||||
if (_startPosX - EntitySailboat.Step > 0)
|
|
||||||
{
|
|
||||||
_startPosX -= (int)EntitySailboat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
//вверх
|
|
||||||
case DirectionType.Up:
|
|
||||||
if (_startPosY - EntitySailboat.Step > 0)
|
|
||||||
{
|
|
||||||
_startPosY -= (int)EntitySailboat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
//вправо
|
|
||||||
case DirectionType.Right:
|
|
||||||
if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth)
|
|
||||||
{
|
|
||||||
_startPosX += (int)EntitySailboat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
//вниз
|
|
||||||
case DirectionType.Down:
|
|
||||||
if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight)
|
|
||||||
{
|
|
||||||
_startPosY += (int)EntitySailboat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Прорисовка объекта
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="g"></param>
|
|
||||||
public void DrawTransport(Graphics g)
|
|
||||||
{
|
|
||||||
if (EntitySailboat == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Pen pen = new(Color.Black, 4);
|
|
||||||
Brush additionalBrush = new
|
Brush additionalBrush = new
|
||||||
SolidBrush(EntitySailboat.AdditionalColor);
|
SolidBrush(sailboat.AdditionalColor);
|
||||||
|
|
||||||
// Усиленный корпус парусника
|
// Усиленный корпус парусника
|
||||||
if (EntitySailboat.Hull)
|
if (sailboat.Hull)
|
||||||
{
|
{
|
||||||
Point[] hullCooler = new Point[]
|
Point[] hullCooler = new Point[]
|
||||||
{
|
{
|
||||||
@ -165,38 +59,18 @@ namespace Sailboat
|
|||||||
g.DrawPolygon(pen, hullCooler);
|
g.DrawPolygon(pen, hullCooler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
base.DrawTransport(g);
|
||||||
// Основной корпус парусника
|
|
||||||
Brush Brush = new
|
|
||||||
SolidBrush(EntitySailboat.BodyColor);
|
|
||||||
|
|
||||||
Point[] hull = new Point[]
|
|
||||||
{
|
|
||||||
new Point(_startPosX + 5, _startPosY + 90),
|
|
||||||
new Point(_startPosX + 120, _startPosY + 90),
|
|
||||||
new Point(_startPosX + 180, _startPosY + 120),
|
|
||||||
new Point(_startPosX + 120, _startPosY + 150),
|
|
||||||
new Point(_startPosX + 5, _startPosY + 150)
|
|
||||||
};
|
|
||||||
g.FillPolygon(Brush, hull);
|
|
||||||
g.DrawPolygon(pen, hull);
|
|
||||||
|
|
||||||
Brush addBrush = new
|
|
||||||
SolidBrush(Color.Green);
|
|
||||||
|
|
||||||
g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 100, 40);
|
|
||||||
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 100, 40);
|
|
||||||
|
|
||||||
// Парус
|
// Парус
|
||||||
if (EntitySailboat.Sail)
|
if (sailboat.Sail)
|
||||||
{
|
{
|
||||||
Brush sailBrush = new
|
Brush sailBrush = new
|
||||||
SolidBrush(EntitySailboat.AdditionalColor);
|
SolidBrush(sailboat.AdditionalColor);
|
||||||
|
|
||||||
Point[] sail = new Point[]
|
Point[] sail = new Point[]
|
||||||
{
|
{
|
||||||
new Point(_startPosX + 65, _startPosY),
|
new Point(_startPosX + 65, _startPosY),
|
||||||
new Point(_startPosX + 150, _startPosY + 120),
|
new Point(_startPosX + 130, _startPosY + 120),
|
||||||
new Point(_startPosX + 15, _startPosY + 120)
|
new Point(_startPosX + 15, _startPosY + 120)
|
||||||
};
|
};
|
||||||
g.FillPolygon(sailBrush, sail);
|
g.FillPolygon(sailBrush, sail);
|
||||||
|
47
Sailboat/Sailboat/EntityBoat.cs
Normal file
47
Sailboat/Sailboat/EntityBoat.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Sailboat.Entities
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-сущность "Лодка"
|
||||||
|
/// </summary>
|
||||||
|
public class EntityBoat
|
||||||
|
{
|
||||||
|
/// <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 => (double)Speed * 100 / Weight;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор с параметрами
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес лодки</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
public EntityBoat(int speed, double weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
Speed = speed;
|
||||||
|
Weight = weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,38 +4,28 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Sailboat
|
namespace Sailboat.Entities
|
||||||
{
|
|
||||||
public class EntitySailboat
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Скорость
|
/// Класс-сущность "Парусная лодка"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Speed { get; private set; }
|
public class EntitySailboat : EntityBoat
|
||||||
/// <summary>
|
{
|
||||||
/// Вес
|
|
||||||
/// </summary>
|
|
||||||
public double Weight { get; private set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Основной цвет
|
|
||||||
/// </summary>
|
|
||||||
public Color BodyColor { get; private set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Дополнительный цвет (для опциональных элементов)
|
/// Дополнительный цвет (для опциональных элементов)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Color AdditionalColor { get; private set; }
|
public Color AdditionalColor { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Признак (опция) наличия усиленного корпуса
|
/// Признак (опция) наличия усиленного корпуса
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Hull { get; private set; }
|
public bool Hull { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Признак (опция) наличия паруса
|
/// Признак (опция) наличия паруса
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Sail { get; private set; }
|
public bool Sail { get; private set; }
|
||||||
/// <summary>
|
|
||||||
/// Шаг перемещения лодки
|
|
||||||
/// </summary>
|
|
||||||
public double Step => (double)Speed * 100 / Weight;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Инициализация полей объекта-класса парусной лодки
|
/// Инициализация полей объекта-класса парусной лодки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -45,12 +35,9 @@ namespace Sailboat
|
|||||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||||
/// <param name="hull">Признак наличия усиленного корпуса</param>
|
/// <param name="hull">Признак наличия усиленного корпуса</param>
|
||||||
/// <param name="sail">Признак наличия паруса</param>
|
/// <param name="sail">Признак наличия паруса</param>
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color
|
public EntitySailboat(int speed, double weight, Color bodyColor, Color
|
||||||
additionalColor, bool hull, bool sail)
|
additionalColor, bool hull, bool sail) : base(speed, weight, bodyColor)
|
||||||
{
|
{
|
||||||
Speed = speed;
|
|
||||||
Weight = weight;
|
|
||||||
BodyColor = bodyColor;
|
|
||||||
AdditionalColor = additionalColor;
|
AdditionalColor = additionalColor;
|
||||||
Hull = hull;
|
Hull = hull;
|
||||||
Sail = sail;
|
Sail = sail;
|
||||||
|
36
Sailboat/Sailboat/IMoveableObject.cs
Normal file
36
Sailboat/Sailboat/IMoveableObject.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Sailboat.DrawingObjects;
|
||||||
|
|
||||||
|
namespace Sailboat.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);
|
||||||
|
}
|
||||||
|
}
|
60
Sailboat/Sailboat/MoveToBorder.cs
Normal file
60
Sailboat/Sailboat/MoveToBorder.cs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Sailboat.MovementStrategy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Стратегия перемещения объекта к краю экрана
|
||||||
|
/// </summary>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
59
Sailboat/Sailboat/MoveToCenter.cs
Normal file
59
Sailboat/Sailboat/MoveToCenter.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Sailboat.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
57
Sailboat/Sailboat/ObjectParameters.cs
Normal file
57
Sailboat/Sailboat/ObjectParameters.cs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Sailboat.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
Sailboat/Sailboat/Status.cs
Normal file
18
Sailboat/Sailboat/Status.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Sailboat.MovementStrategy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Статус выполнения операции перемещения
|
||||||
|
/// </summary>
|
||||||
|
public enum Status
|
||||||
|
{
|
||||||
|
NotInit,
|
||||||
|
InProgress,
|
||||||
|
Finish
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user