This commit is contained in:
antoc0der 2023-10-17 21:30:47 +03:00
parent 31671e87d0
commit d9cadbabc4
13 changed files with 598 additions and 160 deletions

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.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(Direction.Left);
protected bool MoveRight() => MoveTo(Direction.Right);
protected bool MoveUp() => MoveTo(Direction.Up);
protected bool MoveDown() => MoveTo(Direction.Down);
protected ObjectParameteres? 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(Direction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirplaneWithRadar.Entities;
namespace ProjectAirplaneWithRadar.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 = 200;
protected readonly int _airplaneHeight = 78;
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 < _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)
{
if (width < _airplaneWidth || height < _airplaneHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
_airplaneWidth = airplaneWidth;
_airplaneHeight = airplaneHeight;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || y < 0 || x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight)
{
x = y = 10;
_startPosX = x;
_startPosY = y;
}
_startPosX = x;
_startPosY = y;
}
public bool CanMove(Direction direction)
{
if (EntityAirplane == null)
{
return false;
}
switch (direction)
{
case Direction.Left:
return _startPosX - EntityAirplane.Step > 0;
break;
case Direction.Up:
return _startPosY - EntityAirplane.Step > 0;
break;
case Direction.Right:
return _startPosX + EntityAirplane.Step + _airplaneWidth < _pictureWidth;
break;
case Direction.Down:
return _startPosY + EntityAirplane.Step + _airplaneHeight < _pictureHeight;
break;
default:return false;
};
}
public void MoveTransport(Direction direction)
{
if (!CanMove(direction)||EntityAirplane == null)
{
return;
}
switch (direction)
{
case Direction.Left:
if (_startPosX - EntityAirplane.Step > 0)
{
_startPosX -= (int)EntityAirplane.Step;
}
break;
case Direction.Up:
if (_startPosY - EntityAirplane.Step > 0)
{
_startPosY -= (int)EntityAirplane.Step;
}
break;
case Direction.Right:
if (_startPosX + EntityAirplane.Step + _airplaneWidth < _pictureWidth)
{
_startPosX += (int)EntityAirplane.Step;
}
break;
case Direction.Down:
if (_startPosY + EntityAirplane.Step + _airplaneHeight < _pictureHeight)
{
_startPosY += (int)EntityAirplane.Step;
}
break;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityAirplane == null)
{
return;
}
Pen pen = new Pen(Color.Black, 3);
// корпус
Brush br = new SolidBrush(EntityAirplane.BodyColor);
g.DrawEllipse(pen, _startPosX, _startPosY + 25, 180, 30);
g.FillEllipse(br, _startPosX, _startPosY + 25, 180, 30);
// крыло
Brush blackBrush = new SolidBrush(Color.Black);
g.FillEllipse(blackBrush, _startPosX + 70, _startPosY + 35, 80, 10);
// стекла
Pen blackPen = new Pen(Color.Black, 2);
Brush blueBrush = new SolidBrush(Color.LightBlue);
Point point1 = new Point(_startPosX + 170, _startPosY + 30);
Point point2 = new Point(_startPosX + 200, _startPosY + 40);
Point point3 = new Point(_startPosX + 170, _startPosY + 50);
Point[] curvePoints =
{
point1,
point2,
point3,
};
g.FillPolygon(blueBrush, curvePoints);
g.DrawPolygon(blackPen, curvePoints);
g.DrawLine(blackPen, _startPosX + 170, _startPosY + 40, _startPosX + 200, _startPosY + 40);
// хвост
Point point4 = new Point(_startPosX, _startPosY + 35);
Point point5 = new Point(_startPosX, _startPosY + 5);
Point point6 = new Point(_startPosX + 30, _startPosY + 35);
Point[] curvePoints2 =
{
point4,
point5,
point6,
};
g.FillPolygon(br, curvePoints2);
g.DrawPolygon(blackPen, curvePoints2);
// шасси
g.DrawLine(blackPen, _startPosX + 50, _startPosY + 55, _startPosX + 50, _startPosY + 70);
g.DrawLine(blackPen, _startPosX + 150, _startPosY + 51, _startPosX + 150, _startPosY + 70);
g.FillEllipse(blackBrush, _startPosX + 40, _startPosY + 65, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 65, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 145, _startPosY + 65, 10, 10);
}
}
}

View File

@ -7,130 +7,37 @@ using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectAirplaneWithRadar
using ProjectAirplaneWithRadar.Entities;
namespace ProjectAirplaneWithRadar.DrawningObjects
{
public class DrawningAirplaneWithRadar
public class DrawningAirplaneWithRadar : DrawningAirplane
{
public EntityAirplaneWithRadar? EntityAirplaneWithRadar { get; private set; }
public int _pictureWidth;
public int _pictureHeight;
public int _startPosX;
public int _startPosY;
private readonly int _airplaneWidth = 200;
private readonly int _airplaneHeight = 78;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool dopbak, int width, int height)
public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color
additionalColor, bool radar, bool dopbak, int width, int height) : base(speed, weight, bodyColor, width, height, 200, 78)
{
_pictureWidth = width;
_pictureHeight = height;
if (_airplaneWidth > _pictureWidth || _airplaneHeight > _pictureHeight)
return false;
EntityAirplaneWithRadar = new EntityAirplaneWithRadar();
EntityAirplaneWithRadar.Init(speed, weight, bodyColor, additionalColor, radar, dopbak);
return true;
if (EntityAirplane != null)
{
EntityAirplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, additionalColor, radar, dopbak);
}
public void SetPosition(int x, int y)
{
if(x < 0 || y < 0 ||x+_airplaneWidth >= _pictureWidth|| y + _airplaneHeight >= _pictureHeight)
{
x = y = 10;
_startPosX = x;
_startPosY=y;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)
public override void DrawTransport(Graphics g)
{
if (EntityAirplaneWithRadar == null)
if (EntityAirplane is not EntityAirplaneWithRadar airplaneWithRadar)
{
return;
}
switch (direction)
{
case Direction.Left:
if (_startPosX - EntityAirplaneWithRadar.Step > 0)
{
_startPosX -= (int)EntityAirplaneWithRadar.Step;
}
break;
case Direction.Up:
if (_startPosY - EntityAirplaneWithRadar.Step > 0)
{
_startPosY -= (int)EntityAirplaneWithRadar.Step;
}
break;
case Direction.Right:
if (_startPosX + EntityAirplaneWithRadar.Step+_airplaneWidth < _pictureWidth)
{
_startPosX += (int)EntityAirplaneWithRadar.Step;
}
break;
case Direction.Down:
if (_startPosY + EntityAirplaneWithRadar.Step+_airplaneHeight < _pictureHeight)
{
_startPosY += (int)EntityAirplaneWithRadar.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if(EntityAirplaneWithRadar == null)
{
return;
}
Pen pen = new Pen(Color.Black, 3);
Brush additionalBrush = new SolidBrush(EntityAirplaneWithRadar.AdditionalColor);
// корпус
Brush br = new SolidBrush(EntityAirplaneWithRadar.BodyColor);
g.DrawEllipse(pen, _startPosX, _startPosY+25,180, 30) ;
g.FillEllipse(br, _startPosX, _startPosY+25, 180, 30);
// крыло
Brush blackBrush = new SolidBrush(Color.Black);
g.FillEllipse(blackBrush, _startPosX + 70, _startPosY + 35, 80, 10);
// стекла
base.DrawTransport(g);
Pen blackPen = new Pen(Color.Black, 2);
Brush blueBrush = new SolidBrush(Color.LightBlue);
Point point1 = new Point(_startPosX+170, _startPosY+30);
Point point2 = new Point(_startPosX+200, _startPosY+40);
Point point3 = new Point(_startPosX+170, _startPosY + 50);
Point[] curvePoints =
Pen pen = new Pen(Color.Black);
Brush additionalBrush = new
SolidBrush(airplaneWithRadar.AdditionalColor);
if (airplaneWithRadar.DopBak)
{
point1,
point2,
point3,
};
g.FillPolygon(blueBrush, curvePoints);
g.DrawPolygon(blackPen, curvePoints);
g.DrawLine(blackPen, _startPosX + 170, _startPosY + 40, _startPosX + 200, _startPosY + 40);
// хвост
Point point4 = new Point(_startPosX, _startPosY + 35);
Point point5 = new Point(_startPosX, _startPosY +5 );
Point point6 = new Point(_startPosX + 30, _startPosY + 35);
Point[] curvePoints2 =
{
point4,
point5,
point6,
};
g.FillPolygon(br, curvePoints2);
g.DrawPolygon(blackPen, curvePoints2);
// шасси
g.DrawLine(blackPen, _startPosX + 50, _startPosY + 55, _startPosX + 50, _startPosY + 70);
g.DrawLine(blackPen, _startPosX + 150, _startPosY + 51, _startPosX + 150, _startPosY + 70);
g.FillEllipse(blackBrush, _startPosX + 40, _startPosY + 65, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 65, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 145, _startPosY + 65, 10, 10);
if (EntityAirplaneWithRadar.DopBak)
{
//бак
g.FillEllipse(additionalBrush, _startPosX, _startPosY + 45, 40, 20);
g.DrawEllipse(blackPen, _startPosX, _startPosY + 45, 40, 20);
}
if (EntityAirplaneWithRadar.Radar)
if (airplaneWithRadar.Radar)
{
//радар
g.DrawLine(blackPen, _startPosX + 60, _startPosY + 25, _startPosX + 60, _startPosY + 15);
g.DrawLine(blackPen, _startPosX + 60, _startPosY + 15, _startPosX + 67, _startPosY + 11);
Point point7 = new Point(_startPosX + 60, _startPosY + 15);

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirplaneWithRadar.DrawningObjects;
namespace ProjectAirplaneWithRadar.MovementStrategy
{
public class DrawningObjectAirplane : IMoveableObject
{
private readonly DrawningAirplane? _drawningAirplane = null;
public DrawningObjectAirplane(DrawningAirplane drawningCar)
{
_drawningAirplane = drawningCar;
}
public ObjectParameteres? GetObjectPosition
{
get
{
if (_drawningAirplane == null || _drawningAirplane.EntityAirplane ==null)
{
return null;
}
return new ObjectParameteres(_drawningAirplane.GetPosX,
_drawningAirplane.GetPosY, _drawningAirplane.GetWidth, _drawningAirplane.GetHeight);
}
}
public int GetStep => (int)(_drawningAirplane?.EntityAirplane?.Step ?? 0);
public bool CheckCanMove(Direction direction) =>
_drawningAirplane?.CanMove(direction) ?? false;
public void MoveObject(Direction direction) =>
_drawningAirplane?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.Entities
{
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;
}
}
}

View File

@ -5,29 +5,15 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar
namespace ProjectAirplaneWithRadar.Entities
{
public class EntityAirplaneWithRadar
public class EntityAirplaneWithRadar : 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 Radar { get; private set; }
// наличие дополнительных топливных баков
public bool DopBak { get; private set; }
//шаг перемещения самолета
public double Step => (double)Speed*100/Weight;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool dopbak)
public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool dopbak) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Radar = radar;
DopBak = dopbak;

View File

@ -29,11 +29,14 @@
private void InitializeComponent()
{
pictureBoxAirplaneWithRadar = new PictureBox();
buttonCreate = new Button();
buttonCreateAirplaneWithRadar = new Button();
buttonRight = new Button();
buttonDown = new Button();
buttonLeft = new Button();
buttonUp = new Button();
buttonCreateAirplane = new Button();
comboBoxAirplane = new ComboBox();
buttonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).BeginInit();
SuspendLayout();
//
@ -47,16 +50,16 @@
pictureBoxAirplaneWithRadar.TabIndex = 0;
pictureBoxAirplaneWithRadar.TabStop = false;
//
// buttonCreate
// buttonCreateAirplaneWithRadar
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 413);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Create";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
buttonCreateAirplaneWithRadar.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateAirplaneWithRadar.Location = new Point(12, 387);
buttonCreateAirplaneWithRadar.Name = "buttonCreateAirplaneWithRadar";
buttonCreateAirplaneWithRadar.Size = new Size(195, 55);
buttonCreateAirplaneWithRadar.TabIndex = 1;
buttonCreateAirplaneWithRadar.Text = "Create Airplane With Radar";
buttonCreateAirplaneWithRadar.UseVisualStyleBackColor = true;
buttonCreateAirplaneWithRadar.Click += buttonCreateAirplaneWithRadar_Click;
//
// buttonRight
//
@ -102,16 +105,50 @@
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click;
//
// buttonCreateAirplane
//
buttonCreateAirplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateAirplane.Location = new Point(213, 387);
buttonCreateAirplane.Name = "buttonCreateAirplane";
buttonCreateAirplane.Size = new Size(203, 55);
buttonCreateAirplane.TabIndex = 6;
buttonCreateAirplane.Text = "Create Airplane";
buttonCreateAirplane.UseVisualStyleBackColor = true;
buttonCreateAirplane.Click += buttonCreateAirplane_Click;
//
// comboBoxAirplane
//
comboBoxAirplane.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxAirplane.FormattingEnabled = true;
comboBoxAirplane.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
comboBoxAirplane.Location = new Point(724, 12);
comboBoxAirplane.Name = "comboBoxAirplane";
comboBoxAirplane.Size = new Size(151, 28);
comboBoxAirplane.TabIndex = 7;
//
// buttonStep
//
buttonStep.Location = new Point(724, 46);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(151, 29);
buttonStep.TabIndex = 8;
buttonStep.Text = "Step";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// FormAirplaneWithRadar
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(887, 454);
Controls.Add(buttonStep);
Controls.Add(comboBoxAirplane);
Controls.Add(buttonCreateAirplane);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateAirplaneWithRadar);
Controls.Add(pictureBoxAirplaneWithRadar);
Name = "FormAirplaneWithRadar";
Text = "FormAirplaneWithRadar";
@ -123,10 +160,13 @@
#endregion
private PictureBox pictureBoxAirplaneWithRadar;
private Button buttonCreate;
private Button buttonCreateAirplaneWithRadar;
private Button buttonRight;
private Button buttonDown;
private Button buttonLeft;
private Button buttonUp;
private Button buttonCreateAirplane;
private ComboBox comboBoxAirplane;
private Button buttonStep;
}
}

View File

@ -1,17 +1,21 @@
using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.MovementStrategy;
namespace ProjectAirplaneWithRadar
{
public partial class FormAirplaneWithRadar : Form
{
private DrawningAirplaneWithRadar? _drawningAirplaneWithRadar;
private DrawningAirplane _drawningAirplane;
private AbstractStrategy _abstractStrategy;
private void Draw()
{
if (_drawningAirplaneWithRadar == null)
if (_drawningAirplane == null)
{
return;
}
Bitmap bmp = new Bitmap(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAirplaneWithRadar.DrawTransport(gr);
_drawningAirplane.DrawTransport(gr);
pictureBoxAirplaneWithRadar.Image = bmp;
}
public FormAirplaneWithRadar()
@ -20,7 +24,7 @@ namespace ProjectAirplaneWithRadar
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningAirplaneWithRadar == null)
if (_drawningAirplane == null)
{
return;
}
@ -28,36 +32,86 @@ namespace ProjectAirplaneWithRadar
switch (name)
{
case "buttonUp":
_drawningAirplaneWithRadar.MoveTransport(Direction.Up);
_drawningAirplane.MoveTransport(Direction.Up);
break;
case "buttonDown":
_drawningAirplaneWithRadar.MoveTransport(Direction.Down);
_drawningAirplane.MoveTransport(Direction.Down);
break;
case "buttonLeft":
_drawningAirplaneWithRadar.MoveTransport(Direction.Left);
_drawningAirplane.MoveTransport(Direction.Left);
break;
case "buttonRight":
_drawningAirplaneWithRadar.MoveTransport(Direction.Right);
_drawningAirplane.MoveTransport(Direction.Right);
break;
}
Draw();
}
private void buttonCreate_Click(object sender, EventArgs e)
private void buttonCreateAirplaneWithRadar_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawningAirplaneWithRadar = new DrawningAirplaneWithRadar();
if (_drawningAirplaneWithRadar.Init
(random.Next(100, 300), // speed
_drawningAirplane = new DrawningAirplaneWithRadar(random.Next(100, 300), // speed
random.Next(1000, 3000),// weight
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),// bodycolor
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), //additionalColor
Convert.ToBoolean(random.Next(0, 2)), // radar
Convert.ToBoolean(random.Next(0, 2)), //dopbak
pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height))
{
_drawningAirplaneWithRadar.SetPosition(random.Next(10, 100), random.Next(10, 100));
pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateAirplane_Click(object sender, EventArgs e)
{
Random random = new Random();
_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)),
pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawningAirplane == null)
{
return;
}
if (comboBoxAirplane.Enabled)
{
switch (comboBoxAirplane.SelectedIndex)
{
case 0:
_abstractStrategy = new MoveToCenter();
break;
case 1:
_abstractStrategy = new MoveToBorder();
break;
default:
break;
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectAirplane(_drawningAirplane), pictureBoxAirplaneWithRadar.Width,
pictureBoxAirplaneWithRadar.Height);
comboBoxAirplane.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxAirplane.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameteres? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(Direction direction);
void MoveObject(Direction direction);
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.MovementStrategy
{
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = FieldWidth - objParams.RightBorder;
if (diffX > GetStep())
{
MoveRight();
}
var diffY = FieldHeight - objParams.DownBorder;
if (diffY > GetStep())
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.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();
}
}
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.MovementStrategy
{
public class ObjectParameteres
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
public int LeftBorder => _x;
public int TopBorder => _y;
public int RightBorder => _x + _width;
public int DownBorder => _y + _height;
public int ObjectMiddleHorizontal => _x + _width / 2;
public int ObjectMiddleVertical => _y + _height / 2;
public ObjectParameteres(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}