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

This commit is contained in:
Никита Потапов 2023-09-26 12:19:42 +04:00
parent c0a5f08964
commit e789954950
4 changed files with 40 additions and 57 deletions

View File

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

View File

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

View File

@ -1,55 +0,0 @@
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();
}
}
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectStormtrooper
{
public class MoveToRightBottom : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep();
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
if (objParams.RightBorder < FieldWidth - GetStep())
{
MoveRight();
}
if (objParams.TopBorder < FieldHeight - GetStep())
{
MoveDown();
}
}
}
}