Лабораторная 2 базовая

This commit is contained in:
katana 2023-10-07 16:41:58 +04:00
parent b79b94b549
commit 2028f2ded6
14 changed files with 853 additions and 239 deletions

View File

@ -0,0 +1,137 @@
using ProjectSeaplane.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ProjectSeaplane.Direction;
namespace ProjectSeaplane.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;
}
}
}

View File

@ -6,26 +6,29 @@ using System.Threading.Tasks;
namespace ProjectSeaplane
{
/// <summary>
/// Направление перемещения
/// </summary>
public enum DirectionType
public class Direction
{
/// <summary>
/// Вверх
/// Направление перемещения
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
public enum DirectionType
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}
}
}

View File

@ -0,0 +1,243 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectSeaplane.Entities;
using static ProjectSeaplane.Direction;
namespace ProjectSeaplane.DrawningObjects
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningPlane
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityPlane? EntityPlane { 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 int _planeWidth = 200;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private int _planeHeight = 90;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawningPlane(int speed, double weight, Color bodyColor, int width, int height)
{
// TODO: Продумать проверки
if (width > _planeWidth && height > _planeHeight)
{
_pictureWidth = width;
_pictureHeight = height;
EntityPlane = new EntityPlane(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="planeWidth">Ширина прорисовки автомобиля</param>
/// <param name="planeHeight">Высота прорисовки автомобиля</param>
protected DrawningPlane(int speed, double weight, Color bodyColor, int width, int height, int planeWidth, int planeHeight)
{
// TODO: Продумать проверки
if (width > _planeWidth && height > _planeHeight)
{
_pictureWidth = width;
_pictureHeight = height;
_planeWidth = planeWidth;
_planeHeight = planeHeight;
EntityPlane = new EntityPlane(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 < _pictureWidth - _planeWidth))
_startPosX = x;
else _startPosX = 0;
if ((y > 0) && (y < _pictureHeight - _planeHeight))
_startPosY = y;
else _startPosY = 0;
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _planeWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _planeHeight;
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(DirectionType direction)
{
if (EntityPlane == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityPlane.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityPlane.Step > 0,
// вправо
DirectionType.Right => _startPosX + _planeWidth + EntityPlane.Step < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + _planeHeight + EntityPlane.Step < _pictureHeight,
};
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityPlane == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
_startPosX -= (int)EntityPlane.Step;
break;
//вверх
case DirectionType.Up:
_startPosY -= (int)EntityPlane.Step;
break;
// вправо
case DirectionType.Right:
_startPosX += (int)EntityPlane.Step;
break;
//вниз
case DirectionType.Down:
_startPosY += (int)EntityPlane.Step;
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityPlane == null)
{
return;
}
Brush BodyBrush = new SolidBrush(EntityPlane.BodyColor);
Pen pen = new(Color.Black);
g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 10, _startPosY + 30);
g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 40, _startPosY + 30);
g.DrawLine(pen, _startPosX + 10, _startPosY + 30, _startPosX + 150, _startPosY + 30);
g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 150, _startPosY + 80);
g.DrawLine(pen, _startPosX + 10, _startPosY + 80, _startPosX + 150, _startPosY + 80);
g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 200, _startPosY + 55);
g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55);
g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 80);
g.DrawPie(pen, _startPosX, _startPosY + 30, 20, 50, 90, 180);
g.DrawEllipse(pen, _startPosX, _startPosY + 25, 35, 10);
g.DrawEllipse(pen, _startPosX + 40, _startPosY + 50, 80, 10);
//вертикальная стабилизация
Brush mainBrush = new SolidBrush(EntityPlane.BodyColor);
Point point7 = new Point(_startPosX + 11, _startPosY + 1);
Point point8 = new Point(_startPosX + 11, _startPosY + 31);
Point point9 = new Point(_startPosX + 39, _startPosY + 31);
Point[] vert_stab = { point7, point8, point9, point7 };
g.FillPolygon(mainBrush, vert_stab);
//хвостовая часть
g.FillPie(mainBrush, _startPosX, _startPosY + 30, 20, 50, 90, 180);
//нос
Brush brGray = new SolidBrush(Color.Gray);
Point point1 = new Point(_startPosX + 150, _startPosY + 30);
Point point2 = new Point(_startPosX + 200, _startPosY + 55);
Point point3 = new Point(_startPosX + 150, _startPosY + 55);
Point[] nos1 = { point1, point2, point3, point1 };
g.FillPolygon(brGray, nos1);
Point point4 = new Point(_startPosX + 150, _startPosY + 55);
Point point5 = new Point(_startPosX + 200, _startPosY + 55);
Point point6 = new Point(_startPosX + 150, _startPosY + 80);
Point[] nos2 = { point4, point5, point6, point4 };
g.FillPolygon(brGray, nos2);
g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55);
//корпус
Point point10 = new Point(_startPosX + 10, _startPosY + 31);
Point point11 = new Point(_startPosX + 149, _startPosY + 31);
Point point12 = new Point(_startPosX + 149, _startPosY + 79);
Point point13 = new Point(_startPosX + 10, _startPosY + 79);
Point[] body = { point10, point11, point12, point13, point10 };
g.FillPolygon(mainBrush, body);
//крылья
Brush brBlack = new SolidBrush(Color.Black);
g.FillEllipse(brBlack, _startPosX + 40, _startPosY + 50, 80, 10);
g.FillEllipse(brBlack, _startPosX, _startPosY + 25, 35, 10);
}
}
}

View File

@ -3,43 +3,15 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectSeaplane.Entities;
namespace ProjectSeaplane
namespace ProjectSeaplane.DrawningObjects
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningSeaplane
public class DrawningSeaplane : DrawningPlane
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntitySeaplane? EntitySeaplane { 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 int _seaplaneWidth = 200;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private int _seaplaneHeight = 110;
/// <summary>
/// Инициализация свойств
/// </summary>
@ -52,115 +24,32 @@ namespace ProjectSeaplane
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool boat, bool floater, int width, int height)
public DrawningSeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool boat, bool floater, int width, int height) :
base(speed, weight, bodyColor, width, height, 200, 110)
{
if (width < _pictureWidth || height < _pictureHeight)
if (EntityPlane != null)
{
return false;
}
if (!floater)
{
_seaplaneHeight = 90;
}
else
{
_seaplaneHeight = 110;
}
_pictureWidth = width;
_pictureHeight = height;
EntitySeaplane = new EntitySeaplane();
EntitySeaplane.Init(speed, weight, bodyColor, additionalColor, boat, floater);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x <= _pictureWidth - _seaplaneWidth && y <= _pictureHeight - _seaplaneHeight)
{
_startPosX = x;
_startPosY = y;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (EntitySeaplane == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntitySeaplane.Step > 0)
{
_startPosX -= (int)EntitySeaplane.Step;
}
else
{
_startPosX = 0;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntitySeaplane.Step > 0)
{
_startPosY -= (int)EntitySeaplane.Step;
}
else
{
_startPosY = 0;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + _seaplaneWidth + EntitySeaplane.Step <= _pictureWidth)
{
_startPosX += (int)EntitySeaplane.Step;
}
else
{
_startPosX = _pictureWidth - _seaplaneWidth;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + _seaplaneHeight + EntitySeaplane.Step <= _pictureHeight)
{
_startPosY += (int)EntitySeaplane.Step;
}
else
{
_startPosY = _pictureHeight - _seaplaneHeight;
}
break;
EntityPlane = new EntitySeaplane(speed, weight, bodyColor, additionalColor, boat, floater);
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public override void DrawTransport(Graphics g)
{
if (EntitySeaplane == null)
if (EntityPlane is not EntitySeaplane seaPlane)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntitySeaplane.AdditionalColor);
Brush additionalBrush = new SolidBrush(seaPlane.AdditionalColor);
// лодка
if (EntitySeaplane.Boat)
if (seaPlane.Boat)
{
g.DrawEllipse(pen, _startPosX + 40, _startPosY, 90, 20);
g.DrawLine(pen, _startPosX + 60, _startPosY + 20, _startPosX + 50, _startPosY + 30);
@ -169,58 +58,8 @@ namespace ProjectSeaplane
g.DrawLine(pen, _startPosX + 110, _startPosY + 20, _startPosX + 120, _startPosY + 30);
g.FillEllipse(additionalBrush, _startPosX + 40, _startPosY, 90, 20);
}
// границы гидролета
g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 10, _startPosY + 30);
g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 40, _startPosY + 30);
g.DrawLine(pen, _startPosX + 10, _startPosY + 30, _startPosX + 150, _startPosY + 30);
g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 150, _startPosY + 80);
g.DrawLine(pen, _startPosX + 10, _startPosY + 80, _startPosX + 150, _startPosY + 80);
g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 200, _startPosY + 55);
g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55);
g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 80);
g.DrawPie(pen, _startPosX, _startPosY + 30, 20, 50, 90, 180);
g.DrawEllipse(pen, _startPosX, _startPosY + 25, 35, 10);
g.DrawEllipse(pen, _startPosX + 40, _startPosY + 50, 80, 10);
//вертикальная стабилизация
Brush mainBrush = new SolidBrush(EntitySeaplane.BodyColor);
Point point7 = new Point(_startPosX + 11, _startPosY + 1);
Point point8 = new Point(_startPosX + 11, _startPosY + 31);
Point point9 = new Point(_startPosX + 39, _startPosY + 31);
Point[] vert_stab = {point7, point8, point9, point7 };
g.FillPolygon(mainBrush, vert_stab);
//хвостовая часть
g.FillPie(mainBrush, _startPosX, _startPosY + 30, 20, 50, 90, 180);
//нос
Brush brGray = new SolidBrush(Color.Gray);
Point point1 = new Point(_startPosX + 150, _startPosY + 30);
Point point2 = new Point(_startPosX + 200, _startPosY + 55);
Point point3 = new Point(_startPosX + 150, _startPosY + 55);
Point[] nos1 = { point1, point2, point3, point1 };
g.FillPolygon(brGray, nos1);
Point point4 = new Point(_startPosX + 150, _startPosY + 55);
Point point5 = new Point(_startPosX + 200, _startPosY + 55);
Point point6 = new Point(_startPosX + 150, _startPosY + 80);
Point[] nos2 = { point4, point5, point6, point4 };
g.FillPolygon(brGray, nos2);
g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55);
//корпус
Point point10 = new Point(_startPosX + 10, _startPosY + 31);
Point point11 = new Point(_startPosX + 149, _startPosY + 31);
Point point12 = new Point(_startPosX + 149, _startPosY + 79);
Point point13 = new Point(_startPosX + 10, _startPosY + 79);
Point[] body = {point10, point11, point12, point13, point10 };
g.FillPolygon(mainBrush, body);
//крылья
Brush brBlack = new SolidBrush(Color.Black);
g.FillEllipse(brBlack, _startPosX + 40, _startPosY + 50, 80, 10);
g.FillEllipse(brBlack, _startPosX, _startPosY + 25, 35, 10);
//поплавок
if(EntitySeaplane.Floater)
if(seaPlane.Floater)
{
g.DrawLine(pen, _startPosX + 70, _startPosY + 80, _startPosX + 50, _startPosY + 100);
g.DrawLine(pen, _startPosX + 70, _startPosY + 80, _startPosX + 70, _startPosY + 100);
@ -230,6 +69,7 @@ namespace ProjectSeaplane
g.DrawEllipse(pen, _startPosX + 30, _startPosY + 100, 140, 10);
g.FillEllipse(additionalBrush, _startPosX + 30, _startPosY + 100, 140, 10);
}
base.DrawTransport(g);
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectSeaplane.MovementStrategy;
using ProjectSeaplane.DrawningObjects;
using static ProjectSeaplane.Direction;
namespace ProjectSeaplane.MovementStrategy
{
/// <summary>
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningPlane (паттерн Adapter)
/// </summary>
public class DrawningObjectPlane : IMoveableObject
{
private readonly DrawningPlane? _drawningPlane = null;
public DrawningObjectPlane(DrawningPlane drawningPlane)
{
_drawningPlane = drawningPlane;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningPlane == null || _drawningPlane.EntityPlane == null)
{
return null;
}
return new ObjectParameters(_drawningPlane.GetPosX, _drawningPlane.GetPosY, _drawningPlane.GetWidth, _drawningPlane.GetHeight);
}
}
public int GetStep => (int)(_drawningPlane?.EntityPlane?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) => _drawningPlane?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) => _drawningPlane?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.Entities
{
/// <summary>
/// Класс-сущность "Корабль"
/// </summary>
public class EntityPlane
{
/// <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 EntityPlane(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -4,22 +4,10 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane
namespace ProjectSeaplane.Entities
{
public class EntitySeaplane
public class EntitySeaplane : EntityPlane
{
/// <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>
@ -33,22 +21,16 @@ namespace ProjectSeaplane
/// </summary>
public Color AdditionalColor { 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>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool boat, bool floater)
/// /// <param name="Boat">Признак наличия лодки</param>
/// <param name="Floater">Признак наличия поплавка</param>
public EntitySeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool boat, bool floater) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Boat = boat;
Floater = floater;

View File

@ -29,11 +29,14 @@
private void InitializeComponent()
{
pictureBoxSeaplane = new PictureBox();
buttonCreate = new Button();
buttonCreatePlane = new Button();
buttonRight = new Button();
buttonDown = new Button();
buttonLeft = new Button();
buttonUp = new Button();
ComboBox = new ComboBox();
buttonCreateSeaplane = new Button();
buttonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit();
SuspendLayout();
//
@ -47,16 +50,16 @@
pictureBoxSeaplane.TabIndex = 0;
pictureBoxSeaplane.TabStop = false;
//
// buttonCreate
// buttonCreatePlane
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(0, 438);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
buttonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreatePlane.Location = new Point(174, 431);
buttonCreatePlane.Name = "buttonCreatePlane";
buttonCreatePlane.Size = new Size(147, 23);
buttonCreatePlane.TabIndex = 1;
buttonCreatePlane.Text = "Создать самолет";
buttonCreatePlane.UseVisualStyleBackColor = true;
buttonCreatePlane.Click += buttonCreatePlane_Click;
//
// buttonRight
//
@ -106,18 +109,50 @@
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click;
//
// Seaplane
// ComboBox
//
ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
ComboBox.FormattingEnabled = true;
ComboBox.Location = new Point(751, 12);
ComboBox.Name = "ComboBox";
ComboBox.Size = new Size(121, 23);
ComboBox.TabIndex = 6;
//
// buttonCreateSeaplane
//
buttonCreateSeaplane.Location = new Point(9, 431);
buttonCreateSeaplane.Name = "buttonCreateSeaplane";
buttonCreateSeaplane.Size = new Size(159, 23);
buttonCreateSeaplane.TabIndex = 7;
buttonCreateSeaplane.Text = "Создать гидросамолет";
buttonCreateSeaplane.UseVisualStyleBackColor = true;
buttonCreateSeaplane.Click += buttonCreateSeaplane_Click;
//
// buttonStep
//
buttonStep.Location = new Point(797, 41);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(75, 23);
buttonStep.TabIndex = 8;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// FormSeaplane
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(buttonStep);
Controls.Add(buttonCreateSeaplane);
Controls.Add(ComboBox);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonCreate);
Controls.Add(buttonCreatePlane);
Controls.Add(pictureBoxSeaplane);
Name = "Seaplane";
Name = "FormSeaplane";
StartPosition = FormStartPosition.CenterScreen;
Text = "Гидросамолет";
((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).EndInit();
@ -128,10 +163,13 @@
#endregion
private PictureBox pictureBoxSeaplane;
private Button buttonCreate;
private Button buttonCreatePlane;
private Button buttonRight;
private Button buttonDown;
private Button buttonLeft;
private Button buttonUp;
private ComboBox ComboBox;
private Button buttonCreateSeaplane;
private Button buttonStep;
}
}

View File

@ -1,12 +1,23 @@
using ProjectSeaplane.DrawningObjects;
using ProjectSeaplane.MovementStrategy;
using static ProjectSeaplane.Direction;
using System;
namespace ProjectSeaplane
{
public partial class FormSeaplane : Form
{
private DrawningSeaplane? _drawningSeaplane;
private DrawningPlane? _drawningSeaplane;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _abstractStrategy;
public FormSeaplane()
{
InitializeComponent();
ComboBox.Items.Add(0);
ComboBox.Items.Add(1);
}
private void Draw()
@ -21,16 +32,11 @@ namespace ProjectSeaplane
pictureBoxSeaplane.Image = bmp;
}
private void buttonCreate_Click(object sender, EventArgs e)
private void buttonCreatePlane_Click(object sender, EventArgs e)
{
Random random = new();
_drawningSeaplane = new DrawningSeaplane();
_drawningSeaplane.Init(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
_drawningSeaplane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
_drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
@ -55,10 +61,58 @@ namespace ProjectSeaplane
break;
case "buttonRight":
_drawningSeaplane.MoveTransport(DirectionType.Right);
break;
break;
}
Draw();
}
private void buttonCreateSeaplane_Click(object sender, EventArgs e)
{
Random random = new();
_drawningSeaplane = new DrawningSeaplane(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)),
pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
_drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawningSeaplane == null)
{
return;
}
if (ComboBox.Enabled)
{
_abstractStrategy = ComboBox.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawningObjectPlane(_drawningSeaplane), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
ComboBox.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
ComboBox.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -0,0 +1,37 @@
using ProjectSeaplane.MovementStrategy;
using ProjectSeaplane;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ProjectSeaplane.Direction;
namespace ProjectSeaplane.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);
}
}

View File

@ -0,0 +1,57 @@
using ProjectSeaplane.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.MovementStrategy
{
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.ObjectRightBorder <= FieldWidth &&
objParams.ObjectRightBorder + GetStep() >= FieldWidth &&
objParams.ObjectDownBorder <= FieldHeight &&
objParams.ObjectDownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectRightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectDownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,60 @@
using ProjectSeaplane.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.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();
}
}
}
}
}

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.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>
public int ObjectRightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int ObjectDownBorder => _y + _height;
/// <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;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.MovementStrategy
{
public enum Status
{
NotInit, InProgress, Finish
}
}