This commit is contained in:
ValAnn 2023-11-17 17:41:17 +04:00
parent 9689bd79b0
commit b4c0d818b8
13 changed files with 749 additions and 156 deletions

View File

@ -0,0 +1,135 @@
using DumpTruckr.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace DumpTruck.MovementStrategy
{
public abstract class AbstractStrategy
{
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
public void SetData(IMoveableObject moveableObject, int width, int
height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveLeft() => MoveTo(DirectionType.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveRight() => MoveTo(DirectionType.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveUp() => MoveTo(DirectionType.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
protected bool MoveDown() => MoveTo(DirectionType.Down);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -3,111 +3,38 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DumpTruck.Entities;
namespace DumpTruck
namespace DumpTruck.DrawningObjects
{
public class DrawningDumpTruck
public class DrawningDumpTruck : DrawningCar
{
public DumpTruck EntityDumpTruck { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _carWidth = 110;
private readonly int _carHeight = 60;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent, int width, int height)
public DrawningDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent, int width, int height): base(speed, weight, bodyColor, width, height, 110, 60)
{
if (width <= _carWidth || height <= _carHeight)
return false;
_pictureWidth = width;
_pictureHeight = height;
EntityDumpTruck = new DumpTruck();
EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, bodyKit, tent);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
if (EntityCar != null)
{
_startPosX = 0;
_startPosY = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntityDumpTruck == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityDumpTruck.Step > 0)
{
_startPosX -= (int)EntityDumpTruck.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityDumpTruck.Step > 0)
{
_startPosY -= (int)EntityDumpTruck.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + EntityDumpTruck.Step + _carWidth < _pictureWidth)
{
_startPosX += (int)EntityDumpTruck.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + EntityDumpTruck.Step + _carHeight < _pictureHeight)
{
_startPosY += (int)EntityDumpTruck.Step;
}
break;
EntityCar = new EntityDumpTruck(speed, weight, bodyColor,additionalColor, bodyKit, tent);
}
}
public void DrawTransport(Graphics g)
public override void DrawTransport(Graphics g)
{
if (EntityDumpTruck == null)
if (EntityCar is not EntityDumpTruck dumpTruck)
{
return;
}
Pen pen = new Pen(Color.Black);
Brush brush = new SolidBrush(EntityDumpTruck.BodyColor);
Brush addBrush = new SolidBrush(EntityDumpTruck.AdditionalColor);
Brush addBrush = new SolidBrush(dumpTruck.AdditionalColor);
Brush brush = new SolidBrush(dumpTruck.BodyColor);
//границы автомобиля
g.FillRectangle(brush, _startPosX, _startPosY + 35, 110, 10);
g.FillRectangle(brush, _startPosX + 85, _startPosY, 25, 35);
g.FillEllipse(brush, _startPosX, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 15, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 95, _startPosY + 35 + 10, 15, 15);
if (EntityDumpTruck.Tent)
base.DrawTransport(g);
if (dumpTruck.Tent)
{
Point[] points = new Point[3];
points[0].X = _startPosX; points[0].Y = _startPosY + 35;
@ -116,7 +43,7 @@ namespace DumpTruck
g.FillPolygon(addBrush, points);
}
if (EntityDumpTruck.BodyKit)
if (dumpTruck.BodyKit)
{
Point[] points = new Point[4];
points[0].X = _startPosX; points[0].Y = _startPosY + 35;
@ -126,7 +53,7 @@ namespace DumpTruck
g.FillPolygon(addBrush, points);
}
if (EntityDumpTruck.BodyKit && EntityDumpTruck.Tent)
if (dumpTruck.BodyKit && dumpTruck.Tent)
{
int x = _startPosX;
int y = _startPosY - 8;

View File

@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.Entities;
namespace DumpTruck.DrawningObjects
{
public class DrawningCar
{
public EntityCar? EntityCar { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private readonly int _carWidth = 110;
private readonly int _carHeight = 60;
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _carWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _carHeight;
public DrawningCar(int speed, double weight, Color bodyColor, int width, int height)
{
if(width < _carWidth || height < _carHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityCar = new EntityCar(speed, weight, bodyColor);
}
protected DrawningCar(int speed, double weight, Color bodyColor, int
width, int height, int carWidth, int carHeight)
{
if (width <= _carWidth || height <= _carHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_carWidth = carWidth;
_carHeight = carHeight;
EntityCar = new EntityCar(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
{
_startPosX = 0;
_startPosY = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityCar == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
_startPosX -= (int)EntityCar.Step;
break;
//вверх
case DirectionType.Up:
_startPosY -= (int)EntityCar.Step;
break;
// вправо
case DirectionType.Right:
_startPosX += (int)EntityCar.Step;
break;
//вниз
case DirectionType.Down:
_startPosY += (int)EntityCar.Step;
break;
}
}
public bool CanMove(DirectionType direction)
{
if (EntityCar == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityCar.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityCar.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityCar.Step + _carWidth < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityCar.Step + _carHeight < _pictureHeight,
_ => false,
};
}
public virtual void DrawTransport(Graphics g)
{
if (EntityCar == null)
{
return;
}
Pen pen = new Pen(Color.Black);
Brush brush = new SolidBrush(EntityCar.BodyColor);
//границы автомобиля
g.FillRectangle(brush, _startPosX, _startPosY + 35, 110, 10);
g.FillRectangle(brush, _startPosX + 85, _startPosY, 25, 35);
g.FillEllipse(brush, _startPosX, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 15, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 95, _startPosY + 35 + 10, 15, 15);
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.DrawningObjects;
using DumpTruckr.MovementStrategy;
namespace DumpTruck.MovementStrategy
{
internal class DrawningObjectCar : IMoveableObject
{
private readonly DrawningCar? _drawningCar = null;
public DrawningObjectCar(DrawningCar drawningCar)
{
_drawningCar = drawningCar;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningCar == null || _drawningCar.EntityCar ==
null)
{
return null;
}
return new ObjectParameters(_drawningCar.GetPosX,
_drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight);
}
}
public int GetStep => (int)(_drawningCar?.EntityCar?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningCar?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningCar?.MoveTransport(direction);
}
}

View File

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

View File

@ -7,26 +7,11 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DumpTruck
namespace DumpTruck.Entities
{
public class DumpTruck
public class EntityDumpTruck : EntityCar
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
@ -41,17 +26,10 @@ namespace DumpTruck
/// Признак (опция) наличия tent
/// </summary>
public bool Tent { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool bodyKit, bool tent)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
public EntityDumpTruck(int speed, double weight, Color bodyColor, Color
additionalColor, bool bodyKit, bool tent) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Tent = tent;

View File

@ -30,11 +30,14 @@
{
this.button1 = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.button = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDumpTruck = new System.Windows.Forms.Button();
this.buttonCar = new System.Windows.Forms.Button();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.buttonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
@ -57,17 +60,6 @@
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// button
//
this.button.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button.Location = new System.Drawing.Point(283, 385);
this.button.Name = "button";
this.button.Size = new System.Drawing.Size(225, 39);
this.button.TabIndex = 2;
this.button.Text = "Добавить";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -116,16 +108,66 @@
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonDumpTruck
//
this.buttonDumpTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonDumpTruck.Location = new System.Drawing.Point(28, 385);
this.buttonDumpTruck.Name = "buttonDumpTruck";
this.buttonDumpTruck.Size = new System.Drawing.Size(225, 39);
this.buttonDumpTruck.TabIndex = 7;
this.buttonDumpTruck.Text = "Добавить самосвал ";
this.buttonDumpTruck.UseVisualStyleBackColor = true;
this.buttonDumpTruck.Click += new System.EventHandler(this.buttonDumpTruck_Click);
//
// buttonCar
//
this.buttonCar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCar.Location = new System.Drawing.Point(277, 385);
this.buttonCar.Name = "buttonCar";
this.buttonCar.Size = new System.Drawing.Size(225, 39);
this.buttonCar.TabIndex = 8;
this.buttonCar.Text = "Добавить грузовик";
this.buttonCar.UseVisualStyleBackColor = true;
this.buttonCar.Click += new System.EventHandler(this.button2_Click);
//
// comboBoxStrategy
//
this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxStrategy.FormattingEnabled = true;
this.comboBoxStrategy.Items.AddRange(new object[] {
"К центру",
"К границе"});
this.comboBoxStrategy.Location = new System.Drawing.Point(708, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(164, 23);
this.comboBoxStrategy.TabIndex = 9;
//
// buttonStep
//
this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonStep.Location = new System.Drawing.Point(767, 53);
this.buttonStep.Name = "buttonStep";
this.buttonStep.Size = new System.Drawing.Size(105, 26);
this.buttonStep.TabIndex = 10;
this.buttonStep.Text = "Шаг";
this.buttonStep.UseVisualStyleBackColor = true;
this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click);
//
// FormDumpTruck
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(884, 461);
this.Controls.Add(this.buttonStep);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonCar);
this.Controls.Add(this.buttonDumpTruck);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.button);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.button1);
this.Name = "FormDumpTruck";
@ -141,10 +183,13 @@
private Button button1;
private PictureBox pictureBox;
private Button button;
private Button buttonDown;
private Button buttonUp;
private Button buttonRight;
private Button buttonLeft;
private Button buttonDumpTruck;
private Button buttonCar;
private ComboBox comboBoxStrategy;
private Button buttonStep;
}
}

View File

@ -1,44 +1,35 @@
using DumpTruck.DrawningObjects;
using DumpTruck.MovementStrategy;
namespace DumpTruck
{
public partial class FormDumpTruck : Form
{
private DrawningCar? _drawningCar;
private AbstractStrategy? _abstractStrategy;
public FormDumpTruck()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawningDumpTruck = new DrawningDumpTruck();
_drawningDumpTruck.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)),
pictureBox.Width, pictureBox.Height);
_drawningDumpTruck.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
}
private DrawningDumpTruck _drawningDumpTruck;
private void Draw()
{
if (_drawningDumpTruck == null)
if (_drawningCar == null)
{
return;
}
Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningDumpTruck.DrawTransport(gr);
_drawningCar.DrawTransport(gr);
pictureBox.Image = bmp;
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningDumpTruck == null)
if (_drawningCar == null)
{
return;
}
@ -46,20 +37,86 @@ namespace DumpTruck
switch (name)
{
case "buttonUp":
_drawningDumpTruck.MoveTransport(DirectionType.Up);
_drawningCar.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningDumpTruck.MoveTransport(DirectionType.Down);
_drawningCar.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningDumpTruck.MoveTransport(DirectionType.Left);
_drawningCar.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningDumpTruck.MoveTransport(DirectionType.Right);
_drawningCar.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void button2_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCar = new DrawningCar(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBox.Width, pictureBox.Height);
_drawningCar.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void buttonDumpTruck_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCar = new DrawningDumpTruck(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)),
pictureBox.Width, pictureBox.Height);
_drawningCar.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawningCar == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectCar(_drawningCar), pictureBox.Width,
pictureBox.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -0,0 +1,36 @@
using DumpTruck;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.MovementStrategy;
namespace DumpTruckr.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);
}
}

View File

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

View File

@ -0,0 +1,58 @@
using DumpTruck.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
internal 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,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
/// <summary>
/// Параметры-координаты объекта
/// </summary>
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder => _x;
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder => _y;
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

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