Обновлены методы перемещения в классе отрисовки простого объекта

This commit is contained in:
Никита Потапов 2023-09-25 23:29:14 +04:00
parent b595a788a9
commit b7a3f41598

View File

@ -40,6 +40,22 @@ namespace ProjectStormtrooper
/// </summary>
protected readonly int _planeHeight = 110;
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _planeWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _planeHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
@ -81,6 +97,30 @@ namespace ProjectStormtrooper
EntityPlane = new EntityPlane(speed, weight, bodyColor);
}
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(DirectionType direction)
{
if (EntityPlane == null)
{
return false;
}
return direction switch
{
//вверх
DirectionType.Up => _startPosY - EntityPlane.Step > 0,
//вниз
DirectionType.Down => _startPosY + _planeHeight + EntityPlane.Step < _pictureHeight,
//влево
DirectionType.Left => _startPosX - EntityPlane.Step > 0,
//вправо
DirectionType.Right => _startPosX + _planeWidth + EntityPlane.Step < _pictureWidth,
_ => false,
};
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
@ -113,7 +153,7 @@ namespace ProjectStormtrooper
/// <param name="direction">Направление перемещения</param>
public void MoveTransport(DirectionType direction)
{
if (EntityPlane == null)
if (!CanMove(direction) || EntityPlane == null)
{
return;
}
@ -121,31 +161,19 @@ namespace ProjectStormtrooper
{
// Вверх
case DirectionType.Up:
if (_startPosY - EntityPlane.Step >= 0)
{
_startPosY -= (int)EntityPlane.Step;
}
break;
// Вниз
case DirectionType.Down:
if (_startPosY + _planeHeight + EntityPlane.Step <= _pictureHeight)
{
_startPosY += (int)EntityPlane.Step;
}
break;
// Влево
case DirectionType.Left:
if (_startPosX - EntityPlane.Step >= 0)
{
_startPosX -= (int)EntityPlane.Step;
}
break;
// Вправо
case DirectionType.Right:
if (_startPosX + _planeWidth + EntityPlane.Step <= _pictureWidth)
{
_startPosX += (int)EntityPlane.Step;
}
break;
}
}