PIbd-22 Puchkina A.A. LabWork_02 #2
136
AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs
Normal file
136
AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs
Normal file
@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AirplaneWithRadar.DrawningObjects;
|
||||
|
||||
namespace AirplaneWithRadar.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -35,6 +35,9 @@
|
||||
buttonLeft = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonCreateAirplaneWithRadar = new Button();
|
||||
buttonStep = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxAirplane).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -107,11 +110,47 @@
|
||||
buttonDown.UseVisualStyleBackColor = true;
|
||||
buttonDown.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreateAirplaneWithRadar
|
||||
//
|
||||
buttonCreateAirplaneWithRadar.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateAirplaneWithRadar.Location = new Point(112, 411);
|
||||
buttonCreateAirplaneWithRadar.Name = "buttonCreateAirplaneWithRadar";
|
||||
buttonCreateAirplaneWithRadar.Size = new Size(198, 30);
|
||||
buttonCreateAirplaneWithRadar.TabIndex = 6;
|
||||
buttonCreateAirplaneWithRadar.Text = "Создать с дополнениями";
|
||||
buttonCreateAirplaneWithRadar.UseVisualStyleBackColor = true;
|
||||
buttonCreateAirplaneWithRadar.Click += buttonCreateAirplaneWithRadar_Click;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStep.Location = new Point(788, 34);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(94, 29);
|
||||
buttonStep.TabIndex = 7;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "в центр", "к гарнице" });
|
||||
comboBoxStrategy.Location = new Point(731, 0);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(151, 28);
|
||||
comboBoxStrategy.TabIndex = 8;
|
||||
//
|
||||
// AirplaneWithRadarForm
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(882, 453);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(buttonCreateAirplaneWithRadar);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonLeft);
|
||||
@ -134,5 +173,8 @@
|
||||
private Button buttonLeft;
|
||||
private Button buttonRight;
|
||||
private Button buttonDown;
|
||||
private Button buttonCreateAirplaneWithRadar;
|
||||
private Button buttonStep;
|
||||
private ComboBox comboBoxStrategy;
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using AirplaneWithRadar.DrawningObjects;
|
||||
using AirplaneWithRadar.MovementStrategy;
|
||||
|
||||
namespace AirplaneWithRadar
|
||||
{
|
||||
@ -20,6 +21,11 @@ namespace AirplaneWithRadar
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawningAirplane? _drawningAirplane;
|
||||
/// <summary>
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация формы
|
||||
/// </summary>
|
||||
@ -44,23 +50,42 @@ namespace AirplaneWithRadar
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// Обработка нажатия кнопки "Создать самолет"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonCreateAirplane_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningAirplane = new DrawningAirplane(random.Next(100,100),random.Next(1000,3000), Color.FromArgbb(random.Next(0, 256), random.Next(0, 256),
|
||||
_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)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
||||
random.Next(0, 256)),
|
||||
pictureBoxAirplane.Width, pictureBoxAirplane.Height);
|
||||
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
|
||||
}
|
||||
private void buttonCreateAirplaneWithRadar_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningAirplane = new DrawningAirplaneWithRadar(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)), Convert.ToBoolean(random.Next(0, 2)),
|
||||
pictureBoxAirplane.Width, pictureBoxAirplane.Height);
|
||||
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение размеров формы
|
||||
/// Изменение положения самолета
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
@ -88,5 +113,49 @@ namespace AirplaneWithRadar
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Шаг"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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), pictureBoxAirplane.Width,
|
||||
pictureBoxAirplane.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +121,7 @@
|
||||
<data name="buttonLeft.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm
|
||||
AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAFy1JREFUeNrtnV2S6ka2RkFB
|
||||
AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvAAADrwBlbxySQAAFy1JREFUeNrtnV2S6ka2RkFB
|
||||
v7ese98VCmkA6hwBlzsB4tjzn0qTPxLC5lBCKH++qsVDVy932yf5VmxD1lbmPnT+1R78C5RD4pBH4pBH
|
||||
4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pDH6VWFvw6qInHII3HII3HII3HII3HII3HI
|
||||
I3HII3HII3HII3HII3HII3HII3HII3EIY/hJ700WiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMe
|
||||
@ -328,7 +328,7 @@
|
||||
<data name="buttonDown.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm
|
||||
AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAGBNJREFUeNrt3V9y+r7Vx3Fj
|
||||
AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvAAADrwBlbxySQAAGBNJREFUeNrt3V9y+r7Vx3Fj
|
||||
NoARuTeacB9VYQGPM92A2f9eimTZSb5PSAD/05Hens7QTy/6i/WqjqFHkotwlbq7oosf1fWy3WWq7lo+
|
||||
WvsW4+B8jRBCmDahglA6oYUQQgghhBBCCCGEEEIIIRwT+V0IIYQQQjg2KgilE1bqJV7C8FmH/zy2WH5c
|
||||
i5gNI6rCkK4Qq5e6iHSsIIQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGE8K+oIJROaM/xEoaLfuGvka49
|
||||
|
@ -16,7 +16,7 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityAirplane? EntityAirplane{ get; protected set; }
|
||||
public EntityAirplane? EntityAirplane { get; protected set; }
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
@ -24,7 +24,7 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
private int _pictureHeight;
|
||||
/// <summary>
|
||||
/// /// Левая координата прорисовки самолета
|
||||
/// </summary>
|
||||
@ -61,7 +61,7 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
return;
|
||||
}
|
||||
EntityAirplane = new EntityAirplane(speed, weight, bodyColor, additionalColor);
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
@ -96,13 +96,55 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
_startPosX = Math.Min(x, _pictureWidth - _airplaneWidth);
|
||||
_startPosY = Math.Min(y, _pictureHeight - _airplaneHeight);
|
||||
}
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объект
|
||||
/// </summary>
|
||||
public int GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _airplaneWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _airplaneHeight;
|
||||
/// <summary>
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||
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 < _pictureWidth + _airplaneWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + EntityAirplane.Step < _pictureHeight + _airplaneHeight,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityAirplane == null)
|
||||
if (!CanMove(direction) || EntityAirplane == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -120,7 +162,7 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
if (_startPosY - EntityAirplane.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityAirplane.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
@ -157,9 +199,9 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
g.DrawRectangle(penBlack, _startPosX + 4, _startPosY + 40, 150, 30);
|
||||
|
||||
//киль
|
||||
g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40)});
|
||||
g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) });
|
||||
g.DrawPolygon(penBlack, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) });
|
||||
|
||||
|
||||
//стабилизатор и крыло
|
||||
Brush brSilver = new SolidBrush(Color.Silver);
|
||||
g.FillEllipse(brSilver, _startPosX, _startPosY + 35, 55, 10);
|
||||
@ -187,7 +229,6 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
g.DrawLine(penBlue, new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 55));
|
||||
g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 72), new Point(_startPosX + 150, _startPosY + 55));
|
||||
g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 72), new Point(_startPosX + 190, _startPosY + 55));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
|
||||
public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool radar, bool tank, bool pin, int width, int height) :
|
||||
base(speed, weight, bodyColor, additionalColor, width, height, 200, 85)
|
||||
base(speed, weight, bodyColor, additionalColor, width, height)
|
||||
{
|
||||
if (EntityAirplane != null)
|
||||
{
|
||||
@ -42,6 +42,7 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor);
|
||||
Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor);
|
||||
Pen penGray = new Pen(Color.Gray);
|
||||
|
||||
// радар
|
||||
if (airplaneWithRadar.Radar)
|
||||
{
|
||||
@ -56,7 +57,6 @@ namespace AirplaneWithRadar.DrawningObjects
|
||||
{
|
||||
g.DrawLine(penGray, new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 200, _startPosY + 55));
|
||||
}
|
||||
|
||||
// бак
|
||||
if (airplaneWithRadar.Tank)
|
||||
{
|
||||
|
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AirplaneWithRadar.MovementStrategy;
|
||||
using AirplaneWithRadar.DrawningObjects;
|
||||
|
||||
namespace AirplaneWithRadar.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ namespace AirplaneWithRadar.Entities
|
||||
/// <param name="tank">Признак наличия доп.бака</param>
|
||||
/// <param name="pin">Признак наличия штыря</param>
|
||||
public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool radar, bool tank, bool pin)
|
||||
additionalColor, bool radar, bool tank, bool pin) : base (speed, weight, bodyColor, additionalColor)
|
||||
{
|
||||
Radar = radar;
|
||||
Tank = tank;
|
||||
|
36
AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs
Normal file
36
AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AirplaneWithRadar;
|
||||
|
||||
|
||||
namespace AirplaneWithRadar.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
57
AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs
Normal file
57
AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using AirplaneWithRadar.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirplaneWithRadar.MovementStrategy
|
||||
{
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal <= FieldWidth &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth &&
|
||||
objParams.ObjectMiddleVertical <= FieldHeight &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
57
AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs
Normal file
57
AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using AirplaneWithRadar.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirplaneWithRadar.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
58
AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs
Normal file
58
AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirplaneWithRadar.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
15
AirplaneWithRadar/AirplaneWithRadar/Status.cs
Normal file
15
AirplaneWithRadar/AirplaneWithRadar/Status.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirplaneWithRadar.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user