Compare commits

...

2 Commits

15 changed files with 811 additions and 99 deletions

View File

@ -4,28 +4,36 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser;
namespace ProjectCruiser.Drawings;
/// <summary>
/// Направление перемещения
/// </summary>
public enum DirectionType
{
public enum DirectionType
{
/// <summary>
/// Неизвестное направление
/// </summary>
Unknow = -1,
/// <summary>
/// Вверх
/// </summary>
Up = 1,
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влева
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}
}

View File

@ -3,15 +3,16 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.Entities;
namespace ProjectCruiser;
namespace ProjectCruiser.Drawings;
public class DrawingCruiser
public class DrawningCruiser
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityCruiser? EntityCruiser { get; private set; }
public EntityCruiser? EntityCruiser { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
@ -23,41 +24,74 @@ public class DrawingCruiser
/// <summary>
/// Левая координата прорисовки крейсера
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки крейсера
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки крейсера
/// </summary>
private readonly int _drawningCruiserWidth = 110;
private readonly int _drawningCruiserWidth = 90;
/// <summary>
/// Высота прорисовки крейсера
/// </summary>
private readonly int _drawingCruiserHeight = 60;
private readonly int _drawingCruiserHeight = 50;
/// <summary>
/// Инициализация полей объекта класса крейсера
/// Координата X объекта
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="armor">Признак наличия брони</param>
/// <param name="weapon">Признак наличия оружия</param>
public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon)
{
EntityCruiser = new EntityCruiser();
EntityCruiser.Init(speed, weigth, bodyColor, additionalColor, bodyKit, armor, weapon);
public int? GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int? GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _drawningCruiserWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _drawingCruiserHeight;
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningCruiser() {
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Констуктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
public DrawningCruiser(int speed, double weigth, Color bodyColor): this()
{
EntityCruiser = new EntityCruiser(speed, weigth, bodyColor);
}
/// <summary>
/// Констуктор для наследников
/// </summary>
/// <param name="drawningCruiserWidth">Ширина прорисовки крейсера</param>
/// <param name="drawingCruiserHeight">Высота прорисовки крейсера</param>
protected DrawningCruiser(int drawningCruiserWidth, int drawingCruiserHeight) : this()
{
_drawningCruiserWidth = drawningCruiserWidth;
_drawingCruiserHeight = drawingCruiserHeight;
}
public bool SetPictireSize(int width, int height)
{
_pictureWidth = width;
@ -115,7 +149,7 @@ public class DrawingCruiser
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
@ -124,31 +158,21 @@ public class DrawingCruiser
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor);
//Границы крейсера
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 35, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 9, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70, 52);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value, 20, 20);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 30, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 30, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 1, _startPosY.Value + 10, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 10, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 1, 70, 52);
//кузов крейсера
Brush br = new SolidBrush(EntityCruiser.BodyColor);
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70, 50);
// оружие крейсера
if (EntityCruiser.Weapon)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 23, 25, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 23, 35, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 23, 20, 15);
}
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 10, 10, 30);
g.FillRectangle(br, _startPosX.Value + 80, _startPosY.Value + 10, 10, 30);
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value, 70, 50);
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.Entities;
namespace ProjectCruiser.Drawings;
public class DrawningMilitaryCruiser: DrawningCruiser
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="armor">Признак наличия брони</param>
/// <param name="weapon">Признак наличия оружия</param>
public DrawningMilitaryCruiser(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon): base(110, 60)
{
EntityCruiser = new EntityMilitaryCruiser(speed, weigth, bodyColor, additionalColor, bodyKit, armor, weapon);
}
public override void DrawTransport(Graphics g)
{
if (EntityCruiser == null || EntityCruiser is not EntityMilitaryCruiser militaryCruiser || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(militaryCruiser.AdditionalColor);
//Границы крейсера
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 35, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 9, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70, 52);
//кузов крейсера
Brush br = new SolidBrush(militaryCruiser.BodyColor);
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70, 50);
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
// оружие крейсера
if (militaryCruiser.Weapon)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 23, 25, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 23, 35, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 23, 20, 15);
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.Entities;
/// <summary>
/// Класс-сущность "Крейсер" Вариант 18
/// </summary>
public class EntityCruiser
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; protected set; }
/// <summary>
/// Вес
/// </summary>
public double Weigth { get; protected set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; protected set; }
/// <summary>
/// Шаг перемещения крейсера
/// </summary>
public double Step => Speed * 100 / Weigth;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
public EntityCruiser(int speed, double weigth, Color bodyColor)
{
Speed = speed;
Weigth = weigth;
BodyColor = bodyColor;
}
}

View File

@ -1,25 +1,10 @@
namespace ProjectCruiser
namespace ProjectCruiser.Entities
{
/// <summary>
/// Класс-сущность "Крейсер" Вариант 18
/// Класс-сущность "Военный Крейсер" Вариант 18
/// </summary>
public class EntityCruiser
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weigth { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
public class EntityMilitaryCruiser: EntityCruiser
{
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
@ -40,11 +25,6 @@
/// </summary>
public bool Weapon { get; private set; }
/// <summary>
/// Шаг перемещения крейсера
/// </summary>
public double Step => Speed * 100 / Weigth;
/// <summary>
/// Инициализация полей объекта класса крейсера
/// </summary>
@ -55,7 +35,7 @@
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="armor">Признак наличия брони</param>
/// <param name="weapon">Признак наличия оружия</param>
public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon)
public EntityMilitaryCruiser(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon): base(speed, weigth, bodyColor)
{
Speed = speed;
Weigth = weigth;

View File

@ -29,11 +29,14 @@
private void InitializeComponent()
{
pictureBoxCruiser = new PictureBox();
buttonCreateCruiser = new Button();
buttonCreateMilitaryCruiser = new Button();
buttonDown = new Button();
buttonUp = new Button();
buttonRight = new Button();
buttonLeft = new Button();
buttonCreateCruiser = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).BeginInit();
SuspendLayout();
//
@ -46,16 +49,16 @@
pictureBoxCruiser.TabIndex = 0;
pictureBoxCruiser.TabStop = false;
//
// buttonCreateCruiser
// buttonCreateMilitaryCruiser
//
buttonCreateCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateCruiser.Location = new Point(12, 409);
buttonCreateCruiser.Name = "buttonCreateCruiser";
buttonCreateCruiser.Size = new Size(94, 29);
buttonCreateCruiser.TabIndex = 1;
buttonCreateCruiser.Text = "Создать";
buttonCreateCruiser.UseVisualStyleBackColor = true;
buttonCreateCruiser.Click += ButtonCreateCruiser_Click;
buttonCreateMilitaryCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateMilitaryCruiser.Location = new Point(12, 409);
buttonCreateMilitaryCruiser.Name = "buttonCreateMilitaryCruiser";
buttonCreateMilitaryCruiser.Size = new Size(216, 29);
buttonCreateMilitaryCruiser.TabIndex = 1;
buttonCreateMilitaryCruiser.Text = "Создать военный крейсер";
buttonCreateMilitaryCruiser.UseVisualStyleBackColor = true;
buttonCreateMilitaryCruiser.Click += ButtonCreateMilitaryCruiser_Click;
//
// buttonDown
//
@ -105,16 +108,52 @@
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// buttonCreateCruiser
//
buttonCreateCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateCruiser.Location = new Point(234, 409);
buttonCreateCruiser.Name = "buttonCreateCruiser";
buttonCreateCruiser.Size = new Size(179, 29);
buttonCreateCruiser.TabIndex = 6;
buttonCreateCruiser.Text = "Создать крейсер";
buttonCreateCruiser.UseVisualStyleBackColor = true;
buttonCreateCruiser.Click += buttonCreateCruiser_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(637, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 7;
//
// buttonStrategyStep
//
buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonStrategyStep.Location = new Point(694, 46);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(94, 29);
buttonStrategyStep.TabIndex = 8;
buttonStrategyStep.Text = "Шаг";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += ButtonStrategyStep_Click;
//
// FormCruiser
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateCruiser);
Controls.Add(buttonLeft);
Controls.Add(buttonRight);
Controls.Add(buttonUp);
Controls.Add(buttonDown);
Controls.Add(buttonCreateCruiser);
Controls.Add(buttonCreateMilitaryCruiser);
Controls.Add(pictureBoxCruiser);
Name = "FormCruiser";
Text = "Крейсер";
@ -125,10 +164,13 @@
#endregion
private PictureBox pictureBoxCruiser;
private Button buttonCreateCruiser;
private Button buttonCreateMilitaryCruiser;
private Button buttonDown;
private Button buttonUp;
private Button buttonRight;
private Button buttonLeft;
private Button buttonCreateCruiser;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStep;
}
}

View File

@ -1,12 +1,18 @@
namespace ProjectCruiser
using ProjectCruiser.Drawings;
using ProjectCruiser.MovementStrategy;
namespace ProjectCruiser
{
public partial class FormCruiser : Form
{
private DrawingCruiser? _drawingCruiser;
private DrawningCruiser? _drawingCruiser;
private AbstactStrategy? _strategy;
public FormCruiser()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
@ -22,37 +28,67 @@
pictureBoxCruiser.Image = bmp;
}
private void ButtonCreateCruiser_Click(object sender, EventArgs e)
private void CreateObject(string type)
{
Random random = new();
_drawingCruiser = new DrawingCruiser();
_drawingCruiser.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)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)));
switch (type)
{
case nameof(DrawningCruiser):
_drawingCruiser = new DrawningCruiser(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(DrawningMilitaryCruiser):
_drawingCruiser = new DrawningMilitaryCruiser(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)));
break;
default:
return;
}
_drawingCruiser.SetPictireSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawingCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать военный крейсер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateMilitaryCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMilitaryCruiser));
/// <summary>
/// Обработка нажатия кнопки "Создать крейсер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCruiser));
private void ButtonMove_Click(object sender, EventArgs e)
{
if(_drawingCruiser == null)
if (_drawingCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch(name) {
switch (name)
{
case "buttonUp":
result = _drawingCruiser.MoveTransport(DirectionType.Up);
result = _drawingCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawingCruiser.MoveTransport(DirectionType.Down);
@ -65,9 +101,50 @@
break;
}
if (result) {
if (result)
{
Draw();
}
}
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawingCruiser == null) { return; }
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return ;
}
_strategy.SetData(new MoveableCruiser(_drawingCruiser), pictureBoxCruiser.Width, pictureBoxCruiser.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,138 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.MovementStrategy;
/// <summary>
/// Класс-стратегия перемещения объекта
/// </summary>
public abstract class AbstactStrategy
{
/// <summary>
/// Перемещаемы объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private StrategyStatus _state = StrategyStatus.NotInit;
/// <summary>
/// Ширина перемещения
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота перемещения
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public StrategyStatus 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 = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestination())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns></returns>
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns></returns>
protected bool MoveRight() => MoveTo(MovementDirection.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns></returns>
protected bool MoveUp() => MoveTo(MovementDirection.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns></returns>
protected bool MoveDown() => MoveTo(MovementDirection.Down);
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestination();
private bool MoveTo(MovementDirection direction)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
return _moveableObject?.TryMoveObject(direction) ?? false;
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.MovementStrategy;
/// <summary>
/// Интерфейс для работы с перемещением объектов
/// </summary>
public interface IMoveableObject
{
/// <summary>
/// Получение координаты объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Попытка переместить объект в указанном направлении
/// </summary>
/// <param name="direction">Направление</param>
/// <returns></returns>
bool TryMoveObject(MovementDirection direction);
}

View File

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

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.MovementStrategy;
public class MoveToCenter : AbstactStrategy
{
protected override bool IsTargetDestination()
{
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,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.Drawings;
namespace ProjectCruiser.MovementStrategy;
/// <summary>
/// Класс реализация IMoveableObject с использованием DrawningCruiser
/// </summary>
public class MoveableCruiser : IMoveableObject
{
/// <summary>
/// Поле-объект класса DrawningCruiser или его наследника
/// </summary>
private readonly DrawningCruiser? _cruiser = null;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="cruiser"></param>
public MoveableCruiser(DrawningCruiser? cruiser)
{
_cruiser = cruiser;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_cruiser == null || _cruiser.EntityCruiser == null || !_cruiser.GetPosX.HasValue || !_cruiser.GetPosY.HasValue)
{
return null;
}
return new ObjectParameters(_cruiser.GetPosX.Value, _cruiser.GetPosY.Value, _cruiser.GetWidth, _cruiser.GetHeight);
}
}
public int GetStep => (int)(_cruiser?.EntityCruiser?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if (_cruiser == null || _cruiser.EntityCruiser == null)
{
return false;
}
return _cruiser.MoveTransport(GetDirectionType(direction));
}
/// <summary>
/// Конвертация из MovementDirection в DirectionType
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
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,24 @@
namespace ProjectCruiser.MovementStrategy;
/// <summary>
/// Направление перемещения
/// </summary>
public enum MovementDirection
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влева
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}

View File

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.MovementStrategy;
/// <summary>
/// Параметры координаты объекта
/// </summary>
public class ObjectParameters
{
/// <summary>
/// Координата X
/// </summary>
private readonly int _x;
/// <summary>
/// Координата Y
/// </summary>
private readonly int _y;
/// <summary>
/// Ширина объекта
/// </summary>
private readonly int _width;
/// <summary>
/// Высота объекта
/// </summary>
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"></param>
/// <param name="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

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.MovementStrategy;
/// <summary>
/// Статус выполнения операции перемещения
/// </summary>
public enum StrategyStatus
{
/// <summary>
/// Все готово к началу
/// </summary>
NotInit,
/// <summary>
/// Выполняется
/// </summary>
InProgress,
/// <summary>
/// Завершено
/// </summary>
Finish
}