ISEbd-12 Rozhkov.I.E. lab02 Simple #2

Closed
RozhVan wants to merge 1 commits from lab02 into lab01
17 changed files with 726 additions and 300 deletions

View File

@ -1,176 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectPlane;
public class DrawCont
{
public EntityContainer? EntityContainer { get; private set; }
private int? _PictureWidth;
private int? _PictureHeight;
private int? _StartPosX;
private int? _StartPosY;
private readonly int _drawingContWidth = 160;
private readonly int _drawingContHeight = 90;
public void Init(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane)
{
EntityContainer = new EntityContainer();
EntityContainer.Init(speed, weight, shipColor, containerColor, container, crane);
_PictureWidth = null;
_PictureHeight = null;
_StartPosX = null;
_StartPosY = null;
}
public bool SetPictureSize(int width, int height)
{
if (EntityContainer == null)
{
return false;
}
if (width >= _drawingContWidth && height >= _drawingContHeight)
{
_PictureWidth = width;
_PictureHeight = height;
if (_StartPosX.HasValue && _StartPosY.HasValue)
{
if (_StartPosX.Value + _drawingContWidth > _PictureWidth)
{
_StartPosX = _PictureWidth - _drawingContWidth;
}
if (_StartPosY.Value + _drawingContHeight > _PictureHeight)
{
_StartPosY = _PictureHeight - _drawingContHeight;
}
}
return true;
}
return false;
}
public void SetPosition(int x, int y)
{
if (!_PictureHeight.HasValue || !_PictureWidth.HasValue)
{
return;
}
if (x < 0)
{
x = 0;
}
else if (x + _drawingContWidth > _PictureWidth)
{
x = _PictureWidth.Value - _drawingContWidth;
}
if (y < 0)
{
y = 0;
}
else if (y + _drawingContHeight > _PictureHeight)
{
y = _PictureHeight.Value - _drawingContHeight;
}
_StartPosX = x;
_StartPosY = y;
}
public bool MoveTransport(DirectionType direction)
{
if (EntityContainer == null || !_StartPosX.HasValue || !_StartPosY.HasValue)
{
return false;
}
switch (direction)
{
case DirectionType.Left:
if (_StartPosX.Value - EntityContainer.Step > 0)
{
_StartPosX -= (int)EntityContainer.Step;
}
return true;
case DirectionType.Right:
if (_StartPosX.Value + EntityContainer.Step < _PictureWidth - _drawingContWidth)
{
_StartPosX += (int)EntityContainer.Step;
}
return true;
case DirectionType.Up:
if (_StartPosY.Value - EntityContainer.Step > 0)
{
_StartPosY -= (int)EntityContainer.Step;
}
return true;
case DirectionType.Down:
if (_StartPosY.Value + EntityContainer.Step < _PictureHeight - _drawingContHeight)
{
_StartPosY += (int)EntityContainer.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics g)
{
if (EntityContainer == null || !_StartPosX.HasValue || !_StartPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush ContainerBrush = new SolidBrush(EntityContainer.ContainerColor);
// отрисовка контейнера
if (EntityContainer.Container)
{
g.DrawRectangle(pen, _StartPosX.Value + 80, _StartPosY.Value, 60, 40);
g.FillRectangle(ContainerBrush, _StartPosX.Value + 81, _StartPosY.Value + 1, 59, 39);
}
Brush ShipBrush = new SolidBrush(EntityContainer.ShipColor);
//отрисовка корабля
Point[] points =
{
new Point(_StartPosX.Value, _StartPosY.Value + 40),
new Point(_StartPosX.Value + 160, _StartPosY.Value + 40),
new Point(_StartPosX.Value + 150, _StartPosY.Value + 90),
new Point(_StartPosX.Value + 10, _StartPosY.Value + 90),
};
g.DrawPolygon(pen, points);
g.FillPolygon(ShipBrush, points);
//отрисовка крана
if (EntityContainer.Crane)
{
g.DrawLine(pen, _StartPosX.Value + 30, _StartPosY.Value + 50, _StartPosX.Value + 30, _StartPosY.Value + 70);
g.DrawLine(pen, _StartPosX.Value + 20, _StartPosY.Value + 60, _StartPosX.Value + 40, _StartPosY.Value + 60);
}
}
}

View File

@ -4,15 +4,17 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ProjectPlane; namespace ProjectPlane.Drawnings;
public enum DirectionType public enum DirectionType
{ {
Unknow = -1,
Up = 1, Up = 1,
Down = 2, Down = 2,
Left = 3, Left = 3,
Right = 4, Right = 4
} }

View File

@ -0,0 +1,41 @@
using ProjectPlane.Entities;
namespace ProjectPlane.Drawnings;
public class DrawCont : DrawningShip
{
public DrawCont(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane) : base(160, 90)
{
EntityShip = new EntityContainer(speed, weight, shipColor, containerColor, container, crane);
}
public override void DrawTransport(Graphics g)
{
if (EntityShip == null || !_StartPosX.HasValue || !_StartPosY.HasValue || EntityShip is not EntityContainer container)
{
return;
}
Pen pen = new(Color.Black);
Brush ContainerBrush = new SolidBrush(container.ContainerColor);
// отрисовка контейнера
if (container.Container)
{
g.DrawRectangle(pen, _StartPosX.Value + 80, _StartPosY.Value, 60, 40);
g.FillRectangle(ContainerBrush, _StartPosX.Value + 81, _StartPosY.Value + 1, 59, 39);
}
_StartPosY += 40;
base.DrawTransport(g);
_StartPosY -= 40;
//отрисовка крана
if (container.Crane)
{
g.DrawLine(pen, _StartPosX.Value + 30, _StartPosY.Value + 50, _StartPosX.Value + 30, _StartPosY.Value + 70);
g.DrawLine(pen, _StartPosX.Value + 20, _StartPosY.Value + 60, _StartPosX.Value + 40, _StartPosY.Value + 60);
}
}
}

View File

@ -0,0 +1,175 @@
using ProjectPlane.Entities;
namespace ProjectPlane.Drawnings;
public class DrawningShip
{
public EntityShip? EntityShip { get; protected set; }
private int? _PictureWidth;
private int? _PictureHeight;
protected int? _StartPosX;
protected int? _StartPosY;
private readonly int _drawingContWidth = 160;
private readonly int _drawingContHeight = 50;
public int? GetPosX => _StartPosX;
public int? GetPosY => _StartPosY;
public int GetWidth => _drawingContWidth;
public int GetHeight => _drawingContHeight;
private DrawningShip()
{
_PictureWidth = null;
_PictureHeight = null;
_StartPosX = null;
_StartPosY = null;
}
public DrawningShip(int speed, double weight, Color shipColor) : this()
{
EntityShip = new EntityShip(speed, weight, shipColor);
}
protected DrawningShip(int drawingShipWidth, int drawingShipHeight) : this()
{
_drawingContWidth = drawingShipWidth;
_drawingContHeight = drawingShipHeight;
}
public bool SetPictureSize(int width, int height)
{
if (EntityShip == null)
{
return false;
}
if (width >= _drawingContWidth && height >= _drawingContHeight)
{
_PictureWidth = width;
_PictureHeight = height;
if (_StartPosX.HasValue && _StartPosY.HasValue)
{
if (_StartPosX.Value + _drawingContWidth > width)
{
_StartPosX = width - _drawingContWidth;
}
if (_StartPosY.Value + _drawingContHeight > height)
{
_StartPosY = _PictureHeight - height;
}
}
return true;
}
return false;
}
public void SetPosition(int x, int y)
{
if (!_PictureHeight.HasValue || !_PictureWidth.HasValue)
{
return;
}
if (x < 0)
{
x = 0;
}
else if (x + _drawingContWidth > _PictureWidth)
{
x = _PictureWidth.Value - _drawingContWidth;
}
if (y < 0)
{
y = 0;
}
else if (y + _drawingContHeight > _PictureHeight)
{
y = _PictureHeight.Value - _drawingContHeight;
}
_StartPosX = x;
_StartPosY = y;
}
public bool MoveTransport(DirectionType direction)
{
if (EntityShip == null || !_StartPosX.HasValue || !_StartPosY.HasValue)
{
return false;
}
switch (direction)
{
case DirectionType.Left:
if (_StartPosX.Value - EntityShip.Step > 0)
{
_StartPosX -= (int)EntityShip.Step;
}
return true;
case DirectionType.Right:
if (_StartPosX.Value + EntityShip.Step < _PictureWidth - _drawingContWidth)
{
_StartPosX += (int)EntityShip.Step;
}
return true;
case DirectionType.Up:
if (_StartPosY.Value - EntityShip.Step > 0)
{
_StartPosY -= (int)EntityShip.Step;
}
return true;
case DirectionType.Down:
if (_StartPosY.Value + EntityShip.Step < _PictureHeight - _drawingContHeight)
{
_StartPosY += (int)EntityShip.Step;
}
return true;
default:
return false;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityShip == null || !_StartPosX.HasValue || !_StartPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush ShipBrush = new SolidBrush(EntityShip.ShipColor);
//отрисовка корабля
Point[] points =
{
new Point(_StartPosX.Value, _StartPosY.Value),
new Point(_StartPosX.Value + 160, _StartPosY.Value),
new Point(_StartPosX.Value + 150, _StartPosY.Value + 50),
new Point(_StartPosX.Value + 10, _StartPosY.Value + 50),
};
g.DrawPolygon(pen, points);
g.FillPolygon(ShipBrush, points);
}
}

View File

@ -0,0 +1,28 @@
namespace ProjectPlane.Entities;
/// <summary>
/// Класс-сущность "Контейнеровоз"
/// </summary>
public class EntityContainer : EntityShip
{
/// <summary>
/// Цвет контейнера
/// </summary>
public Color ContainerColor { get; private set; }
/// <summary>
/// Признак наличия контейнера
/// </summary>
public bool Container { get; private set; }
/// <summary>
/// Признак наличия крана
/// </summary>
public bool Crane { get; private set; }
public EntityContainer(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane) : base(speed, weight, shipColor)
{
ContainerColor = containerColor;
Container = container;
Crane = crane;
}
}

View File

@ -0,0 +1,24 @@
namespace ProjectPlane.Entities;
public class EntityShip
{
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Цвет контейнеровоза
/// </summary>
public Color ShipColor { get; private set; }
public double Step => Speed * 10 / Weight;
public EntityShip(int speed, double weight, Color shipColor)
{
Speed = speed;
Weight = weight;
ShipColor = shipColor;
}
}

View File

@ -1,48 +0,0 @@
namespace ProjectPlane;
/// <summary>
/// Класс-сущность "Контейнеровоз"
/// </summary>
public class EntityContainer
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Цвет контейнеровоза
/// </summary>
public Color ShipColor { get; private set; }
/// <summary>
/// Цвет контейнера
/// </summary>
public Color ContainerColor { get; private set; }
/// <summary>
/// Признак наличия контейнера
/// </summary>
public bool Container { get; private set; }
/// <summary>
/// Признак наличия крана
/// </summary>
public bool Crane { get; private set; }
public double Step => Speed * 10 / Weight;
public void Init(int speed, double weight, Color shipColor, Color containerColor, bool container, bool crane)
{
Speed = speed;
Weight = weight;
ContainerColor = containerColor;
ShipColor = shipColor;
Container = container;
Crane = crane;
}
}

View File

@ -34,6 +34,9 @@
buttonRight = new Button(); buttonRight = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonCreateShip = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCont).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCont).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -42,18 +45,18 @@
pictureBoxCont.Dock = DockStyle.Fill; pictureBoxCont.Dock = DockStyle.Fill;
pictureBoxCont.Location = new Point(0, 0); pictureBoxCont.Location = new Point(0, 0);
pictureBoxCont.Name = "pictureBoxCont"; pictureBoxCont.Name = "pictureBoxCont";
pictureBoxCont.Size = new Size(804, 468); pictureBoxCont.Size = new Size(898, 554);
pictureBoxCont.TabIndex = 0; pictureBoxCont.TabIndex = 0;
pictureBoxCont.TabStop = false; pictureBoxCont.TabStop = false;
// //
// buttonCreate // buttonCreate
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 433); buttonCreate.Location = new Point(12, 519);
buttonCreate.Name = "buttonCreate"; buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23); buttonCreate.Size = new Size(167, 23);
buttonCreate.TabIndex = 1; buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать контейнеровоз";
buttonCreate.UseVisualStyleBackColor = true; buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click; buttonCreate.Click += ButtonCreate_Click;
// //
@ -62,9 +65,9 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.влево; buttonLeft.BackgroundImage = Properties.Resources.влево;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(643, 416); buttonLeft.Location = new Point(748, 507);
buttonLeft.Name = "buttonLeft"; buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(40, 40); buttonLeft.Size = new Size(34, 35);
buttonLeft.TabIndex = 2; buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click; buttonLeft.Click += ButtonMove_Click;
@ -74,9 +77,9 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.вправо; buttonRight.BackgroundImage = Properties.Resources.вправо;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch; buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(735, 416); buttonRight.Location = new Point(828, 507);
buttonRight.Name = "buttonRight"; buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(40, 40); buttonRight.Size = new Size(34, 35);
buttonRight.TabIndex = 3; buttonRight.TabIndex = 3;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click; buttonRight.Click += ButtonMove_Click;
@ -86,9 +89,9 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.вверх; buttonUp.BackgroundImage = Properties.Resources.вверх;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch; buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(689, 370); buttonUp.Location = new Point(788, 466);
buttonUp.Name = "buttonUp"; buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(40, 40); buttonUp.Size = new Size(34, 35);
buttonUp.TabIndex = 4; buttonUp.TabIndex = 4;
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click; buttonUp.Click += ButtonMove_Click;
@ -98,18 +101,52 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.вниз; buttonDown.BackgroundImage = Properties.Resources.вниз;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch; buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(689, 416); buttonDown.Location = new Point(788, 507);
buttonDown.Name = "buttonDown"; buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(40, 40); buttonDown.Size = new Size(34, 35);
buttonDown.TabIndex = 5; buttonDown.TabIndex = 5;
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click; buttonDown.Click += ButtonMove_Click;
// //
// buttonCreateShip
//
buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateShip.Location = new Point(185, 519);
buttonCreateShip.Name = "buttonCreateShip";
buttonCreateShip.Size = new Size(167, 23);
buttonCreateShip.TabIndex = 6;
buttonCreateShip.Text = "Создать корабль";
buttonCreateShip.UseVisualStyleBackColor = true;
buttonCreateShip.Click += ButtonCreateShip_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(765, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 7;
//
// buttonStrategyStep
//
buttonStrategyStep.Location = new Point(811, 41);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(75, 23);
buttonStrategyStep.TabIndex = 8;
buttonStrategyStep.Text = "Шаг";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += ButtonStrategyStep_Click;
//
// FormContainer // FormContainer
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(804, 468); ClientSize = new Size(898, 554);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateShip);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonRight); Controls.Add(buttonRight);
@ -117,7 +154,7 @@
Controls.Add(buttonCreate); Controls.Add(buttonCreate);
Controls.Add(pictureBoxCont); Controls.Add(pictureBoxCont);
Name = "FormContainer"; Name = "FormContainer";
Text = "FormContainer"; Text = "Контейнеровоз";
((System.ComponentModel.ISupportInitialize)pictureBoxCont).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCont).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -130,5 +167,8 @@
private Button buttonRight; private Button buttonRight;
private Button buttonUp; private Button buttonUp;
private Button buttonDown; private Button buttonDown;
private Button buttonCreateShip;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStep;
} }
} }

View File

@ -1,56 +1,66 @@
using System; using ProjectPlane.Drawnings;
using System.Collections.Generic; using ProjectPlane.MovementStrategy;
using System.ComponentModel;
using System.Data; namespace ProjectPlane;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectPlane
{
public partial class FormContainer : Form public partial class FormContainer : Form
{ {
private DrawCont? _drawCont; private DrawningShip? _drawShip;
private AbstractStrategy? _strategy;
public FormContainer() public FormContainer()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null;
} }
private void Draw() private void Draw()
{ {
if (_drawCont == null) if (_drawShip == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxCont.Width, pictureBoxCont.Height); Bitmap bmp = new(pictureBoxCont.Width, pictureBoxCont.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawCont.DrawTransport(gr); _drawShip.DrawTransport(gr);
pictureBoxCont.Image = bmp; pictureBoxCont.Image = bmp;
} }
private void CreateObject(string type)
private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random random = new Random(); Random random = new Random();
_drawCont = new DrawCont(); switch (type)
_drawCont.Init(random.Next(100, 300), random.Next(100, 300), {
case nameof(DrawningShip):
_drawShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawCont):
_drawShip = new DrawCont(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawCont.SetPictureSize(pictureBoxCont.Width, pictureBoxCont.Height); break;
_drawCont.SetPosition(random.Next(10, 100), random.Next(10, 100)); default:
return;
}
_drawShip.SetPictureSize(pictureBoxCont.Width, pictureBoxCont.Height);
_drawShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw(); Draw();
} }
private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawCont));
private void ButtonCreateShip_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningShip));
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawCont == null) if (_drawShip == null)
{ {
return; return;
} }
@ -60,16 +70,16 @@ namespace ProjectPlane
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
result = _drawCont.MoveTransport(DirectionType.Up); result = _drawShip.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
result = _drawCont.MoveTransport(DirectionType.Down); result = _drawShip.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
result = _drawCont.MoveTransport(DirectionType.Left); result = _drawShip.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
result = _drawCont.MoveTransport(DirectionType.Right); result = _drawShip.MoveTransport(DirectionType.Right);
break; break;
} }
@ -77,7 +87,45 @@ namespace ProjectPlane
{ {
Draw(); Draw();
} }
}
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawShip == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableShip(_drawShip), pictureBoxCont.Width, pictureBoxCont.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
} }
}
} }

View File

@ -0,0 +1,74 @@
namespace ProjectPlane.MovementStrategy;
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public StrategyStatus GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
protected bool MoveRight() => MoveTo(MovementDirection.Right);
protected bool MoveUp() => MoveTo(MovementDirection.Up);
protected bool MoveDown() => MoveTo(MovementDirection.Down);
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion();
private bool MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
}
}

View File

@ -0,0 +1,10 @@
namespace ProjectPlane.MovementStrategy;
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool TryMoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,52 @@
namespace ProjectPlane.MovementStrategy;
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder - GetStep() <= FieldWidth
&& objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder - GetStep() <= FieldHeight
&& objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
int diffX = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,50 @@
namespace ProjectPlane.MovementStrategy;
internal class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2
&& objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2
&& objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
}
protected override void MoveToTarget()
{
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,49 @@
using ProjectPlane.Drawnings;
namespace ProjectPlane.MovementStrategy;
public class MoveableShip : IMoveableObject
{
private readonly DrawningShip? _ship = null;
public MoveableShip(DrawningShip ship)
{
_ship = ship;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_ship == null || _ship.EntityShip == null || !_ship.GetPosX.HasValue || !_ship.GetPosY.HasValue)
{
return null;
}
return new ObjectParameters(_ship.GetPosX.Value, _ship.GetPosY.Value, _ship.GetWidth, _ship.GetHeight);
}
}
public int GetStep => (int)(_ship?.EntityShip?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if (_ship == null || _ship.EntityShip == null)
{
return false;
}
return _ship.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction)
{
return direction switch
{
MovementDirection.Left => DirectionType.Left,
MovementDirection.Right => DirectionType.Right,
MovementDirection.Up => DirectionType.Up,
MovementDirection.Down => DirectionType.Down,
_ => DirectionType.Unknow,
};
}
}

View File

@ -0,0 +1,13 @@

namespace ProjectPlane.MovementStrategy;
public enum MovementDirection
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
}

View File

@ -0,0 +1,33 @@
namespace ProjectPlane.MovementStrategy;
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
public int LeftBorder => _x;
public int TopBorder => _y;
public int RightBorder => _x + _width;
public int DownBorder => _y + _height;
public int ObjectMiddleHorizontal => _x + _width / 2;
public int ObjectMiddleVertical => _y + _height / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

View File

@ -0,0 +1,11 @@

namespace ProjectPlane.MovementStrategy;
public enum StrategyStatus
{
NotInit,
InProgress,
Finish
}