PIbd-21_Kouvshinoff_T._A._W.../laba1Loco/MoveToCenter.java

49 lines
1.3 KiB
Java
Raw Normal View History

2023-10-08 01:10:54 +04:00
package laba1Loco;
public class MoveToCenter extends AbstractStrategy {
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return
Math.abs(objParams.ObjectMiddleHorizontal() - FieldWidth / 2) <= GetStep()
&&
Math.abs(objParams.ObjectMiddleVertical() - FieldHeight / 2) <= GetStep();
}
protected 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();
}
}
}
}