From 64abc50ba41dfd4d0d1a87d44b691821ac0fa1d0 Mon Sep 17 00:00:00 2001 From: Salikh Date: Mon, 27 Nov 2023 23:25:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AbstractStrategy.cs | 75 +++++++++++++ AirBomber/DrawningAirBomber.cs | 145 +++---------------------- AirBomber/DrawningBomber.cs | 162 ++++++++++++++++++++++++++++ AirBomber/DrawningObjectBomber .cs | 33 ++++++ AirBomber/EntityAirBomber.cs | 19 +--- AirBomber/EntityBomber.cs | 23 ++++ AirBomber/FormAirBomber.Designer.cs | 45 +++++++- AirBomber/FormAirBomber.cs | 87 +++++++++++---- AirBomber/IMoveableObject.cs | 18 ++++ AirBomber/MoveToBottomRight .cs | 48 +++++++++ AirBomber/MoveToCenter.cs | 55 ++++++++++ AirBomber/ObjectParameters.cs | 29 +++++ AirBomber/Status.cs | 15 +++ 13 files changed, 585 insertions(+), 169 deletions(-) create mode 100644 AirBomber/AbstractStrategy.cs create mode 100644 AirBomber/DrawningBomber.cs create mode 100644 AirBomber/DrawningObjectBomber .cs create mode 100644 AirBomber/EntityBomber.cs create mode 100644 AirBomber/IMoveableObject.cs create mode 100644 AirBomber/MoveToBottomRight .cs create mode 100644 AirBomber/MoveToCenter.cs create mode 100644 AirBomber/ObjectParameters.cs create mode 100644 AirBomber/Status.cs diff --git a/AirBomber/AbstractStrategy.cs b/AirBomber/AbstractStrategy.cs new file mode 100644 index 0000000..e63db8c --- /dev/null +++ b/AirBomber/AbstractStrategy.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawningObjects; + +namespace AirBomber.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(); + } + protected bool MoveLeft() => MoveTo(Diraction.Left); + protected bool MoveRight() => MoveTo(Diraction.Right); + protected bool MoveUp() => MoveTo(Diraction.Up); + protected bool MoveDown() => MoveTo(Diraction.Down); + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosit; + + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + + protected abstract void MoveToTarget(); + protected abstract bool IsTargetDestinaion(); + + private bool MoveTo(Diraction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/AirBomber/DrawningAirBomber.cs b/AirBomber/DrawningAirBomber.cs index f9f75e6..937c5af 100644 --- a/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/DrawningAirBomber.cs @@ -4,113 +4,30 @@ using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; +using AirBomber.Entities; -namespace AirBomber +namespace AirBomber.DrawningObjects { - public class DrawningAirBomber + public class DrawningAirBomber : DrawningBomber { - public EntityAirBomber? EntityAirBomber { get; private set; } - - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private int _PlaneWidth = 160; - private int _PlaneHeight = 185; - - public bool Init(int speed, int weight, Color bodycolor, Color dopcolor, bool toplivo, bool rocket, int width, int height) + public DrawningAirBomber(int speed, int weight, Color bodycolor, Color dopcolor, bool toplivo, bool rocket, int width, int height) : base(speed, weight, bodycolor, width, height, 160, 185) { - if (width < _pictureWidth || height < _pictureHeight) + if (EntityBomber != null) { - return false; + EntityBomber = new EntityAirBomber(speed, weight, bodycolor, dopcolor, toplivo, rocket); } - _pictureWidth = width; - _pictureHeight = height; - EntityAirBomber = new EntityAirBomber(); - EntityAirBomber.Init(speed, weight, bodycolor, dopcolor, toplivo, rocket); - return true; } - - public void SetPosition(int x, int y) + public override void DrawBomber(Graphics g) { - if (x < 0 || x + _PlaneWidth > _pictureWidth) - { - x = _pictureWidth - _PlaneWidth; - } - if (y < 0 || y + _PlaneWidth > _pictureHeight) - { - y = _pictureHeight - _PlaneWidth; - } - _startPosX = x; - _startPosY = y; - } - - public void MoveTransport(Diraction diraction) - { - if (EntityAirBomber == null) + if (EntityBomber is not EntityAirBomber airBomber) { return; } - - int step = (int)EntityAirBomber.Step; - - switch (diraction) - { - case Diraction.Left: - if (_startPosX - step >= 0) - { - _startPosX -= step; - } - else - { - _startPosX = 0; - } - break; - case Diraction.Right: - if (_startPosX + step + _PlaneWidth <= _pictureWidth) - { - _startPosX += step; - } - else - { - _startPosX = _pictureWidth - _PlaneWidth; - } - break; - case Diraction.Up: - if (_startPosY - step >= 0) - { - _startPosY -= step; - } - else - { - _startPosY = 0; - } - break; - case Diraction.Down: - if (_startPosY + step + _PlaneHeight <= _pictureHeight) - { - _startPosY += step; - } - else - { - _startPosY = _pictureHeight - _PlaneHeight; - } - break; - } - } - - public void DrawCar(Graphics g) - { - if (EntityAirBomber == null) - { - return; - } - Pen pen = new(Color.Black); - Brush dopcolor = new SolidBrush(EntityAirBomber.DopColor); - if (EntityAirBomber.Toplivo) + Brush dopcolor = new SolidBrush(airBomber.DopColor); + //отрисовка ракет + if (airBomber.Rocket) { - //отрисовка ракет GraphicsPath rocket_1 = new GraphicsPath(); rocket_1.AddLine(_startPosX + 70, _startPosY + 35, _startPosX + 80, _startPosY + 25); rocket_1.AddLine(_startPosX + 80, _startPosY + 25, _startPosX + 80, _startPosY + 45); @@ -136,7 +53,8 @@ namespace AirBomber g.FillPath(dopcolor, rocket_4); g.DrawPath(Pens.Black, rocket_4); } - if (EntityAirBomber.Rocket) { + if (airBomber.Toplivo) + { //отрисовка баков g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 5, 8, 10); g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 25, 8, 10); @@ -145,42 +63,7 @@ namespace AirBomber g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 150, 8, 10); g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 170, 8, 10); } - //отрисовка крыла 1 - GraphicsPath fly_1 = new GraphicsPath(); - fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 80, _startPosY + 80); - fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 90, _startPosY + 2); - fly_1.AddLine(_startPosX + 90, _startPosY + 2, _startPosX + 100, _startPosY + 80); - fly_1.AddLine(_startPosX + 100, _startPosY + 80, _startPosX + 80, _startPosY + 80); - g.DrawPath(Pens.Black, fly_1); - //отрисовка кабины пилота - GraphicsPath treygol = new GraphicsPath(); - treygol.AddLine(_startPosX + 3, _startPosY + 95, _startPosX + 30, _startPosY + 80); - treygol.AddLine(_startPosX + 30, _startPosY + 80, _startPosX + 30, _startPosY + 105); - treygol.CloseFigure(); - g.FillPath(Brushes.Black, treygol); - g.DrawPath(Pens.Black, treygol); - //отрисовка корпуса - g.DrawRectangle(pen, _startPosX + 30, _startPosY + 80, 120, 25); - //отрисовка крыла 2 - GraphicsPath fly_2 = new GraphicsPath(); - fly_2.AddLine(_startPosX + 80, _startPosY + 105, _startPosX + 80, _startPosY + 185); - fly_2.AddLine(_startPosX + 80, _startPosY + 185, _startPosX + 90, _startPosY + 185); - fly_2.AddLine(_startPosX + 90, _startPosY + 185, _startPosX + 100, _startPosY + 105); - fly_2.CloseFigure(); - g.DrawPath(Pens.Black, fly_2); - //отриосвка хвоста - GraphicsPath wing = new GraphicsPath(); - wing.AddLine(_startPosX + 135, _startPosY + 80, _startPosX + 135, _startPosY + 70); - wing.AddLine(_startPosX + 135, _startPosY + 70, _startPosX + 150, _startPosY + 50); - wing.AddLine(_startPosX + 150, _startPosY + 50, _startPosX + 150, _startPosY + 80); - wing.CloseFigure(); - g.DrawPath(Pens.Black, wing); - GraphicsPath wing_2 = new GraphicsPath(); - wing_2.AddLine(_startPosX + 135, _startPosY + 105, _startPosX + 135, _startPosY + 115); - wing_2.AddLine(_startPosX + 135, _startPosY + 115, _startPosX + 150, _startPosY + 135); - wing_2.AddLine(_startPosX + 150, _startPosY + 135, _startPosX + 150, _startPosY + 105); - wing_2.CloseFigure(); - g.DrawPath(Pens.Black, wing_2); + base.DrawBomber(g); } } } diff --git a/AirBomber/DrawningBomber.cs b/AirBomber/DrawningBomber.cs new file mode 100644 index 0000000..e0fed4e --- /dev/null +++ b/AirBomber/DrawningBomber.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.Drawing.Drawing2D; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.Entities; + +namespace AirBomber.DrawningObjects +{ + public class DrawningBomber + { + public EntityBomber? EntityBomber { get; protected set; } + + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected int _PlaneWidth = 160; + protected int _PlaneHeight = 185; + + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _PlaneWidth; + public int GetHeight => _PlaneHeight; + + public DrawningBomber(int speed, double weight, Color bodycolor, int width, int height) + { + if (width < _pictureWidth || height < _pictureHeight) + { + throw new InvalidOperationException("Invalid weight or height"); + } + _pictureWidth = width; + _pictureHeight = height; + EntityBomber = new EntityBomber(speed, weight, bodycolor); + } + + protected DrawningBomber(int speed, double weight, Color bodycolor, int width, int height, int planeWidth, int planeHeight) + { + if (width < _pictureWidth || height < _pictureHeight) + { + throw new InvalidOperationException("Invalid width or height"); + } + _pictureWidth = width; + _pictureHeight = height; + _PlaneWidth = planeWidth; + _PlaneHeight = planeHeight; + EntityBomber = new EntityBomber(speed, weight, bodycolor); + } + public void SetPosition(int x, int y) + { + if (x < 0 || x + _PlaneWidth > _pictureWidth) + { + x = _pictureWidth - _PlaneWidth; + } + if (y < 0 || y + _PlaneWidth > _pictureHeight) + { + y = _pictureHeight - _PlaneWidth; + } + _startPosX = x; + _startPosY = y; + } + + public bool CanMove(Diraction direction) + { + if (EntityBomber == null) + { + return false; + } + + int newPosX = _startPosX; + int newPosY = _startPosY; + + switch (direction) + { + case Diraction.Left: + newPosX -= (int)EntityBomber.Step; + break; + case Diraction.Right: + newPosX += (int)EntityBomber.Step; + break; + case Diraction.Up: + newPosY -= (int)EntityBomber.Step; + break; + case Diraction.Down: + newPosY += (int)EntityBomber.Step; + break; + } + + return newPosX >= 0 && newPosX <= _pictureWidth - _PlaneWidth && + newPosY >= 0 && newPosY <= _pictureHeight - _PlaneHeight; + } + + public void MoveTransport(Diraction diraction) + { + if (!CanMove(diraction) || EntityBomber == null) + { + return; + } + + switch (diraction) + { + case Diraction.Left: + _startPosX -= (int)EntityBomber.Step; + break; + case Diraction.Right: + _startPosX += (int)EntityBomber.Step; + break; + case Diraction.Up: + _startPosY -= (int)EntityBomber.Step; + break; + case Diraction.Down: + _startPosY += (int)EntityBomber.Step; + break; + } + } + public virtual void DrawBomber(Graphics g) + { + if (EntityBomber == null) + { + return; + } + Pen pen = new(Color.Black); + //отрисовка крыла 1 + GraphicsPath fly_1 = new GraphicsPath(); + fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 80, _startPosY + 80); + fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 90, _startPosY + 2); + fly_1.AddLine(_startPosX + 90, _startPosY + 2, _startPosX + 100, _startPosY + 80); + fly_1.AddLine(_startPosX + 100, _startPosY + 80, _startPosX + 80, _startPosY + 80); + g.DrawPath(Pens.Black, fly_1); + //отрисовка кабины пилота + GraphicsPath treygol = new GraphicsPath(); + treygol.AddLine(_startPosX + 3, _startPosY + 95, _startPosX + 30, _startPosY + 80); + treygol.AddLine(_startPosX + 30, _startPosY + 80, _startPosX + 30, _startPosY + 105); + treygol.CloseFigure(); + g.FillPath(Brushes.Black, treygol); + g.DrawPath(Pens.Black, treygol); + //отрисовка корпуса + g.DrawRectangle(pen, _startPosX + 30, _startPosY + 80, 120, 25); + //отрисовка крыла 2 + GraphicsPath fly_2 = new GraphicsPath(); + fly_2.AddLine(_startPosX + 80, _startPosY + 105, _startPosX + 80, _startPosY + 185); + fly_2.AddLine(_startPosX + 80, _startPosY + 185, _startPosX + 90, _startPosY + 185); + fly_2.AddLine(_startPosX + 90, _startPosY + 185, _startPosX + 100, _startPosY + 105); + fly_2.CloseFigure(); + g.DrawPath(Pens.Black, fly_2); + //отриосвка хвоста + GraphicsPath wing = new GraphicsPath(); + wing.AddLine(_startPosX + 135, _startPosY + 80, _startPosX + 135, _startPosY + 70); + wing.AddLine(_startPosX + 135, _startPosY + 70, _startPosX + 150, _startPosY + 50); + wing.AddLine(_startPosX + 150, _startPosY + 50, _startPosX + 150, _startPosY + 80); + wing.CloseFigure(); + g.DrawPath(Pens.Black, wing); + GraphicsPath wing_2 = new GraphicsPath(); + wing_2.AddLine(_startPosX + 135, _startPosY + 105, _startPosX + 135, _startPosY + 115); + wing_2.AddLine(_startPosX + 135, _startPosY + 115, _startPosX + 150, _startPosY + 135); + wing_2.AddLine(_startPosX + 150, _startPosY + 135, _startPosX + 150, _startPosY + 105); + wing_2.CloseFigure(); + g.DrawPath(Pens.Black, wing_2); + } + } +} diff --git a/AirBomber/DrawningObjectBomber .cs b/AirBomber/DrawningObjectBomber .cs new file mode 100644 index 0000000..3351391 --- /dev/null +++ b/AirBomber/DrawningObjectBomber .cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawningObjects; + +namespace AirBomber.MovementStrategy +{ + public class DrawningObjectBomber : IMoveableObject + { + private readonly DrawningBomber? _drawningBomber = null; + + public DrawningObjectBomber(DrawningBomber drawningBomber) + { + _drawningBomber = drawningBomber; + } + public ObjectParameters? GetObjectPosit + { + get + { + if (_drawningBomber == null || _drawningBomber.EntityBomber == null) + { + return null; + } + return new ObjectParameters(_drawningBomber.GetPosX, _drawningBomber.GetPosY, _drawningBomber.GetWidth, _drawningBomber.GetHeight); + } + } + public int GetStep => (int)(_drawningBomber?.EntityBomber?.Step ?? 0); + public bool CheckCanMove(Diraction direction) => _drawningBomber?.CanMove(direction) ?? false; + public void MoveObject(Diraction direction) => _drawningBomber?.MoveTransport(direction); + } +} diff --git a/AirBomber/EntityAirBomber.cs b/AirBomber/EntityAirBomber.cs index 0f7f1ef..8f28c76 100644 --- a/AirBomber/EntityAirBomber.cs +++ b/AirBomber/EntityAirBomber.cs @@ -1,26 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirBomber +namespace AirBomber.Entities { - public class EntityAirBomber + public class EntityAirBomber : EntityBomber { - public int Speed { get; private set; } - public int Weight { get; private set; } - public Color BodyColor { get; private set; } public Color DopColor { get; private set; } public bool Toplivo { get; private set; } public bool Rocket { get; private set; } - public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, int weight, Color bodycolor, Color dopcolor, bool toplivo, bool ropcket) + public EntityAirBomber(int speed, double weight, Color bodycolor, Color dopcolor, bool toplivo, bool ropcket) : base(speed, weight, bodycolor) { - Speed = speed; - Weight = weight; - BodyColor = bodycolor; DopColor = dopcolor; Toplivo = toplivo; Rocket = ropcket; diff --git a/AirBomber/EntityBomber.cs b/AirBomber/EntityBomber.cs new file mode 100644 index 0000000..c3c3657 --- /dev/null +++ b/AirBomber/EntityBomber.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber.Entities +{ + public class EntityBomber + { + public int Speed { get; private set; } + public double Weight { get; private set; } + public Color BodyColor { get; private set; } + public double Step => (double)Speed * 100 / Weight; + + public EntityBomber(int speed, double weight, Color bodycolor) + { + Speed = speed; + Weight = weight; + BodyColor = bodycolor; + } + } +} diff --git a/AirBomber/FormAirBomber.Designer.cs b/AirBomber/FormAirBomber.Designer.cs index 950da2f..9d00b25 100644 --- a/AirBomber/FormAirBomber.Designer.cs +++ b/AirBomber/FormAirBomber.Designer.cs @@ -34,6 +34,9 @@ buttonLeft = new Button(); buttonUp = new Button(); buttonCreate = new Button(); + buttonCreateWarBomber = new Button(); + comboBoxStrategy = new ComboBox(); + ButtonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); SuspendLayout(); // @@ -97,19 +100,52 @@ // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(30, 395); + buttonCreate.Location = new Point(223, 385); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(131, 43); + buttonCreate.Size = new Size(181, 59); buttonCreate.TabIndex = 6; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать самолёт"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += buttonCreate_Click; // + // buttonCreateWarBomber + // + buttonCreateWarBomber.Location = new Point(12, 385); + buttonCreateWarBomber.Name = "buttonCreateWarBomber"; + buttonCreateWarBomber.Size = new Size(205, 59); + buttonCreateWarBomber.TabIndex = 7; + buttonCreateWarBomber.Text = "Создать военный самолёт"; + buttonCreateWarBomber.UseVisualStyleBackColor = true; + buttonCreateWarBomber.Click += buttonCreateWarBomber_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "В центр", "В правый нижний угол" }); + comboBoxStrategy.Location = new Point(637, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 8; + // + // ButtonStep + // + ButtonStep.Location = new Point(694, 60); + ButtonStep.Name = "ButtonStep"; + ButtonStep.Size = new Size(94, 29); + ButtonStep.TabIndex = 9; + ButtonStep.Text = "Шаг"; + ButtonStep.UseVisualStyleBackColor = true; + ButtonStep.Click += ButtonStep_Click; + // // FormAirBomber // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(ButtonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateWarBomber); Controls.Add(buttonCreate); Controls.Add(buttonUp); Controls.Add(buttonLeft); @@ -130,5 +166,8 @@ private Button buttonLeft; private Button buttonUp; private Button buttonCreate; + private Button buttonCreateWarBomber; + private ComboBox comboBoxStrategy; + private Button ButtonStep; } } \ No newline at end of file diff --git a/AirBomber/FormAirBomber.cs b/AirBomber/FormAirBomber.cs index fcbc10d..77bd3e3 100644 --- a/AirBomber/FormAirBomber.cs +++ b/AirBomber/FormAirBomber.cs @@ -1,8 +1,13 @@ +using AirBomber.DrawningObjects; +using AirBomber.MovementStrategy; + namespace AirBomber { public partial class FormAirBomber : Form { - private DrawningAirBomber? _drawingair; + private DrawningBomber? _drawningBomber; + private AbstractStrategy? _abstractStrategy; + public FormAirBomber() { InitializeComponent(); @@ -10,37 +15,29 @@ namespace AirBomber private void Draw() { - - if (_drawingair == null) + if (_drawningBomber == null) { return; } Bitmap bmp = new(pictureBox.Width, pictureBox.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingair.DrawCar(gr); + _drawningBomber.DrawBomber(gr); pictureBox.Image = bmp; } private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); - _drawingair = new DrawningAirBomber(); - _drawingair.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)), - pictureBox.Width, pictureBox.Height); - _drawingair.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + _drawningBomber = new DrawningBomber(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBox.Width, pictureBox.Height); + _drawningBomber.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawingair == null) + if (_drawningBomber == null) { return; } @@ -48,19 +45,71 @@ namespace AirBomber switch (name) { case "buttonUp": - _drawingair.MoveTransport(Diraction.Up); + _drawningBomber.MoveTransport(Diraction.Up); break; case "buttonDown": - _drawingair.MoveTransport(Diraction.Down); + _drawningBomber.MoveTransport(Diraction.Down); break; case "buttonLeft": - _drawingair.MoveTransport(Diraction.Left); + _drawningBomber.MoveTransport(Diraction.Left); break; case "buttonRight": - _drawingair.MoveTransport(Diraction.Right); + _drawningBomber.MoveTransport(Diraction.Right); break; } Draw(); } + + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawningBomber == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBottomRight(), + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectBomber(_drawningBomber), pictureBox.Width, + pictureBox.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } + + private void buttonCreateWarBomber_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningBomber = new DrawningAirBomber(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(1, 2)), + pictureBox.Width, pictureBox.Height); + _drawningBomber.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } } } \ No newline at end of file diff --git a/AirBomber/IMoveableObject.cs b/AirBomber/IMoveableObject.cs new file mode 100644 index 0000000..b55a851 --- /dev/null +++ b/AirBomber/IMoveableObject.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawningObjects; + +namespace AirBomber.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosit { get; } + + int GetStep { get; } + bool CheckCanMove(Diraction direction); + void MoveObject(Diraction direction); + } +} diff --git a/AirBomber/MoveToBottomRight .cs b/AirBomber/MoveToBottomRight .cs new file mode 100644 index 0000000..7804b61 --- /dev/null +++ b/AirBomber/MoveToBottomRight .cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.MovementStrategy; + +namespace AirBomber.MovementStrategy +{ + public class MoveToBottomRight : 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/AirBomber/MoveToCenter.cs b/AirBomber/MoveToCenter.cs new file mode 100644 index 0000000..54ee4b3 --- /dev/null +++ b/AirBomber/MoveToCenter.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.MovementStrategy; + +namespace AirBomber.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/AirBomber/ObjectParameters.cs b/AirBomber/ObjectParameters.cs new file mode 100644 index 0000000..0c51d88 --- /dev/null +++ b/AirBomber/ObjectParameters.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber.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/AirBomber/Status.cs b/AirBomber/Status.cs new file mode 100644 index 0000000..3e0828d --- /dev/null +++ b/AirBomber/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}