diff --git a/AirBomber/AirBomber/AbstractStrategy.cs b/AirBomber/AirBomber/AbstractStrategy.cs new file mode 100644 index 0000000..7b44070 --- /dev/null +++ b/AirBomber/AirBomber/AbstractStrategy.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + 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(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(DirectionType.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + 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/AirBomber/AirBomber/DrawningAirBomber.cs b/AirBomber/AirBomber/DrawningAirBomber.cs index b9decf0..5ed19eb 100644 --- a/AirBomber/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/AirBomber/DrawningAirBomber.cs @@ -4,187 +4,54 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using AirBomber; namespace AirBomber { - public class DrawningAirBomber + public class DrawningAirBomber : DrawningAirPlane { - public EntityAirBomber? EntityAirBomber { get; private set; } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private readonly int _bomberWidth = 160; - private readonly int _bomberHeight = 118; - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bombs, Color bombsColor, bool fuelTanks, int width, int height) + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + /// Ширина картинки + /// Высота картинки + public DrawningAirBomber(int speed, double weight, Color bodyColor, Color + additionalColor, bool bombs, Color bombsColor, bool fuelTanks, int width, int height) : + base(speed, weight, bodyColor, width, height, 160, 118) { - _pictureWidth = width; - _pictureHeight = height; - if (width < _bomberWidth || height < _bomberHeight) + if (EntityAirPlane != null) { - return false; - } - EntityAirBomber = new EntityAirBomber(); - EntityAirBomber.Init(speed, weight, bodyColor, additionalColor, bombs, bombsColor, fuelTanks); - return true; - } - public void SetPosition(int x, int y) - { - if (x < 0 || x + _bomberWidth > _pictureWidth) - { - x = _pictureWidth - _bomberWidth; - } - - if (y < 0 || y + _bomberWidth > _pictureHeight) - { - y = _pictureHeight - _bomberHeight; - } - _startPosX = x; - _startPosY = y; - } - public void MoveTransport(DirectionType direction) - { - if (EntityAirBomber == null) - { - return; - } - switch (direction) - { - case DirectionType.Left: - if (_startPosX - EntityAirBomber.Step > 0) - { - _startPosX -= (int)EntityAirBomber.Step; - } - break; - case DirectionType.Up: - if (_startPosY - EntityAirBomber.Step > 0) - { - _startPosY -= (int)EntityAirBomber.Step; - } - break; - case DirectionType.Right: - if (_startPosX + EntityAirBomber.Step + _bomberWidth < _pictureWidth) - { - _startPosX += (int)EntityAirBomber.Step; - } - break; - case DirectionType.Down: - if (_startPosY + EntityAirBomber.Step + _bomberHeight < _pictureHeight) - { - _startPosY += (int)EntityAirBomber.Step; - } - break; + EntityAirPlane = new EntityAirBomber(speed, weight, bodyColor, additionalColor, bombs, bombsColor, fuelTanks); } } - public void DrawBomber(Graphics g) + public override void DrawPlane(Graphics g) { - if (EntityAirBomber == null) + if (EntityAirPlane is not EntityAirBomber airBomber) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAirBomber.AdditionalColor); - Brush bodyColor = new SolidBrush(EntityAirBomber.BodyColor); - Brush bombsColor = new SolidBrush(EntityAirBomber.BombsColor); - Brush wingsColor = new SolidBrush(Color.DeepPink); - if (EntityAirBomber.Bombs) - { - g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 20, 15, 29); - g.DrawEllipse(pen, _startPosX + 90, _startPosY + 20, 15, 29); - g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 70, 15, 29); - g.DrawEllipse(pen, _startPosX + 90, _startPosY + 70, 15, 29); - g.FillEllipse(bombsColor, _startPosX + 140, _startPosY + 50, 15, 15); - g.DrawEllipse(pen, _startPosX + 140, _startPosY + 50, 15, 15); - - } - g.FillPolygon(additionalBrush, new Point[] //nose - { - new Point(_startPosX + 19, _startPosY + 50), - new Point(_startPosX + 19, _startPosY + 69), - new Point(_startPosX + 1, _startPosY + 59), - } - ); - g.FillRectangle(bodyColor, _startPosX + 20, _startPosY + 50, 120, 20); //body - g.FillPolygon(additionalBrush, new Point[] //up left wing - { - new Point(_startPosX + 36, _startPosY + 49), - new Point(_startPosX + 36, _startPosY + 1), - new Point(_startPosX + 45, _startPosY + 1), - new Point(_startPosX + 55, _startPosY + 49), - } - ); - g.FillPolygon(additionalBrush, new Point[] //down left wing - { - new Point(_startPosX + 36, _startPosY + 71), - new Point(_startPosX + 36, _startPosY + 116), - new Point(_startPosX + 45, _startPosY + 116), - new Point(_startPosX + 54, _startPosY + 71), - } - ); - g.FillPolygon(wingsColor, new Point[] //up right wing - { - new Point(_startPosX + 120, _startPosY + 49), - new Point(_startPosX + 120, _startPosY + 42), - new Point(_startPosX + 140, _startPosY + 7), - new Point(_startPosX + 140, _startPosY + 49), - } - ); - g.FillPolygon(wingsColor, new Point[] //down right wing - { - new Point(_startPosX + 120, _startPosY + 70), - new Point(_startPosX + 120, _startPosY + 77), - new Point(_startPosX + 140, _startPosY + 112), - new Point(_startPosX + 140, _startPosY + 70), - } - ); - - g.DrawPolygon(pen, new Point[] //nose - { - new Point(_startPosX + 20, _startPosY + 49), - new Point(_startPosX + 20, _startPosY + 70), - new Point(_startPosX, _startPosY + 59), - } - ); - g.DrawRectangle(pen, _startPosX + 19, _startPosY + 49, 121, 21); //body - g.DrawPolygon(pen, new Point[] //up left wing - { - new Point(_startPosX + 35, _startPosY + 49), - new Point(_startPosX + 35, _startPosY), - new Point(_startPosX + 45, _startPosY), - new Point(_startPosX + 55, _startPosY + 49), - } - ); - g.DrawPolygon(pen, new Point[] //down left wing - { - new Point(_startPosX + 36, _startPosY + 71), - new Point(_startPosX + 36, _startPosY + 116), - new Point(_startPosX + 45, _startPosY + 116), - new Point(_startPosX + 54, _startPosY + 71), - } - ); - g.DrawPolygon(pen, new Point[] //up right wing - { - new Point(_startPosX + 120, _startPosY + 49), - new Point(_startPosX + 120, _startPosY + 42), - new Point(_startPosX + 140, _startPosY + 7), - new Point(_startPosX + 140, _startPosY + 49), - } - ); - g.DrawPolygon(pen, new Point[] //down right wing - { - new Point(_startPosX + 120, _startPosY + 70), - new Point(_startPosX + 120, _startPosY + 77), - new Point(_startPosX + 140, _startPosY + 112), - new Point(_startPosX + 140, _startPosY + 70), - } - ); - if (EntityAirBomber.FuelTanks) - { - g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 34, 20, 15); - g.DrawRectangle(pen, _startPosX + 63, _startPosY + 34, 20, 15); - g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 70, 20, 15); - g.DrawRectangle(pen, _startPosX + 63, _startPosY + 70, 20, 15); - } + Brush additionalBrush = new SolidBrush(airBomber.AdditionalColor); + Brush bombsColor = new SolidBrush(airBomber.BombsColor); + base.DrawPlane(g); + // обвесы + g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 20, 15, 29); + g.DrawEllipse(pen, _startPosX + 90, _startPosY + 20, 15, 29); + g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 70, 15, 29); + g.DrawEllipse(pen, _startPosX + 90, _startPosY + 70, 15, 29); + g.FillEllipse(bombsColor, _startPosX + 140, _startPosY + 50, 15, 15); + g.DrawEllipse(pen, _startPosX + 140, _startPosY + 50, 15, 15); + // fueltanks + g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 34, 20, 15); + g.DrawRectangle(pen, _startPosX + 63, _startPosY + 34, 20, 15); + g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 70, 20, 15); + g.DrawRectangle(pen, _startPosX + 63, _startPosY + 70, 20, 15); } } } diff --git a/AirBomber/AirBomber/DrawningAirPlane.cs b/AirBomber/AirBomber/DrawningAirPlane.cs new file mode 100644 index 0000000..758f92c --- /dev/null +++ b/AirBomber/AirBomber/DrawningAirPlane.cs @@ -0,0 +1,273 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber; + +namespace AirBomber +{ + 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 = 150; + /// + /// Высота прорисовки самолета + /// + protected readonly int _airPlaneHeight = 118; + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningAirPlane(int speed, double weight, Color bodyColor, int + width, int height) + { + // TODO: Продумать проверки + if (width < _airPlaneWidth || height < _airPlaneHeight) + { + 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) + { + // TODO: Продумать проверки + if (width < _airPlaneWidth || height < _airPlaneHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _airPlaneWidth = airPlaneWidth; + _airPlaneHeight = airPlaneHeight; + EntityAirPlane = new EntityAirPlane(speed, weight, bodyColor); + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + // TODO: Изменение x, y, если при установке объект выходит за границы + if (x < 0 || x + _airPlaneWidth > _pictureWidth) + { + x = _pictureWidth - _airPlaneWidth; + } + if (y < 0 || y + _airPlaneHeight > _pictureHeight) + { + y = _pictureHeight - _airPlaneHeight; + } + _startPosX = x; + _startPosY = y; + } + /// + /// Изменение направления перемещения + /// + /// Направление + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawPlane(Graphics g) + { + if (EntityAirPlane == null) + { + return; + } + Pen pen = new(Color.Black); + Brush bodyColor = new SolidBrush(EntityAirPlane.BodyColor); + Brush wingsColor = new SolidBrush(Color.DeepPink); + g.FillPolygon(bodyColor, new Point[] //nose + { + new Point(_startPosX + 19, _startPosY + 50), + new Point(_startPosX + 19, _startPosY + 69), + new Point(_startPosX + 1, _startPosY + 59), + } + ); + g.FillRectangle(bodyColor, _startPosX + 20, _startPosY + 50, 120, 20); //body + g.FillPolygon(bodyColor, new Point[] //up left wing + { + new Point(_startPosX + 36, _startPosY + 49), + new Point(_startPosX + 36, _startPosY + 1), + new Point(_startPosX + 45, _startPosY + 1), + new Point(_startPosX + 55, _startPosY + 49), + } + ); + g.FillPolygon(bodyColor, new Point[] //down left wing + { + new Point(_startPosX + 36, _startPosY + 71), + new Point(_startPosX + 36, _startPosY + 116), + new Point(_startPosX + 45, _startPosY + 116), + new Point(_startPosX + 54, _startPosY + 71), + } + ); + g.FillPolygon(wingsColor, new Point[] //up right wing + { + new Point(_startPosX + 120, _startPosY + 49), + new Point(_startPosX + 120, _startPosY + 42), + new Point(_startPosX + 140, _startPosY + 7), + new Point(_startPosX + 140, _startPosY + 49), + } + ); + g.FillPolygon(wingsColor, new Point[] //down right wing + { + new Point(_startPosX + 120, _startPosY + 70), + new Point(_startPosX + 120, _startPosY + 77), + new Point(_startPosX + 140, _startPosY + 112), + new Point(_startPosX + 140, _startPosY + 70), + } + ); + + g.DrawPolygon(pen, new Point[] //nose + { + new Point(_startPosX + 20, _startPosY + 49), + new Point(_startPosX + 20, _startPosY + 70), + new Point(_startPosX, _startPosY + 59), + } + ); + g.DrawRectangle(pen, _startPosX + 19, _startPosY + 49, 121, 21); //body + g.DrawPolygon(pen, new Point[] //up left wing + { + new Point(_startPosX + 35, _startPosY + 49), + new Point(_startPosX + 35, _startPosY), + new Point(_startPosX + 45, _startPosY), + new Point(_startPosX + 55, _startPosY + 49), + } + ); + g.DrawPolygon(pen, new Point[] //down left wing + { + new Point(_startPosX + 36, _startPosY + 71), + new Point(_startPosX + 36, _startPosY + 116), + new Point(_startPosX + 45, _startPosY + 116), + new Point(_startPosX + 54, _startPosY + 71), + } + ); + g.DrawPolygon(pen, new Point[] //up right wing + { + new Point(_startPosX + 120, _startPosY + 49), + new Point(_startPosX + 120, _startPosY + 42), + new Point(_startPosX + 140, _startPosY + 7), + new Point(_startPosX + 140, _startPosY + 49), + } + ); + g.DrawPolygon(pen, new Point[] //down right wing + { + new Point(_startPosX + 120, _startPosY + 70), + new Point(_startPosX + 120, _startPosY + 77), + new Point(_startPosX + 140, _startPosY + 112), + new Point(_startPosX + 140, _startPosY + 70), + } + ); + } + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _airPlaneWidth; + /// + /// Высота объекта + /// + public int GetHeight => _airPlaneHeight; + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityAirPlane == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityAirPlane.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityAirPlane.Step > 0, + // вправо + DirectionType.Right => _startPosX + EntityAirPlane.Step + _airPlaneWidth < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + EntityAirPlane.Step + _airPlaneHeight < _pictureHeight, + _ => false, + }; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MovePlane(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; + } + } + } +} diff --git a/AirBomber/AirBomber/DrawningObjectAirPlane.cs b/AirBomber/AirBomber/DrawningObjectAirPlane.cs new file mode 100644 index 0000000..bce8cdb --- /dev/null +++ b/AirBomber/AirBomber/DrawningObjectAirPlane.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + 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?.MovePlane(direction); + } +} diff --git a/AirBomber/AirBomber/EntityAirBomber.cs b/AirBomber/AirBomber/EntityAirBomber.cs index 051adc5..e64ad03 100644 --- a/AirBomber/AirBomber/EntityAirBomber.cs +++ b/AirBomber/AirBomber/EntityAirBomber.cs @@ -3,25 +3,19 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using AirBomber; namespace AirBomber { - public class EntityAirBomber + public class EntityAirBomber : 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 Bombs { get; private set; } public Color BombsColor { get; private set; } public bool FuelTanks { get; private set; } - public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool bombs, Color bombsColor, bool fuelTanks) + public EntityAirBomber(int speed, double weight, Color bodyColor, Color + additionalColor, bool bombs, Color bombsColor, bool fuelTanks) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Bombs = bombs; BombsColor = bombsColor; diff --git a/AirBomber/AirBomber/EntityAirPlane.cs b/AirBomber/AirBomber/EntityAirPlane.cs new file mode 100644 index 0000000..a8341c9 --- /dev/null +++ b/AirBomber/AirBomber/EntityAirPlane.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber; + +namespace AirBomber +{ + public class EntityAirPlane + { + /// + /// Скорость + /// + 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 EntityAirPlane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/AirBomber/AirBomber/FormAirBomber.Designer.cs b/AirBomber/AirBomber/FormAirBomber.Designer.cs index 5e931c9..7dd0993 100644 --- a/AirBomber/AirBomber/FormAirBomber.Designer.cs +++ b/AirBomber/AirBomber/FormAirBomber.Designer.cs @@ -28,25 +28,28 @@ /// private void InitializeComponent() { - buttonCreate = new Button(); + buttonCreateAirBomber = new Button(); buttonDown = new Button(); buttonLeft = new Button(); buttonRight = new Button(); buttonUp = new Button(); pictureBoxAirBomber = new PictureBox(); + comboBoxStrategy = new ComboBox(); + buttonCreateAirPlane = new Button(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); SuspendLayout(); // - // buttonCreate + // buttonCreateAirBomber // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(29, 479); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(148, 34); - buttonCreate.TabIndex = 0; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreateAirBomber.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateAirBomber.Location = new Point(29, 447); + buttonCreateAirBomber.Name = "buttonCreateAirBomber"; + buttonCreateAirBomber.Size = new Size(191, 66); + buttonCreateAirBomber.TabIndex = 0; + buttonCreateAirBomber.Text = "Создать бомбардировщик"; + buttonCreateAirBomber.UseVisualStyleBackColor = true; + buttonCreateAirBomber.Click += buttonCreateAirBomber_Click; // // buttonDown // @@ -106,16 +109,52 @@ pictureBoxAirBomber.TabIndex = 5; pictureBoxAirBomber.TabStop = false; // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" }); + comboBoxStrategy.Location = new Point(755, 26); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(219, 33); + comboBoxStrategy.TabIndex = 6; + // + // buttonCreateAirPlane + // + buttonCreateAirPlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateAirPlane.Location = new Point(249, 447); + buttonCreateAirPlane.Name = "buttonCreateAirPlane"; + buttonCreateAirPlane.Size = new Size(191, 65); + buttonCreateAirPlane.TabIndex = 7; + buttonCreateAirPlane.Text = "Создать самолёт"; + buttonCreateAirPlane.UseVisualStyleBackColor = true; + buttonCreateAirPlane.Click += buttonCreateAirPlane_Click; + // + // buttonStep + // + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(862, 77); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(112, 49); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // // FormAirBomber // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(986, 540); + Controls.Add(buttonStep); + Controls.Add(buttonCreateAirPlane); + Controls.Add(comboBoxStrategy); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonDown); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateAirBomber); Controls.Add(pictureBoxAirBomber); Name = "FormAirBomber"; Text = "Бомбардировщик"; @@ -125,11 +164,14 @@ #endregion - private Button buttonCreate; + private Button buttonCreateAirBomber; private Button buttonDown; private Button buttonLeft; private Button buttonRight; private Button buttonUp; private PictureBox pictureBoxAirBomber; + private ComboBox comboBoxStrategy; + private Button buttonCreateAirPlane; + private Button buttonStep; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormAirBomber.cs b/AirBomber/AirBomber/FormAirBomber.cs index cf453fb..aa7e5ba 100644 --- a/AirBomber/AirBomber/FormAirBomber.cs +++ b/AirBomber/AirBomber/FormAirBomber.cs @@ -2,37 +2,52 @@ { public partial class FormAirBomber : Form { - private DrawningAirBomber? _drawningAirBomber; + private DrawningAirPlane? _drawningAirPlane; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _abstractStrategy; public FormAirBomber() { InitializeComponent(); } private void Draw() { - if (_drawningAirBomber == null) + if (_drawningAirPlane == null) { return; } Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningAirBomber.DrawBomber(gr); + _drawningAirPlane.DrawPlane(gr); pictureBoxAirBomber.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateAirBomber_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirBomber = new DrawningAirBomber(); - _drawningAirBomber.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)), + _drawningAirPlane = 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)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); - - _drawningAirBomber.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Convert.ToBoolean(random.Next(0, 2)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), + pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + + _drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + + } + 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)), + pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + + _drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void buttonMove_Click(object sender, EventArgs e) { - if (_drawningAirBomber == null) + if (_drawningAirPlane == null) { return; } @@ -40,19 +55,56 @@ switch (name) { case "buttonUp": - _drawningAirBomber.MoveTransport(DirectionType.Up); + _drawningAirPlane.MovePlane(DirectionType.Up); break; case "buttonDown": - _drawningAirBomber.MoveTransport(DirectionType.Down); + _drawningAirPlane.MovePlane(DirectionType.Down); break; case "buttonLeft": - _drawningAirBomber.MoveTransport(DirectionType.Left); + _drawningAirPlane.MovePlane(DirectionType.Left); break; case "buttonRight": - _drawningAirBomber.MoveTransport(DirectionType.Right); + _drawningAirPlane.MovePlane(DirectionType.Right); break; } Draw(); } + private void buttonStep_Click(object sender, EventArgs e) + { + 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), pictureBoxAirBomber.Width, + pictureBoxAirBomber.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + + } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/IMoveableObject.cs b/AirBomber/AirBomber/IMoveableObject.cs new file mode 100644 index 0000000..2c271c2 --- /dev/null +++ b/AirBomber/AirBomber/IMoveableObject.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/AirBomber/AirBomber/MoveToBorder.cs b/AirBomber/AirBomber/MoveToBorder.cs new file mode 100644 index 0000000..b853ffd --- /dev/null +++ b/AirBomber/AirBomber/MoveToBorder.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) return false; + + return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep(); + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) return; + if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight(); + if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown(); + } + } +} diff --git a/AirBomber/AirBomber/MoveToCenter.cs b/AirBomber/AirBomber/MoveToCenter.cs new file mode 100644 index 0000000..f600563 --- /dev/null +++ b/AirBomber/AirBomber/MoveToCenter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + 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/AirBomber/ObjectParameters.cs b/AirBomber/AirBomber/ObjectParameters.cs new file mode 100644 index 0000000..99e383f --- /dev/null +++ b/AirBomber/AirBomber/ObjectParameters.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + 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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index f167f45..76b85fe 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -7,7 +7,6 @@ namespace AirBomber /// [STAThread] static void Main() - { // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. diff --git a/AirBomber/AirBomber/Status.cs b/AirBomber/AirBomber/Status.cs new file mode 100644 index 0000000..3e0828d --- /dev/null +++ b/AirBomber/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 + } +}