diff --git a/base/Catamaran/Catamaran/AbstractStrategy.cs b/base/Catamaran/Catamaran/AbstractStrategy.cs new file mode 100644 index 0000000..baceafd --- /dev/null +++ b/base/Catamaran/Catamaran/AbstractStrategy.cs @@ -0,0 +1,136 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Catamaran.MovementStrategy; + +namespace Catamaran.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; + } + } +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/Catamaran.Designer.cs b/base/Catamaran/Catamaran/Catamaran.Designer.cs index 101992c..213cc04 100644 --- a/base/Catamaran/Catamaran/Catamaran.Designer.cs +++ b/base/Catamaran/Catamaran/Catamaran.Designer.cs @@ -1,6 +1,6 @@ namespace Catamaran { - partial class Catamaran + partial class FormCatamaran { /// /// Required designer variable. @@ -29,11 +29,14 @@ private void InitializeComponent() { this.pictureBoxCatamaran = new System.Windows.Forms.PictureBox(); - this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonCreateCatamaran = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.buttonStep = new System.Windows.Forms.Button(); + this.buttonCreateSailCatamaran = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).BeginInit(); this.SuspendLayout(); // @@ -47,16 +50,16 @@ this.pictureBoxCatamaran.TabIndex = 0; this.pictureBoxCatamaran.TabStop = false; // - // buttonCreate + // buttonCreateCatamaran // - this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreate.Location = new System.Drawing.Point(12, 426); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(75, 23); - this.buttonCreate.TabIndex = 1; - this.buttonCreate.Text = "Создать"; - this.buttonCreate.UseVisualStyleBackColor = true; - this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + this.buttonCreateCatamaran.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateCatamaran.Location = new System.Drawing.Point(140, 398); + this.buttonCreateCatamaran.Name = "buttonCreateCatamaran"; + this.buttonCreateCatamaran.Size = new System.Drawing.Size(122, 51); + this.buttonCreateCatamaran.TabIndex = 1; + this.buttonCreateCatamaran.Text = "Создать катамаран"; + this.buttonCreateCatamaran.UseVisualStyleBackColor = true; + this.buttonCreateCatamaran.Click += new System.EventHandler(this.buttonCreateCatamaran_Click); // // buttonUp // @@ -106,16 +109,52 @@ this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); // - // Catamaran + // comboBoxStrategy + // + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "До центра", + "До края"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(688, 12); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(151, 23); + this.comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + this.buttonStep.Location = new System.Drawing.Point(770, 41); + this.buttonStep.Name = "buttonStep"; + this.buttonStep.Size = new System.Drawing.Size(75, 23); + this.buttonStep.TabIndex = 7; + this.buttonStep.Text = "Шаг"; + this.buttonStep.UseVisualStyleBackColor = true; + this.buttonStep.Click += new System.EventHandler(this.ButtonStep_Click); + // + // buttonCreateSailCatamaran + // + this.buttonCreateSailCatamaran.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateSailCatamaran.Location = new System.Drawing.Point(12, 398); + this.buttonCreateSailCatamaran.Name = "buttonCreateSailCatamaran"; + this.buttonCreateSailCatamaran.Size = new System.Drawing.Size(122, 51); + this.buttonCreateSailCatamaran.TabIndex = 8; + this.buttonCreateSailCatamaran.Text = "Создать катамаран с парусом"; + this.buttonCreateSailCatamaran.UseVisualStyleBackColor = true; + this.buttonCreateSailCatamaran.Click += new System.EventHandler(this.buttonCreateSailCatamaran_Click); + // + // FormCatamaran // this.ClientSize = new System.Drawing.Size(884, 461); + this.Controls.Add(this.buttonCreateSailCatamaran); + this.Controls.Add(this.buttonStep); + this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.buttonCreateCatamaran); this.Controls.Add(this.pictureBoxCatamaran); - this.Name = "Catamaran"; + this.Name = "FormCatamaran"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Катамаран"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).EndInit(); @@ -127,11 +166,14 @@ #endregion - private Button buttonCreate; + private Button buttonCreateCatamaran; private PictureBox pictureBoxCatamaran; private Button buttonUp; private Button buttonRight; private Button buttonLeft; private Button buttonDown; + private ComboBox comboBoxStrategy; + private Button buttonStep; + private Button buttonCreateSailCatamaran; } } \ No newline at end of file diff --git a/base/Catamaran/Catamaran/Catamaran.cs b/base/Catamaran/Catamaran/Catamaran.cs index 578fcda..ceda018 100644 --- a/base/Catamaran/Catamaran/Catamaran.cs +++ b/base/Catamaran/Catamaran/Catamaran.cs @@ -1,12 +1,33 @@ +using Catamaran.Entities; +using Catamaran.DrawningObjects; +using Catamaran.MovementStrategy; + namespace Catamaran { - public partial class Catamaran : Form + /// + /// "" + /// + + public partial class FormCatamaran : Form { /// /// - /// private DrawningCatamaran? _drawningCatamaran; - + + /// + /// + /// + private AbstractStrategy? _abstractStrategy; + + /// + /// + /// + public FormCatamaran() + { + InitializeComponent(); + } + /// /// /// @@ -22,16 +43,45 @@ namespace Catamaran _drawningCatamaran.DrawTransport(gr); pictureBoxCatamaran.Image = bmp; } + /// - /// "" + /// " " /// /// /// - public Catamaran() + private void buttonCreateSailCatamaran_Click(object sender, EventArgs e) { - InitializeComponent(); + Random random = new(); + _drawningCatamaran = new DrawningSailCatamaran(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)), pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); + _drawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); } - + + /// + /// " " + /// + /// + /// + private void buttonCreateCatamaran_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningCatamaran = new DrawningCatamaran(random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); + _drawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + + /// + /// + /// + /// + /// private void buttonMove_Click(object sender, EventArgs e) { if (_drawningCatamaran == null) @@ -42,41 +92,60 @@ namespace Catamaran switch (name) { case "buttonUp": - _drawningCatamaran.MoveTransport(Direction.Up); + _drawningCatamaran.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawningCatamaran.MoveTransport(Direction.Down); + _drawningCatamaran.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawningCatamaran.MoveTransport(Direction.Left); + _drawningCatamaran.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawningCatamaran.MoveTransport(Direction.Right); + _drawningCatamaran.MoveTransport(DirectionType.Right); break; } Draw(); } - - - private void buttonCreate_Click(object sender, EventArgs e) + /// + /// "" + /// + /// + /// + private void ButtonStep_Click(object sender, EventArgs e) { - Random random = new(); - _drawningCatamaran = new DrawningCatamaran(); - _drawningCatamaran.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)), pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); - _drawningCatamaran.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + if (_drawningCatamaran == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawningObjectCatamaran(_drawningCatamaran), pictureBoxCatamaran.Width, + pictureBoxCatamaran.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } } - - - } } diff --git a/base/Catamaran/Catamaran/Direction.cs b/base/Catamaran/Catamaran/Direction.cs index 4625a88..4ee24c3 100644 --- a/base/Catamaran/Catamaran/Direction.cs +++ b/base/Catamaran/Catamaran/Direction.cs @@ -9,7 +9,7 @@ namespace Catamaran /// /// Направление перемещения /// - public enum Direction + public enum DirectionType { /// /// Вверх diff --git a/base/Catamaran/Catamaran/DrawningCatamaran.cs b/base/Catamaran/Catamaran/DrawningCatamaran.cs index a21f635..c56fe11 100644 --- a/base/Catamaran/Catamaran/DrawningCatamaran.cs +++ b/base/Catamaran/Catamaran/DrawningCatamaran.cs @@ -3,18 +3,19 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Catamaran.Entities; -namespace Catamaran +namespace Catamaran.DrawningObjects { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - internal class DrawningCatamaran + public class DrawningCatamaran { /// /// Класс-сущность /// - public EntityCatamaran? EntityCatamaran { get; private set; } + public EntityCatamaran? EntityCatamaran { get; protected set; } /// /// Ширина окна /// @@ -26,11 +27,11 @@ namespace Catamaran /// /// Левая координата прорисовки катамарана /// - private int _startPosX; + protected int _startPosX; /// - /// Верхняя кооридната прорисовки катамарана + /// Верхняя координата прорисовки катамарана /// - private int _startPosY; + protected int _startPosY; /// /// Ширина прорисовки катамарана /// @@ -40,34 +41,71 @@ namespace Catamaran /// private readonly int _CatamaranHeight = 120; /// - /// Инициализация свойств + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _CatamaranWidth; + /// + /// Высота объекта + /// + public int GetHeight => _CatamaranHeight; + /// + /// Конструктор /// /// Скорость /// Вес - /// Цвет корпуса - /// Дополнительный цвет - /// Признак наличия паруса - /// Признак наличия поплавков + /// Основной цвет /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool sail, bool floatDetail, int width, int height) + public DrawningCatamaran(int speed, double weight, Color bodyColor, int width, int height) { if (width >= _CatamaranWidth && height >= _CatamaranHeight) { _pictureWidth = width; _pictureHeight = height; - EntityCatamaran = new EntityCatamaran(); - EntityCatamaran.Init(speed, weight, bodyColor, additionalColor, sail, floatDetail); - return true; + EntityCatamaran = new EntityCatamaran(speed, weight, bodyColor); + return; } else { - return false; // Возвращаем false, если размеры формы недостаточны + return; + } + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки катамарана + /// Высота прорисовки катамарана + /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах + protected DrawningCatamaran(int speed, double weight, Color bodyColor, int width, int height, int catamaranWidth, int catamaranHeight) + { + if (width >= _CatamaranWidth && height >= _CatamaranHeight) + { + _pictureWidth = width; + _pictureHeight = height; + _CatamaranWidth = catamaranWidth; + _CatamaranHeight = catamaranHeight; + EntityCatamaran = new EntityCatamaran(speed, weight, bodyColor); + return; + } + else + { + return; } - } /// /// Установка позиции @@ -76,48 +114,73 @@ namespace Catamaran /// Координата Y public void SetPosition(int x, int y) { - if (x >= 0 && x + _CatamaranWidth <= _pictureWidth && y >= 0 && y + _CatamaranHeight <= _pictureHeight) + if (x < 0 || x + _CatamaranWidth > _pictureWidth) { - _startPosX = x; - _startPosY = y; + x = 0; } + if (y < 0 || y + _CatamaranHeight > _pictureHeight) + { + y = 0; + } + _startPosX = x; + _startPosY = y; + } + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityCatamaran == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityCatamaran.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityCatamaran.Step > 0, + // вправо + DirectionType.Right => _startPosX + EntityCatamaran.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + EntityCatamaran.Step < _pictureHeight, + _ => false + }; } /// /// Изменение направления перемещения /// /// Направление - public void MoveTransport(Direction direction) - { - if (EntityCatamaran == null) + public void MoveTransport(DirectionType direction) { + if (!CanMove(direction) || EntityCatamaran == null) + { return; } switch (direction) { - //влево - case Direction.Left: + case DirectionType.Left: if (_startPosX - EntityCatamaran.Step > 0) { _startPosX -= (int)EntityCatamaran.Step; } break; - //вверх - case Direction.Up: + case DirectionType.Up: if (_startPosY - EntityCatamaran.Step > 0) { _startPosY -= (int)EntityCatamaran.Step; } break; - // вправо - case Direction.Right: - if (_startPosX + _CatamaranWidth + EntityCatamaran.Step < _pictureWidth ) + case DirectionType.Right: + if (_startPosX + EntityCatamaran.Step + _CatamaranWidth < _pictureWidth) { _startPosX += (int)EntityCatamaran.Step; } break; - //вниз - case Direction.Down: - if (_startPosY + _CatamaranHeight + EntityCatamaran.Step < _pictureHeight) + case DirectionType.Down: + if (_startPosY + EntityCatamaran.Step + _CatamaranHeight < _pictureHeight) { _startPosY += (int)EntityCatamaran.Step; } @@ -128,43 +191,15 @@ namespace Catamaran /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { if (EntityCatamaran == null) { return; } Pen pen = new(Color.Black, 3); - Brush additionalBrush = new - SolidBrush(EntityCatamaran.AdditionalColor); Brush bodyBrush = new - SolidBrush(EntityCatamaran.BodyColor); - - //поплавки - if (EntityCatamaran.Float) - { - - Point[] floatDetail1 = new Point[] - { - new Point(_startPosX, _startPosY + 40), - new Point(_startPosX + 170, _startPosY + 40), - new Point(_startPosX + 190, _startPosY + 55), - new Point(_startPosX + 170, _startPosY + 70), - new Point(_startPosX, _startPosY + 70), - }; - Point[] floatDetail2 = new Point[] - { - new Point(_startPosX, _startPosY + 90), - new Point(_startPosX + 170, _startPosY + 90), - new Point(_startPosX + 190, _startPosY + 105), - new Point(_startPosX + 170, _startPosY + 120), - new Point(_startPosX, _startPosY + 120), - }; - g.FillPolygon(additionalBrush, floatDetail1); - g.FillPolygon(additionalBrush, floatDetail2); - g.DrawPolygon(pen, floatDetail1); - g.DrawPolygon(pen, floatDetail2); - } + SolidBrush(EntityCatamaran.BodyColor); //основание катамарана Point[] hull = new Point[] { @@ -178,23 +213,7 @@ namespace Catamaran g.FillPolygon(bodyBrush, hull); g.DrawPolygon(pen, hull); SolidBrush brushBrown = new SolidBrush(Color.Brown); - g.FillEllipse(brushBrown, _startPosX + 30, _startPosY + 60, 100, 40); - - //парус - if (EntityCatamaran.Sail) - { - Point[] sail = new Point[] - { - new Point(_startPosX + 30, _startPosY + 80), - new Point(_startPosX + 70, _startPosY+10), - new Point(_startPosX + 145, _startPosY + 80), - - }; - g.DrawPolygon(pen, sail); - g.FillPolygon(additionalBrush, sail); - g.DrawPolygon(pen, sail); - g.DrawRectangle(pen, _startPosX + 70, _startPosY, 3, 85); - } + g.FillEllipse(brushBrown, _startPosX + 30, _startPosY + 60, 100, 40); } } } diff --git a/base/Catamaran/Catamaran/DrawningObjectCatamaran.cs b/base/Catamaran/Catamaran/DrawningObjectCatamaran.cs new file mode 100644 index 0000000..2b2f3fe --- /dev/null +++ b/base/Catamaran/Catamaran/DrawningObjectCatamaran.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Catamaran.DrawningObjects; + +namespace Catamaran.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCatamaran (паттерн Adapter) + /// + public class DrawningObjectCatamaran : IMoveableObject + { + private readonly DrawningCatamaran? _drawningCatamaran = null; + public DrawningObjectCatamaran(DrawningCatamaran drawningCatamaran) + { + _drawningCatamaran = drawningCatamaran; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningCatamaran == null || _drawningCatamaran.EntityCatamaran == + null) + { + return null; + } + return new ObjectParameters(_drawningCatamaran.GetPosX, + _drawningCatamaran.GetPosY, _drawningCatamaran.GetWidth, _drawningCatamaran.GetHeight); + } + } + public int GetStep => (int)(_drawningCatamaran?.EntityCatamaran?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawningCatamaran?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawningCatamaran?.MoveTransport(direction); + } +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/DrawningSailCatamaran.cs b/base/Catamaran/Catamaran/DrawningSailCatamaran.cs new file mode 100644 index 0000000..f804fbf --- /dev/null +++ b/base/Catamaran/Catamaran/DrawningSailCatamaran.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Catamaran.Entities; + +namespace Catamaran.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningSailCatamaran : DrawningCatamaran + { + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия паруса + /// Признак наличия поплавков + /// Ширина картинки + /// Высота картинки + public DrawningSailCatamaran(int speed, double weight, Color bodyColor, Color + additionalColor, bool sail, bool floatDetail, int width, int height) : + base(speed, weight, bodyColor, width, height, 190, 120) + { + if (EntityCatamaran != null) + { + EntityCatamaran = new EntitySailCatamaran(speed, weight, bodyColor, + additionalColor, sail, floatDetail); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityCatamaran is not EntitySailCatamaran sailCatamaran) + { + return; + } + Pen pen = new(Color.Black, 3); + Brush additionalBrush = new + SolidBrush(sailCatamaran.AdditionalColor); + + //поплавки + if (sailCatamaran.FloatDetail) + { + Point[] floatDetail1 = new Point[] + { + new Point(_startPosX, _startPosY + 40), + new Point(_startPosX + 170, _startPosY + 40), + new Point(_startPosX + 190, _startPosY + 55), + new Point(_startPosX + 170, _startPosY + 70), + new Point(_startPosX, _startPosY + 70), + }; + Point[] floatDetail2 = new Point[] + { + new Point(_startPosX, _startPosY + 90), + new Point(_startPosX + 170, _startPosY + 90), + new Point(_startPosX + 190, _startPosY + 105), + new Point(_startPosX + 170, _startPosY + 120), + new Point(_startPosX, _startPosY + 120), + }; + g.FillPolygon(additionalBrush, floatDetail1); + g.FillPolygon(additionalBrush, floatDetail2); + g.DrawPolygon(pen, floatDetail1); + g.DrawPolygon(pen, floatDetail2); + } + base.DrawTransport(g); + + //парус + if (sailCatamaran.Sail) + { + Point[] sail = new Point[] + { + new Point(_startPosX + 30, _startPosY + 80), + new Point(_startPosX + 70, _startPosY+10), + new Point(_startPosX + 145, _startPosY + 80), + }; + g.DrawPolygon(pen, sail); + g.FillPolygon(additionalBrush, sail); + g.DrawPolygon(pen, sail); + g.DrawRectangle(pen, _startPosX + 70, _startPosY, 3, 85); + } + } + } +} + \ No newline at end of file diff --git a/base/Catamaran/Catamaran/EntityCatamaran.cs b/base/Catamaran/Catamaran/EntityCatamaran.cs index 8b02403..7f23253 100644 --- a/base/Catamaran/Catamaran/EntityCatamaran.cs +++ b/base/Catamaran/Catamaran/EntityCatamaran.cs @@ -4,9 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Catamaran +namespace Catamaran.Entities { - internal class EntityCatamaran + /// + /// Класс-сущность "Катамаран" + /// + public class EntityCatamaran { /// /// Скорость @@ -21,39 +24,21 @@ namespace Catamaran /// public Color BodyColor { get; private set; } /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor { get; private set; } - /// - /// Признак (опция) наличия паруса - /// - public bool Sail { get; private set; } - /// - /// Признак (опция) наличия поплавков - /// - public bool Float { get; private set; } - /// /// Шаг перемещения катамарана /// public double Step => (double)Speed * 100 / Weight; /// - /// Инициализация полей объекта-класса катамарана + /// Конструктор с параметрами /// /// Скорость /// Вес автомобиля /// Основной цвет - /// Дополнительный цвет - /// Признак наличия паруса - /// Признак наличия поплавков - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool sail, bool floatDetail) + public EntityCatamaran(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; - AdditionalColor = additionalColor; - Sail = sail; - Float = floatDetail; } } } + diff --git a/base/Catamaran/Catamaran/EntitySailCatamaran.cs b/base/Catamaran/Catamaran/EntitySailCatamaran.cs new file mode 100644 index 0000000..4612495 --- /dev/null +++ b/base/Catamaran/Catamaran/EntitySailCatamaran.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.Entities +{ + /// + /// Класс-сущность "Катамаран с парусом" + /// + public class EntitySailCatamaran : EntityCatamaran + { + /// + /// Дополнительный цвет (для опциональных элементов) + /// + public Color AdditionalColor { get; private set; } + /// + /// Признак (опция) наличия паруса + /// + public bool Sail { get; private set; } + /// + /// Признак (опция) наличия поплавков + /// + public bool FloatDetail { get; private set; } + /// + /// Инициализация полей объекта-класса катамарана с парусом + /// + /// Скорость + /// Вес катамарана + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия паруса + /// Признак наличия поплавков + public EntitySailCatamaran(int speed, double weight, Color bodyColor, Color + additionalColor, bool sail, bool floatDetail) : base (speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + Sail = sail; + FloatDetail = floatDetail; + } + } +} diff --git a/base/Catamaran/Catamaran/IMoveableObject.cs b/base/Catamaran/Catamaran/IMoveableObject.cs new file mode 100644 index 0000000..b765eda --- /dev/null +++ b/base/Catamaran/Catamaran/IMoveableObject.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Catamaran.DrawningObjects; +namespace Catamaran.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/base/Catamaran/Catamaran/MoveToBorder.cs b/base/Catamaran/Catamaran/MoveToBorder.cs new file mode 100644 index 0000000..6677969 --- /dev/null +++ b/base/Catamaran/Catamaran/MoveToBorder.cs @@ -0,0 +1,57 @@ +using Catamaran.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/MoveToCenter.cs b/base/Catamaran/Catamaran/MoveToCenter.cs new file mode 100644 index 0000000..546712b --- /dev/null +++ b/base/Catamaran/Catamaran/MoveToCenter.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.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(); + } + } + } + } +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/ObjectParameters.cs b/base/Catamaran/Catamaran/ObjectParameters.cs new file mode 100644 index 0000000..9dd4dcb --- /dev/null +++ b/base/Catamaran/Catamaran/ObjectParameters.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/base/Catamaran/Catamaran/Program.cs b/base/Catamaran/Catamaran/Program.cs index 7bbda1a..cad1371 100644 --- a/base/Catamaran/Catamaran/Program.cs +++ b/base/Catamaran/Catamaran/Program.cs @@ -11,7 +11,7 @@ namespace Catamaran // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Catamaran()); + Application.Run(new FormCatamaran()); } } } \ No newline at end of file diff --git a/base/Catamaran/Catamaran/Status.cs b/base/Catamaran/Catamaran/Status.cs new file mode 100644 index 0000000..64ad0ad --- /dev/null +++ b/base/Catamaran/Catamaran/Status.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +}