Переход на конструкторы

This commit is contained in:
A.Novopoltsev 2022-11-21 20:08:48 +03:00
parent 81d48f5a86
commit 08b6b36d26
2 changed files with 29 additions and 11 deletions

View File

@ -7,20 +7,20 @@ using System.Threading.Tasks;
namespace Warship namespace Warship
{ {
internal class DrawningShip public class DrawningShip
{ {
/// <summary> /// <summary>
/// Класс-сущность /// Класс-сущность
/// </summary> /// </summary>
public EntityShip Ship { private set; get; } public EntityShip Ship { protected set; get; }
/// <summary> /// <summary>
/// Левая координата отрисовки лодки /// Левая координата отрисовки лодки
/// </summary> /// </summary>
private float _startPosX; protected float _startPosX;
/// <summary> /// <summary>
/// Верхняя кооридната отрисовки лодки /// Верхняя кооридната отрисовки лодки
/// </summary> /// </summary>
private float _startPosY; protected float _startPosY;
/// <summary> /// <summary>
/// Ширина окна отрисовки /// Ширина окна отрисовки
/// </summary> /// </summary>
@ -32,7 +32,7 @@ namespace Warship
/// <summary> /// <summary>
/// Ширина отрисовки лодки /// Ширина отрисовки лодки
/// </summary> /// </summary>
private readonly int _shipWidth = 120; private readonly int _shipWidth = 125;
/// <summary> /// <summary>
/// Высота отрисовки лодки /// Высота отрисовки лодки
/// </summary> /// </summary>
@ -43,10 +43,14 @@ namespace Warship
/// <param name="speed">Скорость</param> /// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param> /// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет кузова</param> /// <param name="bodyColor">Цвет кузова</param>
public void Init(int speed, float weight, Color bodyColor) public DrawningShip(int speed, float weight, Color bodyColor)
{ {
Ship = new EntityShip(); Ship = new EntityShip(speed, weight, bodyColor);
Ship.Init(speed, weight, bodyColor); }
protected DrawningShip(int speed, float weight, Color bodyColor, int shipWight, int shipHeight) : this(speed, weight, bodyColor)
{
_shipWidth = shipWight;
_shipHeight = shipHeight;
} }
/// <summary> /// <summary>
/// Установка позиции корабля /// Установка позиции корабля
@ -111,8 +115,17 @@ namespace Warship
break; break;
} }
} }
public bool DrawCheck()
{
if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return false;
}
return true;
}
/// <param name="g"></param> /// <param name="g"></param>
public void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
if (_startPosX < 0 || _startPosY < 0 if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue) || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -165,5 +178,10 @@ namespace Warship
_startPosY = _pictureHeight.Value - _shipHeight; _startPosY = _pictureHeight.Value - _shipHeight;
} }
} }
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _shipWidth, _startPosY + _shipHeight);
}
} }
} }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Warship namespace Warship
{ {
internal class EntityShip public class EntityShip
{ {
/// <summary> /// <summary>
@ -32,7 +32,7 @@ namespace Warship
/// <param name="weight"></param> /// <param name="weight"></param>
/// <param name="bodyColor"></param> /// <param name="bodyColor"></param>
/// <returns></returns> /// <returns></returns>
public void Init(int speed, float weight, Color bodyColor) public EntityShip(int speed, float weight, Color bodyColor)
{ {
Random rnd = new(); Random rnd = new();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed; Speed = speed <= 0 ? rnd.Next(50, 150) : speed;