From 1228318ad1b5c5be13f0c7115de8d9a4ef8f8b77 Mon Sep 17 00:00:00 2001 From: urlilpolly Date: Sat, 2 Dec 2023 15:44:28 +0400 Subject: [PATCH] laba2 --- RPP/RPP/AbstractStrategy.cs | 71 ++++++++++++++++++++ RPP/RPP/Airbus.Designer.cs | 82 +++++++++++++++++------ RPP/RPP/Airbus.cs | 81 ++++++++++++++++++++--- RPP/RPP/Drawning.cs | 91 ++++++++++++++------------ RPP/RPP/DrawningFlyAirbus.cs | 59 +++++++++++++++++ RPP/RPP/DrawningObjectAirbus.cs | 36 ++++++++++ RPP/RPP/{Entity.cs => EntityAirbus.cs} | 17 ++--- RPP/RPP/EntityFlyAirbus.cs | 26 ++++++++ RPP/RPP/IMoveableObject.cs | 21 ++++++ RPP/RPP/MoveToBorder.cs | 48 ++++++++++++++ RPP/RPP/MoveToCenter.cs | 56 ++++++++++++++++ RPP/RPP/ObjectParameters.cs | 36 ++++++++++ RPP/RPP/{dop.cs => Status.cs} | 10 ++- 13 files changed, 544 insertions(+), 90 deletions(-) create mode 100644 RPP/RPP/AbstractStrategy.cs create mode 100644 RPP/RPP/DrawningFlyAirbus.cs create mode 100644 RPP/RPP/DrawningObjectAirbus.cs rename RPP/RPP/{Entity.cs => EntityAirbus.cs} (53%) create mode 100644 RPP/RPP/EntityFlyAirbus.cs create mode 100644 RPP/RPP/IMoveableObject.cs create mode 100644 RPP/RPP/MoveToBorder.cs create mode 100644 RPP/RPP/MoveToCenter.cs create mode 100644 RPP/RPP/ObjectParameters.cs rename RPP/RPP/{dop.cs => Status.cs} (50%) diff --git a/RPP/RPP/AbstractStrategy.cs b/RPP/RPP/AbstractStrategy.cs new file mode 100644 index 0000000..0a62692 --- /dev/null +++ b/RPP/RPP/AbstractStrategy.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RPP.MovementStrategy +{ + internal 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(); + } + protected bool MoveLeft() => MoveTo(Direction.Left); + protected bool MoveRight() => MoveTo(Direction.Right); + protected bool MoveUp() => MoveTo(Direction.Up); + protected bool MoveDown() => MoveTo(Direction.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(); + private bool MoveTo(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/RPP/RPP/Airbus.Designer.cs b/RPP/RPP/Airbus.Designer.cs index f47a627..688ed36 100644 --- a/RPP/RPP/Airbus.Designer.cs +++ b/RPP/RPP/Airbus.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBoxAirbus = new PictureBox(); - buttonCreate = new Button(); + buttonCreateAirbus = new Button(); buttonDown = new Button(); buttonLeft = new Button(); buttonUp = new Button(); buttonRight = new Button(); + comboBoxStrategy = new ComboBox(); + buttonCreateFlyAirbus = new Button(); + ButtonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirbus).BeginInit(); SuspendLayout(); // @@ -50,27 +53,27 @@ pictureBoxAirbus.TabStop = false; pictureBoxAirbus.Click += buttonMove_Click; // - // buttonCreate + // buttonCreateAirbus // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(58, 493); - buttonCreate.Margin = new Padding(3, 4, 3, 4); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(86, 31); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreateAirbus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateAirbus.Location = new Point(224, 456); + buttonCreateAirbus.Margin = new Padding(3, 4, 3, 4); + buttonCreateAirbus.Name = "buttonCreateAirbus"; + buttonCreateAirbus.Size = new Size(166, 80); + buttonCreateAirbus.TabIndex = 1; + buttonCreateAirbus.Text = "Создать аэробус"; + buttonCreateAirbus.UseVisualStyleBackColor = true; + buttonCreateAirbus.Click += ButtonCreateAirbus_Click; // // buttonDown // buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.buttonDown; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(883, 494); + buttonDown.Location = new Point(883, 493); buttonDown.Margin = new Padding(3, 4, 3, 4); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(30, 30); + buttonDown.Size = new Size(30, 29); buttonDown.TabIndex = 2; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += buttonMove_Click; @@ -80,10 +83,10 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.buttonLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(847, 494); + buttonLeft.Location = new Point(847, 493); buttonLeft.Margin = new Padding(3, 4, 3, 4); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); + buttonLeft.Size = new Size(30, 29); buttonLeft.TabIndex = 3; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += buttonMove_Click; @@ -96,7 +99,7 @@ buttonUp.Location = new Point(883, 456); buttonUp.Margin = new Padding(3, 4, 3, 4); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); + buttonUp.Size = new Size(30, 29); buttonUp.TabIndex = 4; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += buttonMove_Click; @@ -106,24 +109,60 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.buttonRight; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(919, 494); + buttonRight.Location = new Point(919, 493); buttonRight.Margin = new Padding(3, 4, 3, 4); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(30, 30); + buttonRight.Size = new Size(30, 29); buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // comboBoxStrategy + // + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); + comboBoxStrategy.Location = new Point(830, 28); + comboBoxStrategy.Margin = new Padding(3, 4, 3, 4); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(138, 28); + comboBoxStrategy.TabIndex = 6; + comboBoxStrategy.SelectedIndexChanged += comboBoxStrategy_SelectedIndexChanged; + // + // buttonCreateFlyAirbus + // + buttonCreateFlyAirbus.Location = new Point(38, 456); + buttonCreateFlyAirbus.Margin = new Padding(3, 4, 3, 4); + buttonCreateFlyAirbus.Name = "buttonCreateFlyAirbus"; + buttonCreateFlyAirbus.Size = new Size(166, 80); + buttonCreateFlyAirbus.TabIndex = 7; + buttonCreateFlyAirbus.Text = "Создать пассажирский аэробус"; + buttonCreateFlyAirbus.UseVisualStyleBackColor = true; + buttonCreateFlyAirbus.Click += buttonCreateFlyAirbus_Click; + // + // ButtonStep + // + ButtonStep.Location = new Point(882, 79); + ButtonStep.Margin = new Padding(3, 4, 3, 4); + ButtonStep.Name = "ButtonStep"; + ButtonStep.Size = new Size(86, 31); + ButtonStep.TabIndex = 8; + ButtonStep.Text = "Шаг"; + ButtonStep.UseVisualStyleBackColor = true; + ButtonStep.Click += ButtonStep_Click; + // // FormAirbus // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(982, 553); + Controls.Add(ButtonStep); + Controls.Add(buttonCreateFlyAirbus); + Controls.Add(comboBoxStrategy); Controls.Add(buttonRight); Controls.Add(buttonUp); Controls.Add(buttonLeft); Controls.Add(buttonDown); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateAirbus); Controls.Add(pictureBoxAirbus); Margin = new Padding(3, 4, 3, 4); Name = "FormAirbus"; @@ -137,10 +176,13 @@ #endregion private PictureBox pictureBoxAirbus; - private Button buttonCreate; + private Button buttonCreateAirbus; private Button buttonDown; private Button buttonLeft; private Button buttonUp; private Button buttonRight; + private ComboBox comboBoxStrategy; + private Button buttonCreateFlyAirbus; + private Button ButtonStep; } } \ No newline at end of file diff --git a/RPP/RPP/Airbus.cs b/RPP/RPP/Airbus.cs index e943ba5..c046996 100644 --- a/RPP/RPP/Airbus.cs +++ b/RPP/RPP/Airbus.cs @@ -1,12 +1,13 @@ +using static System.Windows.Forms.VisualStyles.VisualStyleElement; +using RPP.DrawningObjects; +using RPP.MovementStrategy; + namespace RPP { public partial class FormAirbus : Form { private DrawningAirbus? _drawningAirbus; - /// - /// - /// - + private AbstractStrategy? _abstractStrategy; public FormAirbus() { InitializeComponent(); @@ -25,30 +26,47 @@ namespace RPP } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateFlyAirbus_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirbus = new DrawningAirbus(); - _drawningAirbus.Init(random.Next(100, 300), random.Next(1000, 3000), + _drawningAirbus = new DrawningFlyAirbus(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)), pictureBoxAirbus.Width, pictureBoxAirbus.Height); - _drawningAirbus.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + _drawningAirbus.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); + } + private void ButtonCreateAirbus_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningAirbus = new DrawningAirbus(random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + pictureBoxAirbus.Width, pictureBoxAirbus.Height); + _drawningAirbus.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + + + + private void buttonMove_Click(object sender, EventArgs e) { if (_drawningAirbus == null) { return; } - string name = ((Button)sender)?.Name ?? string.Empty; + string name = ((System.Windows.Forms.Button)sender)?.Name ?? string.Empty; switch (name) { case "buttonUp": @@ -64,9 +82,47 @@ namespace RPP _drawningAirbus.MoveTransport(Direction.Right); break; } + Draw(); } + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawningAirbus == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectAirbus(_drawningAirbus), pictureBoxAirbus.Width, + pictureBoxAirbus.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } + private void pictureBoxAirbus_Click(object sender, EventArgs e) { @@ -76,5 +132,10 @@ namespace RPP { } + + private void comboBoxStrategy_SelectedIndexChanged(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/RPP/RPP/Drawning.cs b/RPP/RPP/Drawning.cs index 66530ac..4d25061 100644 --- a/RPP/RPP/Drawning.cs +++ b/RPP/RPP/Drawning.cs @@ -3,40 +3,74 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using RPP.Entities; -namespace RPP +namespace RPP.DrawningObjects { public class DrawningAirbus { - public EntityAirbus? EntityAirbus { get; private set; } + public EntityAirbus? EntityAirbus { get; protected set; } private int _pictureWidth; private int _pictureHeight; - private int _startPosX; + protected int _startPosX; - private int _startPosY; + protected int _startPosY; private readonly int _AirbusWidth = 200; private readonly int _AirbusHeight = 100; - - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool compartment, bool engine, int width, int height) + public int GetPosX => _startPosX; + + public int GetPosY => _startPosY; + + public int GetWidth => _AirbusWidth; + + public int GetHeight => _AirbusHeight; + + public virtual bool CanMove(Direction direction) { - if (width < _AirbusWidth || height < _AirbusHeight) + if (EntityAirbus == null) { return false; } + return direction switch + { + Direction.Left => _startPosX - EntityAirbus.Step > 5, + Direction.Up => _startPosY - EntityAirbus.Step > 0, + Direction.Right => _startPosX + EntityAirbus.Step + _AirbusWidth < _pictureWidth, + Direction.Down => _startPosY + EntityAirbus.Step + _AirbusHeight < _pictureHeight, + _ => false + }; + } + + public DrawningAirbus(int speed, double weight, Color bodyColor, int width, int height) + { + if (width < _AirbusWidth || height < _AirbusHeight) + { + return; + } _pictureWidth = width; _pictureHeight = height; - EntityAirbus = new EntityAirbus(); - EntityAirbus.Init(speed, weight, bodyColor, additionalColor, compartment, engine); - return true; + EntityAirbus = new EntityAirbus(speed, weight, bodyColor); } + + protected DrawningAirbus(int speed, double weight, Color bodyColor, int + width, int height, int airbusWidth, int airbusHeight) + { + + _pictureWidth = width; + _pictureHeight = height; + _AirbusWidth = airbusWidth; + _AirbusHeight = airbusHeight; + EntityAirbus = new EntityAirbus(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y) { if (x > _pictureWidth || y > _pictureHeight || x < 0 || y < 0) @@ -47,11 +81,11 @@ namespace RPP _startPosX = x; _startPosY = y; } - + public void MoveTransport(Direction direction) { - if (EntityAirbus == null) - { + if (!CanMove(direction) || EntityAirbus == null) + { return; } switch (direction) @@ -87,14 +121,14 @@ namespace RPP } } - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { if (EntityAirbus == null) { return; } Pen pen = new(Color.Black, 3); - Brush additionalBrush = new SolidBrush(EntityAirbus.AdditionalColor); + Brush brush = new SolidBrush(Color.Black); //Тело g.DrawRectangle(pen, _startPosX + 5, _startPosY + 50, 170, 30); g.DrawPie(pen, _startPosX - 5, _startPosY + 50, 20, 30, 90, 180); @@ -105,8 +139,6 @@ namespace RPP g.DrawLine(pen, _startPosX, _startPosY, _startPosX, _startPosY + 52); //Заднее боковые крылья - Brush brush = new SolidBrush(Color.Black); - Pen bigPen = new Pen(Color.Black, 8); g.DrawPie(pen, _startPosX - 7, _startPosY + 45, 5, 10, 90, 180); g.DrawLine(bigPen, _startPosX - 6, _startPosY + 48, _startPosX + 30, _startPosY + 48); @@ -130,29 +162,6 @@ namespace RPP g.DrawLine(pen, _startPosX + 165, _startPosY + 80, _startPosX + 165, _startPosY + 90); g.DrawEllipse(pen, _startPosX + 163, _startPosY + 91, 5, 5); - - //Пассажирсакий доп. отсек - if (EntityAirbus.Compartment) - { - g.DrawPie(pen, _startPosX + 60, _startPosY + 28, 115, 45, 180, 180); - g.FillPie(additionalBrush, _startPosX + 60, _startPosY + 28, 115, 45, 180, 180); - } - // Доп. двигатели - if (EntityAirbus.Engine) - { - g.DrawLine(pen, _startPosX + 95, _startPosY + 65, _startPosX + 95, _startPosY + 75); - Point[] pnts = - { - new Point(_startPosX + 83, _startPosY + 78), - new Point(_startPosX + 103, _startPosY + 73), - new Point(_startPosX + 103, _startPosY + 93), - new Point(_startPosX + 83, _startPosY + 88), - new Point(_startPosX + 83, _startPosY + 78) - }; - - g.DrawLines(pen, pnts); - g.FillPolygon(additionalBrush, pnts); - } } } diff --git a/RPP/RPP/DrawningFlyAirbus.cs b/RPP/RPP/DrawningFlyAirbus.cs new file mode 100644 index 0000000..e1c0c10 --- /dev/null +++ b/RPP/RPP/DrawningFlyAirbus.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RPP.Entities; + +namespace RPP.DrawningObjects +{ + internal class DrawningFlyAirbus : DrawningAirbus + { + + public DrawningFlyAirbus(int speed, double weight, Color bodyColor, Color + additionalColor, bool compartment, bool engine, int width, int height) : + base(speed, weight, bodyColor, width, height, 200, 100) + { + if (EntityAirbus != null) + { + EntityAirbus = new EntityFlyAirbus(speed, weight, bodyColor, + additionalColor, compartment, engine); + } + } + + public override void DrawTransport(Graphics g) + { + if (EntityAirbus is not EntityFlyAirbus FlyAirbus) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new + SolidBrush(FlyAirbus.AdditionalColor); + base.DrawTransport(g); + // Пассажирский отсек + if (FlyAirbus.Compartment) + { + g.DrawPie(pen, _startPosX + 60, _startPosY + 28, 115, 45, 180, 180); + g.FillPie(additionalBrush, _startPosX + 60, _startPosY + 28, 115, 45, 180, 180); + } + // крыло + if (FlyAirbus.Engine) + { + g.DrawLine(pen, _startPosX + 95, _startPosY + 65, _startPosX + 95, _startPosY + 75); + Point[] pnts = + { + new Point(_startPosX + 83, _startPosY + 78), + new Point(_startPosX + 103, _startPosY + 73), + new Point(_startPosX + 103, _startPosY + 93), + new Point(_startPosX + 83, _startPosY + 88), + new Point(_startPosX + 83, _startPosY + 78) + }; + + g.DrawLines(pen, pnts); + g.FillPolygon(additionalBrush, pnts); + } + } + } +} + diff --git a/RPP/RPP/DrawningObjectAirbus.cs b/RPP/RPP/DrawningObjectAirbus.cs new file mode 100644 index 0000000..8f9796d --- /dev/null +++ b/RPP/RPP/DrawningObjectAirbus.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RPP.DrawningObjects; + +namespace RPP.MovementStrategy +{ + internal class DrawningObjectAirbus : IMoveableObject + { + private readonly DrawningAirbus? _drawningAirbus = null; + public DrawningObjectAirbus(DrawningAirbus drawningAirbus) + { + _drawningAirbus = drawningAirbus; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningAirbus == null || _drawningAirbus.EntityAirbus == null) + { + return null; + } + return new ObjectParameters(_drawningAirbus.GetPosX, + _drawningAirbus.GetPosY, _drawningAirbus.GetWidth, _drawningAirbus.GetHeight); + } + } + public int GetStep => (int)(_drawningAirbus?.EntityAirbus?.Step ?? 0); + public bool CheckCanMove(Direction direction) => + _drawningAirbus?.CanMove(direction) ?? false; + public void MoveObject(Direction direction) => + _drawningAirbus?.MoveTransport(direction); + + } +} diff --git a/RPP/RPP/Entity.cs b/RPP/RPP/EntityAirbus.cs similarity index 53% rename from RPP/RPP/Entity.cs rename to RPP/RPP/EntityAirbus.cs index 44be4a3..c0f1718 100644 --- a/RPP/RPP/Entity.cs +++ b/RPP/RPP/EntityAirbus.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace RPP +namespace RPP.Entities { public class EntityAirbus { @@ -15,23 +15,14 @@ namespace RPP public Color BodyColor { get; private set; } - public Color AdditionalColor { get; private set; } - - public bool Compartment { get; private set; } - - public bool Engine { get; private set; } - public double Step => (double)Speed * 100 / Weight; - - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool compartment, bool engine) + + public EntityAirbus(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; - AdditionalColor = additionalColor; - Compartment = compartment; - Engine = engine; } + } } diff --git a/RPP/RPP/EntityFlyAirbus.cs b/RPP/RPP/EntityFlyAirbus.cs new file mode 100644 index 0000000..d5a91c6 --- /dev/null +++ b/RPP/RPP/EntityFlyAirbus.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RPP.Entities +{ + internal class EntityFlyAirbus : EntityAirbus + { + + public Color AdditionalColor { get; private set; } + + public bool Compartment { get; private set; } + + public bool Engine { get; private set; } + + public EntityFlyAirbus(int speed, double weight, Color bodyColor, Color + additionalColor, bool compartment, bool engine) : base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + Compartment = compartment; + Engine = engine; + } + } +} diff --git a/RPP/RPP/IMoveableObject.cs b/RPP/RPP/IMoveableObject.cs new file mode 100644 index 0000000..132cf5f --- /dev/null +++ b/RPP/RPP/IMoveableObject.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RPP.MovementStrategy +{ + interface IMoveableObject + { + + ObjectParameters? GetObjectPosition { get; } + + int GetStep { get; } + + bool CheckCanMove(Direction direction); + + void MoveObject(Direction direction); + + } +} diff --git a/RPP/RPP/MoveToBorder.cs b/RPP/RPP/MoveToBorder.cs new file mode 100644 index 0000000..30bdea4 --- /dev/null +++ b/RPP/RPP/MoveToBorder.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RPP.MovementStrategy +{ + internal class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth && objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + var diffY = objParams.DownBorder - FieldHeight; + if (diffX >= 0) + { + MoveDown(); + } + else if (diffY >= 0) + { + MoveRight(); + } + else if (Math.Abs(diffX) > Math.Abs(diffY)) + { + MoveRight(); + } + else + { + MoveDown(); + } + + } + } +} diff --git a/RPP/RPP/MoveToCenter.cs b/RPP/RPP/MoveToCenter.cs new file mode 100644 index 0000000..74a82d6 --- /dev/null +++ b/RPP/RPP/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RPP.MovementStrategy +{ + internal 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/RPP/RPP/ObjectParameters.cs b/RPP/RPP/ObjectParameters.cs new file mode 100644 index 0000000..056450c --- /dev/null +++ b/RPP/RPP/ObjectParameters.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RPP.MovementStrategy +{ + internal 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/RPP/RPP/dop.cs b/RPP/RPP/Status.cs similarity index 50% rename from RPP/RPP/dop.cs rename to RPP/RPP/Status.cs index 0f2ebec..12d0568 100644 --- a/RPP/RPP/dop.cs +++ b/RPP/RPP/Status.cs @@ -6,12 +6,10 @@ using System.Threading.Tasks; namespace RPP { - public class dop + public enum Status { - public int veter { get; private set;} - - public void Init(int veter) { - this.veter = veter; - } + InProgress = 1, + Finish = 2, + NotInit = 0 } }