In process : Add Border & Center moving strategy

This commit is contained in:
Inna Pruidze 2024-06-12 16:25:55 +04:00
parent 49eff77c1a
commit 603cf3a903
10 changed files with 283 additions and 81 deletions

View File

@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser;
namespace ProjectCruiser;
public enum DirectionType
{
Unknown = -1,
Up = 1,
Down = 2,
Left = 3,

View File

@ -1,33 +1,39 @@
using System.Drawing.Drawing2D;
namespace ProjectCruiser;
namespace ProjectCruiser;
public class DrawningBase
{
// Класс-сущность
public EntityBase? EntityB { get; private set; }
public EntityBase? EntityTransport { get; protected set; }
private int? _pictureWidth; // Ширина окна
private int? _pictureHeight; // Высота окна
private int? _startPosX; // Левая координата прорисовки автомобиля
private int? _startPosY; // Верхняя кооридната прорисовки автомобиля
protected int? _startPosX; // < protected
protected int? _startPosY; // < protected
private readonly int _drawningWidth = 300; // Ширина прорисовки автомобиля
private readonly int _drawningHeight = 42; // Высота прорисовки автомобиля
// Инициализация свойств
public void Init(int speed, double weight,
Color bodyColor, Color additionalColor, bool pad,
bool hangar, bool deckhouse)
// Инициализация свойств (теперь через конструктор)
public DrawningBase(int speed, double weight,
Color bodyColor)
{
EntityB = new EntityBase();
EntityB.Init(speed, weight, bodyColor,
additionalColor, pad, hangar, deckhouse);
EntityTransport = new EntityBase(speed, weight, bodyColor);
}
private DrawningBase()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
// protected >
protected DrawningBase(int drawningCarWidth, int drawningCarHeight) : this()
{
_drawningWidth = drawningCarWidth;
_pictureHeight = drawningCarHeight;
}
public int getHeight() // для создания объекта в нижнем левом углу (*)
{
return _drawningHeight;
@ -72,7 +78,7 @@ public class DrawningBase
public bool MoveTransport(DirectionType direction)
{
if (EntityB == null || !_startPosX.HasValue ||
if (EntityTransport == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
@ -82,32 +88,32 @@ public class DrawningBase
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityB.Step > 0)
if (_startPosX.Value - EntityTransport.Step > 0)
{
_startPosX -= (int)EntityB.Step;
_startPosX -= (int)EntityTransport.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityB.Step > 0)
if (_startPosY.Value - EntityTransport.Step > 0)
{
_startPosY -= (int)EntityB.Step;
_startPosY -= (int)EntityTransport.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningWidth + EntityB.Step
if (_startPosX.Value + _drawningWidth + EntityTransport.Step
< _pictureWidth.Value)
{
_startPosX += (int)EntityB.Step;
_startPosX += (int)EntityTransport.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningHeight + EntityB.Step
if (_startPosY.Value + _drawningHeight + EntityTransport.Step
< _pictureHeight.Value)
{
_startPosY += (int)EntityB.Step;
_startPosY += (int)EntityTransport.Step;
}
return true;
@ -117,18 +123,16 @@ public class DrawningBase
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g) // < virtual
{
if (EntityB == null || !_startPosX.HasValue ||
if (EntityTransport == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black, 2);
Brush PadBrush = new HatchBrush(HatchStyle.DottedDiamond, Color.LightGray, Color.Black);
Brush additionalBrush = new SolidBrush(EntityB.AdditionalColor);
Brush mainBrush = new SolidBrush(EntityB.MainColor);
Brush mainBrush = new SolidBrush(EntityTransport.MainColor);
//границы cruiser
Point point0 = new Point(_startPosX.Value + 2, _startPosY.Value + 7);
@ -152,38 +156,18 @@ public class DrawningBase
g.DrawPolygon(pen, boarders);
g.FillPolygon(mainBrush, boarders);
// вертолетная площадка
if (EntityB.HelicopterPads)
{
g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20);
g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20);
}
// салон на верхней палубе
if (EntityB.Deckhouse)
{
// random location
int y_h = EntityB.values[1];
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24); // 40, 26
int y_h = EntityTransport.values[1];
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24);
g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12);
g.FillRectangle(additionalBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12);
g.FillRectangle(mainBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12);
g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20);
g.FillRectangle(mainBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20);
}
// ангар
if (EntityB.Hangar)
{
int n = EntityB.values[2];
if (n == 1) g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7);
else
{
g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12);
}
}
}
public int? GetPosX => _startPosX;
public int? GetPosY => _startPosY;
public int GetWidth => _drawningWidth;
public int GetHeight => _drawningHeight;
}

View File

@ -0,0 +1,81 @@
using System.Drawing.Drawing2D;
namespace ProjectCruiser;
internal class DrawningCruiser : DrawningBase
{
private EntityCruiser EntityCruiser;
public DrawningCruiser(int speed, double weight, Color bodyColor,
Color additionalColor, bool pads, bool hangar) : base(300, 42)
// all additional featchures 'inside' object, so size remains
{
EntityCruiser = new EntityCruiser(speed, weight,
bodyColor, additionalColor, pads, hangar);
}
public override void DrawTransport(Graphics g)
{
if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black, 2);
Brush PadBrush = new HatchBrush(HatchStyle.DottedDiamond, Color.LightGray, Color.Black);
Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor);
Brush mainBrush = new SolidBrush(EntityTransport.MainColor);
//границы cruiser
Point point0 = new Point(_startPosX.Value + 2, _startPosY.Value + 7);
Point point1 = new Point(_startPosX.Value + 2, _startPosY.Value + 30);
Point point2 = new Point(_startPosX.Value + 184, _startPosY.Value + 42);
Point point3 = new Point(_startPosX.Value + 260, _startPosY.Value + 34);
Point point4 = new Point(_startPosX.Value + 300, _startPosY.Value + 22);
Point point5 = new Point(_startPosX.Value + 260, _startPosY.Value + 10);
Point point6 = new Point(_startPosX.Value + 184, _startPosY.Value + 2);
Point[] boarders = {
point0,
point1,
point2,
point3,
point4,
point5,
point6
};
g.DrawPolygon(pen, boarders);
g.FillPolygon(mainBrush, boarders);
// салон на верхней палубе
// random location
int y_h = EntityCruiser.values[1];
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24);
g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12);
g.FillRectangle(additionalBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12);
g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20);
// вертолетная площадка
if (EntityCruiser.HelicopterPads)
{
g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20);
g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20);
}
// ангар
if (EntityCruiser.Hangar)
{
int n = EntityTransport.values[2];
if (n == 1) g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7);
else
{
g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12);
}
}
}
}

View File

@ -6,29 +6,19 @@ public class EntityBase
public int Speed { get; private set; } // скорость
public double Weight { get; private set; } // вес
public Color MainColor { get; private set; } // основной цвет
public Color AdditionalColor { get; private set; } // доп. цвет
// признаки (наличия)
public bool HelicopterPads { get; private set; } // вертолетная площадка
public bool Hangar { get; private set; } // ангар
public bool Deckhouse { get; private set; } // салон на верхней палубе
// public bool Deckhouse { get; private set; } // салон на верхней палубе
public double Step => Speed * 100 / Weight;
public int[] values = { 0, 0, 0 };
public void Init(int speed, double weight,
Color mainc, Color addtc, bool pad,
bool hangar, bool deckhouse)
public EntityBase(int speed, double weight,
Color mainc) // (bool) deckhouse -> default TRUE now
{
Random rn = new();
Speed = speed;
Weight = weight;
MainColor = mainc;
AdditionalColor = addtc;
HelicopterPads = pad;
Hangar = hangar;
Deckhouse = deckhouse;
// Deckhouse = deckhouse;
values[0] = rn.Next(1, 4);
values[1] = rn.Next(5, 10);
values[2] = rn.Next(1, 3);

View File

@ -0,0 +1,19 @@
namespace ProjectCruiser;
public class EntityCruiser : EntityBase
{
public Color AdditionalColor { get; private set; } // доп. цвет
// признаки (наличия)
public bool HelicopterPads { get; private set; } // вертолетная площадка
public bool Hangar { get; private set; } // ангар
public EntityCruiser(int speed, double weight, Color mainc,
Color additionalColor, bool pads, bool hangar)
: base(speed, weight, mainc)
{
AdditionalColor = additionalColor;
HelicopterPads = pads;
Hangar = hangar;
}
}

View File

@ -0,0 +1,16 @@
namespace ProjectCruiser;
// Интерфейс для работы с перемещаемым объектом
public interface IMoveableObj
{
// Получение координат объекта
ObjParameters? GetObjectPosition { get; }
// Получение шага объекта
int GetStep { get; }
/// Попытка переместить объект в указанном направлении
/// <param name="direction">Направление</param>
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
bool TryMoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,54 @@
namespace ProjectCruiser;
// Класс-реализация IMoveableObject с использованием DrawningBase
public class MoveableTransport : IMoveableObj
{
// Поле-объект класса Drawning(Transport) или его наследника
private readonly DrawningBase? _car = null;
public MoveableTransport(DrawningBase car)
{
_car = car;
}
public ObjParameters? GetObjectPosition
{
get
{
if (_car == null || _car.EntityTransport == null ||
!_car.GetPosX.HasValue || !_car.GetPosY.HasValue)
{
return null;
}
return new ObjParameters(_car.GetPosX.Value,
_car.GetPosY.Value, _car.GetWidth, _car.GetHeight);
}
}
public int GetStep => (int)(_car?.EntityTransport?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if (_car == null || _car.EntityTransport == null)
{
return false;
}
return _car.MoveTransport(GetDirectionType(direction));
}
/// Конвертация из MovementDirection в DirectionType
/// <param name="direction">MovementDirection</param>
/// <returns>DirectionType</returns>
private static DirectionType GetDirectionType(MovementDirection direction)
{
return direction switch
{
MovementDirection.Left => DirectionType.Left,
MovementDirection.Right => DirectionType.Right,
MovementDirection.Up => DirectionType.Up,
MovementDirection.Down => DirectionType.Down,
_ => DirectionType.Unknown,
};
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser;
// Направление перемещения
public enum MovementDirection
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
}

View File

@ -0,0 +1,29 @@
namespace ProjectCruiser;
// Параметры-координаты объекта
public class ObjParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width; // Ширина объекта
private readonly int _height; // Высота объекта
public int LeftBorder => _x; // Левая граница
public int TopBorder => _y; // Верхняя граница
public int RightBorder => _x + _width; // Правая граница
public int DownBorder => _y + _height; // Нижняя граница
// Вертикальная середина объекта
public int ObjectMiddleHorizontal => _x + _width / 2;
// Горизонтальная середина объекта
public int ObjectMiddleVertical => _y + _height / 2;
public ObjParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser;
// Статус выполнения операции перемещения
public enum StrategyStatus
{
// готово к началу
NotInit,
// Выполняется
InProgress,
// Завершено
Finish
}