Added movement strategy through abstract class
This commit is contained in:
parent
c4dc78fc09
commit
0b77e3af08
@ -2,6 +2,7 @@
|
||||
|
||||
public enum DirectionType
|
||||
{
|
||||
None = -1,
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
Left = 3,
|
||||
|
@ -18,6 +18,11 @@ public class DrawingBaseLiner
|
||||
|
||||
private readonly int _drawingLinerHeight = 40;
|
||||
|
||||
public int? GetPosX => _startPosX;
|
||||
public int? GetPosY => _startPosY;
|
||||
public int GetWidth => _drawingLinerWidth;
|
||||
public int GetHeight => _drawingLinerHeight;
|
||||
|
||||
private DrawingBaseLiner()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
|
26
ProjectLiner/ProjectLiner/FormLiner.Designer.cs
generated
26
ProjectLiner/ProjectLiner/FormLiner.Designer.cs
generated
@ -36,6 +36,8 @@
|
||||
buttonMoveDown = new Button();
|
||||
buttonMoveRight = new Button();
|
||||
buttonCreateBaseLiner = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -125,11 +127,33 @@
|
||||
buttonCreateBaseLiner.UseVisualStyleBackColor = true;
|
||||
buttonCreateBaseLiner.Click += buttonCreateBaseLiner_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "To Center", "To Border" });
|
||||
comboBoxStrategy.Location = new Point(862, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(121, 23);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Location = new Point(908, 41);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(75, 23);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "Step";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += buttonStrategyStep_Click;
|
||||
//
|
||||
// FormLiner
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(995, 620);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateBaseLiner);
|
||||
Controls.Add(buttonMoveRight);
|
||||
Controls.Add(buttonMoveDown);
|
||||
@ -154,5 +178,7 @@
|
||||
private Button buttonMoveDown;
|
||||
private Button buttonMoveRight;
|
||||
private Button buttonCreateBaseLiner;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,17 +1,23 @@
|
||||
using ProjectLiner.Drawnings;
|
||||
using ProjectLiner.Entities;
|
||||
using ProjectLiner.MovementStrategy;
|
||||
|
||||
namespace ProjectLiner
|
||||
{
|
||||
public partial class FormLiner : Form
|
||||
{
|
||||
public DrawingBaseLiner? _drawingLiner;
|
||||
private DrawingBaseLiner? _drawingLiner;
|
||||
|
||||
private AbstractStrategy? _strategy;
|
||||
|
||||
public FormLiner()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
|
||||
private void DrawTransoprt()
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawingLiner == null)
|
||||
{
|
||||
@ -45,14 +51,16 @@ namespace ProjectLiner
|
||||
_drawingLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||
_drawingLiner.SetPosition(random.Next(0, pictureBoxLiner.Width), random.Next(0, pictureBoxLiner.Height));
|
||||
|
||||
DrawTransoprt();
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateLiner_Click(object sender, EventArgs e)
|
||||
{
|
||||
CreateObject(nameof(DrawingLiner));
|
||||
}
|
||||
|
||||
|
||||
private void buttonCreateBaseLiner_Click(object sender, EventArgs e)
|
||||
{
|
||||
CreateObject(nameof(DrawingBaseLiner));
|
||||
@ -85,10 +93,43 @@ namespace ProjectLiner
|
||||
|
||||
if (result)
|
||||
{
|
||||
DrawTransoprt();
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingLiner == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableLiner(_drawingLiner),
|
||||
pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
comboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finished)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,60 @@
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
private IMoveableObject? _moveableObject;
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
protected int FieldWidth { get; private set; }
|
||||
protected int FieldHeight { get; private set; }
|
||||
public StrategyStatus GetStatus() => _state;
|
||||
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = StrategyStatus.Finished;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
int GetStep { get; }
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
39
ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs
Normal file
39
ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,39 @@
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return objParams.RightBorder + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (objParams.RightBorder + GetStep() < FieldWidth &&
|
||||
objParams.DownBorder + GetStep() < FieldHeight)
|
||||
{
|
||||
MoveRight();
|
||||
MoveDown();
|
||||
} else if (objParams.RightBorder + GetStep() < FieldWidth)
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else if (objParams.DownBorder + GetStep() < FieldHeight)
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
50
ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs
Normal file
50
ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,50 @@
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
internal class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
47
ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs
Normal file
47
ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using ProjectLiner.Drawnings;
|
||||
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
public class MoveableLiner : IMoveableObject
|
||||
{
|
||||
private readonly DrawingBaseLiner? _liner = null;
|
||||
|
||||
public MoveableLiner(DrawingBaseLiner liner)
|
||||
{
|
||||
_liner = liner;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_liner == null || _liner.BaseLiner == null ||
|
||||
!_liner.GetPosX.HasValue || !_liner.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_liner.GetPosX.Value, _liner.GetPosY.Value, _liner.GetWidth, _liner.GetHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetStep => (int)(_liner?.BaseLiner?.Step ?? 0);
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_liner == null || _liner.BaseLiner == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _liner.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
|
||||
private static DirectionType GetDirectionType(MovementDirection direction)
|
||||
{
|
||||
return direction switch
|
||||
{
|
||||
MovementDirection.Left => DirectionType.Left,
|
||||
MovementDirection.Right => DirectionType.Right,
|
||||
MovementDirection.Up => DirectionType.Up,
|
||||
MovementDirection.Down => DirectionType.Down,
|
||||
_ => DirectionType.None,
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
public enum MovementDirection
|
||||
{
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
Left = 3,
|
||||
Right = 4,
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
public class ObjectParameters
|
||||
{
|
||||
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 ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace ProjectLiner.MovementStrategy;
|
||||
|
||||
public enum StrategyStatus
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finished,
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user