PIbd-21 Kryukov A.I Lab 2 Base #9
134
ProjectExcavator/ProjectExcavator/AbstractStrategy.cs
Normal file
134
ProjectExcavator/ProjectExcavator/AbstractStrategy.cs
Normal file
@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace ProjectExcavator.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 (IsTargetDestination())
|
||||
{
|
||||
_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 IsTargetDestination();
|
||||
/// <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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -43,8 +43,9 @@ namespace ProjectExcavator.DrawningObjects
|
||||
//ыспомогательные опоры
|
||||
if (excavator.BodyKit)
|
||||
{
|
||||
|
||||
g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 100, 110, 10);
|
||||
}
|
||||
//отрисовка базы экскаватора
|
||||
base.DrawTransport(g);
|
||||
//ковш
|
||||
if (excavator.Bucket)
|
||||
|
@ -32,11 +32,26 @@ namespace ProjectExcavator.DrawningObjects
|
||||
/// <summary>
|
||||
/// Ширина прорисовки автомобиля
|
||||
/// </summary>
|
||||
protected readonly int _excavatorWidth = 100;
|
||||
protected readonly int _excavatorWidth = 150;
|
||||
/// <summary>
|
||||
/// Высота прорисовки автомобиля
|
||||
/// </summary>
|
||||
protected readonly int _excavatorHeight = 55;
|
||||
protected readonly int _excavatorHeight = 155;
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int GetPosY => _startPosY;
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _excavatorWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _excavatorHeight;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -87,12 +102,36 @@ namespace ProjectExcavator.DrawningObjects
|
||||
_startPosY = y;
|
||||
}
|
||||
/// <summary>
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||
public bool CanMove(DirectionType direction)
|
||||
{
|
||||
if(EntityExcavator == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
DirectionType.Left => _startPosX - EntityExcavator.Step >0,
|
||||
//вверх
|
||||
DirectionType.Up => _startPosY - EntityExcavator.Step > -50,
|
||||
//вправо
|
||||
DirectionType.Right => _startPosX + EntityExcavator.Step < _pictureWidth - _excavatorWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + EntityExcavator.Step < _pictureHeight - _excavatorHeight,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityExcavator == null)
|
||||
if (!CanMove(direction) || EntityExcavator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -100,31 +139,19 @@ namespace ProjectExcavator.DrawningObjects
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityExcavator.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityExcavator.Step;
|
||||
}
|
||||
_startPosX -= (int)EntityExcavator.Step;
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityExcavator.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityExcavator.Step;
|
||||
}
|
||||
_startPosY -= (int)EntityExcavator.Step;
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + EntityExcavator.Step < _pictureWidth - _excavatorWidth)
|
||||
{
|
||||
_startPosX += (int)EntityExcavator.Step;
|
||||
}
|
||||
_startPosX += (int)EntityExcavator.Step;
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + EntityExcavator.Step < _pictureHeight - _excavatorHeight)
|
||||
{
|
||||
_startPosY += (int)EntityExcavator.Step;
|
||||
}
|
||||
_startPosY += (int)EntityExcavator.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
40
ProjectExcavator/ProjectExcavator/DrawningObjectExcavator.cs
Normal file
40
ProjectExcavator/ProjectExcavator/DrawningObjectExcavator.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectExcavator.DrawningObjects;
|
||||
|
||||
namespace ProjectExcavator.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter)
|
||||
/// </summary>
|
||||
public class DrawningObjectExcavator : IMoveableObject
|
||||
{
|
||||
private readonly DrawningExcavator? _drawningExcavator = null;
|
||||
public DrawningObjectExcavator(DrawningExcavator drawningExcavator)
|
||||
{
|
||||
_drawningExcavator = drawningExcavator;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_drawningExcavator == null || _drawningExcavator.EntityExcavator == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningExcavator.GetPosX,
|
||||
_drawningExcavator.GetPosY,
|
||||
_drawningExcavator.GetWidth,
|
||||
_drawningExcavator.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawningExcavator?.EntityExcavator?.Step ?? 0);
|
||||
|
||||
public bool CheckCanMove(DirectionType direction) =>_drawningExcavator?.CanMove(direction) ?? false;
|
||||
|
||||
public void MoveObject(DirectionType direction) => _drawningExcavator?.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -4,6 +4,8 @@ using System.Linq;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectExcavator.Entities;
|
||||
|
||||
|
||||
namespace ProjectExcavator.Entities
|
||||
{
|
||||
@ -38,7 +40,7 @@ namespace ProjectExcavator.Entities
|
||||
Color bodyColor,
|
||||
Color additionalColor,
|
||||
bool bodyKit,
|
||||
bool bucket)
|
||||
bool bucket) : base(speed, weight, bodyColor)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
BodyKit = bodyKit;
|
||||
|
@ -34,6 +34,9 @@
|
||||
buttonUp = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonRight = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonCreateExcavatorBodyKits = new Button();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -50,11 +53,11 @@
|
||||
// buttonCreate
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 415);
|
||||
buttonCreate.Location = new Point(12, 397);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(75, 23);
|
||||
buttonCreate.Size = new Size(155, 41);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.Text = "Создать простой экскаватор";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
//
|
||||
@ -106,11 +109,44 @@
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += buttonMove_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "0", "1" });
|
||||
comboBoxStrategy.Location = new Point(667, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(121, 23);
|
||||
comboBoxStrategy.TabIndex = 6;
|
||||
//
|
||||
// buttonCreateExcavatorBodyKits
|
||||
//
|
||||
buttonCreateExcavatorBodyKits.Location = new Point(173, 397);
|
||||
buttonCreateExcavatorBodyKits.Name = "buttonCreateExcavatorBodyKits";
|
||||
buttonCreateExcavatorBodyKits.Size = new Size(145, 41);
|
||||
buttonCreateExcavatorBodyKits.TabIndex = 7;
|
||||
buttonCreateExcavatorBodyKits.Text = "Создать Экскаватор с обвесами";
|
||||
buttonCreateExcavatorBodyKits.UseVisualStyleBackColor = true;
|
||||
buttonCreateExcavatorBodyKits.Click += buttonCreateExcavatorBodyKits_Click;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Location = new Point(713, 41);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(75, 23);
|
||||
buttonStep.TabIndex = 8;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// ExcavatorForm
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(buttonCreateExcavatorBodyKits);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonUp);
|
||||
@ -133,5 +169,8 @@
|
||||
private Button buttonUp;
|
||||
private Button buttonLeft;
|
||||
private Button buttonRight;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonCreateExcavatorBodyKits;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
using ProjectExcavator.DrawningObjects;
|
||||
using ProjectExcavator.MovementStrategy;
|
||||
|
||||
namespace ProjectExcavator
|
||||
{
|
||||
public partial class ExcavatorForm : Form
|
||||
@ -7,6 +10,10 @@ namespace ProjectExcavator
|
||||
/// </summary>
|
||||
private DrawningExcavator? _drawnigExcavator;
|
||||
/// <summary>
|
||||
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
|
||||
/// </summary>
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
/// <summary>
|
||||
/// Èíèöèàëèçàöèÿ ôîðìû
|
||||
/// </summary>
|
||||
public ExcavatorForm()
|
||||
@ -41,17 +48,12 @@ namespace ProjectExcavator
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawnigExcavator = new DrawningExcavator();
|
||||
_drawnigExcavator.Init(random.Next(100, 300),
|
||||
_drawnigExcavator = new DrawningExcavator(
|
||||
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)),
|
||||
pictureBoxExcavator.Width,
|
||||
pictureBoxExcavator.Height);
|
||||
_drawnigExcavator.SetPosition(random.Next(10, 100),
|
||||
random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
@ -78,5 +80,56 @@ namespace ProjectExcavator
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonCreateExcavatorBodyKits_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawnigExcavator = new DrawningExcavarorBodyKits(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)),
|
||||
pictureBoxExcavator.Width,
|
||||
pictureBoxExcavator.Height);
|
||||
_drawnigExcavator.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawnigExcavator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawningObjectExcavator(_drawnigExcavator),
|
||||
pictureBoxExcavator.Width,
|
||||
pictureBoxExcavator.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
ProjectExcavator/ProjectExcavator/IMoveableObject.cs
Normal file
34
ProjectExcavator/ProjectExcavator/IMoveableObject.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectExcavator.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);
|
||||
}
|
||||
}
|
49
ProjectExcavator/ProjectExcavator/MoveToBorder.cs
Normal file
49
ProjectExcavator/ProjectExcavator/MoveToBorder.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using ProjectExcavator.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectExcavator.MovementStrategy
|
||||
{
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
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 = objParams.RightBorder - FieldWidth - 150;
|
||||
var diffY = objParams.DownBorder - FieldHeight - 150;
|
||||
if(diffX >= 0)
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
else if (diffY >= 0)
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else if(Math.Abs(diffX) > Math.Abs(diffY))
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
57
ProjectExcavator/ProjectExcavator/MoveToCenter.cs
Normal file
57
ProjectExcavator/ProjectExcavator/MoveToCenter.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectExcavator.MovementStrategy
|
||||
{
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
54
ProjectExcavator/ProjectExcavator/ObjectParameters.cs
Normal file
54
ProjectExcavator/ProjectExcavator/ObjectParameters.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectExcavator.MovementStrategy
|
||||
{
|
||||
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
ProjectExcavator/ProjectExcavator/Status.cs
Normal file
15
ProjectExcavator/ProjectExcavator/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 ProjectExcavator
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit = 0,
|
||||
InProgress = 1,
|
||||
Finish = 2
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user