Новые методы в DrawningCar и реализация интерфейса IMoveableObject

This commit is contained in:
Галина Федоренко 2023-10-08 21:22:30 +04:00
parent 196eb60f0c
commit 1da62721f7
2 changed files with 85 additions and 7 deletions

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hydroplane.DrawningObjects;
namespace Hydroplane.MovementStrategy
{
/// <summary>
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter)
/// </summary>
public class DrawningObjectCar : IMoveableObject
{
private readonly DrawningPlane? _drawningPlane = null;
public DrawningObjectCar(DrawningPlane drawningCar)
{
_drawningPlane = drawningCar;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningPlane == null || _drawningPlane.EntityPlane ==
null)
{
return null;
}
return new ObjectParameters(_drawningPlane.GetPosX,
_drawningPlane.GetPosY, _drawningPlane.GetWidth, _drawningPlane.GetHeight);
}
}
public int GetStep => (int)(_drawningPlane?.EntityPlane?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningPlane?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningPlane?.MoveTransport(direction);
}
}

View File

@ -92,42 +92,80 @@ namespace Hydroplane
_startPosY = Math.Min(y, _pictureHeight - _planeHeight);
}
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// /// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _pictureWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _pictureHeight;
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(DirectionType direction)
{
if (EntityPlane == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityPlane.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityPlane.Step > 0,
//вправо
DirectionType.Right => _startPosX + EntityPlane.Step < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityPlane.Step < _pictureHeight,
_ => false,
};
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (EntityPlane == null)
if (!CanMove(direction) || EntityPlane == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityPlane.Step > 0)
{
_startPosX -= (int)EntityPlane.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityPlane.Step > 0)
{
_startPosY -= (int)EntityPlane.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + EntityPlane.Step + _planeWidth < _pictureWidth)
if (_startPosX + EntityPlane.Step + _pictureWidth < _pictureWidth)
{
_startPosX += (int)EntityPlane.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + EntityPlane.Step + _planeHeight < _pictureHeight)
if (_startPosY + EntityPlane.Step + _pictureHeight < _pictureHeight)
{
_startPosY += (int)EntityPlane.Step;
}