Создала классы
This commit is contained in:
parent
289e6b6192
commit
6dceedfa0d
109
WarmlyShip/WarmlyShip/AbstractStrategy.cs
Normal file
109
WarmlyShip/WarmlyShip/AbstractStrategy.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WarmlyShip.DrawingObjects;
|
||||
|
||||
|
||||
namespace WarmlyShip.MovementStrategy
|
||||
{
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
private IMoveableObject? _moveableObject;
|
||||
private Status _state = Status.NotInit;
|
||||
protected int FieldWidth { get; private set; }
|
||||
protected int FieldHeight { get; private set; }
|
||||
public Status GetStatus() { return _state; }
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
35
WarmlyShip/WarmlyShip/DrawingObjectShip.cs
Normal file
35
WarmlyShip/WarmlyShip/DrawingObjectShip.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WarmlyShip.DrawingObjects;
|
||||
|
||||
namespace WarmlyShip.MovementStrategy
|
||||
{
|
||||
public class DrawingObjectShip : IMoveableObject
|
||||
{
|
||||
private readonly DrawingWarmlyShip? _drawingWarmlyShip = null;
|
||||
public DrawingObjectShip(DrawingWarmlyShip drawingWarmlyShip)
|
||||
{
|
||||
_drawingWarmlyShip = drawingWarmlyShip;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawingWarmlyShip == null || _drawingWarmlyShip.EntityWarmlyShip == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingWarmlyShip.GetPosX,
|
||||
_drawingWarmlyShip.GetPosY, _drawingWarmlyShip.GetWidth, _drawingWarmlyShip.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawingWarmlyShip?.EntityWarmlyShip?.Step ?? 0);
|
||||
public bool CheckCanMove(DirectionType direction) =>
|
||||
_drawingWarmlyShip?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionType direction) =>
|
||||
_drawingWarmlyShip?.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -3,18 +3,16 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WarmlyShip.Entities;
|
||||
|
||||
namespace WarmlyShip
|
||||
namespace WarmlyShip.DrawingObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawingWarmlyShip
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityWarmlyShip? EntityWarmlyShip { get; private set; }
|
||||
public EntityWarmlyShip? EntityWarmlyShip { get; protected set; }
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
@ -24,45 +22,63 @@ namespace WarmlyShip
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки теплохода
|
||||
/// Левая координата прорисовки
|
||||
/// </summary>
|
||||
private int _startPosX;
|
||||
protected int _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки теплохода
|
||||
/// Верхняя кооридната прорисовки
|
||||
/// </summary>
|
||||
private int _startPosY;
|
||||
protected int _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки теплохода
|
||||
/// Ширина прорисовки
|
||||
/// </summary>
|
||||
private readonly int _WarmlyShipWidth = 180;
|
||||
protected readonly int _WarmlyShipWidth = 185;
|
||||
/// <summary>
|
||||
/// Высота прорисовки теплохода
|
||||
/// Высота прорисовки
|
||||
/// </summary>
|
||||
private readonly int _WarmlyShipHeight = 185;
|
||||
protected readonly int _WarmlyShipHeight = 180;
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Цвет корпуса</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="pipes">Признак наличия труб</param>
|
||||
/// <param name="section">Признак наличия отсека для топлива</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 pipes, bool section,
|
||||
int width, int height)
|
||||
/// <returns>true - объект создан, false - проверка не пройдена,
|
||||
///нельзя создать объект в этих размерах</retu rns>
|
||||
|
||||
public DrawingWarmlyShip(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if (width < _WarmlyShipWidth || height < _WarmlyShipHeight)
|
||||
{
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityWarmlyShip = new EntityWarmlyShip();
|
||||
EntityWarmlyShip.Init(speed, weight, bodyColor, additionalColor, pipes, section);
|
||||
return true;
|
||||
EntityWarmlyShip = new EntityWarmlyShip(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="buttleshipWidth">Ширина прорисовки автомобиля</param>
|
||||
/// <param name="buttleshipHeight">Высота прорисовки автомобиля</param>
|
||||
protected DrawingWarmlyShip(int speed, double weight, Color bodyColor, int
|
||||
width, int height, int warmlyShipWidth, int warmlyShipHeight)
|
||||
{
|
||||
if (width <= _WarmlyShipWidth || height <= _WarmlyShipHeight)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_WarmlyShipWidth = warmlyShipWidth;
|
||||
_WarmlyShipHeight = warmlyShipHeight;
|
||||
EntityWarmlyShip = new EntityWarmlyShip(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
@ -81,49 +97,12 @@ namespace WarmlyShip
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityWarmlyShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityWarmlyShip.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityWarmlyShip.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _WarmlyShipWidth + EntityWarmlyShip.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _WarmlyShipHeight + EntityWarmlyShip.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityWarmlyShip == null)
|
||||
{
|
||||
@ -131,7 +110,7 @@ namespace WarmlyShip
|
||||
}
|
||||
Pen pen = new(Color.Black, 2);
|
||||
Pen anchor = new(Color.Black, 4);
|
||||
Brush additionalBrush = new SolidBrush(EntityWarmlyShip.BodyColor);
|
||||
Brush bodyBrush = new SolidBrush(EntityWarmlyShip.BodyColor);
|
||||
//корпус теплохода
|
||||
Point[] hull = new Point[]
|
||||
{
|
||||
@ -140,31 +119,84 @@ namespace WarmlyShip
|
||||
new Point(_startPosX + 140, _startPosY + 185),
|
||||
new Point(_startPosX + 40, _startPosY + 185),
|
||||
};
|
||||
g.FillPolygon(additionalBrush, hull);
|
||||
g.FillPolygon(bodyBrush, hull);
|
||||
g.DrawPolygon(pen, hull);
|
||||
Brush bra = new SolidBrush(EntityWarmlyShip.AdditionalColor);
|
||||
//палуба
|
||||
g.FillRectangle(bra, _startPosX + 25, _startPosY + 80, 130, 30);
|
||||
g.FillRectangle(bodyBrush, _startPosX + 25, _startPosY + 80, 130, 30);
|
||||
g.DrawRectangle(pen, _startPosX + 25, _startPosY + 80, 130, 30);
|
||||
//отсек для топлива
|
||||
Brush brGray = new SolidBrush(Color.Gray);
|
||||
if (EntityWarmlyShip.Section)
|
||||
{
|
||||
g.FillEllipse(brGray, _startPosX + 130, _startPosY + 130, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX + 130, _startPosY + 130, 20, 20);
|
||||
}
|
||||
//трубы
|
||||
if (EntityWarmlyShip.Pipes)
|
||||
{
|
||||
g.FillRectangle(brGray, _startPosX + 55, _startPosY, 25, 80);
|
||||
g.DrawRectangle(pen, _startPosX + 55, _startPosY, 25, 80);
|
||||
g.FillRectangle(brGray, _startPosX + 90, _startPosY + 20, 25, 60);
|
||||
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 20, 25, 60);
|
||||
}
|
||||
|
||||
//якорь
|
||||
g.DrawLine(anchor, new Point(_startPosX + 50, _startPosY + 130), new Point(_startPosX + 50,_startPosY + 150));
|
||||
g.DrawLine(anchor, new Point(_startPosX + 40, _startPosY + 140), new Point(_startPosX + 60, _startPosY + 140));
|
||||
g.DrawLine(anchor, new Point(_startPosX + 45, _startPosY + 150), new Point(_startPosX + 55, _startPosY + 150));
|
||||
}
|
||||
public int GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// /// </summary>
|
||||
public int GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _WarmlyShipWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _WarmlyShipHeight;
|
||||
/// <summary>
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||
public bool CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityWarmlyShip == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
DirectionType.Left => _startPosX - EntityWarmlyShip.Step > 0,
|
||||
//вверх
|
||||
DirectionType.Up => _startPosY - EntityWarmlyShip.Step > 0,
|
||||
//вправо
|
||||
DirectionType.Right => _startPosX + EntityWarmlyShip.Step + _WarmlyShipWidth < _pictureWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + EntityWarmlyShip.Step + _WarmlyShipHeight < _pictureHeight,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityWarmlyShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
_startPosX -= (int)EntityWarmlyShip.Step;
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
_startPosY -= (int)EntityWarmlyShip.Step;
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
_startPosX += (int)EntityWarmlyShip.Step;
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
_startPosY += (int)EntityWarmlyShip.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
48
WarmlyShip/WarmlyShip/DrawingWarmlyShipWithPipes.cs
Normal file
48
WarmlyShip/WarmlyShip/DrawingWarmlyShipWithPipes.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WarmlyShip.Entities;
|
||||
|
||||
namespace WarmlyShip.DrawingObjects
|
||||
{
|
||||
public class DrawingWarmlyShipWithPipes : DrawingWarmlyShip
|
||||
{
|
||||
public DrawingWarmlyShipWithPipes(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool pipes, bool section, int width, int height)
|
||||
: base(speed, weight, bodyColor, width, height, 185, 180)
|
||||
{
|
||||
if (EntityWarmlyShip != null)
|
||||
{
|
||||
EntityWarmlyShip = new EntityWarmlyShipWithPipes(speed, weight, bodyColor, additionalColor, pipes, section);
|
||||
}
|
||||
}
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityWarmlyShip is not EntityWarmlyShipWithPipes warmlyShip)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black, 2);
|
||||
Pen anchor = new(Color.Black, 4);
|
||||
Brush additionalBrush = new SolidBrush(EntityWarmlyShip.BodyColor);
|
||||
//отсек для топлива
|
||||
Brush brGray = new SolidBrush(Color.Gray);
|
||||
if (EntityWarmlyShip.Section)
|
||||
{
|
||||
g.FillEllipse(brGray, _startPosX + 130, _startPosY + 130, 20, 20);
|
||||
g.DrawEllipse(pen, _startPosX + 130, _startPosY + 130, 20, 20);
|
||||
}
|
||||
//трубы
|
||||
if (EntityWarmlyShip.Pipes)
|
||||
{
|
||||
g.FillRectangle(brGray, _startPosX + 55, _startPosY, 25, 80);
|
||||
g.DrawRectangle(pen, _startPosX + 55, _startPosY, 25, 80);
|
||||
g.FillRectangle(brGray, _startPosX + 90, _startPosY + 20, 25, 60);
|
||||
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 20, 25, 60);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,56 +4,19 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WarmlyShip
|
||||
namespace WarmlyShip.Entities
|
||||
{
|
||||
public class EntityWarmlyShip
|
||||
{
|
||||
/// <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 Color AdditionalColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия труб
|
||||
/// </summary>
|
||||
public bool Pipes { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия отсека для топлива
|
||||
/// </summary>
|
||||
public bool Section { 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="pipes">Признак наличия труб</param>
|
||||
/// <param name="section">Признак наличия отсека для топлива</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool pipes, bool section)
|
||||
public EntityWarmlyShip(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Pipes = pipes;
|
||||
Section = section;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
WarmlyShip/WarmlyShip/EntityWarmlyShipWithPipes.cs
Normal file
23
WarmlyShip/WarmlyShip/EntityWarmlyShipWithPipes.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WarmlyShip.Entities
|
||||
{
|
||||
public class EntityWarmlyShipWithPipes : EntityWarmlyShip
|
||||
{
|
||||
public Color AdditionalColor { get; private set; }
|
||||
public bool Pipes { get; private set; }
|
||||
public bool Section { get; private set; }
|
||||
public EntityWarmlyShipWithPipes(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool pipes, bool section)
|
||||
: base (speed, weight, bodyColor)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
Pipes = pipes;
|
||||
Section = section;
|
||||
}
|
||||
}
|
||||
}
|
18
WarmlyShip/WarmlyShip/IMoveableObject.cs
Normal file
18
WarmlyShip/WarmlyShip/IMoveableObject.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WarmlyShip.DrawingObjects;
|
||||
|
||||
|
||||
namespace WarmlyShip.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
int GetStep { get; }
|
||||
bool CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
||||
}
|
57
WarmlyShip/WarmlyShip/MoveToBorder.cs
Normal file
57
WarmlyShip/WarmlyShip/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 WarmlyShip.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
WarmlyShip/WarmlyShip/MoveToCenter.cs
Normal file
56
WarmlyShip/WarmlyShip/MoveToCenter.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WarmlyShip.MovementStrategy
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
54
WarmlyShip/WarmlyShip/ObjectParameters.cs
Normal file
54
WarmlyShip/WarmlyShip/ObjectParameters.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WarmlyShip.MovementStrategy
|
||||
{
|
||||
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
WarmlyShip/WarmlyShip/Status.cs
Normal file
15
WarmlyShip/WarmlyShip/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 WarmlyShip.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user