diff --git a/DumpTruck/DumpTruck/AbstractStrategy.cs b/DumpTruck/DumpTruck/AbstractStrategy.cs index ae39402..6d34abf 100644 --- a/DumpTruck/DumpTruck/AbstractStrategy.cs +++ b/DumpTruck/DumpTruck/AbstractStrategy.cs @@ -3,11 +3,10 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using static DumpTruck.DrawningDumpTruck; namespace DumpTruck.MovementStrategy { - internal class AbstractStrategy + abstract class AbstractStrategy { /// @@ -104,12 +103,12 @@ namespace DumpTruck.MovementStrategy /// /// Перемещение к цели /// - // protected abstract void MoveToTarget(); + protected abstract void MoveToTarget(); /// /// Достигнута ли цель /// /// - // protected abstract bool IsTargetDestinaion(); + protected abstract bool IsTargetDestinaion(); /// /// Попытка перемещения в требуемом направлении /// diff --git a/DumpTruck/DumpTruck/DrawningDumpTruck.cs b/DumpTruck/DumpTruck/DrawningDumpTruck.cs index a51c445..85140dd 100644 --- a/DumpTruck/DumpTruck/DrawningDumpTruck.cs +++ b/DumpTruck/DumpTruck/DrawningDumpTruck.cs @@ -3,39 +3,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using DumpTruck.Entities; -namespace DumpTruck +namespace DumpTruck.DrawningObjects { - internal class DrawningDumpTruck + internal class DrawningDumpTruck : DrawningTruck { - /// - /// Класс-сущность - /// - public DumpTruck? DumpTruck { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// Левая координата прорисовки автомобиля - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки автомобиля - /// - private int _startPosY; - /// - /// Ширина прорисовки автомобиля - /// - private readonly int _DumpTruckWidth = 110; - /// - /// Высота прорисовки автомобиля - /// - private readonly int _DumpTruckHeight = 85; /// /// Инициализация свойств /// @@ -46,102 +19,24 @@ namespace DumpTruck /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool body, bool trailer, int width, int height) - { - if ((height < _DumpTruckHeight) || (width < _DumpTruckWidth)) - { - return false; - } - _pictureWidth = width; - _pictureHeight = height; - DumpTruck = new DumpTruck(); - DumpTruck.Init(speed, weight, bodyColor, additionalColor, - body, trailer); - return true; + public DrawningDumpTruck(int speed, double weight, Color bodyColor, Color + additionalColor, bool trailer, int width, int height) : base (speed, weight, bodyColor, additionalColor, width, height,100, 55) + { + if (EntityTruck != null) + { + EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, trailer); + } } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) + + public override void DrawTransport(Graphics g) { - if ((x < 0 || y < 0) || (x + _DumpTruckWidth > _pictureWidth || y + _DumpTruckHeight > _pictureHeight)) - { - _startPosX = 0; - _startPosY = 0; - } - else - { - _startPosX = x; - _startPosY = y; - } - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) - { - if (DumpTruck == null) + if (EntityTruck is not EntityDumpTruck dumpTruck + ) { return; } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - DumpTruck.Step > 0) - { - _startPosX -= (int)DumpTruck.Step; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - DumpTruck.Step > 0) - { - _startPosY -= (int)DumpTruck.Step; - } - break; - // вправо - case DirectionType.Right: - if (_startPosX + _DumpTruckWidth + DumpTruck.Step < _pictureWidth) - { - _startPosX += (int)DumpTruck.Step; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + _DumpTruckHeight + DumpTruck.Step < _pictureHeight) - { - _startPosY += (int)DumpTruck.Step; - } - break; - } - } - public void DrawTransport(Graphics g) - { - if (DumpTruck == null) - { - return; - } - Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(DumpTruck.AdditionalColor); - //грани - g.DrawRectangle(pen, _startPosX + 80, _startPosY, 30, 40); - g.DrawRectangle(pen, _startPosX, _startPosY + 40, 110, 20); - //кузов - Brush br = new SolidBrush(DumpTruck.BodyColor); - g.FillRectangle(br, _startPosX + 81, _startPosY + 1, 29, 40); - Brush br1 = new SolidBrush(DumpTruck.AdditionalColor); - g.FillRectangle(br1, _startPosX + 1, _startPosY + 41, 109, 19); - //колеса - Brush wheels = new SolidBrush(Color.Black); - g.FillEllipse(wheels, _startPosX, _startPosY + 63, 25, 25); - g.FillEllipse(wheels, _startPosX + 25, _startPosY + 63, 25, 25); - g.FillEllipse(wheels, _startPosX + 85, _startPosY + 63, 25, 25); - if (DumpTruck.Trailer) + base.DrawTransport(g); + if (dumpTruck.Trailer) { //прицеп Brush trailer = new SolidBrush(Color.Black); diff --git a/DumpTruck/DumpTruck/DrawningObjectTruck.cs b/DumpTruck/DumpTruck/DrawningObjectTruck.cs new file mode 100644 index 0000000..5b046d3 --- /dev/null +++ b/DumpTruck/DumpTruck/DrawningObjectTruck.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawningObjects; + + +namespace DumpTruck.MovementStrategy +{ + internal class DrawningObjectTruck : IMoveableObject + + { + private readonly DrawningTruck? _drawningTruck = null; + public DrawningObjectTruck(DrawningTruck drawningTruck) + { + _drawningTruck = drawningTruck; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningTruck == null || _drawningTruck.EntityTruck == null) + { + return null; + } + return new ObjectParameters(_drawningTruck.GetPosX, _drawningTruck.GetPosY, _drawningTruck.GetWidth, _drawningTruck.GetHeight); + } + } + public int GetStep => (int)(_drawningTruck?.EntityTruck?.Step ?? 0); + + public bool CheckCanMove(DirectionType direction) => _drawningTruck?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawningTruck?.MoveTransport(direction); + } +} +} diff --git a/DumpTruck/DumpTruck/DrawningTruck.cs b/DumpTruck/DumpTruck/DrawningTruck.cs index 9c05918..721fa11 100644 --- a/DumpTruck/DumpTruck/DrawningTruck.cs +++ b/DumpTruck/DumpTruck/DrawningTruck.cs @@ -15,7 +15,7 @@ namespace DumpTruck.DrawningObjects /// /// Класс-сущность /// - public Truck? Truck { get; protected set; } + public EntityTruck? EntityTruck { get; protected set; } /// /// Ширина окна /// @@ -40,6 +40,23 @@ namespace DumpTruck.DrawningObjects /// Высота прорисовки автомобиля /// protected readonly int _truckHeight = 55; + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _truckWidth; + /// + /// Высота объекта + /// + public int GetHeight => _truckHeight; + /// /// Конструктор /// @@ -53,7 +70,7 @@ namespace DumpTruck.DrawningObjects // TODO: Продумать проверки _pictureWidth = width; _pictureHeight = height; - Truck = new Truck(speed, weight, bodyColor, additionalColor); + EntityTruck = new EntityTruck(speed, weight, bodyColor, additionalColor); } /// /// Конструктор @@ -72,7 +89,7 @@ namespace DumpTruck.DrawningObjects _pictureHeight = height; _truckWidth = truckWidth; _truckHeight = truckHeight; - Truck = new Truck(speed, weight, bodyColor, additionalColor); + EntityTruck = new EntityTruck(speed, weight, bodyColor, additionalColor); } /// /// Установка позиции @@ -81,17 +98,49 @@ namespace DumpTruck.DrawningObjects /// Координата Y public void SetPosition(int x, int y) { - // TODO: Изменение x, y, если при установке объект выходит за границы + if (x < 0 || x + _truckWidth > _pictureWidth) + { + x = Math.Max(0, _pictureWidth - _truckWidth); + } + if (y < 0 || y + _truckHeight > _pictureHeight) + { + y = Math.Max(0, _pictureHeight - _truckHeight); + } _startPosX = x; _startPosY = y; } + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityTruck == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityTruck.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityTruck.Step > 0, + // вправо + DirectionType.Right => _startPosX + _truckWidth + EntityTruck.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + _truckHeight + EntityTruck.Step < _pictureHeight, + _ => false, + }; + } + /// /// Изменение направления перемещения /// /// Направление public void MoveTransport(DirectionType direction) { - if (Truck == null) + if (!CanMove(direction) || EntityTruck == null) { return; } @@ -99,37 +148,37 @@ namespace DumpTruck.DrawningObjects { //влево case DirectionType.Left: - if (_startPosX - Truck.Step > 0) + if (_startPosX - EntityTruck.Step > 0) { - _startPosX -= (int)Truck.Step; + _startPosX -= (int)EntityTruck.Step; } break; //вверх case DirectionType.Up: - if (_startPosY - Truck.Step > 0) + if (_startPosY - EntityTruck.Step > 0) { - _startPosY -= (int)Truck.Step; + _startPosY -= (int)EntityTruck.Step; } break; // вправо case DirectionType.Right: - if (_startPosX + _truckWidth + Truck.Step < _pictureWidth) + if (_startPosX + _truckWidth + EntityTruck.Step < _pictureWidth) { - _startPosX += (int)Truck.Step; + _startPosX += (int)EntityTruck.Step; } break; //вниз case DirectionType.Down: - if (_startPosY + _truckHeight + Truck.Step < _pictureHeight) + if (_startPosY + _truckHeight + EntityTruck.Step < _pictureHeight) { - _startPosY += (int)Truck.Step; + _startPosY += (int)EntityTruck.Step; } break; } } public virtual void DrawTransport(Graphics g) { - if (Truck == null) + if (EntityTruck == null) { return; } @@ -138,9 +187,9 @@ namespace DumpTruck.DrawningObjects g.DrawRectangle(pen, _startPosX + 80, _startPosY, 30, 40); g.DrawRectangle(pen, _startPosX, _startPosY + 40, 110, 20); //кузов - Brush br = new SolidBrush(Truck.BodyColor); + Brush br = new SolidBrush(EntityTruck.BodyColor); g.FillRectangle(br, _startPosX + 81, _startPosY + 1, 29, 40); - Brush br1 = new SolidBrush(Truck.AdditionalColor); + Brush br1 = new SolidBrush(EntityTruck.AdditionalColor); g.FillRectangle(br1, _startPosX + 1, _startPosY + 41, 109, 19); //колеса Brush wheels = new SolidBrush(Color.Black); diff --git a/DumpTruck/DumpTruck/DumpTruck.cs b/DumpTruck/DumpTruck/DumpTruck.cs deleted file mode 100644 index 3c6cfa1..0000000 --- a/DumpTruck/DumpTruck/DumpTruck.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DumpTruck -{ - internal class DumpTruck - { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor { get; private set; } - /// - /// Признак (опция) наличия обвеса - /// - public bool Body { get; private set; } - /// - /// Признак (опция) наличия антикрыла - /// - public bool Trailer { get; private set; } - /// - /// Шаг перемещения автомобиля - /// - public double Step => (double)Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса - /// - /// Скорость - /// Вес автомобиля - /// Основной цвет - /// Дополнительный цвет - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool body, bool trailer) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Body = body; - Trailer = trailer; - } - } -} diff --git a/DumpTruck/DumpTruck/EntityDumpTruck.cs b/DumpTruck/DumpTruck/EntityDumpTruck.cs new file mode 100644 index 0000000..5536a41 --- /dev/null +++ b/DumpTruck/DumpTruck/EntityDumpTruck.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Entities +{ + public class EntityDumpTruck : EntityTruck + { + /// + /// Признак (опция) наличия антикрыла + /// + public bool Trailer { get; private set; } + + /// Инициализация полей объекта-класса + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + /// Дополнительный цвет + public EntityDumpTruck(int speed, double weight, Color bodyColor, Color + additionalColor, bool trailer) : base (speed,weight, bodyColor,additionalColor) + { + Trailer = trailer; + } + } +} diff --git a/DumpTruck/DumpTruck/Truck.cs b/DumpTruck/DumpTruck/EntityTruck.cs similarity index 91% rename from DumpTruck/DumpTruck/Truck.cs rename to DumpTruck/DumpTruck/EntityTruck.cs index fe99a67..bef2ece 100644 --- a/DumpTruck/DumpTruck/Truck.cs +++ b/DumpTruck/DumpTruck/EntityTruck.cs @@ -9,7 +9,7 @@ namespace DumpTruck.Entities /// /// Класс-сущность "Автомобиль" /// - public class Truck + public class EntityTruck { /// /// Скорость @@ -36,7 +36,7 @@ namespace DumpTruck.Entities /// Скорость /// Вес автомобиля /// Основной цвет - public Truck(int speed, double weight, Color bodyColor, Color additionalColor) + public EntityTruck(int speed, double weight, Color bodyColor,Color additionalColor) { Speed = speed; Weight = weight; diff --git a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs index 781d9b0..86ae448 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs @@ -29,7 +29,6 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormDumpTruck)); this.pictureBoxDumpTruck = new System.Windows.Forms.PictureBox(); - this.Button_Create = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); @@ -46,16 +45,6 @@ this.pictureBoxDumpTruck.TabIndex = 0; this.pictureBoxDumpTruck.TabStop = false; // - // Button_Create - // - this.Button_Create.Location = new System.Drawing.Point(30, 407); - this.Button_Create.Name = "Button_Create"; - this.Button_Create.Size = new System.Drawing.Size(75, 23); - this.Button_Create.TabIndex = 1; - this.Button_Create.Text = "Создать"; - this.Button_Create.UseVisualStyleBackColor = true; - this.Button_Create.Click += new System.EventHandler(this.Button_Create_Click); - // // buttonUp // this.buttonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonUp.BackgroundImage"))); @@ -114,7 +103,6 @@ this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonUp); - this.Controls.Add(this.Button_Create); this.Controls.Add(this.pictureBoxDumpTruck); this.Name = "FormDumpTruck"; this.Text = "FormDumpTruck"; @@ -126,7 +114,6 @@ #endregion private PictureBox pictureBoxDumpTruck; - private Button Button_Create; private Button buttonUp; private Button buttonLeft; private Button buttonRight; diff --git a/DumpTruck/DumpTruck/FormDumpTruck.cs b/DumpTruck/DumpTruck/FormDumpTruck.cs index db3b870..8f9d1cb 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.cs @@ -7,6 +7,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using DumpTruck.DrawningObjects; +using DumpTruck.MovementStrategy; + namespace DumpTruck { @@ -35,16 +38,9 @@ namespace DumpTruck /// /// /// - private void Button_Create_Click(object sender, EventArgs e) + private void Butt1on_Create_Click(object sender, EventArgs e) { - Random random = new(); - _drawningDumpTruck = new DrawningDumpTruck(); - _drawningDumpTruck.Init(130, 1000, - 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)), true, - Convert.ToBoolean(random.Next(0, 2)), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); - _drawningDumpTruck.SetPosition(random.Next(30, 100), random.Next(30, 100)); - Draw(); + } private void ButtonMove_Click(object sender, EventArgs e) { diff --git a/DumpTruck/DumpTruck/FormDumpTruck.resx b/DumpTruck/DumpTruck/FormDumpTruck.resx index abf2eae..ff0022e 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.resx +++ b/DumpTruck/DumpTruck/FormDumpTruck.resx @@ -61,7 +61,7 @@ iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOwAAADsABataJCQAAGRlJREFUeNrt3Vt26jy2huHg + AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAGRlJREFUeNrt3Vt26jy2huHg dCDC1QCTsu+jobUaALQgofrflkIHOySLEA4+aE69vvH49kX9yM/WXIEpyU/pql7jRZQaeRziI49DfORx iI88DvGRxyE+8jjERx6H+MjjEB95HOIjj0N85HGIjzwO8ZHHIT7yOATHdG/S/50oLvI4xEfVI6z+10Ao Ozb1B4SyY2teGgglx+q9/gOh6FgZaz8glBxbU7s1hILjsY66YyXVO8AntUPq47GOumMl1TvAJ7VD6mNn @@ -175,7 +175,7 @@ iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAFy1JREFUeNrtnV2S6ka2RkFB + AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvAAADrwBlbxySQAAFy1JREFUeNrtnV2S6ka2RkFB v7ese98VCmkA6hwBlzsB4tjzn0qTPxLC5lBCKH++qsVDVy932yf5VmxD1lbmPnT+1R78C5RD4pBH4pBH 4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pDH6VWFvw6qInHII3HII3HII3HII3HII3HI I3HII3HII3HII3HII3HII3HII3HII3EIY/hJ700WiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMe @@ -280,7 +280,7 @@ iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOwAAADsABataJCQAAFlpJREFUeNrtndFy47h2RT36 + AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAFlpJREFUeNrtndFy47h2RT36 ArNov8tKfgAF3XdHlfueuCr//ysRQJCy3W7LkkUSC1p80aypm+45e+W0dRoE8LAbnu3D8Ig4NA48Ggce jQOPxoFH48CjceDROPBoHHg0DjwaBx6NA4/GgUfjwKNx4HF8NuXfi1Q0DjwaBx6NA4/GgUfjwKNx4NE4 8GgceDQOPBoHHo0Dj8aBR+PAo3GAsXy69oZF48CjceDROPBoHHg0DjwaBx6NA4/GgUfjwKNx4NE48Ggc @@ -382,7 +382,7 @@ iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOwAAADsABataJCQAAGBNJREFUeNrt3V9y+r7Vx3Fj + AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAGBNJREFUeNrt3V9y+r7Vx3Fj NoARuTeacB9VYQGPM92A2f9eimTZSb5PSAD/05Hens7QTy/6i/WqjqFHkotwlbq7oosf1fWy3WWq7lo+ WvsW4+B8jRBCmDahglA6oYUQQgghhBBCCCGEEEIIIRwT+V0IIYQQQjg2KgilE1bqJV7C8FmH/zy2WH5c i5gNI6rCkK4Qq5e6iHSsIIQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGE8K+oIJROaM/xEoaLfuGvka49 diff --git a/DumpTruck/DumpTruck/IMoveableObject.cs b/DumpTruck/DumpTruck/IMoveableObject.cs index 87d8028..0e59b6e 100644 --- a/DumpTruck/DumpTruck/IMoveableObject.cs +++ b/DumpTruck/DumpTruck/IMoveableObject.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using static DumpTruck.DrawningDumpTruck; namespace DumpTruck.MovementStrategy diff --git a/DumpTruck/DumpTruck/MoveToBorder.cs b/DumpTruck/DumpTruck/MoveToBorder.cs new file mode 100644 index 0000000..6cadba1 --- /dev/null +++ b/DumpTruck/DumpTruck/MoveToBorder.cs @@ -0,0 +1,58 @@ +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(); + } + } + } + } +} diff --git a/DumpTruck/DumpTruck/MoveToCenter.cs b/DumpTruck/DumpTruck/MoveToCenter.cs new file mode 100644 index 0000000..0b4ea5a --- /dev/null +++ b/DumpTruck/DumpTruck/MoveToCenter.cs @@ -0,0 +1,54 @@ +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(); + } + } + } + } +}