diff --git a/AirFighter/AbstractStrategy.cs b/AirFighter/AbstractStrategy.cs new file mode 100644 index 0000000..65269db --- /dev/null +++ b/AirFighter/AbstractStrategy.cs @@ -0,0 +1,121 @@ + + +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace ProjectAirFighter.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(DirectionType.Left); + +protected bool MoveRight() => MoveTo(DirectionType.Right); + +protected bool MoveUp() => MoveTo(DirectionType.Up); + +protected bool MoveDown() => MoveTo(DirectionType.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + +private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} + diff --git a/AirFighter/DrawningAirFighter.cs b/AirFighter/DrawningAirFighter.cs index 0db1893..88a698b 100644 --- a/AirFighter/DrawningAirFighter.cs +++ b/AirFighter/DrawningAirFighter.cs @@ -1,156 +1,31 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ProjectAirFighter.Entities; -namespace ProjectAirFighter +namespace ProjectAirFighter.DrawningObjects { - public class DrawningAirFighter + public class DrawningAirFighter : DrawningAirplane { - /// - /// Класс-сущность - /// - public EntityAirFighter? EntityAirFighter { get; private set; } - /// - - /// - private int _pictureWidth; - /// - - /// - private int _pictureHeight; - /// - - - /// - private int _startPosX; - /// - - /// - private int _startPosY; - /// - - /// - private readonly int _airfighterWidth = 163; - /// - - /// - private readonly int _airfighterHeight = 70; - - private readonly int _airfighterwingkorpusHeight = 90; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// - /// Дополнительный цвет - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена, - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool racket, bool wing, int width, int height) + public DrawningAirFighter(int speed, double weight, Color bodyColor, Color + additionalColor, bool racket, bool wing, int width, int height) : + base(speed, weight, bodyColor, width, height, 160, 160) { - // TODO: Продумать проверки - if (width <= _airfighterWidth || height <= _airfighterHeight) - return false; - _pictureWidth = width; - _pictureHeight = height; - EntityAirFighter = new EntityAirFighter(); - EntityAirFighter.Init(speed, weight, bodyColor, additionalColor, - racket, wing); - return true; - } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - // TODO: Изменение x, y - _startPosX = x; - _startPosY = y; - if (x + _airfighterWidth >= _pictureWidth || y + _airfighterHeight >= _pictureHeight) + if (EntityAirplane != null) { - _startPosX = 1; - _startPosY = (_airfighterHeight+_airfighterwingkorpusHeight)/2; + EntityAirplane = new EntityAirFighter(speed, weight, bodyColor, additionalColor, racket, wing); } } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) + + public override void DrawTransport(Graphics g) { - if (EntityAirFighter == null) - + if (EntityAirplane is not EntityAirFighter airFighter) { return; } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - EntityAirFighter.Step > 0) - { - _startPosX -= (int)EntityAirFighter.Step; - } - - break; - //вверх - case DirectionType.Up: - if (_startPosY - _airfighterHeight - EntityAirFighter.Step > 0) - { - _startPosY -= (int)EntityAirFighter.Step; - } - break; - // вправо - case DirectionType.Right: - if (_startPosX + EntityAirFighter.Step + _airfighterWidth < _pictureWidth) - { - _startPosX += (int)EntityAirFighter.Step; - } - else - _startPosX = _pictureWidth - _airfighterWidth; - break; - //вниз - case DirectionType.Down: - if (_startPosY + EntityAirFighter.Step + _airfighterwingkorpusHeight < _pictureHeight) - { - - _startPosY += (int)EntityAirFighter.Step; - } - else - _startPosY = _pictureHeight - _airfighterwingkorpusHeight; - break; - - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityAirFighter == null) - { - return; - } + Brush additionalBrush = new SolidBrush(airFighter.AdditionalColor); Pen pen = new(Color.Black); - Brush additionalBrush = new - SolidBrush(EntityAirFighter.AdditionalColor); - // ракеты - if (EntityAirFighter.Racket) + base.DrawTransport(g); + if (airFighter.Racket) { - Brush brGrey = new SolidBrush(Color.LightGray); g.FillRectangle(brGrey, _startPosX + 70, _startPosY - 15, 10, 10); g.DrawRectangle(pen, _startPosX + 70, _startPosY - 15, 10, 10); @@ -172,7 +47,6 @@ namespace ProjectAirFighter new Point(_startPosX + 70, _startPosY - 40), new Point(_startPosX + 60,_startPosY -35) }; - g.FillPolygon(brRed, noseracketPoints2); g.DrawPolygon(pen, noseracketPoints2); g.FillPolygon(brRed, noseracketPoints); @@ -186,11 +60,9 @@ namespace ProjectAirFighter new Point(_startPosX + 70, _startPosY + 69), new Point(_startPosX + 60,_startPosY + 64) }; - g.FillPolygon(brRed, noseracketPoints3); g.DrawPolygon(pen, noseracketPoints3); - g.FillRectangle(brGrey, _startPosX + 70, _startPosY + 34, 10, 10); g.DrawRectangle(pen, _startPosX + 70, _startPosY + 34, 10, 10); Point[] noseracketPoints4 = @@ -199,88 +71,10 @@ namespace ProjectAirFighter new Point(_startPosX + 70, _startPosY + 44), new Point(_startPosX + 60,_startPosY + 39) }; - g.FillPolygon(brRed, noseracketPoints4); g.DrawPolygon(pen, noseracketPoints4); - } - - - - - Point[] nosePoints = - { - new Point(_startPosX + 20, _startPosY + 4), - new Point(_startPosX + 20, _startPosY + 24), - new Point(_startPosX-3,_startPosY + 12) - }; - Brush brBlack = new SolidBrush(Color.Black); - g.FillPolygon(brBlack, nosePoints); - g.DrawPolygon(pen, nosePoints); - - Point[] rightwingPoints = - { - new Point(_startPosX + 80, _startPosY + 4), - new Point(_startPosX+80,_startPosY - 66), - new Point(_startPosX+85,_startPosY - 66), - new Point(_startPosX + 100, _startPosY + 4) - - - - }; - g.FillPolygon(additionalBrush, rightwingPoints); - g.DrawPolygon(pen, rightwingPoints); - - Point[] lefttwingPoints = - { - new Point(_startPosX + 80, _startPosY + 24), - new Point(_startPosX + 100, _startPosY + 24), - new Point(_startPosX+85,_startPosY + 94), - new Point(_startPosX+80,_startPosY + 94) - - }; - g.FillPolygon(additionalBrush, lefttwingPoints); - g.DrawPolygon(pen, lefttwingPoints); - - - Point[] leftenginePoints = - { - new Point(_startPosX + 140, _startPosY + 24), - new Point(_startPosX + 160, _startPosY + 24), - new Point(_startPosX+160,_startPosY + 50), - new Point(_startPosX+140,_startPosY + 32) - - - - - }; - g.FillPolygon(additionalBrush, leftenginePoints); - g.DrawPolygon(pen, leftenginePoints); - - - - Point[] rightenginePoints = - { - new Point(_startPosX + 140, _startPosY + 24), - new Point(_startPosX + 160, _startPosY + 24), - new Point(_startPosX+160,_startPosY - 16), - new Point(_startPosX+140,_startPosY -4) - - - - - }; - g.FillPolygon(additionalBrush, rightenginePoints); - g.DrawPolygon(pen, rightenginePoints); - - g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 4, 140, 20); - g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 140, 20); - - - - - // крыло - if (EntityAirFighter.Wing) + if (airFighter.Wing) { Point[] doprightwingPoints = { @@ -289,8 +83,6 @@ namespace ProjectAirFighter new Point(_startPosX+35,_startPosY - 34), new Point(_startPosX + 45, _startPosY + 4) - - }; g.FillPolygon(additionalBrush, doprightwingPoints); g.DrawPolygon(pen, doprightwingPoints); @@ -305,9 +97,7 @@ namespace ProjectAirFighter }; g.FillPolygon(additionalBrush, doplefttwingPoints); g.DrawPolygon(pen, doplefttwingPoints); - - } } } -} \ No newline at end of file +} diff --git a/AirFighter/DrawningAirplane.cs b/AirFighter/DrawningAirplane.cs new file mode 100644 index 0000000..984c2b3 --- /dev/null +++ b/AirFighter/DrawningAirplane.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAirFighter.Entities; + +namespace ProjectAirFighter.DrawningObjects +{ + public class DrawningAirplane + { + public EntityAirplane? EntityAirplane { get; protected set; } + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected readonly int _airplaneWidth = 163; + protected readonly int _airplaneHeight = 160; + protected readonly int _airplanewingHeight = 70; + protected readonly int _airplanerwingkorpusHeight = 90; + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _airplaneWidth; + public int GetHeight => _airplaneHeight; + + public DrawningAirplane(int speed, double weight, Color bodyColor,int width, int height) + { + if (width <= _airplaneWidth || height <= _airplanewingHeight) + return; + _pictureWidth = width; + + _pictureHeight = height; + + EntityAirplane = new EntityAirplane(speed, weight, bodyColor); + } + + protected DrawningAirplane(int speed, double weight, Color bodyColor, int +width, int height, int airplaneWidth, int airplaneHeight) + { + if (width <= _airplaneWidth || height <= _airplanewingHeight) + return; + _pictureWidth = width; + _pictureHeight = height; + _airplaneWidth = airplaneWidth; + _airplanewingHeight = airplaneHeight; + + EntityAirplane = new EntityAirplane(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y) + { + if (EntityAirplane == null) + return; + _startPosX = x; + _startPosY = y; + if (x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight) + { + _startPosX = 1; + _startPosY = (_airplanewingHeight+_airplanerwingkorpusHeight)/2; + } + } + public bool CanMove(DirectionType direction) + { + if (EntityAirplane == null) + { + return false; + } + return direction switch + { + DirectionType.Left => _startPosX - EntityAirplane.Step > 0, + + DirectionType.Up => _startPosY - EntityAirplane.Step - (_airplaneHeight - _airplaneHeight * 125 / 1000) / 2 > 0, + + DirectionType.Right => _startPosX+ EntityAirplane.Step + _airplaneWidth < _pictureWidth, + + DirectionType.Down => _startPosY + EntityAirplane.Step + _airplanerwingkorpusHeight < _pictureHeight, + + }; + } + + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityAirplane == null) + + { + return; + } + switch (direction) + { + case DirectionType.Left: + _startPosX -= (int)EntityAirplane.Step; + break; + + case DirectionType.Up: + _startPosY -= (int)EntityAirplane.Step; + break; + + case DirectionType.Right: + _startPosX += (int)EntityAirplane.Step; + break; + + case DirectionType.Down: + _startPosY += (int)EntityAirplane.Step; + break; + } + } + + public virtual void DrawTransport(Graphics g) + { + if (EntityAirplane == null) + { + return; + } + Pen pen = new(Color.Black); + + Brush br = new SolidBrush(EntityAirplane.BodyColor); + Point[] nosePoints = + { + new Point(_startPosX + 20, _startPosY + 4), + new Point(_startPosX + 20, _startPosY + 24), + new Point(_startPosX-3,_startPosY + 12) + }; + Brush brBlack = new SolidBrush(Color.Black); + g.FillPolygon(brBlack, nosePoints); + g.DrawPolygon(pen, nosePoints); + + Point[] rightwingPoints = + { + new Point(_startPosX + 80, _startPosY + 4), + new Point(_startPosX+80,_startPosY - 64), + new Point(_startPosX+85,_startPosY - 64), + new Point(_startPosX + 100, _startPosY + 4) + + }; + + g.DrawPolygon(pen, rightwingPoints); + g.FillPolygon(br, rightwingPoints); + + Point[] lefttwingPoints = + { + new Point(_startPosX + 80, _startPosY + 24), + new Point(_startPosX + 100, _startPosY + 24), + new Point(_startPosX+85,_startPosY + 94), + new Point(_startPosX+80,_startPosY + 94) + + }; + + g.DrawPolygon(pen, lefttwingPoints); + g.FillPolygon(br, lefttwingPoints); + + Point[] leftenginePoints = + { + new Point(_startPosX + 140, _startPosY + 24), + new Point(_startPosX + 160, _startPosY + 24), + new Point(_startPosX+160,_startPosY + 50), + new Point(_startPosX+140,_startPosY + 32) + + }; + + g.DrawPolygon(pen, leftenginePoints); + g.FillPolygon(br, leftenginePoints); + + Point[] rightenginePoints = + { + new Point(_startPosX + 140, _startPosY + 24), + new Point(_startPosX + 160, _startPosY + 24), + new Point(_startPosX+160,_startPosY - 16), + new Point(_startPosX+140,_startPosY -4) + + }; + + g.DrawPolygon(pen, rightenginePoints); + g.FillPolygon(br, rightenginePoints); + + g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 140, _airplaneHeight * 125 / 1000); + g.FillRectangle(br, _startPosX + 20, _startPosY + 4, 140, _airplaneHeight * 125 / 1000); + + } + } +} \ No newline at end of file diff --git a/AirFighter/DrawningObjectAirplane.cs b/AirFighter/DrawningObjectAirplane.cs new file mode 100644 index 0000000..ad2c66f --- /dev/null +++ b/AirFighter/DrawningObjectAirplane.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAirFighter.DrawningObjects; + +namespace ProjectAirFighter.MovementStrategy +{ + public class DrawningObjectAirplane: IMoveableObject + { + private readonly DrawningAirplane? _drawningAirplane = null; + public DrawningObjectAirplane(DrawningAirplane drawningAirplane) + { + _drawningAirplane = drawningAirplane; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningAirplane == null || _drawningAirplane.EntityAirplane == + null) + { + return null; + } + return new ObjectParameters(_drawningAirplane.GetPosX, + _drawningAirplane.GetPosY, _drawningAirplane.GetWidth, _drawningAirplane.GetHeight); + } + } + public int GetStep => (int)(_drawningAirplane?.EntityAirplane?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawningAirplane?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawningAirplane?.MoveTransport(direction); + } +} + + + diff --git a/AirFighter/EntityAirFighter.cs b/AirFighter/EntityAirFighter.cs index 60d79c1..ceecf1b 100644 --- a/AirFighter/EntityAirFighter.cs +++ b/AirFighter/EntityAirFighter.cs @@ -4,63 +4,21 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectAirFighter +namespace ProjectAirFighter.Entities + { - public class EntityAirFighter + public class EntityAirFighter: EntityAirplane { - /// - /// Скорость - /// - 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 Racket { get; private set; } - /// - /// Признак (опция) наличия антикрыла - /// public bool Wing { get; private set; } - /// - /// Признак (опция) наличия гоночной полосы - /// - /// - /// Шаг перемещения автомобиля - /// - public double Step => (double)Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса спортивного автомобиля - /// - /// Скорость - /// Вес автомобиля - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия обвеса - /// (double)Speed * 100 / Weight; + + public EntityAirplane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} + + diff --git a/AirFighter/FormAirFighter.Designer.cs b/AirFighter/FormAirFighter.Designer.cs index 5d55849..91438ca 100644 --- a/AirFighter/FormAirFighter.Designer.cs +++ b/AirFighter/FormAirFighter.Designer.cs @@ -2,15 +2,8 @@ { partial class FormAirFighter { - /// - /// Required designer variable. - /// private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) @@ -19,7 +12,6 @@ } base.Dispose(disposing); } - #region Windows Form Designer generated code /// @@ -34,6 +26,9 @@ this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.ButtonCreateAirplane = new System.Windows.Forms.Button(); + this.ButtonStep = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirFighter)).BeginInit(); this.SuspendLayout(); // @@ -50,11 +45,11 @@ // ButtonCreateAirFighter // this.ButtonCreateAirFighter.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.ButtonCreateAirFighter.Location = new System.Drawing.Point(0, 424); + this.ButtonCreateAirFighter.Location = new System.Drawing.Point(0, 405); this.ButtonCreateAirFighter.Name = "ButtonCreateAirFighter"; - this.ButtonCreateAirFighter.Size = new System.Drawing.Size(94, 29); + this.ButtonCreateAirFighter.Size = new System.Drawing.Size(144, 48); this.ButtonCreateAirFighter.TabIndex = 1; - this.ButtonCreateAirFighter.Text = "Создать"; + this.ButtonCreateAirFighter.Text = "Создать военный самолёт"; this.ButtonCreateAirFighter.UseVisualStyleBackColor = true; this.ButtonCreateAirFighter.Click += new System.EventHandler(this.ButtonCreateAirFighter_Click); // @@ -106,11 +101,47 @@ this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); // + // comboBoxStrategy + // + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "0", + "1"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(731, 0); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28); + this.comboBoxStrategy.TabIndex = 6; + // + // ButtonCreateAirplane + // + this.ButtonCreateAirplane.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.ButtonCreateAirplane.Location = new System.Drawing.Point(150, 405); + this.ButtonCreateAirplane.Name = "ButtonCreateAirplane"; + this.ButtonCreateAirplane.Size = new System.Drawing.Size(143, 47); + this.ButtonCreateAirplane.TabIndex = 7; + this.ButtonCreateAirplane.Text = "Создать самолёт"; + this.ButtonCreateAirplane.UseVisualStyleBackColor = true; + this.ButtonCreateAirplane.Click += new System.EventHandler(this.ButtonCreateAirplane_Click); + // + // ButtonStep + // + this.ButtonStep.Location = new System.Drawing.Point(788, 34); + this.ButtonStep.Name = "ButtonStep"; + this.ButtonStep.Size = new System.Drawing.Size(94, 29); + this.ButtonStep.TabIndex = 8; + this.ButtonStep.Text = "Шаг"; + this.ButtonStep.UseVisualStyleBackColor = true; + this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click); + // // FormAirFighter // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(882, 453); + this.Controls.Add(this.ButtonStep); + this.Controls.Add(this.ButtonCreateAirplane); + this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonLeft); @@ -127,12 +158,14 @@ } #endregion - private PictureBox pictureBoxAirFighter; private Button ButtonCreateAirFighter; private Button buttonUp; private Button buttonLeft; private Button buttonDown; private Button buttonRight; + private ComboBox comboBoxStrategy; + private Button ButtonCreateAirplane; + private Button ButtonStep; } } \ No newline at end of file diff --git a/AirFighter/FormAirFighter.cs b/AirFighter/FormAirFighter.cs index 4546eff..c1d6bfa 100644 --- a/AirFighter/FormAirFighter.cs +++ b/AirFighter/FormAirFighter.cs @@ -1,84 +1,118 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using ProjectAirFighter.MovementStrategy; +using ProjectAirFighter.DrawningObjects; namespace ProjectAirFighter { public partial class FormAirFighter : Form { - - private DrawningAirFighter? _drawningAirFighter; + private DrawningAirplane? _drawningAirplane; + private AbstractStrategy? _abstractStrategy; public FormAirFighter() { InitializeComponent(); } + private void Draw() { - if (_drawningAirFighter == null) + if (_drawningAirplane == null) { return; } Bitmap bmp = new(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningAirFighter.DrawTransport(gr); + _drawningAirplane.DrawTransport(gr); pictureBoxAirFighter.Image = bmp; } + private void ButtonCreateAirplane_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningAirplane = new DrawningAirplane(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); + _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(70, 100)); + + Draw(); + } private void ButtonCreateAirFighter_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirFighter = new DrawningAirFighter(); - - _drawningAirFighter.Init(random.Next(100, 300), + _drawningAirplane = new DrawningAirFighter (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)), + 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)), - Convert.ToBoolean(random.Next(0, 2)), - pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); - _drawningAirFighter.SetPosition(random.Next(10, 100), - random.Next(70, 100)); + _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(70, 100)); Draw(); } - - private void buttonMove_Click(object sender, EventArgs e) - { - if (_drawningAirFighter == null) + private void buttonMove_Click(object sender, EventArgs e) { - return; + if (_drawningAirplane == null) + { + return; + } + string name = ((Button)sender)?.Name ?? string.Empty; + switch (name) + { + case "buttonUp": + _drawningAirplane.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + _drawningAirplane.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + _drawningAirplane.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + _drawningAirplane.MoveTransport(DirectionType.Right); + break; + } + Draw(); } - string name = ((Button)sender)?.Name ?? string.Empty; - switch (name) + + private void ButtonStep_Click(object sender, EventArgs e) { - case "buttonUp": - _drawningAirFighter.MoveTransport(DirectionType.Up); - break; - case "buttonDown": - _drawningAirFighter.MoveTransport(DirectionType.Down); - break; - case "buttonLeft": - _drawningAirFighter.MoveTransport(DirectionType.Left); - break; - case "buttonRight": - _drawningAirFighter.MoveTransport(DirectionType.Right); - break; + if (_drawningAirplane == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectAirplane(_drawningAirplane), pictureBoxAirFighter.Width, + pictureBoxAirFighter.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } } - Draw(); } - } - } - + diff --git a/AirFighter/IMoveableObject.cs b/AirFighter/IMoveableObject.cs new file mode 100644 index 0000000..3363beb --- /dev/null +++ b/AirFighter/IMoveableObject.cs @@ -0,0 +1,28 @@ +namespace ProjectAirFighter.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/AirFighter/MoveToBorder.cs b/AirFighter/MoveToBorder.cs new file mode 100644 index 0000000..fed5469 --- /dev/null +++ b/AirFighter/MoveToBorder.cs @@ -0,0 +1,50 @@ +using ProjectAirFighter.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirFighter.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 + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - (FieldWidth - 1); + if (Math.Abs(diffX) > GetStep()) + { + if (diffX < 0) + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - (FieldHeight - 1); + if (Math.Abs(diffY) > GetStep()) + { + if (diffY < 0) + { + MoveDown(); + } + } + } + } +} \ No newline at end of file diff --git a/AirFighter/MoveToCenter.cs b/AirFighter/MoveToCenter.cs new file mode 100644 index 0000000..8026431 --- /dev/null +++ b/AirFighter/MoveToCenter.cs @@ -0,0 +1,51 @@ +namespace ProjectAirFighter.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/AirFighter/ObjectParameters.cs b/AirFighter/ObjectParameters.cs new file mode 100644 index 0000000..9f857b7 --- /dev/null +++ b/AirFighter/ObjectParameters.cs @@ -0,0 +1,31 @@ +namespace ProjectAirFighter.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 * 125 / 1000 + (_height-_height * 125 / 1000)/2 ; + + public int ObjectMiddleHorizontal => _x + _width / 2; + + public int ObjectMiddleVertical => _y +(_height - _height * 125 / 1000) / 2 / 2; + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + + } +} diff --git a/AirFighter/Program.cs b/AirFighter/Program.cs index 5f16c92..e7a24a0 100644 --- a/AirFighter/Program.cs +++ b/AirFighter/Program.cs @@ -4,18 +4,12 @@ namespace ProjectAirFighter { internal static class Program { - /// - /// The main entry point for the application. - /// [STAThread] static void Main() { - // To customize application configuration such as set high DPI ApplicationConfiguration.Initialize(); Application.Run(new FormAirFighter()); } } - - } \ No newline at end of file diff --git a/AirFighter/Status.cs b/AirFighter/Status.cs new file mode 100644 index 0000000..f2c66bf --- /dev/null +++ b/AirFighter/Status.cs @@ -0,0 +1,12 @@ +namespace ProjectAirFighter.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +} \ No newline at end of file