From 603cf3a903fcee722ad2186aa5487f3cd2758e31 Mon Sep 17 00:00:00 2001 From: DelphyAlmond Date: Wed, 12 Jun 2024 16:25:55 +0400 Subject: [PATCH 1/2] In process : Add Border & Center moving strategy --- ProjectCruiser/DirectionType.cs | 12 +--- ProjectCruiser/DrawningBase.cs | 100 ++++++++++++---------------- ProjectCruiser/DrawningCruiser.cs | 81 ++++++++++++++++++++++ ProjectCruiser/EntityBase.cs | 18 ++--- ProjectCruiser/EntityCruiser.cs | 19 ++++++ ProjectCruiser/IMoveableObj.cs | 16 +++++ ProjectCruiser/MoveableTransport.cs | 54 +++++++++++++++ ProjectCruiser/MovementDirection.cs | 16 +++++ ProjectCruiser/ObjParameters.cs | 29 ++++++++ ProjectCruiser/StrategyStatus.cs | 19 ++++++ 10 files changed, 283 insertions(+), 81 deletions(-) create mode 100644 ProjectCruiser/DrawningCruiser.cs create mode 100644 ProjectCruiser/EntityCruiser.cs create mode 100644 ProjectCruiser/IMoveableObj.cs create mode 100644 ProjectCruiser/MoveableTransport.cs create mode 100644 ProjectCruiser/MovementDirection.cs create mode 100644 ProjectCruiser/ObjParameters.cs create mode 100644 ProjectCruiser/StrategyStatus.cs diff --git a/ProjectCruiser/DirectionType.cs b/ProjectCruiser/DirectionType.cs index d8cd3ba..04733e1 100644 --- a/ProjectCruiser/DirectionType.cs +++ b/ProjectCruiser/DirectionType.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectCruiser; - +namespace ProjectCruiser; public enum DirectionType { + Unknown = -1, Up = 1, Down = 2, Left = 3, Right = 4 -} +} \ No newline at end of file diff --git a/ProjectCruiser/DrawningBase.cs b/ProjectCruiser/DrawningBase.cs index 169cdc2..34e98c0 100644 --- a/ProjectCruiser/DrawningBase.cs +++ b/ProjectCruiser/DrawningBase.cs @@ -1,33 +1,39 @@ -using System.Drawing.Drawing2D; - -namespace ProjectCruiser; +namespace ProjectCruiser; public class DrawningBase { // Класс-сущность - public EntityBase? EntityB { get; private set; } + public EntityBase? EntityTransport { get; protected set; } private int? _pictureWidth; // Ширина окна private int? _pictureHeight; // Высота окна - private int? _startPosX; // Левая координата прорисовки автомобиля - private int? _startPosY; // Верхняя кооридната прорисовки автомобиля + + protected int? _startPosX; // < protected + protected int? _startPosY; // < protected private readonly int _drawningWidth = 300; // Ширина прорисовки автомобиля private readonly int _drawningHeight = 42; // Высота прорисовки автомобиля - // Инициализация свойств - public void Init(int speed, double weight, - Color bodyColor, Color additionalColor, bool pad, - bool hangar, bool deckhouse) + // Инициализация свойств (теперь через конструктор) + public DrawningBase(int speed, double weight, + Color bodyColor) { - EntityB = new EntityBase(); - EntityB.Init(speed, weight, bodyColor, - additionalColor, pad, hangar, deckhouse); + EntityTransport = new EntityBase(speed, weight, bodyColor); + } + private DrawningBase() + { _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + // protected > + protected DrawningBase(int drawningCarWidth, int drawningCarHeight) : this() + { + _drawningWidth = drawningCarWidth; + _pictureHeight = drawningCarHeight; + } + public int getHeight() // для создания объекта в нижнем левом углу (*) { return _drawningHeight; @@ -72,7 +78,7 @@ public class DrawningBase public bool MoveTransport(DirectionType direction) { - if (EntityB == null || !_startPosX.HasValue || + if (EntityTransport == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; @@ -82,32 +88,32 @@ public class DrawningBase { //влево case DirectionType.Left: - if (_startPosX.Value - EntityB.Step > 0) + if (_startPosX.Value - EntityTransport.Step > 0) { - _startPosX -= (int)EntityB.Step; + _startPosX -= (int)EntityTransport.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntityB.Step > 0) + if (_startPosY.Value - EntityTransport.Step > 0) { - _startPosY -= (int)EntityB.Step; + _startPosY -= (int)EntityTransport.Step; } return true; // вправо case DirectionType.Right: - if (_startPosX.Value + _drawningWidth + EntityB.Step + if (_startPosX.Value + _drawningWidth + EntityTransport.Step < _pictureWidth.Value) { - _startPosX += (int)EntityB.Step; + _startPosX += (int)EntityTransport.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + _drawningHeight + EntityB.Step + if (_startPosY.Value + _drawningHeight + EntityTransport.Step < _pictureHeight.Value) { - _startPosY += (int)EntityB.Step; + _startPosY += (int)EntityTransport.Step; } return true; @@ -117,18 +123,16 @@ public class DrawningBase } } - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) // < virtual { - if (EntityB == null || !_startPosX.HasValue || + if (EntityTransport == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black, 2); - Brush PadBrush = new HatchBrush(HatchStyle.DottedDiamond, Color.LightGray, Color.Black); - Brush additionalBrush = new SolidBrush(EntityB.AdditionalColor); - Brush mainBrush = new SolidBrush(EntityB.MainColor); + Brush mainBrush = new SolidBrush(EntityTransport.MainColor); //границы cruiser Point point0 = new Point(_startPosX.Value + 2, _startPosY.Value + 7); @@ -152,38 +156,18 @@ public class DrawningBase g.DrawPolygon(pen, boarders); g.FillPolygon(mainBrush, boarders); - // вертолетная площадка - if (EntityB.HelicopterPads) - { - g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); - g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); - } - // салон на верхней палубе - if (EntityB.Deckhouse) - { - // random location - int y_h = EntityB.values[1]; - g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24); // 40, 26 - - g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); - g.FillRectangle(additionalBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); - g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); - g.FillRectangle(additionalBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); - } - - // ангар - if (EntityB.Hangar) - { - int n = EntityB.values[2]; - if (n == 1) g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7); - - else - { - g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20); - g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12); - } - } + int y_h = EntityTransport.values[1]; + g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24); + g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); + g.FillRectangle(mainBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); + g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); + g.FillRectangle(mainBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); } + + public int? GetPosX => _startPosX; + public int? GetPosY => _startPosY; + public int GetWidth => _drawningWidth; + public int GetHeight => _drawningHeight; } diff --git a/ProjectCruiser/DrawningCruiser.cs b/ProjectCruiser/DrawningCruiser.cs new file mode 100644 index 0000000..3225a76 --- /dev/null +++ b/ProjectCruiser/DrawningCruiser.cs @@ -0,0 +1,81 @@ +using System.Drawing.Drawing2D; + +namespace ProjectCruiser; +internal class DrawningCruiser : DrawningBase +{ + + private EntityCruiser EntityCruiser; + + public DrawningCruiser(int speed, double weight, Color bodyColor, + Color additionalColor, bool pads, bool hangar) : base(300, 42) + // all additional featchures 'inside' object, so size remains + { + EntityCruiser = new EntityCruiser(speed, weight, + bodyColor, additionalColor, pads, hangar); + } + + public override void DrawTransport(Graphics g) + { + if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black, 2); + Brush PadBrush = new HatchBrush(HatchStyle.DottedDiamond, Color.LightGray, Color.Black); + Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor); + Brush mainBrush = new SolidBrush(EntityTransport.MainColor); + + //границы cruiser + Point point0 = new Point(_startPosX.Value + 2, _startPosY.Value + 7); + Point point1 = new Point(_startPosX.Value + 2, _startPosY.Value + 30); + Point point2 = new Point(_startPosX.Value + 184, _startPosY.Value + 42); + Point point3 = new Point(_startPosX.Value + 260, _startPosY.Value + 34); + Point point4 = new Point(_startPosX.Value + 300, _startPosY.Value + 22); + Point point5 = new Point(_startPosX.Value + 260, _startPosY.Value + 10); + Point point6 = new Point(_startPosX.Value + 184, _startPosY.Value + 2); + + Point[] boarders = { + point0, + point1, + point2, + point3, + point4, + point5, + point6 + }; + + g.DrawPolygon(pen, boarders); + g.FillPolygon(mainBrush, boarders); + + // салон на верхней палубе + // random location + int y_h = EntityCruiser.values[1]; + g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24); + + g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); + g.FillRectangle(additionalBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); + g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); + g.FillRectangle(additionalBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); + + // вертолетная площадка + if (EntityCruiser.HelicopterPads) + { + g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); + g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); + } + + // ангар + if (EntityCruiser.Hangar) + { + int n = EntityTransport.values[2]; + if (n == 1) g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7); + + else + { + g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20); + g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12); + } + } + } +} diff --git a/ProjectCruiser/EntityBase.cs b/ProjectCruiser/EntityBase.cs index 8d454ef..e2d865b 100644 --- a/ProjectCruiser/EntityBase.cs +++ b/ProjectCruiser/EntityBase.cs @@ -6,29 +6,19 @@ public class EntityBase public int Speed { get; private set; } // скорость public double Weight { get; private set; } // вес public Color MainColor { get; private set; } // основной цвет - public Color AdditionalColor { get; private set; } // доп. цвет - - // признаки (наличия) - public bool HelicopterPads { get; private set; } // вертолетная площадка - public bool Hangar { get; private set; } // ангар - public bool Deckhouse { get; private set; } // салон на верхней палубе + // public bool Deckhouse { get; private set; } // салон на верхней палубе public double Step => Speed * 100 / Weight; public int[] values = { 0, 0, 0 }; - public void Init(int speed, double weight, - Color mainc, Color addtc, bool pad, - bool hangar, bool deckhouse) + public EntityBase(int speed, double weight, + Color mainc) // (bool) deckhouse -> default TRUE now { Random rn = new(); - Speed = speed; Weight = weight; MainColor = mainc; - AdditionalColor = addtc; - HelicopterPads = pad; - Hangar = hangar; - Deckhouse = deckhouse; + // Deckhouse = deckhouse; values[0] = rn.Next(1, 4); values[1] = rn.Next(5, 10); values[2] = rn.Next(1, 3); diff --git a/ProjectCruiser/EntityCruiser.cs b/ProjectCruiser/EntityCruiser.cs new file mode 100644 index 0000000..7bfb66b --- /dev/null +++ b/ProjectCruiser/EntityCruiser.cs @@ -0,0 +1,19 @@ +namespace ProjectCruiser; + +public class EntityCruiser : EntityBase +{ + public Color AdditionalColor { get; private set; } // доп. цвет + + // признаки (наличия) + public bool HelicopterPads { get; private set; } // вертолетная площадка + public bool Hangar { get; private set; } // ангар + + public EntityCruiser(int speed, double weight, Color mainc, + Color additionalColor, bool pads, bool hangar) + : base(speed, weight, mainc) + { + AdditionalColor = additionalColor; + HelicopterPads = pads; + Hangar = hangar; + } +} diff --git a/ProjectCruiser/IMoveableObj.cs b/ProjectCruiser/IMoveableObj.cs new file mode 100644 index 0000000..ca821e7 --- /dev/null +++ b/ProjectCruiser/IMoveableObj.cs @@ -0,0 +1,16 @@ +namespace ProjectCruiser; + +// Интерфейс для работы с перемещаемым объектом +public interface IMoveableObj +{ + // Получение координат объекта + ObjParameters? GetObjectPosition { get; } + + // Получение шага объекта + int GetStep { get; } + + /// Попытка переместить объект в указанном направлении + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectCruiser/MoveableTransport.cs b/ProjectCruiser/MoveableTransport.cs new file mode 100644 index 0000000..3717287 --- /dev/null +++ b/ProjectCruiser/MoveableTransport.cs @@ -0,0 +1,54 @@ +namespace ProjectCruiser; + +// Класс-реализация IMoveableObject с использованием DrawningBase +public class MoveableTransport : IMoveableObj +{ + // Поле-объект класса Drawning(Transport) или его наследника + private readonly DrawningBase? _car = null; + + public MoveableTransport(DrawningBase car) + { + _car = car; + } + + public ObjParameters? GetObjectPosition + { + get + { + if (_car == null || _car.EntityTransport == null || + !_car.GetPosX.HasValue || !_car.GetPosY.HasValue) + { + return null; + } + return new ObjParameters(_car.GetPosX.Value, + _car.GetPosY.Value, _car.GetWidth, _car.GetHeight); + } + } + + public int GetStep => (int)(_car?.EntityTransport?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_car == null || _car.EntityTransport == null) + { + return false; + } + return _car.MoveTransport(GetDirectionType(direction)); + } + + /// Конвертация из MovementDirection в DirectionType + /// MovementDirection + /// DirectionType + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknown, + }; + } +} + diff --git a/ProjectCruiser/MovementDirection.cs b/ProjectCruiser/MovementDirection.cs new file mode 100644 index 0000000..3a83b0d --- /dev/null +++ b/ProjectCruiser/MovementDirection.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser; + +// Направление перемещения +public enum MovementDirection +{ + Up = 1, + Down = 2, + Left = 3, + Right = 4 +} diff --git a/ProjectCruiser/ObjParameters.cs b/ProjectCruiser/ObjParameters.cs new file mode 100644 index 0000000..b2eb447 --- /dev/null +++ b/ProjectCruiser/ObjParameters.cs @@ -0,0 +1,29 @@ +namespace ProjectCruiser; + +// Параметры-координаты объекта +public class ObjParameters +{ + 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 ObjParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/ProjectCruiser/StrategyStatus.cs b/ProjectCruiser/StrategyStatus.cs new file mode 100644 index 0000000..233da87 --- /dev/null +++ b/ProjectCruiser/StrategyStatus.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser; + +// Статус выполнения операции перемещения +public enum StrategyStatus +{ + // готово к началу + NotInit, + // Выполняется + InProgress, + // Завершено + Finish +} + -- 2.25.1 From cb015204b91a7e62bfd1dad10bb39c44caeb452a Mon Sep 17 00:00:00 2001 From: DelphyAlmond Date: Wed, 12 Jun 2024 19:21:59 +0400 Subject: [PATCH 2/2] inheritance --- ProjectCruiser/DrawningCruiser.cs | 81 ---------------- .../{ => DrawningSamples}/DirectionType.cs | 2 +- .../{ => DrawningSamples}/DrawningBase.cs | 14 +-- .../DrawningSamples/DrawningCruiser.cs | 54 +++++++++++ ProjectCruiser/{ => Entities}/EntityBase.cs | 7 +- .../{ => Entities}/EntityCruiser.cs | 2 +- .../MoveStrategy/AbstractStrategy.cs | 88 ++++++++++++++++++ .../{ => MoveStrategy}/IMoveableObj.cs | 2 +- ProjectCruiser/MoveStrategy/MoveToBorder.cs | 53 +++++++++++ ProjectCruiser/MoveStrategy/MoveToCentre.cs | 50 ++++++++++ .../{ => MoveStrategy}/MoveableTransport.cs | 4 +- .../MoveStrategy/MovementDirection.cs | 10 ++ .../{ => MoveStrategy}/ObjParameters.cs | 2 +- .../{ => MoveStrategy}/StrategyStatus.cs | 8 +- ProjectCruiser/MovementDirection.cs | 16 ---- ProjectCruiser/OceanForm1.Designer.cs | 52 +++++++++-- ProjectCruiser/OceanForm1.cs | 93 +++++++++++++++---- 17 files changed, 392 insertions(+), 146 deletions(-) delete mode 100644 ProjectCruiser/DrawningCruiser.cs rename ProjectCruiser/{ => DrawningSamples}/DirectionType.cs (68%) rename ProjectCruiser/{ => DrawningSamples}/DrawningBase.cs (92%) create mode 100644 ProjectCruiser/DrawningSamples/DrawningCruiser.cs rename ProjectCruiser/{ => Entities}/EntityBase.cs (82%) rename ProjectCruiser/{ => Entities}/EntityCruiser.cs (93%) create mode 100644 ProjectCruiser/MoveStrategy/AbstractStrategy.cs rename ProjectCruiser/{ => MoveStrategy}/IMoveableObj.cs (93%) create mode 100644 ProjectCruiser/MoveStrategy/MoveToBorder.cs create mode 100644 ProjectCruiser/MoveStrategy/MoveToCentre.cs rename ProjectCruiser/{ => MoveStrategy}/MoveableTransport.cs (95%) create mode 100644 ProjectCruiser/MoveStrategy/MovementDirection.cs rename ProjectCruiser/{ => MoveStrategy}/ObjParameters.cs (95%) rename ProjectCruiser/{ => MoveStrategy}/StrategyStatus.cs (62%) delete mode 100644 ProjectCruiser/MovementDirection.cs diff --git a/ProjectCruiser/DrawningCruiser.cs b/ProjectCruiser/DrawningCruiser.cs deleted file mode 100644 index 3225a76..0000000 --- a/ProjectCruiser/DrawningCruiser.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Drawing.Drawing2D; - -namespace ProjectCruiser; -internal class DrawningCruiser : DrawningBase -{ - - private EntityCruiser EntityCruiser; - - public DrawningCruiser(int speed, double weight, Color bodyColor, - Color additionalColor, bool pads, bool hangar) : base(300, 42) - // all additional featchures 'inside' object, so size remains - { - EntityCruiser = new EntityCruiser(speed, weight, - bodyColor, additionalColor, pads, hangar); - } - - public override void DrawTransport(Graphics g) - { - if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return; - } - - Pen pen = new(Color.Black, 2); - Brush PadBrush = new HatchBrush(HatchStyle.DottedDiamond, Color.LightGray, Color.Black); - Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor); - Brush mainBrush = new SolidBrush(EntityTransport.MainColor); - - //границы cruiser - Point point0 = new Point(_startPosX.Value + 2, _startPosY.Value + 7); - Point point1 = new Point(_startPosX.Value + 2, _startPosY.Value + 30); - Point point2 = new Point(_startPosX.Value + 184, _startPosY.Value + 42); - Point point3 = new Point(_startPosX.Value + 260, _startPosY.Value + 34); - Point point4 = new Point(_startPosX.Value + 300, _startPosY.Value + 22); - Point point5 = new Point(_startPosX.Value + 260, _startPosY.Value + 10); - Point point6 = new Point(_startPosX.Value + 184, _startPosY.Value + 2); - - Point[] boarders = { - point0, - point1, - point2, - point3, - point4, - point5, - point6 - }; - - g.DrawPolygon(pen, boarders); - g.FillPolygon(mainBrush, boarders); - - // салон на верхней палубе - // random location - int y_h = EntityCruiser.values[1]; - g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24); - - g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); - g.FillRectangle(additionalBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); - g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); - g.FillRectangle(additionalBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); - - // вертолетная площадка - if (EntityCruiser.HelicopterPads) - { - g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); - g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); - } - - // ангар - if (EntityCruiser.Hangar) - { - int n = EntityTransport.values[2]; - if (n == 1) g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7); - - else - { - g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20); - g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12); - } - } - } -} diff --git a/ProjectCruiser/DirectionType.cs b/ProjectCruiser/DrawningSamples/DirectionType.cs similarity index 68% rename from ProjectCruiser/DirectionType.cs rename to ProjectCruiser/DrawningSamples/DirectionType.cs index 04733e1..508a382 100644 --- a/ProjectCruiser/DirectionType.cs +++ b/ProjectCruiser/DrawningSamples/DirectionType.cs @@ -1,4 +1,4 @@ -namespace ProjectCruiser; +namespace ProjectCruiser.DrawningSamples; public enum DirectionType { Unknown = -1, diff --git a/ProjectCruiser/DrawningBase.cs b/ProjectCruiser/DrawningSamples/DrawningBase.cs similarity index 92% rename from ProjectCruiser/DrawningBase.cs rename to ProjectCruiser/DrawningSamples/DrawningBase.cs index 34e98c0..4b7884f 100644 --- a/ProjectCruiser/DrawningBase.cs +++ b/ProjectCruiser/DrawningSamples/DrawningBase.cs @@ -1,4 +1,5 @@ -namespace ProjectCruiser; +using ProjectCruiser.Entities; +namespace ProjectCruiser.DrawningSamples; public class DrawningBase { // Класс-сущность @@ -9,12 +10,11 @@ public class DrawningBase protected int? _startPosX; // < protected protected int? _startPosY; // < protected - private readonly int _drawningWidth = 300; // Ширина прорисовки автомобиля + private readonly int _drawningWidth = 302; // Ширина прорисовки автомобиля private readonly int _drawningHeight = 42; // Высота прорисовки автомобиля // Инициализация свойств (теперь через конструктор) - public DrawningBase(int speed, double weight, - Color bodyColor) + public DrawningBase(int speed, double weight, Color bodyColor) : this() { EntityTransport = new EntityBase(speed, weight, bodyColor); } @@ -42,7 +42,8 @@ public class DrawningBase // Установка границ поля public bool SetPictureSize(int w, int h) { - if (w > _pictureWidth || h > _pictureHeight) + if (w < _drawningWidth || h < _drawningHeight) + // canvas always bigger then obj to fit it { return false; } @@ -78,8 +79,7 @@ public class DrawningBase public bool MoveTransport(DirectionType direction) { - if (EntityTransport == null || !_startPosX.HasValue || - !_startPosY.HasValue) + if (EntityTransport == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } diff --git a/ProjectCruiser/DrawningSamples/DrawningCruiser.cs b/ProjectCruiser/DrawningSamples/DrawningCruiser.cs new file mode 100644 index 0000000..90127e5 --- /dev/null +++ b/ProjectCruiser/DrawningSamples/DrawningCruiser.cs @@ -0,0 +1,54 @@ +using System.Drawing.Drawing2D; +using ProjectCruiser.Entities; +namespace ProjectCruiser.DrawningSamples; + +public class DrawningCruiser : DrawningBase +{ + // Инициализация свойств (все параметры класса (сущности)) + public DrawningCruiser(int speed, double weight, Color bodyColor, + Color additionalColor, bool pads, bool hangar) : base(302, 42) + // all additional featchures 'inside' object, so size remains + { + EntityTransport = new EntityCruiser(speed, weight, + bodyColor, additionalColor, pads, hangar); + } + + public override void DrawTransport(Graphics g) + { + if (EntityTransport == null || EntityTransport is not EntityCruiser ship || + !_startPosX.HasValue || !_startPosY.HasValue) // [ !!! ] :O + { + return; + } + + Pen pen = new(Color.Black, 2); + Brush PadBrush = new HatchBrush(HatchStyle.DottedDiamond, Color.LightGray, Color.Black); + Brush additionalBrush = new SolidBrush(ship.AdditionalColor); + + //границы cruiser <...> + // & + // салон на верхней палубе : + + base.DrawTransport(g); + + // вертолетная площадка + if (ship.HelicopterPads) + { + g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); + g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); + } + + // ангар + if (ship.Hangar) + { + int n = EntityTransport.values[2]; + if (n == 1) g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7); + + else + { + g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20); + g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12); + } + } + } +} diff --git a/ProjectCruiser/EntityBase.cs b/ProjectCruiser/Entities/EntityBase.cs similarity index 82% rename from ProjectCruiser/EntityBase.cs rename to ProjectCruiser/Entities/EntityBase.cs index e2d865b..f9aac46 100644 --- a/ProjectCruiser/EntityBase.cs +++ b/ProjectCruiser/Entities/EntityBase.cs @@ -1,4 +1,4 @@ -namespace ProjectCruiser; +namespace ProjectCruiser.Entities; public class EntityBase { @@ -6,13 +6,14 @@ public class EntityBase public int Speed { get; private set; } // скорость public double Weight { get; private set; } // вес public Color MainColor { get; private set; } // основной цвет + // public bool Deckhouse { get; private set; } // салон на верхней палубе public double Step => Speed * 100 / Weight; public int[] values = { 0, 0, 0 }; - public EntityBase(int speed, double weight, - Color mainc) // (bool) deckhouse -> default TRUE now + public EntityBase(int speed, double weight, Color mainc) + // (bool) deckhouse -> default TRUE now { Random rn = new(); Speed = speed; diff --git a/ProjectCruiser/EntityCruiser.cs b/ProjectCruiser/Entities/EntityCruiser.cs similarity index 93% rename from ProjectCruiser/EntityCruiser.cs rename to ProjectCruiser/Entities/EntityCruiser.cs index 7bfb66b..e251e02 100644 --- a/ProjectCruiser/EntityCruiser.cs +++ b/ProjectCruiser/Entities/EntityCruiser.cs @@ -1,4 +1,4 @@ -namespace ProjectCruiser; +namespace ProjectCruiser.Entities; public class EntityCruiser : EntityBase { diff --git a/ProjectCruiser/MoveStrategy/AbstractStrategy.cs b/ProjectCruiser/MoveStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..e2004ba --- /dev/null +++ b/ProjectCruiser/MoveStrategy/AbstractStrategy.cs @@ -0,0 +1,88 @@ +namespace ProjectCruiser.MoveStrategy; + +public abstract class AbstractStrategy +{ + // Перемещаемый объект + private IMoveableObj? _moveableObject; + + // Статус перемещения (default установка) + private StrategyStatus _state = StrategyStatus.NotInit; + + // Ширина поля + protected int FieldWidth { get; private set; } + + // Высота поля + protected int FieldHeight { get; private set; } + + // Получение статуса + public StrategyStatus GetStatus() { return _state; } + + // Установка данных + public void SetData(IMoveableObj moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + // Шаг перемещения + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + if (IsTargetDestination()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + + // Результаты перемещения + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + protected bool MoveRight() => MoveTo(MovementDirection.Right); + protected bool MoveUp() => MoveTo(MovementDirection.Up); + protected bool MoveDown() => MoveTo(MovementDirection.Down); + + + // Параметры объекта + protected ObjParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; + + // Шаг объекта + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + + // Перемещение к цели + protected abstract void MoveToTarget(); + + // Проверка, достигнута ли цель + protected abstract bool IsTargetDestination(); + + + /// Попытка перемещения в требуемом направлении + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} diff --git a/ProjectCruiser/IMoveableObj.cs b/ProjectCruiser/MoveStrategy/IMoveableObj.cs similarity index 93% rename from ProjectCruiser/IMoveableObj.cs rename to ProjectCruiser/MoveStrategy/IMoveableObj.cs index ca821e7..3af89c4 100644 --- a/ProjectCruiser/IMoveableObj.cs +++ b/ProjectCruiser/MoveStrategy/IMoveableObj.cs @@ -1,4 +1,4 @@ -namespace ProjectCruiser; +namespace ProjectCruiser.MoveStrategy; // Интерфейс для работы с перемещаемым объектом public interface IMoveableObj diff --git a/ProjectCruiser/MoveStrategy/MoveToBorder.cs b/ProjectCruiser/MoveStrategy/MoveToBorder.cs new file mode 100644 index 0000000..22137e2 --- /dev/null +++ b/ProjectCruiser/MoveStrategy/MoveToBorder.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser.MoveStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjParameters? objP = GetObjectParameters; + if (objP == null) + { + return false; + } + + return FieldWidth - GetStep() < objP.RightBorder + && FieldHeight - GetStep() < objP.DownBorder; + } + + protected override void MoveToTarget() + { + ObjParameters? objP = GetObjectParameters; + if (objP == null) + { + return; + } + + int s = (int)GetStep(); + + int diffx = objP.RightBorder - FieldWidth; + if (Math.Abs(diffx) > GetStep()) + { + if (diffx > 0) + { + MoveLeft(); + } + else { MoveRight(); } + } + + int diffy = objP.DownBorder - FieldHeight; // (... - s) - step unnecessary + if (Math.Abs(diffy) > GetStep()) + { + if (diffy > 0) + { + MoveUp(); + } + else { MoveDown(); } + } + } +} diff --git a/ProjectCruiser/MoveStrategy/MoveToCentre.cs b/ProjectCruiser/MoveStrategy/MoveToCentre.cs new file mode 100644 index 0000000..493cee5 --- /dev/null +++ b/ProjectCruiser/MoveStrategy/MoveToCentre.cs @@ -0,0 +1,50 @@ +namespace ProjectCruiser.MoveStrategy; + +public class MoveToCentre : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjParameters? objP = GetObjectParameters; + if (objP == null) + { + return false; + } + + return objP.ObjectMiddleHorizontal - GetStep() + <= FieldWidth / 2 && objP.ObjectMiddleHorizontal + + GetStep() >= FieldWidth / 2 + + && objP.ObjectMiddleVertical - GetStep() + <= FieldHeight / 2 && objP.ObjectMiddleVertical + + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjParameters? objP = GetObjectParameters; + if (objP == null) + { + return; + } + + int diffx = objP.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffx) > GetStep()) + { + if (diffx > 0) + { + MoveLeft(); + } + else { MoveRight(); } + } + + int diffy = objP.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffy) > GetStep()) + { + if (diffy > 0) + { + MoveUp(); + } + else { MoveDown(); } + } + } +} diff --git a/ProjectCruiser/MoveableTransport.cs b/ProjectCruiser/MoveStrategy/MoveableTransport.cs similarity index 95% rename from ProjectCruiser/MoveableTransport.cs rename to ProjectCruiser/MoveStrategy/MoveableTransport.cs index 3717287..aab74a4 100644 --- a/ProjectCruiser/MoveableTransport.cs +++ b/ProjectCruiser/MoveStrategy/MoveableTransport.cs @@ -1,4 +1,6 @@ -namespace ProjectCruiser; +using ProjectCruiser.DrawningSamples; + +namespace ProjectCruiser.MoveStrategy; // Класс-реализация IMoveableObject с использованием DrawningBase public class MoveableTransport : IMoveableObj diff --git a/ProjectCruiser/MoveStrategy/MovementDirection.cs b/ProjectCruiser/MoveStrategy/MovementDirection.cs new file mode 100644 index 0000000..9404d45 --- /dev/null +++ b/ProjectCruiser/MoveStrategy/MovementDirection.cs @@ -0,0 +1,10 @@ +namespace ProjectCruiser.MoveStrategy; + +// Направление перемещения +public enum MovementDirection +{ + Up = 1, + Down = 2, + Left = 3, + Right = 4 +} diff --git a/ProjectCruiser/ObjParameters.cs b/ProjectCruiser/MoveStrategy/ObjParameters.cs similarity index 95% rename from ProjectCruiser/ObjParameters.cs rename to ProjectCruiser/MoveStrategy/ObjParameters.cs index b2eb447..428f1eb 100644 --- a/ProjectCruiser/ObjParameters.cs +++ b/ProjectCruiser/MoveStrategy/ObjParameters.cs @@ -1,4 +1,4 @@ -namespace ProjectCruiser; +namespace ProjectCruiser.MoveStrategy; // Параметры-координаты объекта public class ObjParameters diff --git a/ProjectCruiser/StrategyStatus.cs b/ProjectCruiser/MoveStrategy/StrategyStatus.cs similarity index 62% rename from ProjectCruiser/StrategyStatus.cs rename to ProjectCruiser/MoveStrategy/StrategyStatus.cs index 233da87..d2d7608 100644 --- a/ProjectCruiser/StrategyStatus.cs +++ b/ProjectCruiser/MoveStrategy/StrategyStatus.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectCruiser; +namespace ProjectCruiser.MoveStrategy; // Статус выполнения операции перемещения public enum StrategyStatus diff --git a/ProjectCruiser/MovementDirection.cs b/ProjectCruiser/MovementDirection.cs deleted file mode 100644 index 3a83b0d..0000000 --- a/ProjectCruiser/MovementDirection.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectCruiser; - -// Направление перемещения -public enum MovementDirection -{ - Up = 1, - Down = 2, - Left = 3, - Right = 4 -} diff --git a/ProjectCruiser/OceanForm1.Designer.cs b/ProjectCruiser/OceanForm1.Designer.cs index eeab7be..611ed06 100644 --- a/ProjectCruiser/OceanForm1.Designer.cs +++ b/ProjectCruiser/OceanForm1.Designer.cs @@ -27,12 +27,15 @@ partial class OceanForm1 btnUpArrow = new Button(); btnCreateBase = new Button(); pictureBoxCr = new PictureBox(); + comboBoxStrategy = new ComboBox(); + btnCreateAdvanced = new Button(); + btnActivateStrategy = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCr).BeginInit(); SuspendLayout(); // // btnLeftArrow // - btnLeftArrow.Location = new Point(926, 634); + btnLeftArrow.Location = new Point(926, 445); btnLeftArrow.Name = "btnLeftArrow"; btnLeftArrow.Size = new Size(141, 132); btnLeftArrow.TabIndex = 0; @@ -41,7 +44,7 @@ partial class OceanForm1 // // btnDownArrow // - btnDownArrow.Location = new Point(1073, 634); + btnDownArrow.Location = new Point(1073, 445); btnDownArrow.Name = "btnDownArrow"; btnDownArrow.Size = new Size(141, 132); btnDownArrow.TabIndex = 1; @@ -50,7 +53,7 @@ partial class OceanForm1 // // btnRightArrow // - btnRightArrow.Location = new Point(1220, 634); + btnRightArrow.Location = new Point(1220, 445); btnRightArrow.Name = "btnRightArrow"; btnRightArrow.Size = new Size(141, 132); btnRightArrow.TabIndex = 2; @@ -59,7 +62,7 @@ partial class OceanForm1 // // btnUpArrow // - btnUpArrow.Location = new Point(1073, 496); + btnUpArrow.Location = new Point(1073, 307); btnUpArrow.Name = "btnUpArrow"; btnUpArrow.Size = new Size(141, 132); btnUpArrow.TabIndex = 3; @@ -70,11 +73,11 @@ partial class OceanForm1 // btnCreateBase.Location = new Point(928, 12); btnCreateBase.Name = "btnCreateBase"; - btnCreateBase.Size = new Size(435, 46); + btnCreateBase.Size = new Size(433, 46); btnCreateBase.TabIndex = 4; - btnCreateBase.Text = "Create"; + btnCreateBase.Text = "Create base object"; btnCreateBase.UseVisualStyleBackColor = true; - btnCreateBase.Click += ButtonCreateBase_Click; + btnCreateBase.Click += btnCreateBase_Click; // // pictureBoxCr // @@ -85,12 +88,44 @@ partial class OceanForm1 pictureBoxCr.TabIndex = 5; pictureBoxCr.TabStop = false; // + // comboBoxStrategy + // + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "centre", "border" }); + comboBoxStrategy.Location = new Point(928, 120); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(435, 40); + comboBoxStrategy.TabIndex = 6; + // + // btnCreateAdvanced + // + btnCreateAdvanced.Location = new Point(928, 58); + btnCreateAdvanced.Name = "btnCreateAdvanced"; + btnCreateAdvanced.Size = new Size(433, 46); + btnCreateAdvanced.TabIndex = 7; + btnCreateAdvanced.Text = "Create advanced object"; + btnCreateAdvanced.UseVisualStyleBackColor = true; + btnCreateAdvanced.Click += btnCreateAdvanced_Click; + // + // btnActivateStrategy + // + btnActivateStrategy.Location = new Point(928, 166); + btnActivateStrategy.Name = "btnActivateStrategy"; + btnActivateStrategy.Size = new Size(435, 46); + btnActivateStrategy.TabIndex = 8; + btnActivateStrategy.Text = "Activate path"; + btnActivateStrategy.UseVisualStyleBackColor = true; + btnActivateStrategy.Click += ButtonStrategyStep_Click; + // // OceanForm1 // AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleMode = AutoScaleMode.Font; BackColor = Color.Turquoise; ClientSize = new Size(1375, 778); + Controls.Add(btnActivateStrategy); + Controls.Add(btnCreateAdvanced); + Controls.Add(comboBoxStrategy); Controls.Add(btnCreateBase); Controls.Add(btnUpArrow); Controls.Add(btnRightArrow); @@ -111,4 +146,7 @@ partial class OceanForm1 private Button btnUpArrow; private Button btnCreateBase; private PictureBox pictureBoxCr; + private ComboBox comboBoxStrategy; + private Button btnCreateAdvanced; + private Button btnActivateStrategy; } \ No newline at end of file diff --git a/ProjectCruiser/OceanForm1.cs b/ProjectCruiser/OceanForm1.cs index 9c55215..f214473 100644 --- a/ProjectCruiser/OceanForm1.cs +++ b/ProjectCruiser/OceanForm1.cs @@ -1,12 +1,5 @@ -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 ProjectCruiser.DrawningSamples; +using ProjectCruiser.MoveStrategy; namespace ProjectCruiser { @@ -15,9 +8,13 @@ namespace ProjectCruiser // Поле-объект для прорисовки объекта private DrawningBase? _drawningCruiser; + // Стратегия перемещения + private AbstractStrategy? _strategy; + public OceanForm1() { InitializeComponent(); + _strategy = null; } // Метод прорисовки transport @@ -27,32 +24,52 @@ namespace ProjectCruiser { return; } - Bitmap bmp = new(pictureBoxCr.Width, - pictureBoxCr.Height); + Bitmap bmp = new(pictureBoxCr.Width, pictureBoxCr.Height); Graphics gr = Graphics.FromImage(bmp); _drawningCruiser.DrawTransport(gr); pictureBoxCr.Image = bmp; } - // Обработка нажатия кнопки "Create" - private void ButtonCreateBase_Click(object sender, EventArgs e) + // Обработка нажатия кнопок "Create(...)" + + private void btnCreateBase_Click(object sender, EventArgs e) => + CreateObject(nameof(DrawningBase)); + + private void btnCreateAdvanced_Click(object sender, EventArgs e) => + CreateObject(nameof(DrawningCruiser)); + + // Создание объекта класса-перемещения + /// Тип создаваемого объекта + private void CreateObject(string type) { Random random = new(); - _drawningCruiser = new DrawningBase(); + switch (type) + { + case nameof(DrawningBase): + _drawningCruiser = new DrawningBase(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); + break; - _drawningCruiser.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)), - Convert.ToBoolean(random.Next(0, 2))); + case nameof(DrawningCruiser): + _drawningCruiser = new DrawningCruiser(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))); + break; + + default: + return; + } _drawningCruiser.SetPictureSize(pictureBoxCr.Width, pictureBoxCr.Height); _drawningCruiser.SetPosition(random.Next(10, 100), pictureBoxCr.Height - random.Next(10, 100) - _drawningCruiser.getHeight()); + + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } - private void BtnMove_Click(object sender, EventArgs e) { if (_drawningCruiser == null) @@ -85,5 +102,41 @@ namespace ProjectCruiser Draw(); } } + + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningCruiser == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCentre(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableTransport(_drawningCruiser), + pictureBoxCr.Width, pictureBoxCr.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } -- 2.25.1