From e40ae5a4ee2902a2d35990ac82f78f8bd0f8ced7 Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Wed, 13 Dec 2023 02:50:15 +0400 Subject: [PATCH] lab_2 --- AirFighter/AirFighter/AbstractStrategy.cs | 76 +++++++ AirFighter/AirFighter/DirectionAirFighter.cs | 2 +- AirFighter/AirFighter/DrawningAirFighter.cs | 193 ++++++------------ .../AirFighter/DrawningAirFighterMilitary.cs | 104 ++++++++++ AirFighter/AirFighter/EntityAirFighter.cs | 15 +- .../AirFighter/EntityAirFighterMilitary.cs | 22 ++ .../AirFighter/FormAirFighter.Designer.cs | 65 ++++-- AirFighter/AirFighter/FormAirFighter.cs | 78 ++++++- AirFighter/AirFighter/IMoveableObject.cs | 18 ++ .../AirFighter/IMoveableObject_Realise.cs | 37 ++++ AirFighter/AirFighter/MoveToBorder.cs | 45 ++++ AirFighter/AirFighter/MoveToCenter.cs | 64 ++++++ AirFighter/AirFighter/ObjectParameters.cs | 29 +++ AirFighter/AirFighter/Program.cs | 2 +- AirFighter/AirFighter/Status.cs | 15 ++ 15 files changed, 603 insertions(+), 162 deletions(-) create mode 100644 AirFighter/AirFighter/AbstractStrategy.cs create mode 100644 AirFighter/AirFighter/DrawningAirFighterMilitary.cs create mode 100644 AirFighter/AirFighter/EntityAirFighterMilitary.cs create mode 100644 AirFighter/AirFighter/IMoveableObject.cs create mode 100644 AirFighter/AirFighter/IMoveableObject_Realise.cs create mode 100644 AirFighter/AirFighter/MoveToBorder.cs create mode 100644 AirFighter/AirFighter/MoveToCenter.cs create mode 100644 AirFighter/AirFighter/ObjectParameters.cs create mode 100644 AirFighter/AirFighter/Status.cs diff --git a/AirFighter/AirFighter/AbstractStrategy.cs b/AirFighter/AirFighter/AbstractStrategy.cs new file mode 100644 index 0000000..5ca942b --- /dev/null +++ b/AirFighter/AirFighter/AbstractStrategy.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using AirFighter.Drawnings; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace AirFighter.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(DirectionAirFighter.Left); + protected bool MoveRight() => MoveTo(DirectionAirFighter.Right); + protected bool MoveUp() => MoveTo(DirectionAirFighter.Up); + protected bool MoveDown() => MoveTo(DirectionAirFighter.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(DirectionAirFighter directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/AirFighter/AirFighter/DirectionAirFighter.cs b/AirFighter/AirFighter/DirectionAirFighter.cs index ccf5252..005e0dd 100644 --- a/AirFighter/AirFighter/DirectionAirFighter.cs +++ b/AirFighter/AirFighter/DirectionAirFighter.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AirFighter +namespace AirFighter.Drawnings { public enum DirectionAirFighter { diff --git a/AirFighter/AirFighter/DrawningAirFighter.cs b/AirFighter/AirFighter/DrawningAirFighter.cs index 53dd7d4..586a028 100644 --- a/AirFighter/AirFighter/DrawningAirFighter.cs +++ b/AirFighter/AirFighter/DrawningAirFighter.cs @@ -6,37 +6,48 @@ using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; +using AirFighter.Entities; +using AirFighter.Drawnings; +using System.Drawing.Drawing2D; -namespace AirFighter +namespace AirFighter.DrawningObjects { public class DrawningAirFighter { - public EntityAirFighter? EntityAirFighter { get; private set; } + public EntityAirFighter? EntityAirFighter { get; protected set; } private int _pictureWidth; private int _pictureHeight; - - private int _startPosX; + + protected int _startPosX; + + protected int _startPosY; + + protected readonly int _fighterWidth = 195; + + protected readonly int _fighterHeight = 166; - private int _startPosY; - - private readonly int _fighterWidth = 195; - - private readonly int _fighterHeight = 166; - - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool wing, bool rocket, int width, int height) + public DrawningAirFighter(int speed, double weight, Color bodyColor, int width, int height) { - if (width < _pictureWidth || height < _pictureHeight) + _pictureWidth = width; + _pictureHeight = height; + EntityAirFighter = new EntityAirFighter(speed, weight, bodyColor); + } + + protected DrawningAirFighter(int speed, double weight, Color bodyColor, int + width, int height, int fighterWidth, int fighterHeight) + { + if (width <= _pictureWidth || height <= _pictureHeight) { - return false; + return; } _pictureWidth = width; _pictureHeight = height; - EntityAirFighter = new EntityAirFighter(); - EntityAirFighter.Init(speed, weight, bodyColor, additionalColor, wing, rocket); - return true; - } + _fighterWidth = fighterWidth; + _fighterHeight = fighterHeight; + EntityAirFighter = new EntityAirFighter(speed, weight, bodyColor); + } public void SetPosition(int x, int y) { if (x < 0) @@ -57,62 +68,60 @@ namespace AirFighter } _startPosX = x; _startPosY = y; - } - public void MoveTransport(DirectionAirFighter direction) + } + protected int PictureWidth + { + get { return _pictureWidth; } + } + protected int PictureHeight + { + get { return _pictureHeight; } + } + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _fighterWidth; + public int GetHeight => _fighterHeight; + public virtual bool CanMove(DirectionAirFighter direction) { if (EntityAirFighter == null) + { + return false; + } + return direction switch + { + DirectionAirFighter.Left => _startPosX - EntityAirFighter.Step > 0, + + DirectionAirFighter.Up => _startPosY - EntityAirFighter.Step > 7, + + DirectionAirFighter.Right => _startPosX + EntityAirFighter.Step + _fighterWidth < _pictureWidth,// TODO: Продумать логику + + DirectionAirFighter.Down => _startPosY + EntityAirFighter.Step + _fighterHeight < _pictureHeight,// TODO: Продумать логику + _ => false, + }; + } + public virtual void MoveTransport(DirectionAirFighter direction) + { + if (!CanMove(direction) || EntityAirFighter == null) { return; } switch (direction) { case DirectionAirFighter.Left: - if (_startPosX - EntityAirFighter.Step > 0) - { - _startPosX -= (int)EntityAirFighter.Step; - } - else if (_startPosX - EntityAirFighter.Step < 0) - { - _startPosX = _startPosX - _startPosX + 3; - } + _startPosX -= (int)EntityAirFighter.Step; break; - case DirectionAirFighter.Up: - if (_startPosY - EntityAirFighter.Step > 0) - { - _startPosY -= (int)EntityAirFighter.Step; - } - else if (_startPosY - EntityAirFighter.Step < 0) - { - _startPosY = _startPosY - _startPosY + 0; - } + _startPosY -= (int)EntityAirFighter.Step; break; - case DirectionAirFighter.Right: - if (_startPosX + EntityAirFighter.Step + _fighterWidth < _pictureWidth) - { - _startPosX += (int)EntityAirFighter.Step; - } - else if (_startPosX + EntityAirFighter.Step + _fighterWidth > _pictureWidth) - { - _startPosX += _pictureWidth - _startPosX - _fighterWidth; - } - + _startPosX += (int)EntityAirFighter.Step; break; - case DirectionAirFighter.Down: - if (_startPosY + EntityAirFighter.Step + _fighterHeight < _pictureHeight) - { - _startPosY += (int)EntityAirFighter.Step; - } - else if (_startPosY + EntityAirFighter.Step + _fighterHeight > _pictureHeight) - { - _startPosY += _pictureHeight - _startPosY - _fighterHeight; - } + _startPosY += (int)EntityAirFighter.Step; break; } } - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { if (EntityAirFighter == null) { @@ -162,78 +171,8 @@ namespace AirFighter g.DrawPolygon(pen, wingBottom); g.DrawRectangle(pen, _startPosX, _startPosY + 70, 160, 26); g.FillPolygon(brushBlack, front); - - Brush additionalBrush = new SolidBrush(EntityAirFighter.AdditionalColor); - - if (EntityAirFighter.Wing) - { - PointF[] topDopWing = - { - new(_startPosX + 78, _startPosY + 56), - new(_startPosX + 75, _startPosY + 70), - new(_startPosX + 55, _startPosY + 50), - new(_startPosX + 60, _startPosY + 45), - }; - - PointF[] bottomDopWing = - { - new(_startPosX + 78, _startPosY + 110), - new(_startPosX + 75, _startPosY + 96), - new(_startPosX + 55, _startPosY + 116), - new(_startPosX + 60, _startPosY + 121), - }; - - g.FillPolygon(additionalBrush, topDopWing); - g.FillPolygon(additionalBrush, bottomDopWing); - - } - - if (EntityAirFighter.Rocket) - { - PointF[] topRocket1 = - { - new(_startPosX + 100, _startPosY + 20), - new(_startPosX + 100, _startPosY + 30), - new(_startPosX + 112, _startPosY + 30), - new(_startPosX + 120, _startPosY + 25), - new(_startPosX + 112, _startPosY + 20) - }; - - PointF[] topRocket2 = - { - new(_startPosX + 100, _startPosY + 35), - new(_startPosX + 100, _startPosY + 45), - new(_startPosX + 112, _startPosY + 45), - new(_startPosX + 120, _startPosY + 40), - new(_startPosX + 112, _startPosY + 35) - }; - - PointF[] bottomRocket1 = - { - new(_startPosX + 100, _startPosY + 146), - new(_startPosX + 100, _startPosY + 136), - new(_startPosX + 112, _startPosY + 136), - new(_startPosX + 120, _startPosY + 141), - new(_startPosX + 112, _startPosY + 146) - }; - - PointF[] bottomRocket2 = - { - new(_startPosX + 100, _startPosY + 131), - new(_startPosX + 100, _startPosY + 121), - new(_startPosX + 112, _startPosY + 121), - new(_startPosX + 120, _startPosY + 126), - new(_startPosX + 112, _startPosY + 131) - }; - - g.FillPolygon(additionalBrush, topRocket1); - g.FillPolygon(additionalBrush, topRocket2); - g.FillPolygon(additionalBrush, bottomRocket1); - g.FillPolygon(additionalBrush, bottomRocket2); - } } - } - + } } diff --git a/AirFighter/AirFighter/DrawningAirFighterMilitary.cs b/AirFighter/AirFighter/DrawningAirFighterMilitary.cs new file mode 100644 index 0000000..66ef9f8 --- /dev/null +++ b/AirFighter/AirFighter/DrawningAirFighterMilitary.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Drawing.Drawing2D; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using AirFighter.Drawnings; +using AirFighter.Entities; + + + +namespace AirFighter.DrawningObjects +{ + public class DrawningAirFighterMilitary : DrawningAirFighter + { + public DrawningAirFighterMilitary(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool wing, int width, int height) : + base(speed, weight, bodyColor, width, height, 195, 166) + { + if (EntityAirFighter != null) + { + EntityAirFighter = new EntityAirFighterMilitary(speed, weight, bodyColor, + additionalColor, rocket, wing); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityAirFighter is not EntityAirFighterMilitary fighterMilitary) + { + return; + } + + Brush additionalBrush = new SolidBrush(fighterMilitary.AdditionalColor); + + if (fighterMilitary.Wing) + { + PointF[] topDopWing = + { + new(_startPosX + 78, _startPosY + 56), + new(_startPosX + 75, _startPosY + 70), + new(_startPosX + 55, _startPosY + 50), + new(_startPosX + 60, _startPosY + 45), + }; + + PointF[] bottomDopWing = + { + new(_startPosX + 78, _startPosY + 110), + new(_startPosX + 75, _startPosY + 96), + new(_startPosX + 55, _startPosY + 116), + new(_startPosX + 60, _startPosY + 121), + }; + + g.FillPolygon(additionalBrush, topDopWing); + g.FillPolygon(additionalBrush, bottomDopWing); + + } + base.DrawTransport(g); + + if (fighterMilitary.Rocket) + { + PointF[] topRocket1 = + { + new(_startPosX + 100, _startPosY + 20), + new(_startPosX + 100, _startPosY + 30), + new(_startPosX + 112, _startPosY + 30), + new(_startPosX + 120, _startPosY + 25), + new(_startPosX + 112, _startPosY + 20) + }; + + PointF[] topRocket2 = + { + new(_startPosX + 100, _startPosY + 35), + new(_startPosX + 100, _startPosY + 45), + new(_startPosX + 112, _startPosY + 45), + new(_startPosX + 120, _startPosY + 40), + new(_startPosX + 112, _startPosY + 35) + }; + + PointF[] bottomRocket1 = + { + new(_startPosX + 100, _startPosY + 146), + new(_startPosX + 100, _startPosY + 136), + new(_startPosX + 112, _startPosY + 136), + new(_startPosX + 120, _startPosY + 141), + new(_startPosX + 112, _startPosY + 146) + }; + + PointF[] bottomRocket2 = + { + new(_startPosX + 100, _startPosY + 131), + new(_startPosX + 100, _startPosY + 121), + new(_startPosX + 112, _startPosY + 121), + new(_startPosX + 120, _startPosY + 126), + new(_startPosX + 112, _startPosY + 131) + }; + + g.FillPolygon(additionalBrush, topRocket1); + g.FillPolygon(additionalBrush, topRocket2); + g.FillPolygon(additionalBrush, bottomRocket1); + g.FillPolygon(additionalBrush, bottomRocket2); + } + } + } +} diff --git a/AirFighter/AirFighter/EntityAirFighter.cs b/AirFighter/AirFighter/EntityAirFighter.cs index 2873405..0390203 100644 --- a/AirFighter/AirFighter/EntityAirFighter.cs +++ b/AirFighter/AirFighter/EntityAirFighter.cs @@ -5,28 +5,19 @@ using System.Net.Sockets; using System.Text; using System.Threading.Tasks; -namespace AirFighter +namespace AirFighter.Entities { public class EntityAirFighter { public int Speed { get; private set; } public double Weight { get; private set; } public Color BodyColor { get; private set; } - public Color AdditionalColor { get; private set; } - public bool Rocket { get; private set; } - public bool Wing { get; private set; } public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool wing) - { - + public EntityAirFighter(int speed, double weight, Color bodyColor) + { Speed = speed; Weight = weight; BodyColor = bodyColor; - AdditionalColor = additionalColor; - Rocket = rocket; - Wing = wing; - } } - } diff --git a/AirFighter/AirFighter/EntityAirFighterMilitary.cs b/AirFighter/AirFighter/EntityAirFighterMilitary.cs new file mode 100644 index 0000000..4f2fd97 --- /dev/null +++ b/AirFighter/AirFighter/EntityAirFighterMilitary.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter.Entities +{ + public class EntityAirFighterMilitary : EntityAirFighter + { + public Color AdditionalColor { get; private set; } + public bool Rocket { get; private set; } + public bool Wing { get; private set; } + public EntityAirFighterMilitary(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool wing) + : base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + Rocket = rocket; + Wing = wing; + } + } +} diff --git a/AirFighter/AirFighter/FormAirFighter.Designer.cs b/AirFighter/AirFighter/FormAirFighter.Designer.cs index 0d240db..89dc618 100644 --- a/AirFighter/AirFighter/FormAirFighter.Designer.cs +++ b/AirFighter/AirFighter/FormAirFighter.Designer.cs @@ -1,6 +1,6 @@ namespace AirFighter { - partial class AirFighter + partial class FormAirFighter { /// /// Required designer variable. @@ -34,6 +34,9 @@ buttonRight = new Button(); buttonUp = new Button(); buttonCreate = new Button(); + ButtonStepAirFighter = new Button(); + comboBoxAirFighter = new ComboBox(); + ButtonCreateFighter = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).BeginInit(); SuspendLayout(); // @@ -97,29 +100,64 @@ // // buttonCreate // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.BackColor = Color.Azure; - buttonCreate.Font = new Font("Times New Roman", 14.25F, FontStyle.Bold, GraphicsUnit.Point); - buttonCreate.Location = new Point(12, 405); + buttonCreate.BackColor = SystemColors.Control; + buttonCreate.Font = new Font("Times New Roman", 12F, FontStyle.Regular, GraphicsUnit.Point); + buttonCreate.Location = new Point(12, 390); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(95, 33); - buttonCreate.TabIndex = 5; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = false; - buttonCreate.Click += buttonCreate_Click; + buttonCreate.Size = new Size(164, 48); + buttonCreate.TabIndex = 1; + buttonCreate.Text = "Создать истребитель с ракетами\r\n\r\n"; + buttonCreate.UseVisualStyleBackColor = true; + buttonCreate.Click += buttonCreateAirFighterMilitary_Click; // - // AirFighter + // ButtonStepAirFighter + // + ButtonStepAirFighter.BackColor = Color.Azure; + ButtonStepAirFighter.Font = new Font("Times New Roman", 9.75F, FontStyle.Bold, GraphicsUnit.Point); + ButtonStepAirFighter.Location = new Point(713, 57); + ButtonStepAirFighter.Name = "ButtonStepAirFighter"; + ButtonStepAirFighter.Size = new Size(75, 23); + ButtonStepAirFighter.TabIndex = 3; + ButtonStepAirFighter.Text = "Шаг"; + ButtonStepAirFighter.UseVisualStyleBackColor = true; + ButtonStepAirFighter.Click += ButtonStep_Click; + // + // comboBoxAirFighter + // + comboBoxAirFighter.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxAirFighter.FormattingEnabled = true; + comboBoxAirFighter.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder", "-" }); + comboBoxAirFighter.Location = new Point(667, 12); + comboBoxAirFighter.Name = "comboBoxAirFighter"; + comboBoxAirFighter.Size = new Size(121, 23); + comboBoxAirFighter.TabIndex = 6; + // + // ButtonCreateFighter + // + ButtonCreateFighter.Font = new Font("Times New Roman", 12F, FontStyle.Regular, GraphicsUnit.Point); + ButtonCreateFighter.Location = new Point(182, 390); + ButtonCreateFighter.Name = "ButtonCreateFighter"; + ButtonCreateFighter.Size = new Size(164, 47); + ButtonCreateFighter.TabIndex = 2; + ButtonCreateFighter.Text = "Создаать истребитель"; + ButtonCreateFighter.UseVisualStyleBackColor = true; + ButtonCreateFighter.Click += ButtonCreateFighter_Click; + // + // FormAirFighter // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(ButtonCreateFighter); + Controls.Add(comboBoxAirFighter); + Controls.Add(ButtonStepAirFighter); Controls.Add(buttonCreate); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(pictureBoxAirFighter); - Name = "AirFighter"; + Name = "FormAirFighter"; Text = "AirFighter"; ((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).EndInit(); ResumeLayout(false); @@ -134,5 +172,8 @@ private Button buttonRight; private Button buttonUp; private Button buttonCreate; + private Button ButtonStepAirFighter; + private ComboBox comboBoxAirFighter; + private Button ButtonCreateFighter; } } \ No newline at end of file diff --git a/AirFighter/AirFighter/FormAirFighter.cs b/AirFighter/AirFighter/FormAirFighter.cs index 49bee52..2875458 100644 --- a/AirFighter/AirFighter/FormAirFighter.cs +++ b/AirFighter/AirFighter/FormAirFighter.cs @@ -1,9 +1,14 @@ +using AirFighter.DrawningObjects; +using AirFighter.Drawnings; +using AirFighter.MovementStrategy; + namespace AirFighter { - public partial class AirFighter : Form + public partial class FormAirFighter : Form { private DrawningAirFighter? _drawningAirFighter; - public AirFighter() + private AbstractStrategy? _abstractStrategy; + public FormAirFighter() { InitializeComponent(); } @@ -19,15 +24,31 @@ namespace AirFighter pictureBoxAirFighter.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateAirFighterMilitary_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirFighter = new DrawningAirFighter(); - _drawningAirFighter.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)), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); - _drawningAirFighter.SetPosition(random.Next(10, 100), - random.Next(10, 100)); - + _drawningAirFighter = new DrawningAirFighterMilitary(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)), + pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); + _drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + private void ButtonCreateFighter_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningAirFighter = new DrawningAirFighter(random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); + _drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } private void buttonMove_Click(object sender, EventArgs e) @@ -54,5 +75,44 @@ Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pict } Draw(); } + + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawningAirFighter == null) + { + return; + } + if (comboBoxAirFighter.Enabled) + { + _abstractStrategy = comboBoxAirFighter.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectAirFighter(_drawningAirFighter), pictureBoxAirFighter.Width, + pictureBoxAirFighter.Height); + comboBoxAirFighter.Enabled = false; + } + if (_abstractStrategy != null) + { + _abstractStrategy.MakeStep(); + Draw(); + + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxAirFighter.Enabled = true; + _abstractStrategy = null; + } + } + + + } } } \ No newline at end of file diff --git a/AirFighter/AirFighter/IMoveableObject.cs b/AirFighter/AirFighter/IMoveableObject.cs new file mode 100644 index 0000000..dd3d71e --- /dev/null +++ b/AirFighter/AirFighter/IMoveableObject.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using AirFighter.Drawnings; + +namespace AirFighter.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + int GetStep { get; } + bool CheckCanMove(DirectionAirFighter direction); + void MoveObject(DirectionAirFighter direction); + } +} diff --git a/AirFighter/AirFighter/IMoveableObject_Realise.cs b/AirFighter/AirFighter/IMoveableObject_Realise.cs new file mode 100644 index 0000000..d2e1ede --- /dev/null +++ b/AirFighter/AirFighter/IMoveableObject_Realise.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using AirFighter.DrawningObjects; +using AirFighter.Drawnings; + +namespace AirFighter.MovementStrategy +{ + public class DrawningObjectAirFighter : IMoveableObject + { + private readonly DrawningAirFighter? _drawningAirFighter = null; + public DrawningObjectAirFighter(DrawningAirFighter drawningAirFighter) + { + _drawningAirFighter = drawningAirFighter; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningAirFighter == null || _drawningAirFighter.EntityAirFighter == + null) + { + return null; + } + return new ObjectParameters(_drawningAirFighter.GetPosX, _drawningAirFighter.GetPosY, _drawningAirFighter.GetWidth, _drawningAirFighter.GetHeight); + } + } + public int GetStep => (int)(_drawningAirFighter?.EntityAirFighter?.Step ?? 0); + public bool CheckCanMove(DirectionAirFighter direction) => + _drawningAirFighter?.CanMove(direction) ?? false; + public void MoveObject(DirectionAirFighter direction) => + _drawningAirFighter?.MoveTransport(direction); + } +} diff --git a/AirFighter/AirFighter/MoveToBorder.cs b/AirFighter/AirFighter/MoveToBorder.cs new file mode 100644 index 0000000..9469a5f --- /dev/null +++ b/AirFighter/AirFighter/MoveToBorder.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter.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()) + { + MoveRight(); + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } + } +} diff --git a/AirFighter/AirFighter/MoveToCenter.cs b/AirFighter/AirFighter/MoveToCenter.cs new file mode 100644 index 0000000..2cd9077 --- /dev/null +++ b/AirFighter/AirFighter/MoveToCenter.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter.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; + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + + if (Math.Abs(diffX) > GetStep() || Math.Abs(diffY) > GetStep()) + { + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } + } +} diff --git a/AirFighter/AirFighter/ObjectParameters.cs b/AirFighter/AirFighter/ObjectParameters.cs new file mode 100644 index 0000000..acda725 --- /dev/null +++ b/AirFighter/AirFighter/ObjectParameters.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter.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/AirFighter/AirFighter/Program.cs b/AirFighter/AirFighter/Program.cs index 2181c9e..61bec04 100644 --- a/AirFighter/AirFighter/Program.cs +++ b/AirFighter/AirFighter/Program.cs @@ -11,7 +11,7 @@ namespace AirFighter // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new AirFighter()); + Application.Run(new FormAirFighter()); } } } \ No newline at end of file diff --git a/AirFighter/AirFighter/Status.cs b/AirFighter/AirFighter/Status.cs new file mode 100644 index 0000000..ad3b2b7 --- /dev/null +++ b/AirFighter/AirFighter/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +} -- 2.25.1