diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/AbstractStrategy.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/AbstractStrategy.cs new file mode 100644 index 0000000..c849b30 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/AbstractStrategy.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.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(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(DirectionType.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksParal.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksParal.cs new file mode 100644 index 0000000..1eff479 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksParal.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip +{ + public class DrawDecksParal : IDrawingDecks + { + private NumDecks numDecks; + public int SomeProperty + { + set + { + switch (value) + { + case 1: + numDecks = NumDecks.OneDeck; + break; + case 2: + numDecks = NumDecks.TwoDecks; + break; + case 3: + numDecks = NumDecks.ThreeDecks; + break; + default: + numDecks = NumDecks.OneDeck; + MessageBox.Show("Было введено неверное количество палуб, поэтому была выведена 1 палуба"); + break; + } + } + } + public void Draw(int _startPosX, int _startPosY, Color Os, Graphics g) + { + Pen pen = new Pen(Color.Black); + Brush os = new SolidBrush(Os); + if (numDecks == NumDecks.OneDeck) + { + Point point1 = new Point(_startPosX + 40, _startPosY + 12); + Point point2 = new Point(_startPosX + 50, _startPosY); + Point point3 = new Point(_startPosX + 175, _startPosY); + Point point4 = new Point(_startPosX + 165, _startPosY + 12); + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(os, curvePoints1); + } + if (numDecks == NumDecks.TwoDecks) + { + Point point1 = new Point(_startPosX + 40, _startPosY + 24); + Point point2 = new Point(_startPosX + 50, _startPosY + 12); + Point point3 = new Point(_startPosX + 175, _startPosY + 12); + Point point4 = new Point(_startPosX + 165, _startPosY + 24); + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(os, curvePoints1); + + + Point point5 = new Point(_startPosX + 62, _startPosY + 12); + Point point6 = new Point(_startPosX + 72, _startPosY); + Point point7 = new Point(_startPosX + 153, _startPosY); + Point point8 = new Point(_startPosX + 143, _startPosY + 12); + Point[] curvePoints2 = { point5, point6, point7, point8 }; + g.FillPolygon(os, curvePoints2); + } + if (numDecks == NumDecks.ThreeDecks) + { + Point point1 = new Point(_startPosX + 40, _startPosY + 36); + Point point2 = new Point(_startPosX + 50, _startPosY + 24); + Point point3 = new Point(_startPosX + 175, _startPosY + 24); + Point point4 = new Point(_startPosX + 165, _startPosY + 36); + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(os, curvePoints1); + + + Point point5 = new Point(_startPosX + 62, _startPosY + 24); + Point point6 = new Point(_startPosX + 72, _startPosY + 12); + Point point7 = new Point(_startPosX + 153, _startPosY + 12); + Point point8 = new Point(_startPosX + 143, _startPosY + 24); + Point[] curvePoints2 = { point5, point6, point7, point8 }; + g.FillPolygon(os, curvePoints2); + + Point point9 = new Point(_startPosX + 87, _startPosY + 12); + Point point10 = new Point(_startPosX + 97, _startPosY); + Point point11 = new Point(_startPosX + 131, _startPosY); + Point point12 = new Point(_startPosX + 121, _startPosY + 12); + Point[] curvePoints3 = { point9, point10, point11, point12 }; + g.FillPolygon(os, curvePoints3); + } + } + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DecksDrawing.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksSquare.cs similarity index 57% rename from ProjectWarmlyShipHard/ProjectWarmlyShipHard/DecksDrawing.cs rename to ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksSquare.cs index 62b15b4..adb393e 100644 --- a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DecksDrawing.cs +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksSquare.cs @@ -6,16 +6,12 @@ using System.Threading.Tasks; namespace ProjectWarmlyShip { - public class DecksDrawing + public class DrawDecksSquare : IDrawingDecks { private NumDecks numDecks; - public int? SomeProperty + public int SomeProperty { - get - { - return SomeProperty; - } set { switch (value) @@ -34,23 +30,33 @@ namespace ProjectWarmlyShip MessageBox.Show("Было введено неверное количество палуб, поэтому была выведена 1 палуба"); break; } - } } - public void Draw(int _startPosX, int _startPosY, Color Os, Color Ad, Graphics g) + public void Draw(int _startPosX, int _startPosY, Color Os, Graphics g) { - Brush ad = new SolidBrush(Ad); + Pen pen = new Pen(Color.Black); Brush os = new SolidBrush(Os); + if (numDecks == NumDecks.OneDeck) + { + g.DrawRectangle(pen, _startPosX + 50, _startPosY + 0, 125, 12); + g.FillRectangle(os, _startPosX + 50, _startPosY + 0, 125, 12); + } if (numDecks == NumDecks.TwoDecks) { - g.FillRectangle(ad, _startPosX + 75, _startPosY, 75, 12); + g.DrawRectangle(pen, _startPosX + 50, _startPosY + 12, 125, 12); + g.DrawRectangle(pen, _startPosX + 75, _startPosY, 75, 12); + g.FillRectangle(os, _startPosX + 50, _startPosY + 12, 125, 12); + g.FillRectangle(os, _startPosX + 75, _startPosY, 75, 12); } if (numDecks == NumDecks.ThreeDecks) { - g.FillRectangle(ad, _startPosX + 75, _startPosY + 12, 75, 12); + g.DrawRectangle(pen, _startPosX + 50, _startPosY + 24, 125, 12); + g.DrawRectangle(pen, _startPosX + 75, _startPosY + 12, 75, 12); + g.DrawRectangle(pen, _startPosX + 100, _startPosY, 25, 12); + g.FillRectangle(os, _startPosX + 50, _startPosY + 24, 125, 12); + g.FillRectangle(os, _startPosX + 75, _startPosY + 12, 75, 12); g.FillRectangle(os, _startPosX + 100, _startPosY, 25, 12); } - } } } diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksTrapeze.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksTrapeze.cs new file mode 100644 index 0000000..fdf0c2b --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawDecksTrapeze.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip +{ + public class DrawDecksTrapeze : IDrawingDecks + { + private NumDecks numDecks; + public int SomeProperty + { + set + { + switch (value) + { + case 1: + numDecks = NumDecks.OneDeck; + break; + case 2: + numDecks = NumDecks.TwoDecks; + break; + case 3: + numDecks = NumDecks.ThreeDecks; + break; + default: + numDecks = NumDecks.OneDeck; + MessageBox.Show("Было введено неверное количество палуб, поэтому была выведена 1 палуба"); + break; + } + } + } + public void Draw(int _startPosX, int _startPosY, Color Os, Graphics g) + { + Pen pen = new Pen(Color.Black); + Brush os = new SolidBrush(Os); + if (numDecks == NumDecks.OneDeck) + { + Point point1 = new Point(_startPosX + 40, _startPosY + 12); + Point point2 = new Point(_startPosX + 50, _startPosY); + Point point3 = new Point(_startPosX + 165, _startPosY); + Point point4 = new Point(_startPosX + 175, _startPosY + 12); + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(os, curvePoints1); + + } + if (numDecks == NumDecks.TwoDecks) + { + Point point1 = new Point(_startPosX + 40, _startPosY + 24); + Point point2 = new Point(_startPosX + 50, _startPosY + 12); + Point point3 = new Point(_startPosX + 165, _startPosY + 12); + Point point4 = new Point(_startPosX + 175, _startPosY + 24); + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(os, curvePoints1); + + Point point5 = new Point(_startPosX + 62, _startPosY + 12); + Point point6 = new Point(_startPosX + 72, _startPosY); + Point point7 = new Point(_startPosX + 143, _startPosY); + Point point8 = new Point(_startPosX + 153, _startPosY + 12); + Point[] curvePoints2 = { point5, point6, point7, point8 }; + g.FillPolygon(os, curvePoints2); + } + if (numDecks == NumDecks.ThreeDecks) + { + Point point1 = new Point(_startPosX + 40, _startPosY + 36); + Point point2 = new Point(_startPosX + 50, _startPosY + 24); + Point point3 = new Point(_startPosX + 165, _startPosY + 24); + Point point4 = new Point(_startPosX + 175, _startPosY + 36); + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(os, curvePoints1); + + Point point5 = new Point(_startPosX + 62, _startPosY + 24); + Point point6 = new Point(_startPosX + 72, _startPosY + 12); + Point point7 = new Point(_startPosX + 143, _startPosY + 12); + Point point8 = new Point(_startPosX + 153, _startPosY + 24); + Point[] curvePoints2 = { point5, point6, point7, point8 }; + g.FillPolygon(os, curvePoints2); + + Point point9 = new Point(_startPosX + 87, _startPosY + 12); + Point point10 = new Point(_startPosX + 97, _startPosY); + Point point11 = new Point(_startPosX + 121, _startPosY); + Point point12 = new Point(_startPosX + 131, _startPosY + 12); + Point[] curvePoints3 = { point9, point10, point11, point12 }; + g.FillPolygon(os, curvePoints3); + } + } + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawingWarmlyShip.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawingWarmlyShip.cs index 6f55310..4b28011 100644 --- a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawingWarmlyShip.cs +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawingWarmlyShip.cs @@ -3,50 +3,13 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectWarmlyShip.Entities; -namespace ProjectWarmlyShip +namespace ProjectWarmlyShip.DrawningObjects { // Класс, отвечающий за прорисовку и перемещение объекта-сущности - internal class DrawingWarmlyShip + public class DrawingWarmlyShip : DrawningShip { - /// - /// Класс-сущность - /// - public EntityWarmlyShip? EntityWarmlyShip { get; private set; } - - public DecksDrawing? DrawingDecks; - - /// - ///Ширина окна - /// - private int _pictureWidth; - - /// - ///Высота окна - /// - private int _pictureHeight; - - /// - /// Левая координата прорисовки теплохода - /// - private int _startPosX; - - /// - /// Верхняя кооридната прорисовки теплохода - /// - /// - private int _startPosY; - - /// - /// Ширина прорисовки теплохода - /// - private int _shipWidth = 200; - - /// - /// Высота прорисовки теплохода - /// - private int _shipHeight = 30; - /// /// Инициализация свойств /// @@ -58,191 +21,67 @@ namespace ProjectWarmlyShip /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool shipPipes, bool shipFuel, int width, int height, int _numDecks) + public DrawingWarmlyShip(int speed, double weight, Color bodyColor, Color additionalColor, bool shipPipes, bool shipFuel, int width, int height, int numdecks, int nummode) : + base(speed, weight, bodyColor, width, height, numdecks, nummode) { - if (width < _shipWidth || height < _shipHeight) + if (EntityShip != null) { - return false; + EntityShip = new EntityWarmlyShip(speed, weight, bodyColor, additionalColor, shipPipes, shipFuel); } - _pictureWidth = width; - _pictureHeight = height; - EntityWarmlyShip = new EntityWarmlyShip(); - EntityWarmlyShip.Init(speed, weight, bodyColor, additionalColor, shipPipes, shipFuel, _numDecks); - DrawingDecks = new DecksDrawing(); - DrawingDecks.SomeProperty = _numDecks; - return true; } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) + public override void DrawTransport(Graphics g) { - if ((x > 0) && (x < _pictureWidth)) _startPosX = x; - else _startPosX = 0; - if ((y > 0) && (y < _pictureHeight)) _startPosY = y; - else _startPosY = 0; - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) - { - if (EntityWarmlyShip == null) + if (EntityShip is not EntityWarmlyShip warmlyShip) { return; } - switch (direction) + Brush additionalBrush = new SolidBrush(warmlyShip.AdditionalColor); + if (_shipHeight == 30) { - //влево - case DirectionType.Left: - if (_startPosX - EntityWarmlyShip.Step > 0) - { - _startPosX -= (int)EntityWarmlyShip.Step; - } - else - { - _startPosX = 0; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - EntityWarmlyShip.Step > 0) - { - _startPosY -= (int)EntityWarmlyShip.Step; - } - else - { - _startPosY = 0; - } - break; - // вправо - case DirectionType.Right: - if (_startPosX + _shipWidth + EntityWarmlyShip.Step < _pictureWidth) - { - _startPosX += (int)EntityWarmlyShip.Step; - } - else - { - _startPosX = _pictureWidth - _shipWidth; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + _shipHeight + EntityWarmlyShip.Step < _pictureHeight) - { - _startPosY += (int)EntityWarmlyShip.Step; - } - else - { - _startPosY = _pictureHeight - _shipHeight; - } - break; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityWarmlyShip == null) - { - return; - } - - Brush additionalBrush = new SolidBrush(EntityWarmlyShip.AdditionalColor); - Brush bodyBrush = new SolidBrush(EntityWarmlyShip.BodyColor); - Brush brAqua = new SolidBrush(Color.Aquamarine); - //границы корабля - if (EntityWarmlyShip.numDecks == 1) - { - Point point1 = new Point(_startPosX, _startPosY + 12); - Point point2 = new Point(_startPosX + 25, _startPosY + 35); - Point point3 = new Point(_startPosX + 175, _startPosY + 35); - Point point4 = new Point(_startPosX + 200, _startPosY + 12); - - Point[] curvePoints1 = { point1, point2, point3, point4 }; - g.FillPolygon(bodyBrush, curvePoints1); - - //граница палубы - g.FillRectangle(brAqua, _startPosX + 50, _startPosY + 0, 125, 12); + //трубы + if (warmlyShip.ShipPipes) + { + g.FillRectangle(additionalBrush, _startPosX + 180, _startPosY + 2, 3, 12); + g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 4, 5, 10); + } //топливо - if (EntityWarmlyShip.ShipFuel) + if (warmlyShip.ShipFuel) { g.FillRectangle(additionalBrush, _startPosX + 12, _startPosY + 5, 27, 7); } - //трубы - if (EntityWarmlyShip.ShipPipes) - { - g.FillRectangle(additionalBrush, _startPosX + 180, _startPosY + 2, 3, 10); - g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 4, 5, 8); - } - //палубы - DrawingDecks.Draw(_startPosX, _startPosY, EntityWarmlyShip.BodyColor, EntityWarmlyShip.AdditionalColor, g); - } - if (EntityWarmlyShip.numDecks == 2) + if (_shipHeight == 42) { - _shipHeight = 42; - Point point1 = new Point(_startPosX, _startPosY + 24); - Point point2 = new Point(_startPosX + 25, _startPosY + 47); - Point point3 = new Point(_startPosX + 175, _startPosY + 47); - Point point4 = new Point(_startPosX + 200, _startPosY + 22); - - Point[] curvePoints1 = { point1, point2, point3, point4 }; - g.FillPolygon(bodyBrush, curvePoints1); - - //граница палубы - - g.FillRectangle(brAqua, _startPosX + 50, _startPosY + 12, 125, 12); + //трубы + if (warmlyShip.ShipPipes) + { + g.FillRectangle(additionalBrush, _startPosX + 180, _startPosY + 14, 3, 12); + g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 16, 5, 10); + } //топливо - if (EntityWarmlyShip.ShipFuel) + if (warmlyShip.ShipFuel) { g.FillRectangle(additionalBrush, _startPosX + 12, _startPosY + 17, 27, 7); } - //трубы - if (EntityWarmlyShip.ShipPipes) - { - g.FillRectangle(additionalBrush, _startPosX + 180, _startPosY + 14, 3, 10); - g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 16, 5, 8); - } - //палубы - DrawingDecks.Draw(_startPosX, _startPosY, EntityWarmlyShip.BodyColor, EntityWarmlyShip.AdditionalColor, g); } - if (EntityWarmlyShip.numDecks == 3) + if (_shipHeight == 54) { - _shipHeight = 54; - Point point1 = new Point(_startPosX, _startPosY + 36); - Point point2 = new Point(_startPosX + 25, _startPosY + 59); - Point point3 = new Point(_startPosX + 175, _startPosY + 59); - Point point4 = new Point(_startPosX + 200, _startPosY + 34); - - Point[] curvePoints1 = { point1, point2, point3, point4 }; - g.FillPolygon(bodyBrush, curvePoints1); - - //граница палубы - - g.FillRectangle(brAqua, _startPosX + 50, _startPosY + 24, 125, 12); + //трубы + if (warmlyShip.ShipPipes) + { + g.FillRectangle(additionalBrush, _startPosX + 180, _startPosY + 26, 3, 12); + g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 28, 5, 10); + } //топливо - if (EntityWarmlyShip.ShipFuel) + if (warmlyShip.ShipFuel) { g.FillRectangle(additionalBrush, _startPosX + 12, _startPosY + 29, 27, 7); } - //трубы - if (EntityWarmlyShip.ShipPipes) - { - g.FillRectangle(additionalBrush, _startPosX + 180, _startPosY + 26, 3, 10); - g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 28, 5, 8); - } - //палубы - DrawingDecks.Draw(_startPosX, _startPosY, EntityWarmlyShip.BodyColor, EntityWarmlyShip.AdditionalColor, g); } + base.DrawTransport(g); } } } diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawningObjectShip.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawningObjectShip.cs new file mode 100644 index 0000000..fa1b1ac --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawningObjectShip.cs @@ -0,0 +1,35 @@ +using ProjectWarmlyShip.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningShip (паттерн Adapter) + /// + public class DrawningObjectShip : IMoveableObject + { + private readonly DrawningShip? _drawningShip = null; + public DrawningObjectShip(DrawningShip drawningShip) + { + _drawningShip = drawningShip; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningShip == null || _drawningShip.EntityShip == null) + { + return null; + } + return new ObjectParameters(_drawningShip.GetPosX, _drawningShip.GetPosY, _drawningShip.GetWidth, _drawningShip.GetHeight); + } + } + public int GetStep => (int)(_drawningShip?.EntityShip?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => _drawningShip?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawningShip?.MoveTransport(direction); + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawningShip.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawningShip.cs new file mode 100644 index 0000000..ee4f848 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/DrawningShip.cs @@ -0,0 +1,236 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectWarmlyShip.Entities; + +namespace ProjectWarmlyShip.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningShip + { + /// + /// Класс-сущность + /// + public EntityShip? EntityShip { get; protected set; } + private IDrawingDecks? DrawingDecks; + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + /// + /// Левая координата прорисовки автомобиля + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки автомобиля + /// + protected int _startPosY; + /// + /// Ширина прорисовки автомобиля + /// + protected readonly int _shipWidth = 200; + /// + /// Высота прорисовки автомобиля + /// + public int _shipHeight = 30; + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int numDecks, int nummode) + { + // TODO: Продумать проверки + if (width > _shipWidth && height > _shipHeight) + { + _pictureWidth = width; + _pictureHeight = height; + EntityShip = new EntityShip(speed, weight, bodyColor); + int mode = nummode % 3; + switch (mode) + { + case 0: + DrawingDecks = new DrawDecksSquare(); + break; + case 1: + DrawingDecks = new DrawDecksTrapeze(); + break; + case 2: + DrawingDecks = new DrawDecksParal(); + break; + } + DrawingDecks.SomeProperty = numDecks; + if (numDecks == 2) + _shipHeight = 42; + if (numDecks == 3) + _shipHeight = 54; + } + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight, int numDecks) + { + // TODO: Продумать проверки + //if (width > _shipWidth && height > _shipHeight) + + _pictureWidth = width; + _pictureHeight = height; + _shipWidth = shipWidth; + _shipHeight = shipHeight; + EntityShip = new EntityShip(speed, weight, bodyColor); + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if ((x > 0) && (x < _pictureWidth)) + _startPosX = x; + else _startPosX = 0; + if ((y > 0) && (y < _pictureHeight)) + _startPosY = y; + else _startPosY = 0; + _startPosX = x; + _startPosY = y; + } + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _shipWidth; + /// + /// Высота объекта + /// + public int GetHeight => _shipHeight; + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityShip == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityShip.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityShip.Step > 0, + // вправо + DirectionType.Right => _startPosX + _shipWidth + EntityShip.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + _shipHeight + EntityShip.Step < _pictureHeight, + }; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityShip == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + _startPosX -= (int)EntityShip.Step; + break; + //вверх + case DirectionType.Up: + _startPosY -= (int)EntityShip.Step; + break; + // вправо + case DirectionType.Right: + _startPosX += (int)EntityShip.Step; + break; + //вниз + case DirectionType.Down: + _startPosY += (int)EntityShip.Step; + break; + } + } + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityShip == null) + { + return; + } + Brush BodyBrush = new SolidBrush(EntityShip.BodyColor); + + //границы корабля + + if (_shipHeight == 30) + { + Point point1 = new Point(_startPosX, _startPosY + 12); + Point point2 = new Point(_startPosX + 25, _startPosY + 35); + Point point3 = new Point(_startPosX + 175, _startPosY + 35); + Point point4 = new Point(_startPosX + 200, _startPosY + 12); + + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(BodyBrush, curvePoints1); + + } + if (_shipHeight == 42) + { + Point point1 = new Point(_startPosX, _startPosY + 24); + Point point2 = new Point(_startPosX + 25, _startPosY + 47); + Point point3 = new Point(_startPosX + 175, _startPosY + 47); + Point point4 = new Point(_startPosX + 200, _startPosY + 22); + + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(BodyBrush, curvePoints1); + + } + if (_shipHeight == 54) + { + Point point1 = new Point(_startPosX, _startPosY + 36); + Point point2 = new Point(_startPosX + 25, _startPosY + 59); + Point point3 = new Point(_startPosX + 175, _startPosY + 59); + Point point4 = new Point(_startPosX + 200, _startPosY + 34); + + Point[] curvePoints1 = { point1, point2, point3, point4 }; + g.FillPolygon(BodyBrush, curvePoints1); + + } + DrawingDecks.Draw(_startPosX, _startPosY, EntityShip.BodyColor, g); + } + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/EntityShip.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/EntityShip.cs new file mode 100644 index 0000000..dc1f15e --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/EntityShip.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Entities +{ + /// + /// Класс-сущность "Корабль" + /// + public class EntityShip + { + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public double Weight { get; private set; } + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + /// + /// Шаг перемещения автомобиля + /// + public double Step => (double)Speed * 100 / Weight; + + public int numDecks; + /// + /// Конструктор с параметрами + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + public EntityShip(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} \ No newline at end of file diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/EntityWarmlyShip.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/EntityWarmlyShip.cs index 1badd7a..f2a814d 100644 --- a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/EntityWarmlyShip.cs +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/EntityWarmlyShip.cs @@ -3,23 +3,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectWarmlyShip.DrawningObjects; -namespace ProjectWarmlyShip +namespace ProjectWarmlyShip.Entities { - public class EntityWarmlyShip + internal class EntityWarmlyShip : EntityShip { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } /// /// Дополнительный цвет (для опциональных элементов) /// @@ -31,15 +20,10 @@ namespace ProjectWarmlyShip /// /// Признак (опция) наличия дополнительного топлива /// - public bool ShipFuel { get; private set; } - /// - /// Шаг перемещения теплохода - /// - public double Step => (double)Speed * 100 / Weight; - /// + /// /// /// количество палуб [2;3] /// - public int numDecks; + public bool ShipFuel { get; private set; } /// /// Инициализация полей объекта-класса теплохода /// @@ -49,17 +33,11 @@ namespace ProjectWarmlyShip /// Дополнительный цвет /// Признак наличия труб /// Признак наличия топлива - /// /// Признак наличия топлива - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool shipPipes, bool shipFuel, int _numDecks) + public EntityWarmlyShip(int speed, double weight, Color bodyColor, Color additionalColor, bool shipPipes, bool shipFuel) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; ShipPipes = shipPipes; ShipFuel = shipFuel; - numDecks = _numDecks; } } } diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.Designer.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.Designer.cs index 7a22a1a..7951362 100644 --- a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.Designer.cs +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.Designer.cs @@ -31,6 +31,9 @@ buttonDown = new Button(); buttonUp = new Button(); buttonRight = new Button(); + buttonCreateDeckShip = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); SuspendLayout(); // @@ -45,7 +48,7 @@ // // buttonCreate // - buttonCreate.Location = new Point(12, 426); + buttonCreate.Location = new Point(195, 426); buttonCreate.Name = "buttonCreate"; buttonCreate.Size = new Size(75, 23); buttonCreate.TabIndex = 1; @@ -97,11 +100,42 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // buttonCreateDeckShip + // + buttonCreateDeckShip.Location = new Point(12, 426); + buttonCreateDeckShip.Name = "buttonCreateDeckShip"; + buttonCreateDeckShip.Size = new Size(177, 23); + buttonCreateDeckShip.TabIndex = 6; + buttonCreateDeckShip.Text = "Создать корабль с обвесами"; + buttonCreateDeckShip.UseVisualStyleBackColor = true; + buttonCreateDeckShip.Click += buttonCreateDeckShip_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Location = new Point(706, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(166, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Location = new Point(761, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += ButtonStep_Click; + // // FormWarmlyShip // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateDeckShip); Controls.Add(buttonRight); Controls.Add(buttonUp); Controls.Add(buttonDown); @@ -123,5 +157,8 @@ private Button buttonDown; private Button buttonUp; private Button buttonRight; + private Button buttonCreateDeckShip; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.cs index 17e59c0..cd59e0a 100644 --- a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.cs +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.cs @@ -1,4 +1,6 @@ using ProjectWarmlyShip; +using ProjectWarmlyShip.DrawningObjects; +using ProjectWarmlyShip.MovementStrategy; using static ProjectWarmlyShip.DirectionType; namespace ProjectWarmlyShipHard { @@ -7,13 +9,19 @@ namespace ProjectWarmlyShipHard /// /// - /// - private DrawingWarmlyShip? _drawingWarmlyShip; + private DrawningShip? _drawingWarmlyShip; + /// + /// + /// + private AbstractStrategy? _abstractStrategy; /// /// /// public FormWarmlyShip() { InitializeComponent(); + comboBoxStrategy.Items.Add(" "); + comboBoxStrategy.Items.Add(" "); } /// /// @@ -37,12 +45,25 @@ namespace ProjectWarmlyShipHard private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); - _drawingWarmlyShip = new DrawingWarmlyShip(); - _drawingWarmlyShip.Init(random.Next(100, 300), random.Next(1000, 3000), + _drawingWarmlyShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height, random.Next(1, 4), random.Next(1, 4)); + + _drawingWarmlyShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + /// + /// " " + /// + /// + /// + private void buttonCreateDeckShip_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingWarmlyShip = new DrawingWarmlyShip(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)), - pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height, random.Next(1, 4)); + pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height, random.Next(1, 4), random.Next(1, 4)); _drawingWarmlyShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -75,5 +96,44 @@ namespace ProjectWarmlyShipHard } Draw(); } + /// + /// "" + /// + /// + /// + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawingWarmlyShip == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawningObjectShip(_drawingWarmlyShip), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.resx b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.resx index 2fe6a2a..8c18930 100644 --- a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.resx +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/FormWarmlyShip.resx @@ -123,7 +123,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOwQAADsEBuJFr7QAAAkFJREFUeF7t3GtT2zAQhWErF5JAnIQQSrmFW///b+wCR6LY/Vg3nqP3+cJE + AAAOvwAADr8BOAVTJAAAAkFJREFUeF7t3GtT2zAQhWErF5JAnIQQSrmFW///b+wCR6LY/Vg3nqP3+cJE O5Nhh1kjrSQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAACMyVQ/Tc02P1J6WuuToUP6dKfPds6VYErXGjFz pfTCRkNWpgtl926uQSeHeMR8WWjUyFcJfpho2MdGmWVuD9PpTyWWPSngYvuqxLKVAi7OlFfxqICLnfIq bhQwseyW4MJsOrP+pcSy1VIRE3fKq7hVwMW98iouFTCxbJVX9mC2Llw/KLFsYlaCl8qruFfAxa3yKsxm @@ -141,7 +141,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOwQAADsEBuJFr7QAAA2JJREFUeF7t3dtyolAQheHgIZqTicacMznO+z/jBFgoLRtwLqame9f/XUnj + AAAOvwAADr8BOAVTJAAAA2JJREFUeF7t3dtyolAQheHgIZqTicacMznO+z/jBFgoLRtwLqame9f/XUnj xV6W1UBTBScAAAAAAAAAAAAAAAAA8K/MtqfbqT5n6fytKIq3c21l6OInX+lC29m5VMCiuFYlNzfKVxQr VXIzUb6imKiSGxLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgf CeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhK6d7ea @@ -164,7 +164,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOwQAADsEBuJFr7QAAA01JREFUeF7t3dluo0AURVHwEGdO7DjznPT/f2PHcAgULsD90FKd0l5P7jKR + AAAOvwAADr8BOAVTJAAAA01JREFUeF7t3dluo0AURVHwEGdO7DjznPT/f2PHcAgULsD90FKd0l5P7jKR 2FF0jYuWXQAAAAAAAAAAAAAAAADA/7JZP643epyls3LnTP/K0GUVWJaX+nd2vhVYlt9aycyV8nautJaV xZvqdt4WWs3JUnG1pVYz0kyZRnbTpp0yjcymzVZZXVs9l4W5okJzPZuDcMo0Mpo2/SnTyGbanCpo36mO MHevnJh7HWMtPmUaOUyblVriVjrK2I1ShtzoOFuvChn2qiNNjU2ZhvW02ShinPHGzfGLGirXF3pQlhfX @@ -186,7 +186,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOwQAADsEBuJFr7QAAAipJREFUeF7t3NlS20AQRmEJGzAgL+ybIQTy/s8YOfyawtJtZIWT891QVleB + AAAOvwAADr8BOAVTJAAAAipJREFUeF7t3NlS20AQRmEJGzAgL+ybIQTy/s8YOfyawtJtZIWT891QVleB u6iWZnpmVEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmS9F3N8hOqWdb13WqeT0BP9aebfMa5TYJ1fZ4rNKvk 17rOJZjjpLdzirzjnCa7P+6IxXiU5AJYjN2ttLPKdZAfSa3zwCvGs6TWeV8nwPGS1IqTBDjuk1mxSYDj du+Z0XrADVMX/WJ8axLheExqxVMCHJfJrHhOgKPZJrXOcpEIxqKdCu/Z8orxOakVlwlw9Eep9WMCHM1b diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/IDrawingDecks.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/IDrawingDecks.cs new file mode 100644 index 0000000..261e4fb --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/IDrawingDecks.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip +{ + public interface IDrawingDecks + { + int SomeProperty { set;} + void Draw(int _startPosX, int _startPosY, Color Os, Graphics g); + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/IMoveableObject.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/IMoveableObject.cs new file mode 100644 index 0000000..6b87d88 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/IMoveableObject.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/MoveToBorder.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/MoveToBorder.cs new file mode 100644 index 0000000..cf1afe9 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/MoveToBorder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.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(); + } + } + } + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/MoveToBroder.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/MoveToBroder.cs new file mode 100644 index 0000000..ebbb669 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/MoveToBroder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.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(); + } + } + } + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/ObjectParametrs.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/ObjectParametrs.cs new file mode 100644 index 0000000..c76f691 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/ObjectParametrs.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ProjectWarmlyShip.MovementStrategy +{ + /// + /// Параметры-координаты объекта + /// + public class ObjectParameters + { + private readonly int _x; + private readonly int _y; + private readonly int _width; + private readonly int _height; + /// + /// Левая граница + /// + public int LeftBorder => _x; + /// + /// Верхняя граница + /// + public int TopBorder => _y; + /// + /// Правая граница + /// + public int RightBorder => _x + _width; + /// + /// Нижняя граница + /// + public int DownBorder => _y + _height; + /// + /// Середина объекта + /// + public int ObjectMiddleHorizontal => _x + _width / 2; + /// + /// Середина объекта + /// + public int ObjectMiddleVertical => _y + _height / 2; + /// + /// Правая граница + /// + public int ObjectRightBorder => _x + _width; + /// + /// Нижняя граница + /// + public int ObjectDownBorder => _y + _height; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/ProjectWarmlyShipHard/ProjectWarmlyShipHard/Status.cs b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/Status.cs new file mode 100644 index 0000000..4f50869 --- /dev/null +++ b/ProjectWarmlyShipHard/ProjectWarmlyShipHard/Status.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.MovementStrategy +{ + public enum Status + { + NotInit, InProgress, Finish + } +}