done
This commit is contained in:
parent
744fd1c3c0
commit
f6e2956536
136
MotorBoat/MotorBoat/AbstractStrategy.cs
Normal file
136
MotorBoat/MotorBoat/AbstractStrategy.cs
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
using MotorBoat.MovementStrategy;
|
||||||
|
using MotorBoat;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MotorBoat.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
209
MotorBoat/MotorBoat/DrawningBoat.cs
Normal file
209
MotorBoat/MotorBoat/DrawningBoat.cs
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MotorBoat.Entities;
|
||||||
|
using MotorBoat;
|
||||||
|
|
||||||
|
namespace MotorBoat.DrawningObjects
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||||
|
/// </summary>
|
||||||
|
public class DrawningBoat
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс-сущность
|
||||||
|
/// </summary>
|
||||||
|
public EntityBoat? EntityBoat { get; protected set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина окна
|
||||||
|
/// </summary>
|
||||||
|
private int _pictureWidth = 800;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Высота окна
|
||||||
|
/// </summary>
|
||||||
|
private int _pictureHeight = 450;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Левая координата прорисовки
|
||||||
|
/// </summary>
|
||||||
|
protected int _startPosX;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Верхняя кооридната прорисовки
|
||||||
|
/// </summary>
|
||||||
|
protected int _startPosY;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина прорисовки
|
||||||
|
/// </summary>
|
||||||
|
protected readonly int _boatWeight = 165;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Высота прорисовки
|
||||||
|
/// </summary>
|
||||||
|
protected readonly int _boatHeight = 80;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
/// <param name="width">Ширина картинки</param>
|
||||||
|
/// <param name="height">Высота картинки</param>
|
||||||
|
/// <returns>true - объект создан, false - проверка не пройдена,
|
||||||
|
///нельзя создать объект в этих размерах</returns>
|
||||||
|
public DrawningBoat(int speed, double weight, Color bodyColor, int width, int height)
|
||||||
|
{
|
||||||
|
if (width < _boatWeight || 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 DrawningBoat(int speed, double weight, Color bodyColor, int
|
||||||
|
width, int height, int boatWidth, int boatHeight)
|
||||||
|
{
|
||||||
|
if (width <= _boatWeight || height <= _boatHeight)
|
||||||
|
return;
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
_boatWeight = 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 + _boatWeight <= _pictureWidth && y >= 0 && y + _boatHeight <= _pictureHeight)
|
||||||
|
{
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Прорисовка объекта
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
public virtual void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (EntityBoat == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pen pen = new(Color.Black, 1);
|
||||||
|
|
||||||
|
Brush mainBrush = new SolidBrush(EntityBoat.BodyColor);
|
||||||
|
|
||||||
|
// корпус
|
||||||
|
Point[] hull = new Point[]
|
||||||
|
{
|
||||||
|
new Point(_startPosX + 5, _startPosY + 0),
|
||||||
|
new Point(_startPosX + 120, _startPosY + 0),
|
||||||
|
new Point(_startPosX + 160, _startPosY + 35),
|
||||||
|
new Point(_startPosX + 120, _startPosY + 70),
|
||||||
|
new Point(_startPosX + 5, _startPosY + 70),
|
||||||
|
};
|
||||||
|
g.FillPolygon(mainBrush, hull);
|
||||||
|
g.DrawPolygon(pen, hull);
|
||||||
|
|
||||||
|
// осн часть
|
||||||
|
Brush blockBrush = new SolidBrush(Color.Olive);
|
||||||
|
g.FillRectangle(blockBrush, _startPosX + 20, _startPosY + 15, 80, 40);
|
||||||
|
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 15, 80, 40);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Координата X объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetPosX => _startPosX;
|
||||||
|
/// <summary>
|
||||||
|
/// Координата Y объекта
|
||||||
|
/// /// </summary>
|
||||||
|
public int GetPosY => _startPosY;
|
||||||
|
/// <summary>
|
||||||
|
/// Ширина объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetWidth => _boatWeight;
|
||||||
|
/// <summary>
|
||||||
|
/// Высота объекта
|
||||||
|
/// </summary>
|
||||||
|
public int GetHeight => _boatHeight;
|
||||||
|
/// <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 + _boatWeight < _pictureWidth,
|
||||||
|
//вниз
|
||||||
|
DirectionType.Down => _startPosY + EntityBoat.Step + _boatHeight < _pictureHeight,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Изменение направления перемещения
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Направление</param>
|
||||||
|
public void MoveTransport(DirectionType direction)
|
||||||
|
{
|
||||||
|
if (!CanMove(direction) || EntityBoat == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
//влево
|
||||||
|
case DirectionType.Left:
|
||||||
|
_startPosX -= (int)EntityBoat.Step;
|
||||||
|
break;
|
||||||
|
//вверх
|
||||||
|
case DirectionType.Up:
|
||||||
|
_startPosY -= (int)EntityBoat.Step;
|
||||||
|
break;
|
||||||
|
//вправо
|
||||||
|
case DirectionType.Right:
|
||||||
|
_startPosX += (int)EntityBoat.Step;
|
||||||
|
break;
|
||||||
|
//вниз
|
||||||
|
case DirectionType.Down:
|
||||||
|
_startPosY += (int)EntityBoat.Step;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,142 +3,34 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MotorBoat;
|
||||||
|
using MotorBoat.DrawningObjects;
|
||||||
|
using MotorBoat.Entities;
|
||||||
|
|
||||||
namespace MotorBoat
|
namespace MotorBoat.DrawningObjects
|
||||||
{
|
{
|
||||||
/// <summary>.
|
/// <summary>.
|
||||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DrawningMotorboat
|
public class DrawningMotorBoat : DrawningBoat
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность
|
/// Конструктор
|
||||||
/// </summary>
|
|
||||||
public EntityMotorBoat? EntityMotorBoat { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ширина окна
|
|
||||||
/// </summary>
|
|
||||||
private int _pictureWight = 800;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Высота окна
|
|
||||||
/// </summary>
|
|
||||||
private int _pictureHeight = 450;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Левая координата прорисовки катера
|
|
||||||
/// </summary>
|
|
||||||
private int _startPosX = 0;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Правая координата прорисовки катера
|
|
||||||
/// </summary>
|
|
||||||
private int _startPosY = 0;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ширина прорисовки катера
|
|
||||||
/// </summary>
|
|
||||||
private readonly int _boatWight = 165;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Высота прорисовки катера
|
|
||||||
/// </summary>
|
|
||||||
private readonly int _boatHeight = 80;
|
|
||||||
|
|
||||||
/// <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="engine">Признак наличия двигателя в корме</param>
|
/// <param name="glass">Признак наличия стекла спереди</param>
|
||||||
/// <param name="glass">Признак наличия защитного стекла впереди</param>
|
/// <param name="engine">Признак наличия двигателя</param>
|
||||||
/// <param name="width">Ширина картинки</param>
|
/// <param name="width">Ширина картинки</param>
|
||||||
/// <param name="height">Выслота картинки</param>
|
/// <param name="height">Высота картинки</param>
|
||||||
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
|
public DrawningMotorBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool glass,
|
||||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool engine, bool glass, int width, int height)
|
bool engine, int width, int height) : base (speed, weight, bodyColor, width, height, 165, 80)
|
||||||
{
|
{
|
||||||
if (width <= _boatWight || height <= _boatHeight)
|
if (EntityBoat != null)
|
||||||
return false;
|
|
||||||
_pictureWight = width;
|
|
||||||
_pictureHeight = height;
|
|
||||||
EntityMotorBoat = new EntityMotorBoat();
|
|
||||||
EntityMotorBoat.Init(speed, weight, bodyColor, additionalColor, engine, glass);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Установка позиции
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">Координата X</param>
|
|
||||||
/// <param name="y">Координата Y</param>
|
|
||||||
public void SetPosition(int x, int y)
|
|
||||||
{
|
|
||||||
if (EntityMotorBoat == null) return;
|
|
||||||
while (x + _boatWight > _pictureWight)
|
|
||||||
{
|
{
|
||||||
x -= (int)EntityMotorBoat.Step;
|
EntityBoat = new EntityMotorBoat(speed, weight, bodyColor, additionalColor, glass, engine);
|
||||||
}
|
|
||||||
while (x < 0)
|
|
||||||
{
|
|
||||||
x += (int)EntityMotorBoat.Step;
|
|
||||||
}
|
|
||||||
while (y + _boatHeight > _pictureHeight)
|
|
||||||
{
|
|
||||||
y -= (int)EntityMotorBoat.Step;
|
|
||||||
}
|
|
||||||
while (y < 0)
|
|
||||||
{
|
|
||||||
y += (int)EntityMotorBoat.Step;
|
|
||||||
}
|
|
||||||
_startPosX = x;
|
|
||||||
_startPosY = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Изменение направления перемещения
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="direction">Направление</param>
|
|
||||||
public void MoveTransport(DirectionType direction)
|
|
||||||
{
|
|
||||||
if (EntityMotorBoat == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
//влево
|
|
||||||
case DirectionType.Left:
|
|
||||||
if (_startPosX - EntityMotorBoat.Step > 0)
|
|
||||||
{
|
|
||||||
_startPosX -= (int)EntityMotorBoat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
//вверх
|
|
||||||
case DirectionType.Up:
|
|
||||||
if (_startPosY - EntityMotorBoat.Step > 0)
|
|
||||||
{
|
|
||||||
_startPosY -= (int)EntityMotorBoat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
//вправо
|
|
||||||
case DirectionType.Right:
|
|
||||||
if (_startPosX + _boatWight + EntityMotorBoat.Step < _pictureWight)
|
|
||||||
{
|
|
||||||
_startPosX += (int)EntityMotorBoat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
//вниз
|
|
||||||
case DirectionType.Down:
|
|
||||||
if (_startPosY + _boatHeight + EntityMotorBoat.Step < _pictureHeight)
|
|
||||||
{
|
|
||||||
_startPosY += (int)EntityMotorBoat.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,42 +38,32 @@ namespace MotorBoat
|
|||||||
/// Прорисовка объекта
|
/// Прорисовка объекта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="g"></param>
|
/// <param name="g"></param>
|
||||||
public void DrawTransport(Graphics g)
|
public override void DrawTransport(Graphics g)
|
||||||
{
|
{
|
||||||
if (EntityMotorBoat == null)
|
if (EntityBoat is not EntityMotorBoat motorBoat)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Pen pen = new(Color.Black, 1);
|
Pen pen = new(Color.Black, 1);
|
||||||
Brush mainBrush = new SolidBrush(EntityMotorBoat.BodyColor);
|
Brush additionalBrush = new SolidBrush(motorBoat.AdditionalColor);
|
||||||
|
|
||||||
// корпус
|
if (motorBoat.Glass)
|
||||||
Point[] hull = new Point[]
|
|
||||||
{
|
{
|
||||||
new Point(_startPosX + 5, _startPosY + 0),
|
// стекло впереди
|
||||||
new Point(_startPosX + 120, _startPosY + 0),
|
Brush glassBrush = new SolidBrush(Color.LightBlue);
|
||||||
new Point(_startPosX + 160, _startPosY + 35),
|
g.FillEllipse(glassBrush, _startPosX + 20, _startPosY + 15, 100, 40);
|
||||||
new Point(_startPosX + 120, _startPosY + 70),
|
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 15, 100, 40);
|
||||||
new Point(_startPosX + 5, _startPosY + 70),
|
}
|
||||||
};
|
|
||||||
g.FillPolygon(mainBrush, hull);
|
|
||||||
g.DrawPolygon(pen, hull);
|
|
||||||
|
|
||||||
// стекло впереди
|
base.DrawTransport(g);
|
||||||
Brush glassBrush = new SolidBrush(Color.LightBlue);
|
|
||||||
g.FillEllipse(glassBrush, _startPosX + 20, _startPosY + 15, 100, 40);
|
|
||||||
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 15, 100, 40);
|
|
||||||
|
|
||||||
// осн часть
|
if (motorBoat.Engine)
|
||||||
Brush blockBrush = new SolidBrush(EntityMotorBoat.AdditionalColor);
|
{
|
||||||
g.FillRectangle(blockBrush, _startPosX + 20, _startPosY + 15, 80, 40);
|
// двигатель
|
||||||
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 15, 80, 40);
|
g.FillRectangle(additionalBrush, _startPosX + 0, _startPosY + 10, 5, 50);
|
||||||
|
g.DrawRectangle(pen, _startPosX + 0, _startPosY + 10, 5, 50);
|
||||||
// двигатель
|
}
|
||||||
Brush engineBrush = new
|
|
||||||
SolidBrush(EntityMotorBoat.AdditionalColor);
|
|
||||||
g.FillRectangle(engineBrush, _startPosX + 0, _startPosY + 10, 5, 50);
|
|
||||||
g.DrawRectangle(pen, _startPosX + 0, _startPosY + 10, 5, 50);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
39
MotorBoat/MotorBoat/DrawningObjectBoat.cs
Normal file
39
MotorBoat/MotorBoat/DrawningObjectBoat.cs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
using MotorBoat.MovementStrategy;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MotorBoat.DrawningObjects;
|
||||||
|
|
||||||
|
namespace MotorBoat.MovementStrategy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningBoat (паттерн Adapter)
|
||||||
|
/// </summary>
|
||||||
|
public class DrawningObjectBoat : IMoveableObject
|
||||||
|
{
|
||||||
|
private readonly DrawningBoat? _drawningBoat = null;
|
||||||
|
public DrawningObjectBoat(DrawningBoat drawningBoat)
|
||||||
|
{
|
||||||
|
_drawningBoat = drawningBoat;
|
||||||
|
}
|
||||||
|
public ObjectParameters? GetObjectPosition
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_drawningBoat == null || _drawningBoat.EntityBoat == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_drawningBoat.GetPosX,
|
||||||
|
_drawningBoat.GetPosY, _drawningBoat.GetWidth, _drawningBoat.GetHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int GetStep => (int)(_drawningBoat?.EntityBoat?.Step ?? 0);
|
||||||
|
public bool CheckCanMove(DirectionType direction) =>
|
||||||
|
_drawningBoat?.CanMove(direction) ?? false;
|
||||||
|
public void MoveObject(DirectionType direction) =>
|
||||||
|
_drawningBoat?.MoveTransport(direction);
|
||||||
|
}
|
||||||
|
}
|
47
MotorBoat/MotorBoat/EntityBoat.cs
Normal file
47
MotorBoat/MotorBoat/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 MotorBoat.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,26 +3,15 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MotorBoat.Entities;
|
||||||
|
|
||||||
namespace MotorBoat
|
namespace MotorBoat.Entities
|
||||||
{
|
{
|
||||||
public class EntityMotorBoat
|
/// <summary>
|
||||||
|
/// Класс-сущность моторная лодка
|
||||||
|
/// </summary>
|
||||||
|
public class EntityMotorBoat : 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>
|
||||||
/// Дополнительный цвет
|
/// Дополнительный цвет
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -38,11 +27,6 @@ namespace MotorBoat
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Glass { get; private set; }
|
public bool Glass { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Шаг перемещения моторной лодки
|
|
||||||
/// </summary>
|
|
||||||
public double Step => (double)Speed * 100 / Weight;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Инициализация полей объекта-класса моторной лодки
|
/// Инициализация полей объекта-класса моторной лодки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -53,11 +37,9 @@ namespace MotorBoat
|
|||||||
/// <param name="engine">Признак наличия двигателя в корме</param>
|
/// <param name="engine">Признак наличия двигателя в корме</param>
|
||||||
/// <param name="glass">Признак наличия защитного стекла впереди</param>
|
/// <param name="glass">Признак наличия защитного стекла впереди</param>
|
||||||
|
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool engine, bool glass)
|
public EntityMotorBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool engine, bool glass) :
|
||||||
|
base (speed, weight, bodyColor)
|
||||||
{
|
{
|
||||||
Speed = speed;
|
|
||||||
Weight = weight;
|
|
||||||
BodyColor = bodyColor;
|
|
||||||
AdditionalColor = additionalColor;
|
AdditionalColor = additionalColor;
|
||||||
Engine = engine;
|
Engine = engine;
|
||||||
Glass = glass;
|
Glass = glass;
|
||||||
|
45
MotorBoat/MotorBoat/FormMotorBoat.Designer.cs
generated
45
MotorBoat/MotorBoat/FormMotorBoat.Designer.cs
generated
@ -34,6 +34,9 @@
|
|||||||
buttonRight = new Button();
|
buttonRight = new Button();
|
||||||
buttonUp = new Button();
|
buttonUp = new Button();
|
||||||
buttonDown = new Button();
|
buttonDown = new Button();
|
||||||
|
ButonCreateMotorBoat = new Button();
|
||||||
|
comboBoxStrategy = new ComboBox();
|
||||||
|
ButtonStep = new Button();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxMotorBoat).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxMotorBoat).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -50,11 +53,11 @@
|
|||||||
// buttonCreate
|
// buttonCreate
|
||||||
//
|
//
|
||||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
buttonCreate.Location = new Point(12, 409);
|
buttonCreate.Location = new Point(189, 412);
|
||||||
buttonCreate.Name = "buttonCreate";
|
buttonCreate.Name = "buttonCreate";
|
||||||
buttonCreate.Size = new Size(90, 40);
|
buttonCreate.Size = new Size(171, 37);
|
||||||
buttonCreate.TabIndex = 1;
|
buttonCreate.TabIndex = 1;
|
||||||
buttonCreate.Text = "Создать";
|
buttonCreate.Text = "Создать лодку";
|
||||||
buttonCreate.UseVisualStyleBackColor = true;
|
buttonCreate.UseVisualStyleBackColor = true;
|
||||||
buttonCreate.Click += ButtonCreate_Click;
|
buttonCreate.Click += ButtonCreate_Click;
|
||||||
//
|
//
|
||||||
@ -106,11 +109,44 @@
|
|||||||
buttonDown.UseVisualStyleBackColor = true;
|
buttonDown.UseVisualStyleBackColor = true;
|
||||||
buttonDown.Click += ButtonMove_Click;
|
buttonDown.Click += ButtonMove_Click;
|
||||||
//
|
//
|
||||||
|
// ButonCreateMotorBoat
|
||||||
|
//
|
||||||
|
ButonCreateMotorBoat.Location = new Point(12, 412);
|
||||||
|
ButonCreateMotorBoat.Name = "ButonCreateMotorBoat";
|
||||||
|
ButonCreateMotorBoat.Size = new Size(171, 37);
|
||||||
|
ButonCreateMotorBoat.TabIndex = 6;
|
||||||
|
ButonCreateMotorBoat.Text = "Создать моторную лодку";
|
||||||
|
ButonCreateMotorBoat.UseVisualStyleBackColor = true;
|
||||||
|
ButonCreateMotorBoat.Click += ButonCreateMotorBoat_Click;
|
||||||
|
//
|
||||||
|
// comboBoxStrategy
|
||||||
|
//
|
||||||
|
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||||
|
comboBoxStrategy.FormattingEnabled = true;
|
||||||
|
comboBoxStrategy.Items.AddRange(new object[] { "Перемещение в центр", "Перемещение в правый нижний угол" });
|
||||||
|
comboBoxStrategy.Location = new Point(749, 12);
|
||||||
|
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||||
|
comboBoxStrategy.Size = new Size(121, 23);
|
||||||
|
comboBoxStrategy.TabIndex = 7;
|
||||||
|
//
|
||||||
|
// ButtonStep
|
||||||
|
//
|
||||||
|
ButtonStep.Location = new Point(795, 41);
|
||||||
|
ButtonStep.Name = "ButtonStep";
|
||||||
|
ButtonStep.Size = new Size(75, 23);
|
||||||
|
ButtonStep.TabIndex = 8;
|
||||||
|
ButtonStep.Text = "Шаг";
|
||||||
|
ButtonStep.UseVisualStyleBackColor = true;
|
||||||
|
ButtonStep.Click += ButtonStep_Click;
|
||||||
|
//
|
||||||
// FormMotorBoat
|
// FormMotorBoat
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(884, 461);
|
ClientSize = new Size(884, 461);
|
||||||
|
Controls.Add(ButtonStep);
|
||||||
|
Controls.Add(comboBoxStrategy);
|
||||||
|
Controls.Add(ButonCreateMotorBoat);
|
||||||
Controls.Add(buttonDown);
|
Controls.Add(buttonDown);
|
||||||
Controls.Add(buttonUp);
|
Controls.Add(buttonUp);
|
||||||
Controls.Add(buttonRight);
|
Controls.Add(buttonRight);
|
||||||
@ -133,5 +169,8 @@
|
|||||||
private Button buttonRight;
|
private Button buttonRight;
|
||||||
private Button buttonUp;
|
private Button buttonUp;
|
||||||
private Button buttonDown;
|
private Button buttonDown;
|
||||||
|
private Button ButonCreateMotorBoat;
|
||||||
|
private ComboBox comboBoxStrategy;
|
||||||
|
private Button ButtonStep;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Data;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using MotorBoat;
|
||||||
|
using MotorBoat.MovementStrategy;
|
||||||
|
using MotorBoat.DrawningObjects;
|
||||||
|
|
||||||
namespace MotorBoat
|
namespace MotorBoat
|
||||||
{
|
{
|
||||||
public partial class FormMotorBoat : Form
|
public partial class FormMotorBoat : Form
|
||||||
@ -5,7 +18,12 @@ namespace MotorBoat
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
|
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private DrawningMotorboat? _drawningMotorboat;
|
private DrawningBoat? _drawningBoat;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
|
||||||
|
/// </summary>
|
||||||
|
private AbstractStrategy? _abstractStrategy;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Èíèöèàëèçàöèÿ ôîðìû
|
/// Èíèöèàëèçàöèÿ ôîðìû
|
||||||
@ -20,42 +38,56 @@ namespace MotorBoat
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void Draw()
|
private void Draw()
|
||||||
{
|
{
|
||||||
if (_drawningMotorboat == null)
|
if (_drawningBoat == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Bitmap bmp = new(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
Bitmap bmp = new(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||||
Graphics gr = Graphics.FromImage(bmp);
|
Graphics gr = Graphics.FromImage(bmp);
|
||||||
_drawningMotorboat.DrawTransport(gr);
|
_drawningBoat.DrawTransport(gr);
|
||||||
pictureBoxMotorBoat.Image = bmp;
|
pictureBoxMotorBoat.Image = bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
|
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ëîäêó"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Random random = new();
|
Random random = new();
|
||||||
_drawningMotorboat = new DrawningMotorboat();
|
_drawningBoat = new DrawningBoat(random.Next(100, 300), random.Next(1000, 3000),
|
||||||
_drawningMotorboat.Init(random.Next(100, 300), random.Next(1000, 3000),
|
|
||||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
|
||||||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
|
|
||||||
pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||||
_drawningMotorboat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
|
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ìîòîðíóþ ëîäêó"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButonCreateMotorBoat_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Random random = new();
|
||||||
|
_drawningBoat = new DrawningMotorBoat(random.Next(100, 300), random.Next(1000, 3000),
|
||||||
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||||
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||||
|
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
|
||||||
|
pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height);
|
||||||
|
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Èçìåíåíèå ïîëîæåíèÿ ëîäêè
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonMove_Click(object sender, EventArgs e)
|
private void ButtonMove_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (_drawningMotorboat == null)
|
if (_drawningBoat == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -63,19 +95,63 @@ namespace MotorBoat
|
|||||||
switch (name)
|
switch (name)
|
||||||
{
|
{
|
||||||
case "buttonUp":
|
case "buttonUp":
|
||||||
_drawningMotorboat.MoveTransport(DirectionType.Up);
|
_drawningBoat.MoveTransport(DirectionType.Up);
|
||||||
break;
|
break;
|
||||||
case "buttonDown":
|
case "buttonDown":
|
||||||
_drawningMotorboat.MoveTransport(DirectionType.Down);
|
_drawningBoat.MoveTransport(DirectionType.Down);
|
||||||
break;
|
break;
|
||||||
case "buttonLeft":
|
case "buttonLeft":
|
||||||
_drawningMotorboat.MoveTransport(DirectionType.Left);
|
_drawningBoat.MoveTransport(DirectionType.Left);
|
||||||
break;
|
break;
|
||||||
case "buttonRight":
|
case "buttonRight":
|
||||||
_drawningMotorboat.MoveTransport(DirectionType.Right);
|
_drawningBoat.MoveTransport(DirectionType.Right);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonStep_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_drawningBoat == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (comboBoxStrategy.Enabled)
|
||||||
|
{
|
||||||
|
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||||
|
switch
|
||||||
|
{
|
||||||
|
0 => new MoveToCenter(),
|
||||||
|
1 => new MoveToBorder(),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
if (_abstractStrategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_abstractStrategy.SetData(new
|
||||||
|
DrawningObjectBoat(_drawningBoat), pictureBoxMotorBoat.Width,
|
||||||
|
pictureBoxMotorBoat.Height);
|
||||||
|
comboBoxStrategy.Enabled = false;
|
||||||
|
}
|
||||||
|
if (_abstractStrategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_abstractStrategy.MakeStep();
|
||||||
|
Draw();
|
||||||
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
_abstractStrategy = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
36
MotorBoat/MotorBoat/IMovableObject.cs
Normal file
36
MotorBoat/MotorBoat/IMovableObject.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MotorBoat.MovementStrategy;
|
||||||
|
using MotorBoat;
|
||||||
|
|
||||||
|
namespace MotorBoat.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);
|
||||||
|
}
|
||||||
|
}
|
61
MotorBoat/MotorBoat/MoveToBorder.cs
Normal file
61
MotorBoat/MotorBoat/MoveToBorder.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MotorBoat.MovementStrategy;
|
||||||
|
|
||||||
|
namespace MotorBoat.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
60
MotorBoat/MotorBoat/MoveToCenter.cs
Normal file
60
MotorBoat/MotorBoat/MoveToCenter.cs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
using MotorBoat.MovementStrategy;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MotorBoat.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
MotorBoat/MotorBoat/ObjectParameters.cs
Normal file
57
MotorBoat/MotorBoat/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 MotorBoat.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
MotorBoat/MotorBoat/Status.cs
Normal file
18
MotorBoat/MotorBoat/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 MotorBoat.MovementStrategy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Статус выполнения операции перемещения
|
||||||
|
/// </summary>
|
||||||
|
public enum Status
|
||||||
|
{
|
||||||
|
NotInit,
|
||||||
|
InProgress,
|
||||||
|
Finish
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user