до формы

This commit is contained in:
Efi 2023-03-18 09:35:32 +04:00
parent cc11f52447
commit 823459e86a
8 changed files with 376 additions and 9 deletions

View File

@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal abstract class AbstractMap
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected readonly Random _random = new();
protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
(float leftX, float topY, float rightX, float bottomY) = _drawingObject.GetCurrentPosition();
float locomotiveWidth = rightX - leftX;
float locomotiveHeight = bottomY - topY;
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{
switch (direction)
{
case Direction.Up:
if (_size_y * (j + 1) >= topY - _drawingObject.Step && _size_y * (j + 1) < topY && _size_x * (i + 1) > leftX
&& _size_x * (i + 1) <= rightX)
{
return DrawMapWithObject();
}
break;
case Direction.Down:
if (_size_y * j <= bottomY + _drawingObject.Step && _size_y * j > bottomY && _size_x * (i + 1) > leftX
&& _size_x * (i + 1) <= rightX)
{
return DrawMapWithObject();
}
break;
case Direction.Left:
if (_size_x * (i + 1) >= leftX - _drawingObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
case Direction.Right:
if (_size_x * i <= rightX + _drawingObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
}
}
}
}
if (true)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
(float leftX, float topY, float rightX, float bottomY) = _drawingObject.GetCurrentPosition();
if (_drawingObject == null || _map == null)
{
return false;
}
float locomotiveWidth = rightX - leftX;
float locomotiveHeight = bottomY - topY;
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _barrier)
{
if (x + locomotiveWidth >= _size_x * i && x <= _size_x * i && y + locomotiveHeight > _size_y * j && y <= _size_y * j)
{
return false;
}
}
}
}
_drawingObject.SetObject(x, y, _width, _height);
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawingObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawingObject.DrawingObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}

View File

@ -8,7 +8,7 @@ namespace Monorail
{ {
internal class DrawingLocomotive internal class DrawingLocomotive
{ {
public EntityLocomotive Locomotive { get; private set; } public EntityLocomotive Locomotive { get; protected set; }
private float _startPosX; private float _startPosX;
private float _startPosY; private float _startPosY;
private int? _pictureWidth = null; private int? _pictureWidth = null;
@ -16,12 +16,6 @@ namespace Monorail
private readonly int _locomotiveWidth = 80; private readonly int _locomotiveWidth = 80;
private readonly int _locomotiveHeight = 50; private readonly int _locomotiveHeight = 50;
public void Init(int speed, float weight, Color bodyColor)
{
Locomotive = new EntityLocomotive();
Locomotive.Init(speed, weight, bodyColor);
}
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
//Сделать проверки (все параметры больше 0 и координаты не выходят за границы полей) //Сделать проверки (все параметры больше 0 и координаты не выходят за границы полей)
@ -99,7 +93,19 @@ namespace Monorail
} }
public void DrawTransport(Graphics g) public DrawingLocomotive(int speed, float weight, Color bodyColor)
{
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
}
protected DrawingLocomotive(int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) :
this(speed, weight, bodyColor)
{
_locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight;
}
public virtual void DrawTransport(Graphics g)
{ {
if (_startPosX < 0 || _startPosY < 0 if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue) || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -175,5 +181,10 @@ namespace Monorail
} }
} }
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _locomotiveWidth, _startPosY + _locomotiveHeight);
}
} }
} }

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal class DrawingMonorailLocomotive : DrawingLocomotive
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="monorail">Признак наличия магнитного рельса</param>
/// <param name="dopCabin">Признак наличия второй кабины сзади</param>
public DrawingMonorailLocomotive(int speed, float weight, Color bodyColor,
Color dopColor, bool monorail, bool dopCabin) :
base(speed, weight, bodyColor, 110,60)
{
Locomotive = new EntityMonorailLocomotive(speed, weight, bodyColor, dopColor, monorail, dopCabin);
}
public override void DrawTransport(Graphics g)
{
if (Locomotive is not EntityMonorailLocomotive monorailLocomotive)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(monorailLocomotive.DopColor);
base.DrawTransport(g);
if (monorailLocomotive.DopCabin)
{
// задняя кабина
}
if (monorailLocomotive.Monorail)
{
//монорельса
}
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal class DrawingObjectLocomotive : IDrawingObject
{
private DrawingLocomotive _locomotive = null;
public DrawingObjectLocomotive(DrawingLocomotive locomotive)
{
_locomotive = locomotive;
}
public float Step => _locomotive?.Locomotive?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom)
GetCurrentPosition()
{
return _locomotive?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_locomotive?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_locomotive.SetPosition(x, y, width, height);
}
void IDrawingObject.DrawingObject(Graphics g)
{
_locomotive.DrawTransport(g);
}
}
}

View File

@ -12,12 +12,14 @@ namespace Monorail
public float Weight { get; private set; } public float Weight { get; private set; }
public Color BodyColor { get; private set; } public Color BodyColor { get; private set; }
public float Step => Speed * 100 / Weight; public float Step => Speed * 100 / Weight;
public void Init(int speed, float weight, Color bodyColor) public EntityLocomotive(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;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight; Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor; BodyColor = bodyColor;
} }
} }
} }

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal class EntityMonorailLocomotive : EntityLocomotive
{
public Color DopColor { get; private set; }
public bool Monorail { get; private set; }
public bool DopCabin { get; private set; }
public EntityMonorailLocomotive(int speed, float weight, Color bodyColor,
Color dopColor, bool monorail, bool dopCabin) :
base(speed,weight,bodyColor)
{
DopColor = dopColor;
Monorail = monorail;
DopCabin = dopCabin;
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal interface IDrawingObject
{
/// <summary>
/// Шаг перемещения объекта
/// </summary>
public float Step { get; }
/// <summary>
/// Установка позиции объекта
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина полотна</param>
/// <param name="height">Высота полотна</param>
void SetObject(int x, int y, int width, int height);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
/// <returns></returns>
void MoveObject(Direction direction);
/// <summary>
/// Отрисовка объекта
/// </summary>
/// <param name="g"></param>
void DrawingObject(Graphics g);
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal class SimpleMap : AbstractMap
{
private readonly Brush barrierColor = new SolidBrush(Color.Black);
private readonly Brush roadColor = new SolidBrush(Color.Gray);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}