From 72e2df80a7933a8acf0fecf212b3f00849a35081 Mon Sep 17 00:00:00 2001 From: chtzsch ~ Date: Wed, 20 Sep 2023 13:54:36 +0300 Subject: [PATCH] lab_02 --- speed_Boat/speed_Boat/AbstractStrategy.cs | 132 ++++++++++++++ speed_Boat/speed_Boat/Direction.cs | 2 +- speed_Boat/speed_Boat/DrawingObjectBoat.cs | 38 +++++ speed_Boat/speed_Boat/DrawingSpeedBoat.cs | 57 +++++++ .../{SpeedBoat.cs => EntityBoat.cs} | 27 +-- speed_Boat/speed_Boat/EntitySpeedboat.cs | 41 +++++ .../speed_Boat/FormSpeedBoat.Designer.cs | 49 +++++- speed_Boat/speed_Boat/FormSpeedBoat.cs | 94 ++++++++-- speed_Boat/speed_Boat/IMovementObject.cs | 26 +++ speed_Boat/speed_Boat/MoveToBorder.cs | 44 +++++ speed_Boat/speed_Boat/MoveToCenter.cs | 59 +++++++ speed_Boat/speed_Boat/ObjectParameters.cs | 53 ++++++ speed_Boat/speed_Boat/SpeedBoatMovement.cs | 161 ++++++++++++------ speed_Boat/speed_Boat/Status.cs | 15 ++ 14 files changed, 706 insertions(+), 92 deletions(-) create mode 100644 speed_Boat/speed_Boat/AbstractStrategy.cs create mode 100644 speed_Boat/speed_Boat/DrawingObjectBoat.cs create mode 100644 speed_Boat/speed_Boat/DrawingSpeedBoat.cs rename speed_Boat/speed_Boat/{SpeedBoat.cs => EntityBoat.cs} (53%) create mode 100644 speed_Boat/speed_Boat/EntitySpeedboat.cs create mode 100644 speed_Boat/speed_Boat/IMovementObject.cs create mode 100644 speed_Boat/speed_Boat/MoveToBorder.cs create mode 100644 speed_Boat/speed_Boat/MoveToCenter.cs create mode 100644 speed_Boat/speed_Boat/ObjectParameters.cs create mode 100644 speed_Boat/speed_Boat/Status.cs diff --git a/speed_Boat/speed_Boat/AbstractStrategy.cs b/speed_Boat/speed_Boat/AbstractStrategy.cs new file mode 100644 index 0000000..59b56cb --- /dev/null +++ b/speed_Boat/speed_Boat/AbstractStrategy.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SpeedBoatLab.Drawings; +using SpeedBoatLab.Entity; + +namespace speed_Boat.MovementStrategy +{ + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMovementObject? _movementObject; + /// + /// Статус перемещения + /// + 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(IMovementObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _movementObject = 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 => _movementObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _movementObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_movementObject?.CheckCanMove(directionType) ?? false) + { + _movementObject.MoveObject(directionType); + return true; + } + return false; + } + + } +} diff --git a/speed_Boat/speed_Boat/Direction.cs b/speed_Boat/speed_Boat/Direction.cs index ee9bc9e..5841103 100644 --- a/speed_Boat/speed_Boat/Direction.cs +++ b/speed_Boat/speed_Boat/Direction.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace SpeedBoatLab +namespace SpeedBoatLab.Drawings { /// /// Направление перемещения diff --git a/speed_Boat/speed_Boat/DrawingObjectBoat.cs b/speed_Boat/speed_Boat/DrawingObjectBoat.cs new file mode 100644 index 0000000..ff455d6 --- /dev/null +++ b/speed_Boat/speed_Boat/DrawingObjectBoat.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SpeedBoatLab.Entity; +using SpeedBoatLab.Drawings; + +namespace speed_Boat.MovementStrategy +{ + class DrawingObjectBoat : IMovementObject + { + private readonly DrawingBoat? _drawingBoat = null; + + public DrawingObjectBoat(DrawingBoat drawingBoat) + { + _drawingBoat = drawingBoat; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if(_drawingBoat == null || _drawingBoat._entityBoat == null) + { + return null; + } + return new ObjectParameters(_drawingBoat.GetPoseX, _drawingBoat.GetPoseY, _drawingBoat.GetWidth, _drawingBoat.GetHeight); + } + } + + public int GetStep => (int)(_drawingBoat?._entityBoat?.Step ?? 0); + + public bool CheckCanMove(DirectionType direction) => _drawingBoat?.CanMove(direction) ?? false; + + public void MoveObject(DirectionType direction) => _drawingBoat?.MoveBoat(direction); + } +} diff --git a/speed_Boat/speed_Boat/DrawingSpeedBoat.cs b/speed_Boat/speed_Boat/DrawingSpeedBoat.cs new file mode 100644 index 0000000..dc93f62 --- /dev/null +++ b/speed_Boat/speed_Boat/DrawingSpeedBoat.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SpeedBoatLab.Entity; +using System.Drawing; + +namespace SpeedBoatLab.Drawings +{ + public class DrawingSpeedBoat : DrawingBoat + { + public DrawingSpeedBoat(int speed, double weight, Color mainColor, Color secondColor, bool _isMotor, bool _isProtectedGlass, int width, int height) : + base(speed, weight, mainColor, width, height, 100, 80) + { + if (_entityBoat != null) + { + _entityBoat = new EntitySpeedboat(speed, weight, mainColor, secondColor, _isMotor, _isProtectedGlass); + } + } + public override void DrawTransport(Graphics g) + { + if (_entityBoat is not EntitySpeedboat speedBoat) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(speedBoat.SecondColor); + + #region Координаты защитного стекла + Point g1 = new Point(startXCoord + 70, startYCoord + 25); + Point g2 = new Point(startXCoord + 80, startYCoord + 20); + Point g3 = new Point(startXCoord + 80, startYCoord + 60); + Point g4 = new Point(startXCoord + 70, startYCoord + 55); + Point[] pointsGlass = { g1, g2, g3, g4 }; + #endregion + + //мотор + if (speedBoat.isMotor) + { + g.DrawRectangle(pen, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60); + + g.FillRectangle(additionalBrush, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60); + } + + //защитное стекло + if (speedBoat.isProtectedGlass) + { + g.DrawPolygon(pen, pointsGlass); + + g.FillPolygon(new SolidBrush(Color.Aqua), pointsGlass); + } + + base.DrawTransport(g); + } + } +} diff --git a/speed_Boat/speed_Boat/SpeedBoat.cs b/speed_Boat/speed_Boat/EntityBoat.cs similarity index 53% rename from speed_Boat/speed_Boat/SpeedBoat.cs rename to speed_Boat/speed_Boat/EntityBoat.cs index 8f46029..bf44246 100644 --- a/speed_Boat/speed_Boat/SpeedBoat.cs +++ b/speed_Boat/speed_Boat/EntityBoat.cs @@ -5,9 +5,12 @@ using System.Text; using System.Threading.Tasks; using System.Drawing; -namespace SpeedBoatLab +namespace SpeedBoatLab.Entity { - public class SpeedBoat + /// + /// Катер + /// + public class EntityBoat { /// /// Скорость катера @@ -19,39 +22,21 @@ namespace SpeedBoatLab /// public double Weight { get; private set; } - /// - /// Наличие мотора - /// - public bool isMotor { get; private set; } - - /// - /// Наличие защитного стекла - /// - public bool isProtectedGlass { get; private set; } - /// /// Основной цвет /// public Color MainColor { get; private set; } - /// - /// Доп. цвет - /// - public Color SecondColor { get; private set; } - /// /// Шаг перемещения катера /// public double Step => (double)Speed * 100 / (Weight); - public void Init(int speed, double weight, bool _isMotor, bool _isProtectedGlass, Color mainColor, Color secondColor) + public EntityBoat(int speed, double weight, Color mainColor) { Speed = speed; Weight = weight; - isMotor = _isMotor; - isProtectedGlass = _isProtectedGlass; MainColor = mainColor; - SecondColor = secondColor; } } diff --git a/speed_Boat/speed_Boat/EntitySpeedboat.cs b/speed_Boat/speed_Boat/EntitySpeedboat.cs new file mode 100644 index 0000000..b4c5052 --- /dev/null +++ b/speed_Boat/speed_Boat/EntitySpeedboat.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace SpeedBoatLab.Entity +{ + /// + /// Класс-сущность скоростного катера + /// + public class EntitySpeedboat : EntityBoat + { + /// + /// Наличие мотора + /// + public bool isMotor { get; private set; } + + /// + /// Наличие защитного стекла + /// + public bool isProtectedGlass { get; private set; } + + /// + /// Доп. цвет + /// + public Color SecondColor { get; private set; } + + /// + /// Параметры катера + /// + public EntitySpeedboat(int speed, double weight, Color mainColor, Color secondColor, bool _isMotor, bool _isProtectedGlass) : + base(speed, weight, mainColor) + { + isMotor = _isMotor; + isProtectedGlass = _isProtectedGlass; + SecondColor = secondColor; + } + } +} diff --git a/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs b/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs index ce07184..71530d4 100644 --- a/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs +++ b/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs @@ -35,6 +35,9 @@ namespace SpeedBoatLab this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); + this.comboBox1 = new System.Windows.Forms.ComboBox(); + this.StepButton = new System.Windows.Forms.Button(); + this.buttonCreateSpeedBoat = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxSpeedBoat)).BeginInit(); this.SuspendLayout(); // @@ -56,7 +59,7 @@ namespace SpeedBoatLab this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Size = new System.Drawing.Size(103, 51); this.buttonCreate.TabIndex = 2; - this.buttonCreate.Text = "Создать обьект"; + this.buttonCreate.Text = "Создать катер"; this.buttonCreate.UseVisualStyleBackColor = false; this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); // @@ -112,11 +115,52 @@ namespace SpeedBoatLab this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); // + // comboBox1 + // + this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBox1.FormattingEnabled = true; + this.comboBox1.Items.AddRange(new object[] { + "MoveToCenter", + "MoveToBorder"}); + this.comboBox1.Location = new System.Drawing.Point(637, 12); + this.comboBox1.Name = "comboBox1"; + this.comboBox1.Size = new System.Drawing.Size(151, 28); + this.comboBox1.TabIndex = 11; + // + // StepButton + // + this.StepButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.StepButton.BackColor = System.Drawing.Color.White; + this.StepButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.StepButton.Location = new System.Drawing.Point(731, 46); + this.StepButton.Name = "StepButton"; + this.StepButton.Size = new System.Drawing.Size(57, 33); + this.StepButton.TabIndex = 12; + this.StepButton.Text = "Шаг"; + this.StepButton.UseVisualStyleBackColor = false; + this.StepButton.Click += new System.EventHandler(this.StepButton_Click); + // + // buttonCreateSpeedBoat + // + this.buttonCreateSpeedBoat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateSpeedBoat.BackColor = System.Drawing.Color.White; + this.buttonCreateSpeedBoat.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.buttonCreateSpeedBoat.Location = new System.Drawing.Point(121, 387); + this.buttonCreateSpeedBoat.Name = "buttonCreateSpeedBoat"; + this.buttonCreateSpeedBoat.Size = new System.Drawing.Size(143, 51); + this.buttonCreateSpeedBoat.TabIndex = 13; + this.buttonCreateSpeedBoat.Text = "Создать скоростной катер"; + this.buttonCreateSpeedBoat.UseVisualStyleBackColor = false; + this.buttonCreateSpeedBoat.Click += new System.EventHandler(this.buttonCreateSpeedBoat_Click); + // // FormSpeedBoat // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonCreateSpeedBoat); + this.Controls.Add(this.StepButton); + this.Controls.Add(this.comboBox1); this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonDown); @@ -138,6 +182,9 @@ namespace SpeedBoatLab private System.Windows.Forms.Button buttonLeft; private System.Windows.Forms.Button buttonDown; private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.ComboBox comboBox1; + private System.Windows.Forms.Button StepButton; + private System.Windows.Forms.Button buttonCreateSpeedBoat; } } diff --git a/speed_Boat/speed_Boat/FormSpeedBoat.cs b/speed_Boat/speed_Boat/FormSpeedBoat.cs index 7239e35..9cd4248 100644 --- a/speed_Boat/speed_Boat/FormSpeedBoat.cs +++ b/speed_Boat/speed_Boat/FormSpeedBoat.cs @@ -7,6 +7,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using SpeedBoatLab.Drawings; +using SpeedBoatLab.Entity; +using speed_Boat.MovementStrategy; namespace SpeedBoatLab { @@ -15,25 +18,30 @@ namespace SpeedBoatLab /// /// Поле-объект для прорисовки объекта /// - private SpeedBoatMovement? _tankerMovement; + private DrawingBoat? _boatMovement; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _abstractStrategy; + public FormSpeedBoat() { InitializeComponent(); } /// - /// Метод прорисовки машины + /// Метод прорисовки лодки /// private void Draw() { - if (_tankerMovement == null) + if (_boatMovement == null) { return; } Bitmap bmp = new(pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height); Graphics gr = Graphics.FromImage(bmp); - _tankerMovement.DrawTransport(gr); + _boatMovement.DrawTransport(gr); pictureBoxSpeedBoat.Image = bmp; } @@ -43,18 +51,33 @@ namespace SpeedBoatLab private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); - _tankerMovement = new SpeedBoatMovement(); - - _tankerMovement.Init(random.Next(100, 300), + _boatMovement = new DrawingBoat(random.Next(100, 300), random.Next(1000, 3000), - Convert.ToBoolean(random.Next(0, 2)), - Convert.ToBoolean(random.Next(0, 2)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height); //startXCoord and startYCoord - _tankerMovement.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _boatMovement.SetPosition(random.Next(10, 100), random.Next(10, 100)); + + Draw(); + } + + /// + /// Обработка создания скоростного обьекта + /// + private void buttonCreateSpeedBoat_Click(object sender, EventArgs e) + { + Random random = new(); + _boatMovement = new DrawingSpeedBoat(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)), + pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height); + + //startXCoord and startYCoord + _boatMovement.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -64,7 +87,7 @@ namespace SpeedBoatLab /// private void buttonMove_Click(object sender, EventArgs e) { - if (_tankerMovement == null) + if (_boatMovement == null) { return; } @@ -72,19 +95,56 @@ namespace SpeedBoatLab switch (name) { case "buttonUp": - _tankerMovement.MoveTransport(DirectionType.Up); + _boatMovement.MoveTransport(DirectionType.Up); break; case "buttonDown": - _tankerMovement.MoveTransport(DirectionType.Down); + _boatMovement.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _tankerMovement.MoveTransport(DirectionType.Left); + _boatMovement.MoveTransport(DirectionType.Left); break; case "buttonRight": - _tankerMovement.MoveTransport(DirectionType.Right); + _boatMovement.MoveTransport(DirectionType.Right); break; } Draw(); } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + private void StepButton_Click(object sender, EventArgs e) + { + if(_boatMovement == null) + { + return; + } + if(comboBox1.Enabled) + { + _abstractStrategy = comboBox1.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null + }; + if(_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawingObjectBoat(_boatMovement), pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height); + comboBox1.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if(_abstractStrategy.GetStatus() == Status.Finish) + { + comboBox1.Enabled = true; + _abstractStrategy = null; + } + } } } diff --git a/speed_Boat/speed_Boat/IMovementObject.cs b/speed_Boat/speed_Boat/IMovementObject.cs new file mode 100644 index 0000000..88899ec --- /dev/null +++ b/speed_Boat/speed_Boat/IMovementObject.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SpeedBoatLab.Drawings; + +namespace speed_Boat.MovementStrategy +{ + public interface IMovementObject + { + //Получение координаты Х обьекта + ObjectParameters? GetObjectPosition { get; } + + //шаг обьекта + int GetStep { get; } + + //проверка на перемещение + bool CheckCanMove(DirectionType direction); + + //изменение направления перемещения обьекта + void MoveObject(DirectionType direction); + } + + +} diff --git a/speed_Boat/speed_Boat/MoveToBorder.cs b/speed_Boat/speed_Boat/MoveToBorder.cs new file mode 100644 index 0000000..1d56658 --- /dev/null +++ b/speed_Boat/speed_Boat/MoveToBorder.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace speed_Boat.MovementStrategy +{ + class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + //return objParams.RightBorder == FieldWidth && objParams.DownBorder == FieldHeight; + 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 = FieldWidth - objParams.RightBorder; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + var diffY = FieldHeight - objParams.DownBorder; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } + } +} diff --git a/speed_Boat/speed_Boat/MoveToCenter.cs b/speed_Boat/speed_Boat/MoveToCenter.cs new file mode 100644 index 0000000..f7eca63 --- /dev/null +++ b/speed_Boat/speed_Boat/MoveToCenter.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SpeedBoatLab.Drawings; +using SpeedBoatLab.Entity; + +namespace speed_Boat.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/speed_Boat/speed_Boat/ObjectParameters.cs b/speed_Boat/speed_Boat/ObjectParameters.cs new file mode 100644 index 0000000..0d127d8 --- /dev/null +++ b/speed_Boat/speed_Boat/ObjectParameters.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace speed_Boat.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 ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + + } +} diff --git a/speed_Boat/speed_Boat/SpeedBoatMovement.cs b/speed_Boat/speed_Boat/SpeedBoatMovement.cs index 10de65e..9cf2918 100644 --- a/speed_Boat/speed_Boat/SpeedBoatMovement.cs +++ b/speed_Boat/speed_Boat/SpeedBoatMovement.cs @@ -5,16 +5,17 @@ using System.Text; using System.Threading.Tasks; using SpeedBoatLab; using System.Drawing; +using SpeedBoatLab.Entity; -namespace SpeedBoatLab +namespace SpeedBoatLab.Drawings { - class SpeedBoatMovement + public class DrawingBoat { /// /// Класс-сущность /// - public SpeedBoat? _speedBoat { get; private set; } + public EntityBoat? _entityBoat { get; protected set; } /// /// Ширина окна @@ -28,27 +29,92 @@ namespace SpeedBoatLab /// /// Х-координата обьекта /// - private int startXCoord; + protected int startXCoord; /// /// Y-координата обьекта /// - private int startYCoord; + protected int startYCoord; /// /// Ширина обьекта /// - private readonly int widthBoat = 100; + protected readonly int widthBoat = 100; /// /// Высота обьекта /// - private readonly int heightBoat = 80; + protected readonly int heightBoat = 80; - public bool Init(int speed, double weight, bool _isMotor, bool _isProtectedGlass, Color mainColor, Color secondColor, int width, int height) + /// + /// Х-координата обьекта + /// + public int GetPoseX => startXCoord; + /// + /// Y-координата обьекта + /// + public int GetPoseY => startYCoord; + + /// + /// Ширина обьекта + /// + public int GetWidth => widthBoat; + /// + /// Высота обьекта + /// + public int GetHeight => heightBoat; + + public bool CanMove(DirectionType direction) + { + if (_entityBoat == null) + { + return false; + } + return direction switch + { + //Left + DirectionType.Left => startXCoord - _entityBoat.Step > 0, + //Up + DirectionType.Up => startYCoord - _entityBoat.Step > 0, + //Down + DirectionType.Down => startYCoord + _entityBoat.Step < screenHeight, + //Right + DirectionType.Right => startXCoord + _entityBoat.Step < screenWidth + }; + } + + //Изменение направления перемещения + public void MoveTransport(DirectionType direction) + { + if(!CanMove(direction) || _entityBoat == null) + { + return; + } + switch(direction) + { + case DirectionType.Left: + startXCoord -= (int)_entityBoat.Step; + break; + case DirectionType.Up: + startYCoord -= (int)_entityBoat.Step; + break; + case DirectionType.Right: + startXCoord += (int)_entityBoat.Step; + break; + case DirectionType.Down: + startYCoord += (int)_entityBoat.Step; + break; + } + } + + + + /// + /// конструктор + /// + public DrawingBoat(int speed, double weight, Color mainColor, int width, int height) { screenWidth = width; screenHeight = height; - _speedBoat = new SpeedBoat(); - _speedBoat.Init(speed, weight, _isMotor, _isProtectedGlass, mainColor, secondColor); + _entityBoat = new EntityBoat(speed, weight, mainColor); /// /// Проверка на вместимость обьекта в рамки сцены @@ -56,13 +122,30 @@ namespace SpeedBoatLab if ((widthBoat >= screenWidth) || (heightBoat >= screenHeight)) { Console.WriteLine("проверка не пройдена, нельзя создать объект в этих размерах"); - return false; + if(widthBoat >= screenWidth) + { + widthBoat = screenWidth - widthBoat; + } + if (heightBoat >= screenWidth) + { + heightBoat = screenWidth - heightBoat; + } } else Console.WriteLine("объект создан"); - return true; } + /// + /// конструктор + /// + protected DrawingBoat(int speed, double weight, Color mainColor, int width, int height, int _widthBoat, int _heightBoat) + { + screenWidth = width; + screenHeight = height; + widthBoat = _widthBoat; + heightBoat = _heightBoat; + _entityBoat = new EntityBoat(speed, weight, mainColor); + } /// /// Установка позиции /// @@ -83,9 +166,9 @@ namespace SpeedBoatLab /// /// Изменение направления перемещения /// - public void MoveTransport(DirectionType direction) + public void MoveBoat(DirectionType direction) { - if (_speedBoat == null) + if (_entityBoat == null) { return; } @@ -94,30 +177,30 @@ namespace SpeedBoatLab { //влево case DirectionType.Left: - if (startXCoord - _speedBoat.Step > 0) + if (startXCoord - _entityBoat.Step > 0) { - startXCoord -= (int)_speedBoat.Step; + startXCoord -= (int)_entityBoat.Step; } break; //вверх case DirectionType.Up: - if (startYCoord - _speedBoat.Step > 0) + if (startYCoord - _entityBoat.Step > 0) { - startYCoord -= (int)_speedBoat.Step; + startYCoord -= (int)_entityBoat.Step; } break; // вправо case DirectionType.Right: - if (startXCoord + _speedBoat.Step + widthBoat < screenWidth) + if (startXCoord + _entityBoat.Step + widthBoat < screenWidth) { - startXCoord += (int)_speedBoat.Step; + startXCoord += (int)_entityBoat.Step; } break; //вниз case DirectionType.Down: - if (startYCoord + _speedBoat.Step + heightBoat < screenHeight) + if (startYCoord + _entityBoat.Step + heightBoat < screenHeight) { - startYCoord += (int)_speedBoat.Step; + startYCoord += (int)_entityBoat.Step; } break; } @@ -126,16 +209,15 @@ namespace SpeedBoatLab /// /// Прорисовка объекта /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (_speedBoat == null) + if (_entityBoat == null) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(_speedBoat.SecondColor); - Brush mainBrush = new SolidBrush(_speedBoat.MainColor); + Brush mainBrush = new SolidBrush(_entityBoat.MainColor); #region Координаты переда лодки Point b1 = new Point(startXCoord + 80, startYCoord + 20); @@ -144,39 +226,14 @@ namespace SpeedBoatLab Point[] pointsBoat = { b1, b2, b3 }; #endregion - #region Координаты защитного стекла - Point g1 = new Point(startXCoord + 70, startYCoord + 25); - Point g2 = new Point(startXCoord + 80, startYCoord + 20); - Point g3 = new Point(startXCoord + 80, startYCoord + 60); - Point g4 = new Point(startXCoord + 70, startYCoord + 55); - Point[] pointsGlass = { g1, g2, g3, g4 }; - #endregion - //основа катера g.DrawRectangle(pen, startXCoord + 20, startYCoord + 20, widthBoat - 40, heightBoat - 40); g.DrawEllipse(pen, startXCoord + 25, startYCoord + 25, widthBoat - 50, heightBoat - 50); g.DrawPolygon(pen, pointsBoat); g.FillRectangle(mainBrush, startXCoord + 20, startYCoord + 20, widthBoat - 40, heightBoat - 40); - g.FillEllipse(additionalBrush, startXCoord + 25, startYCoord + 25, widthBoat - 50, heightBoat - 50); + g.FillEllipse(mainBrush, startXCoord + 25, startYCoord + 25, widthBoat - 50, heightBoat - 50); g.FillPolygon(mainBrush, pointsBoat); - - - //мотор - if (_speedBoat.isMotor) - { - g.DrawRectangle(pen, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60); - - g.FillRectangle(additionalBrush, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60); - } - - //защитное стекло - if (_speedBoat.isProtectedGlass) - { - g.DrawPolygon(pen, pointsGlass); - - g.FillPolygon(new SolidBrush(Color.Aqua), pointsGlass); - } } } } diff --git a/speed_Boat/speed_Boat/Status.cs b/speed_Boat/speed_Boat/Status.cs new file mode 100644 index 0000000..1fc6088 --- /dev/null +++ b/speed_Boat/speed_Boat/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace speed_Boat.MovementStrategy +{ + public enum Status + { + NotInit = 1, + InProgress = 2, + Finish = 3 + } +}