Compare commits

..

4 Commits

11 changed files with 405 additions and 44 deletions

View File

@ -6,7 +6,126 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal class AbstractStrategy public abstract class AbstractStrategy
{ {
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
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;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveLeft() => MoveTo(DirectionType.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveRight() => MoveTo(DirectionType.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveUp() => MoveTo(DirectionType.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveDown() => MoveTo(DirectionType.Down);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
} }
} }

View File

@ -42,23 +42,20 @@ namespace AirBomber
Brush bombsColor = new SolidBrush(airBomber.BombsColor); Brush bombsColor = new SolidBrush(airBomber.BombsColor);
base.DrawPlane(g); base.DrawPlane(g);
// обвесы // обвесы
if (airBomber.Bombs)
{
g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 20, 15, 29); g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 20, 15, 29);
g.DrawEllipse(pen, _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.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 70, 15, 29);
g.DrawEllipse(pen, _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.FillEllipse(bombsColor, _startPosX + 140, _startPosY + 50, 15, 15);
g.DrawEllipse(pen, _startPosX + 140, _startPosY + 50, 15, 15); g.DrawEllipse(pen, _startPosX + 140, _startPosY + 50, 15, 15);
}
// fueltanks // fueltanks
if (airBomber.FuelTanks)
{
g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 34, 20, 15); g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 34, 20, 15);
g.DrawRectangle(pen, _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.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 70, 20, 15);
g.DrawRectangle(pen, _startPosX + 63, _startPosY + 70, 20, 15); g.DrawRectangle(pen, _startPosX + 63, _startPosY + 70, 20, 15);
}
} }
} }
} }

View File

@ -112,11 +112,17 @@ namespace AirBomber
break; break;
// вправо // вправо
case DirectionType.Right: case DirectionType.Right:
// TODO: Продумать логику if (_startPosX + EntityAirPlane.Step + _airPlaneWidth < _pictureWidth)
{
_startPosX += (int)EntityAirPlane.Step;
}
break; break;
//вниз //вниз
case DirectionType.Down: case DirectionType.Down:
// TODO: Продумать логику if (_startPosY + EntityAirPlane.Step + _airPlaneHeight < _pictureHeight)
{
_startPosY += (int)EntityAirPlane.Step;
}
break; break;
} }
} }
@ -249,9 +255,9 @@ namespace AirBomber
//вверх //вверх
DirectionType.Up => _startPosY - EntityAirPlane.Step > 0, DirectionType.Up => _startPosY - EntityAirPlane.Step > 0,
// вправо // вправо
DirectionType.Right => false,// TODO: Продумать логику DirectionType.Right => _startPosX + EntityAirPlane.Step < _pictureWidth,
//вниз //вниз
DirectionType.Down => false,// TODO: Продумать логику DirectionType.Down => _startPosY + EntityAirPlane.Step < _pictureHeight,
_ => false, _ => false,
}; };
} }

View File

@ -6,7 +6,30 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal class DrawningObjectAirPlane 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);
} }
} }

View File

@ -14,7 +14,7 @@ namespace AirBomber
public Color BombsColor { get; private set; } public Color BombsColor { get; private set; }
public bool FuelTanks { get; private set; } public bool FuelTanks { get; private set; }
public EntityAirBomber(int speed, double weight, Color bodyColor, Color public EntityAirBomber(int speed, double weight, Color bodyColor, Color
additionalColor, bool bombs, Color bombsColor, bool fuelTanks) additionalColor, bool bombs, Color bombsColor, bool fuelTanks) : base(speed, weight, bodyColor)
{ {
AdditionalColor = additionalColor; AdditionalColor = additionalColor;
Bombs = bombs; Bombs = bombs;

View File

@ -28,25 +28,28 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
buttonCreate = new Button(); buttonCreateAirBomber = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonUp = new Button(); buttonUp = new Button();
pictureBoxAirBomber = new PictureBox(); pictureBoxAirBomber = new PictureBox();
comboBoxStrategy = new ComboBox();
buttonCreateAirPlane = new Button();
buttonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// buttonCreate // buttonCreateAirBomber
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateAirBomber.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(29, 479); buttonCreateAirBomber.Location = new Point(29, 447);
buttonCreate.Name = "buttonCreate"; buttonCreateAirBomber.Name = "buttonCreateAirBomber";
buttonCreate.Size = new Size(148, 34); buttonCreateAirBomber.Size = new Size(191, 66);
buttonCreate.TabIndex = 0; buttonCreateAirBomber.TabIndex = 0;
buttonCreate.Text = "Создать"; buttonCreateAirBomber.Text = "Создать бомбардировщик";
buttonCreate.UseVisualStyleBackColor = true; buttonCreateAirBomber.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click; buttonCreateAirBomber.Click += buttonCreateAirBomber_Click;
// //
// buttonDown // buttonDown
// //
@ -106,16 +109,52 @@
pictureBoxAirBomber.TabIndex = 5; pictureBoxAirBomber.TabIndex = 5;
pictureBoxAirBomber.TabStop = false; pictureBoxAirBomber.TabStop = false;
// //
// comboBoxStrategy
//
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToRightEdge" });
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 // FormAirBomber
// //
AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(986, 540); ClientSize = new Size(986, 540);
Controls.Add(buttonStep);
Controls.Add(buttonCreateAirPlane);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonCreate); Controls.Add(buttonCreateAirBomber);
Controls.Add(pictureBoxAirBomber); Controls.Add(pictureBoxAirBomber);
Name = "FormAirBomber"; Name = "FormAirBomber";
Text = "Бомбардировщик"; Text = "Бомбардировщик";
@ -125,11 +164,14 @@
#endregion #endregion
private Button buttonCreate; private Button buttonCreateAirBomber;
private Button buttonDown; private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private Button buttonRight; private Button buttonRight;
private Button buttonUp; private Button buttonUp;
private PictureBox pictureBoxAirBomber; private PictureBox pictureBoxAirBomber;
private ComboBox comboBoxStrategy;
private Button buttonCreateAirPlane;
private Button buttonStep;
} }
} }

View File

@ -2,37 +2,52 @@
{ {
public partial class FormAirBomber : Form public partial class FormAirBomber : Form
{ {
private DrawningAirBomber? _drawningAirBomber; private DrawningAirPlane? _drawningAirPlane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _abstractStrategy;
public FormAirBomber() public FormAirBomber()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw() private void Draw()
{ {
if (_drawningAirBomber == null) if (_drawningAirPlane == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningAirBomber.DrawBomber(gr); _drawningAirPlane.DrawPlane(gr);
pictureBoxAirBomber.Image = bmp; pictureBoxAirBomber.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreateAirBomber_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawningAirBomber = new DrawningAirBomber(); _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)),
_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)),
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); 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);
_drawningAirBomber.SetPosition(random.Next(10, 100), random.Next(10, 100)); _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(); Draw();
} }
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
{ {
if (_drawningAirBomber == null) if (_drawningAirPlane == null)
{ {
return; return;
} }
@ -40,19 +55,56 @@
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawningAirBomber.MoveTransport(DirectionType.Up); _drawningAirPlane.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawningAirBomber.MoveTransport(DirectionType.Down); _drawningAirPlane.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawningAirBomber.MoveTransport(DirectionType.Left); _drawningAirPlane.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawningAirBomber.MoveTransport(DirectionType.Right); _drawningAirPlane.MoveTransport(DirectionType.Right);
break; break;
} }
Draw(); 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;
}
}
} }
} }

View File

@ -6,7 +6,27 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal interface IMoveableObject public interface IMoveableObject
{ {
/// <summary>
/// Получение координаты X объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(DirectionType direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(DirectionType direction);
} }
} }

View File

@ -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();
}
}
}
}
}

View File

@ -6,7 +6,49 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal class ObjectParameters public class ObjectParameters
{ {
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder => _x;
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder => _y;
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
} }
} }

View File

@ -6,7 +6,10 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal class Status public enum Status
{ {
NotInit,
InProgress,
Finish
} }
} }