diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/AbstractStrategy.cs b/ProjectWarmlyShip/ProjectWarmlyShip/AbstractStrategy.cs new file mode 100644 index 0000000..2698dfa --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/AbstractStrategy.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectWarmlyShip.DrawingObjects; + +namespace ProjectWarmlyShip.MovementStrategy +{ + public abstract class AbstractStrategy + { + private IMoveableObject? _movebleObject; + private Status _state = Status.NotInit; + protected int FieldWidth { get; private set; } + protected int FieldHeight { get; private set; } + public Status GetStatus() { return _state; } + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _movebleObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestination()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + protected bool MoveLeft() => MoveTo(DirectionType.Left); + protected bool MoveRight() => MoveTo(DirectionType.Right); + protected bool MoveUp() => MoveTo(DirectionType.Up); + protected bool MoveDown() => MoveTo(DirectionType.Down); + + protected ObjectParametrs? GetObjectParametrs => _movebleObject?.GetObjectPosition; + + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _movebleObject?.GetStep; + } + protected abstract void MoveToTarget(); + protected abstract bool IsTargetDestination(); + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_movebleObject?.CheckCanMove(directionType) ?? false) + { + _movebleObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingObjectShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingObjectShip.cs new file mode 100644 index 0000000..c2fed50 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingObjectShip.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectWarmlyShip.DrawingObjects; + +namespace ProjectWarmlyShip.MovementStrategy +{ + public class DrawingObjectShip : IMoveableObject + { + private readonly DrawingShip? _drawingShip = null; + public DrawingObjectShip(DrawingShip drawingShip) + { + _drawingShip = drawingShip; + } + public ObjectParametrs? GetObjectPosition + { + get + { + if (_drawingShip == null || _drawingShip.EntityShip == null) + { + return null; + } + return new ObjectParametrs(_drawingShip.GetPosX, + _drawingShip.GetPosY, _drawingShip.GetWidth, + _drawingShip.GetHeight); + } + } + public int GetStep => (int)(_drawingShip?.EntityShip?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawingShip?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawingShip?.MoveTransport(direction); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShip.cs new file mode 100644 index 0000000..afde554 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShip.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectWarmlyShip.Entities; + +namespace ProjectWarmlyShip.DrawingObjects +{ + public class DrawingShip + { + public EntityShip? EntityShip { get; protected set; } + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected readonly int _shipWidth = 100; + protected readonly int _shipHeight = 30; + + public DrawingShip(int speed, double weight, Color mainColor, int width, int heigth) + { + if (width <= _shipWidth || heigth <= _shipHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = heigth; + EntityShip = new EntityShip(speed, weight, mainColor); + } + protected DrawingShip(int speed, double weight, + Color mainColor, int width, int heigth, + int shipWidth, int shipHeight) + { + if (width <= shipWidth || heigth <= shipHeight) + { + return; + } + _pictureHeight = heigth; + _pictureWidth = width; + _shipHeight = shipHeight; + _shipWidth = shipWidth; + EntityShip = new EntityShip(speed, weight, mainColor); + } + public void SetPosition(int x, int y) + { + if (x < 0 || y < 0 || x + _shipWidth > _pictureWidth || y + _shipHeight > _pictureHeight) + { + x = 10; + y = 10; + } + _startPosX = x; + _startPosY = y; + } + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _shipWidth; + public int GetHeight => _shipHeight; + public bool CanMove(DirectionType direction) + { + if (EntityShip == null) + { + return false; + } + return direction switch + { + DirectionType.Left => _startPosX - EntityShip.Step > 0, + DirectionType.Up => _startPosY - EntityShip.Step > 0, + DirectionType.Right => _startPosX + EntityShip.Step + _shipWidth <= _pictureWidth, + DirectionType.Down => _startPosY + EntityShip.Step + _shipHeight <= _pictureHeight, + _ => false, + }; + } + + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityShip == null) + { + return; + } + switch (direction) + { + case DirectionType.Left: + _startPosX -= (int)EntityShip.Step; + break; + case DirectionType.Up: + _startPosY -= (int)EntityShip.Step; + break; + case DirectionType.Right: + _startPosX += (int)EntityShip.Step; + break; + case DirectionType.Down: + _startPosY += (int)EntityShip.Step; + break; + } + } + public virtual void DrawTrasport(Graphics g) + { + if (EntityShip == null) + { + return; + } + Pen pen = new(Color.Black); + Brush mainBrush = new SolidBrush(EntityShip.MainColor); + //палуба + g.FillRectangle(mainBrush, _startPosX + 30, _startPosY, 60, 10); + g.DrawRectangle(pen, _startPosX + 30, _startPosY, 60, 10); + //корпус + g.FillPolygon(mainBrush, new Point[] + { + new Point(_startPosX, _startPosY + 10), + new Point(_startPosX + 100, _startPosY + 10), + new Point(_startPosX + 90, _startPosY + 30), + new Point(_startPosX + 20, _startPosY + 30), + new Point(_startPosX, _startPosY + 10), + } + ); + g.DrawPolygon(pen, new Point[] + { + new Point(_startPosX, _startPosY + 10), + new Point(_startPosX + 100, _startPosY + 10), + new Point(_startPosX + 90, _startPosY + 30), + new Point(_startPosX + 20, _startPosY + 30), + new Point(_startPosX, _startPosY + 10), + } + ); + //якорь + g.DrawLine(pen, _startPosX + 25, _startPosY + 15, _startPosX + 25, _startPosY + 25); + g.DrawLine(pen, _startPosX + 20, _startPosY + 20, _startPosX + 30, _startPosY + 20); + g.DrawLine(pen, _startPosX + 23, _startPosY + 25, _startPosX + 27, _startPosY + 25); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs index f76c3e2..4aa66ef 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs @@ -3,131 +3,46 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectWarmlyShip.Entities; -namespace ProjectWarmlyShip +namespace ProjectWarmlyShip.DrawingObjects { - public class DrawingWarmlyShip + public class DrawingWarmlyShip : DrawingShip { - public EntityWarmlyShip? EntityWarmlyShip { get; private set; } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private readonly int _shipWidth = 100; - private readonly int _shipHeight = 70; - public bool Init(int speed, double weight, Color mainColor, Color optionalColor, - bool pipes, bool fuelCompartment, int width, int height) + public DrawingWarmlyShip(int speed, double weight, + Color mainColor, Color optionalColor, bool pipes, + bool fuelCompartment, int width, int height) : + base(speed, weight, mainColor, width, height, 100, 60) { - /// - /// проверка, что размеры формы больше или равны размерам рисуемого объекта - /// - if (width < _shipWidth || height < _shipHeight) + if (EntityShip != null) { - return false; - } - _pictureWidth = width; - _pictureHeight = height; - EntityWarmlyShip = new EntityWarmlyShip(); - EntityWarmlyShip.Init(speed, weight, mainColor, optionalColor, pipes, fuelCompartment); - return true; - } - public void SetPosition(int x, int y) - { - /// - /// Проверка, что x и y не выходят за пределы формы - /// - if (x < 0 || x + _shipWidth > _pictureWidth) - { - x = 20; - } - if (y < 0 || y + _shipHeight > _pictureHeight) - { - y = 20; - } - _startPosX = x; - _startPosY = y; - } - public void MoveTransport(DirectionType direction) - { - if (EntityWarmlyShip == null) - { - return; - } - switch (direction) - { - case DirectionType.Left: - if (_startPosX - EntityWarmlyShip.Step > 0) - { - _startPosX -= (int)EntityWarmlyShip.Step; - } - break; - case DirectionType.Up: - if (_startPosY - EntityWarmlyShip.Step > 0) - { - _startPosY -= (int)EntityWarmlyShip.Step; - } - break; - case DirectionType.Right: - if (_startPosX + EntityWarmlyShip.Step + _shipWidth < _pictureWidth) - { - _startPosX += (int)EntityWarmlyShip.Step; - } - break; - case DirectionType.Down: - if (_startPosY + EntityWarmlyShip.Step + _shipHeight < _pictureHeight) - { - _startPosY += (int)EntityWarmlyShip.Step; - } - break; + EntityShip = new EntityWarmlyShip(speed, weight, mainColor, + optionalColor, pipes, fuelCompartment); } } - public void DrawTransport(Graphics g) + public override void DrawTrasport(Graphics g) { - if (EntityWarmlyShip == null) + if (EntityShip is not EntityWarmlyShip warmlyShip) { return; } Pen pen = new(Color.Black); - Brush optionalBrush = new SolidBrush(EntityWarmlyShip.OptionalColor); - if (EntityWarmlyShip.Pipes) + Brush optionalBrush = new SolidBrush(warmlyShip.OptionalColor); + if (warmlyShip.Pipes) { g.FillRectangle(optionalBrush, _startPosX + 70, _startPosY, 10, 30); g.FillRectangle(optionalBrush, _startPosX + 50, _startPosY + 10, 10, 20); g.DrawRectangle(pen, _startPosX + 50, _startPosY + 10, 10, 20); g.DrawRectangle(pen, _startPosX + 70, _startPosY, 10, 30); } - if (EntityWarmlyShip.FuelCompartment) + if (warmlyShip.FuelCompartment) { g.FillRectangle(optionalBrush, _startPosX + 10, _startPosY + 30, 10, 10); g.DrawRectangle(pen, _startPosX + 10, _startPosY + 30, 10, 10); } - Brush mainBrush = new SolidBrush(EntityWarmlyShip.MainColor); - //палуба - g.FillRectangle(mainBrush, _startPosX + 30, _startPosY + 30, 60, 10); - g.DrawRectangle(pen, _startPosX + 30, _startPosY + 30, 60, 10); - //корпус - g.FillPolygon(mainBrush, new Point[] - { - new Point(_startPosX, _startPosY + 40), - new Point(_startPosX + 100, _startPosY + 40), - new Point(_startPosX + 90, _startPosY + 60), - new Point(_startPosX + 20, _startPosY + 60), - new Point(_startPosX, _startPosY + 40), - } - ); - g.DrawPolygon(pen, new Point[] - { - new Point(_startPosX, _startPosY + 40), - new Point(_startPosX + 100, _startPosY + 40), - new Point(_startPosX + 90, _startPosY + 60), - new Point(_startPosX + 20, _startPosY + 60), - new Point(_startPosX, _startPosY + 40), - } - ); - //якорь - g.DrawLine(pen, _startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55); - g.DrawLine(pen, _startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50); - g.DrawLine(pen, _startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55); + _startPosY += 30; + base.DrawTrasport(g); + _startPosY -= 30; } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/EntityShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/EntityShip.cs new file mode 100644 index 0000000..b55c0db --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/EntityShip.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Entities +{ + public class EntityShip + { + public int Speed { get; private set; } + public double Weight { get; private set; } + public Color MainColor { get; private set; } + public double Step => (double)Speed * 100 / Weight; + public EntityShip(int speed, double weight, Color mainColor) + { + Speed = speed; + Weight = weight; + MainColor = mainColor; + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs index 179eccc..4ff179b 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs @@ -4,22 +4,17 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectWarmlyShip +namespace ProjectWarmlyShip.Entities { - public class EntityWarmlyShip + public class EntityWarmlyShip : EntityShip { - public int Speed { get; private set; } - public double Weight { get; private set; } - public Color MainColor { get; private set; } public Color OptionalColor { get; private set; } public bool Pipes { get; private set; } public bool FuelCompartment { get; private set; } - public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, double weight, Color mainColor, Color optionalColor, bool pipes, bool fuelCompartment) + public EntityWarmlyShip(int speed, double weight, + Color mainColor, Color optionalColor, + bool pipes, bool fuelCompartment) : base(speed, weight, mainColor) { - Speed = speed; - Weight = weight; - MainColor = mainColor; OptionalColor = optionalColor; Pipes = pipes; FuelCompartment = fuelCompartment; diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs index a702839..8ed3ab7 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs @@ -34,6 +34,9 @@ buttonDown = new Button(); buttonLeft = new Button(); buttonRight = new Button(); + button1 = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); SuspendLayout(); // @@ -54,9 +57,9 @@ buttonCreate.Name = "buttonCreate"; buttonCreate.Size = new Size(75, 23); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Create"; + buttonCreate.Text = "Create Ship"; buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += ButtonCreateWarmlyShip; + buttonCreate.Click += ButtonCreateShip_Click; // // buttonUp // @@ -106,11 +109,46 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // button1 + // + button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + button1.Location = new Point(93, 426); + button1.Name = "button1"; + button1.Size = new Size(120, 23); + button1.TabIndex = 6; + button1.Text = "Create Warmly Ship"; + button1.UseVisualStyleBackColor = true; + button1.Click += ButtonCreateWarmlyShip_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "Move to center", "Move to border" }); + comboBoxStrategy.Location = new Point(751, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(797, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Step"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += ButtonStep_Click; + // // WarmlyShipForm // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(button1); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonDown); @@ -133,5 +171,8 @@ private Button buttonDown; private Button buttonLeft; private Button buttonRight; + private Button button1; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs index 7cec8df..c9d8c8c 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs @@ -1,43 +1,57 @@ +using ProjectWarmlyShip.DrawingObjects; +using ProjectWarmlyShip.MovementStrategy; + namespace ProjectWarmlyShip { public partial class WarmlyShipForm : Form { - private DrawingWarmlyShip? _drawingWarmlyShip; + private DrawingShip? _drawingShip; + private AbstractStrategy? _abstractStrategy; public WarmlyShipForm() { InitializeComponent(); } private void Draw() { - if (_drawingWarmlyShip == null) + if (_drawingShip == null) { return; } - Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + Bitmap bmp = new(pictureBoxWarmlyShip.Width, + pictureBoxWarmlyShip.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingWarmlyShip.DrawTransport(gr); + _drawingShip.DrawTrasport(gr); pictureBoxWarmlyShip.Image = bmp; } - private void ButtonCreateWarmlyShip(object sender, EventArgs e) + private void ButtonCreateWarmlyShip_Click(object sender, EventArgs e) { - Random rnd = new Random(); - _drawingWarmlyShip = new DrawingWarmlyShip(); - _drawingWarmlyShip.Init( - rnd.Next(100, 300), - rnd.Next(1000, 3000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Convert.ToBoolean(rnd.Next(0, 2)), - Convert.ToBoolean(rnd.Next(0, 2)), + Random random = new Random(); + _drawingShip = new DrawingWarmlyShip( + 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)), pictureBoxWarmlyShip.Width, - pictureBoxWarmlyShip.Height - ); - _drawingWarmlyShip.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); + pictureBoxWarmlyShip.Height); + _drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void ButtonCreateShip_Click(object sender, EventArgs e) + { + Random random = new Random(); + _drawingShip = new DrawingShip( + random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + pictureBoxWarmlyShip.Width, + pictureBoxWarmlyShip.Height); + _drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void buttonMove_Click(object sender, EventArgs e) { - if (_drawingWarmlyShip == null) + if (_drawingShip == null) { return; } @@ -45,19 +59,56 @@ namespace ProjectWarmlyShip switch (name) { case "buttonUp": - _drawingWarmlyShip.MoveTransport(DirectionType.Up); + _drawingShip.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawingWarmlyShip.MoveTransport(DirectionType.Down); + _drawingShip.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawingWarmlyShip.MoveTransport(DirectionType.Left); + _drawingShip.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawingWarmlyShip.MoveTransport(DirectionType.Right); + _drawingShip.MoveTransport(DirectionType.Right); break; } Draw(); } + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawingShip == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData( + new DrawingObjectShip(_drawingShip), + pictureBoxWarmlyShip.Width, + pictureBoxWarmlyShip.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/IMoveableObject.cs b/ProjectWarmlyShip/ProjectWarmlyShip/IMoveableObject.cs new file mode 100644 index 0000000..07e3d8c --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/IMoveableObject.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectWarmlyShip.DrawingObjects; + +namespace ProjectWarmlyShip.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParametrs? GetObjectPosition { get; } + int GetStep { get; } + bool CheckCanMove(DirectionType direction); + void MoveObject(DirectionType direction); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/MoveToBorder.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MoveToBorder.cs new file mode 100644 index 0000000..8136c0a --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MoveToBorder.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.MovementStrategy +{ + internal class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestination() + { + var objParams = GetObjectParametrs; + 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 = GetObjectParametrs; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/MoveToCenter.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MoveToCenter.cs new file mode 100644 index 0000000..6d27973 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestination() + { + var objParams = GetObjectParametrs; + if (objParams == null) + { + return false; + } + return + Math.Abs(objParams.ObjectMiddleHorizontal - FieldWidth / 2) <= GetStep() + && + Math.Abs(objParams.ObjectMiddleVertical - FieldHeight / 2) <= GetStep(); + } + protected override void MoveToTarget() + { + var objParams = GetObjectParametrs; + 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(); + } + } + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ObjectParametrs.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ObjectParametrs.cs new file mode 100644 index 0000000..de0e185 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ObjectParametrs.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.MovementStrategy +{ + public class ObjectParametrs + { + private readonly int _x; + private readonly int _y; + private readonly int _width; + private readonly int _height; + public int LeftBorder => _x; + public int TopBorder => _y; + public int RightBorder => _x + _width; + public int DownBorder => _y + _height; + public int ObjectMiddleHorizontal => _x + _width / 2; + public int ObjectMiddleVertical => _y + _height / 2; + public ObjectParametrs(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Status.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Status.cs new file mode 100644 index 0000000..2a41a38 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}