process before form

This commit is contained in:
ValAnn 2023-10-07 15:14:04 +04:00
parent 7b827b2468
commit 1f834e3abf
10 changed files with 540 additions and 117 deletions

View File

@ -0,0 +1,135 @@
using DumpTruckr.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace DumpTruck.MovementStrategy
{
public abstract class AbstractStrategy
{
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
public void SetData(IMoveableObject moveableObject, int width, int
height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveLeft() => MoveTo(DirectionType.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveRight() => MoveTo(DirectionType.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveUp() => MoveTo(DirectionType.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
protected bool MoveDown() => MoveTo(DirectionType.Down);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -3,113 +3,43 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DumpTruck.Entities;
namespace DumpTruck
namespace DumpTruck.DrawningObjects
{
public class DrawningDumpTruck
{
public DumpTruck EntityDumpTruck { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _carWidth = 110;
private readonly int _carHeight = 60;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth > _carWidth && _pictureHeight > _carHeight)
public class DrawningDumpTruck : DrawningCar
{
EntityDumpTruck = new DumpTruck();
EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, bodyKit, tent);
return true;
}
return false;
}
public void SetPosition(int x, int y)
public DrawningDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool tent, int width, int height):
base(speed, weight, bodyColor, width, height, 110, 60)
{
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
if (EntityCar != null)
{
_startPosX = 0;
_startPosY = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntityDumpTruck == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityDumpTruck.Step > 0)
{
_startPosX -= (int)EntityDumpTruck.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityDumpTruck.Step > 0)
{
_startPosY -= (int)EntityDumpTruck.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + EntityDumpTruck.Step + _carWidth < _pictureWidth)
{
_startPosX += (int)EntityDumpTruck.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + EntityDumpTruck.Step + _carHeight < _pictureHeight)
{
_startPosY += (int)EntityDumpTruck.Step;
}
break;
EntityCar = new EntityDumpTruck(speed, weight, bodyColor,
additionalColor, bodyKit, tent);
}
}
public void DrawTransport(Graphics g)
public override void DrawTransport(Graphics g)
{
if (EntityDumpTruck == null)
if (EntityCar is not EntityDumpTruck dumpTruck)
{
return;
}
Pen pen = new Pen(Color.Black);
Brush brush = new SolidBrush(EntityDumpTruck.BodyColor);
Brush addBrush = new SolidBrush(EntityDumpTruck.AdditionalColor);
Brush addBrush = new SolidBrush(dumpTruck.AdditionalColor);
Brush brush = new SolidBrush(dumpTruck.BodyColor);
//границы автомобиля
g.FillRectangle(brush, _startPosX, _startPosY + 35, 110, 10);
g.FillRectangle(brush, _startPosX + 85, _startPosY, 25, 35);
g.FillEllipse(brush, _startPosX, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 15, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 95, _startPosY + 35 + 10, 15, 15);
if (EntityDumpTruck.Tent)
if (dumpTruck.Tent)
{
Point[] points = new Point[3];
points[0].X = _startPosX; points[0].Y = _startPosY + 35;
@ -118,7 +48,7 @@ namespace DumpTruck
g.FillPolygon(addBrush, points);
}
if (EntityDumpTruck.BodyKit)
if (dumpTruck.BodyKit)
{
Point[] points = new Point[4];
points[0].X = _startPosX; points[0].Y = _startPosY + 35;
@ -128,7 +58,7 @@ namespace DumpTruck
g.FillPolygon(addBrush, points);
}
if (EntityDumpTruck.BodyKit && EntityDumpTruck.Tent)
if (dumpTruck.BodyKit && dumpTruck.Tent)
{
int x = _startPosX;
int y = _startPosY - 8;

View File

@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.Entities;
namespace DumpTruck.DrawningObjects
{
public class DrawningCar
{
public EntityCar? EntityCar { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private readonly int _carWidth = 110;
private readonly int _carHeight = 60;
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _carWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _carHeight;
public DrawningCar(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth > _carWidth && _pictureHeight > _carHeight)
{
EntityCar = new EntityCar(speed, weight, bodyColor);
}
}
protected DrawningCar(int speed, double weight, Color bodyColor, int
width, int height, int carWidth, int carHeight)
{
// TODO: Продумать проверки
_pictureWidth = width;
_pictureHeight = height;
_carWidth = carWidth;
_carHeight = carHeight;
EntityCar = new EntityCar(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
{
_startPosX = 0;
_startPosY = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityCar == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
_startPosX -= (int)EntityCar.Step;
break;
//вверх
case DirectionType.Up:
_startPosY -= (int)EntityCar.Step;
break;
// вправо
case DirectionType.Right:
_startPosX += (int)EntityCar.Step;
break;
//вниз
case DirectionType.Down:
_startPosY += (int)EntityCar.Step;
break;
}
}
public bool CanMove(DirectionType direction)
{
if (EntityCar == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityCar.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityCar.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityCar.Step < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityCar.Step < _pictureHeight,
_ => false,
};
}
public virtual void DrawTransport(Graphics g)
{
if (EntityCar == null)
{
return;
}
Pen pen = new Pen(Color.Black);
Brush brush = new SolidBrush(EntityCar.BodyColor);
//границы автомобиля
g.FillRectangle(brush, _startPosX, _startPosY + 35, 110, 10);
g.FillRectangle(brush, _startPosX + 85, _startPosY, 25, 35);
g.FillEllipse(brush, _startPosX, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 15, _startPosY + 35 + 10, 15, 15);
g.FillEllipse(brush, _startPosX + 95, _startPosY + 35 + 10, 15, 15);
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.DrawningObjects;
using DumpTruckr.MovementStrategy;
namespace DumpTruck.MovementStrategy
{
internal class DrawningObjectCar : IMoveableObject
{
private readonly DrawningCar? _drawningCar = null;
public DrawningObjectCar(DrawningCar drawningCar)
{
_drawningCar = drawningCar;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningCar == null || _drawningCar.EntityCar ==
null)
{
return null;
}
return new ObjectParameters(_drawningCar.GetPosX,
_drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight);
}
}
public int GetStep => (int)(_drawningCar?.EntityCar?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningCar?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningCar?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.Entities
{
public class EntityCar
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight;
/// <summary>
/// Конструктор с параметрами
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityCar(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -7,26 +7,11 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DumpTruck
namespace DumpTruck.Entities
{
public class DumpTruck
public class EntityDumpTruck : EntityCar
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
@ -42,16 +27,10 @@ namespace DumpTruck
/// </summary>
public bool Tent { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color
public EntityDumpTruck(int speed, double weight, Color bodyColor, Color
additionalColor, bool bodyKit, bool tent)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Tent = tent;

View File

@ -0,0 +1,36 @@
using DumpTruck;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.MovementStrategy;
namespace DumpTruckr.MovementStrategy
{
/// <summary>
/// Интерфейс для работы с перемещаемым объектом
/// </summary>
public interface IMoveableObject
{
/// <summary>
/// Получение координаты X объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(DirectionType direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,57 @@
using DumpTruck.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck
{
internal class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
}
protected override 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();
}
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
/// <summary>
/// Параметры-координаты объекта
/// </summary>
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder => _x;
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder => _y;
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -4,9 +4,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck
namespace DumpTruck.MovementStrategy
{
internal class CLAAAAAAASS
public enum Status
{
NotInit,
InProgress,
Finish
}
}