Реализована вторая стратегия перемещения

This commit is contained in:
Никита Потапов 2023-09-26 00:42:01 +04:00
parent 18e8449fdf
commit c0a5f08964
3 changed files with 57 additions and 1 deletions

View File

@ -124,7 +124,7 @@
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter" });
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToLeftUpCorner" });
comboBoxStrategy.Location = new Point(719, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);

View File

@ -115,6 +115,7 @@ namespace ProjectStormtrooper
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToLeftUpCorner(),
_ => null,
};
if (_abstractStrategy == null)

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectStormtrooper
{
public class MoveToLeftUpCorner : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal <= (objParams.RightBorder - objParams.LeftBorder) / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= (objParams.RightBorder - objParams.LeftBorder) / 2 &&
objParams.ObjectMiddleVertical <= (objParams.DownBorder - objParams.TopBorder) / 2 && objParams.ObjectMiddleVertical + GetStep() >= (objParams.DownBorder - objParams.TopBorder) / 2;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal - (objParams.RightBorder - objParams.LeftBorder) / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - (objParams.DownBorder - objParams.TopBorder) / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}