Коммит

This commit is contained in:
Stranni15k 2022-11-19 21:07:38 +04:00
parent 72a6c7d036
commit 6db3ae138c
10 changed files with 235 additions and 80 deletions

View File

@ -32,23 +32,85 @@ namespace ElectricLocomotive
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
// TODO проверка, что объект может переместится в требуемом направлении (float leftX, float topY, float rightX, float bottomY) = _drawningObject.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 - _drawningObject.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 + _drawningObject.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 - _drawningObject.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 + _drawningObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
}
}
}
}
if (true) if (true)
{ {
_drawningObject.MoveObject(direction); _drawningObject.MoveObject(direction);
} }
return DrawMapWithObject(); return DrawMapWithObject();
} }
private bool SetObjectOnMap() private bool SetObjectOnMap()
{ {
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
if (_drawningObject == null || _map == null) if (_drawningObject == null || _map == null)
{ {
return false; return false;
} }
float locomotiveWidth = rightX - leftX;
float locomotiveHeight = bottomY - topY;
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _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;
}
}
}
}
_drawningObject.SetObject(x, y, _width, _height); _drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
return true; return true;
} }
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
{
internal class BushesMap : AbstractMap
{
/// <summary>
/// Цвет участка закрытого
/// </summary>
private readonly Pen barrierColor = new Pen(Color.DarkGreen, 3);
/// <summary>
/// Цвет участка открытого
/// </summary>
private readonly Brush roadColor = new SolidBrush(Color.Brown);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1)), Convert.ToInt32(j * (_size_y - 1))), new Point(Convert.ToInt32(i * (_size_x - 1)+7), Convert.ToInt32(j * (_size_y - 1))+7));
g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1)+7), Convert.ToInt32(j * (_size_y - 1))), new Point(Convert.ToInt32(i * (_size_x - 1) + 7), Convert.ToInt32(j * (_size_y - 1)) + 7));
g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1)+7), Convert.ToInt32(j * (_size_y - 1))+7), new Point(Convert.ToInt32(i * (_size_x - 1) + 14), Convert.ToInt32(j * (_size_y - 1)) ));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
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 < 20)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -32,29 +32,20 @@ namespace ElectricLocomotive
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Pen window = new(Color.Blue);
Brush dopBrush = new SolidBrush(sportCar.DopColor); Brush dopBrush = new SolidBrush(sportCar.DopColor);
if (sportCar.BodyKit) if (sportCar.BodyKit)
{ {
g.FillRectangle(dopBrush, _startPosX, _startPosY + 20, 60, 10); Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 15, Convert.ToInt32(_startPosY) - 20), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY) - 30), new Point(Convert.ToInt32(_startPosX) + 75, Convert.ToInt32(_startPosY) - 20), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY) + 10) };
g.DrawRectangle(pen, _startPosX, _startPosY + 20, 60, 10); g.DrawPolygon(window, pts);
g.FillRectangle(dopBrush, _startPosX - 10, _startPosY + 10, 10, 30);
g.DrawRectangle(pen, _startPosX - 10, _startPosY + 10 , 10, 30);
} }
if (sportCar.SportLine) if (sportCar.SportLine)
{ {
g.FillRectangle(dopBrush, _startPosX + 10, _startPosY - 20, 60, 10); Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 150, Convert.ToInt32(_startPosY) + 60)};
g.FillRectangle(dopBrush, _startPosX, _startPosY + 20, 60, 10); g.FillPolygon(Brushes.Black, pts);
g.DrawRectangle(pen, _startPosX + 10, _startPosY - 20, 60, 10);
g.DrawRectangle(pen, _startPosX, _startPosY + 20, 60, 10);
}
if (sportCar.Wing)
{
} }
base.DrawTransport(g); base.DrawTransport(g);

View File

@ -9,68 +9,33 @@ namespace ElectricLocomotive
{ {
internal class DrawningLocomotive internal class DrawningLocomotive
{ {
/// <summary>
/// Класс-сущность
/// </summary>
public EntityLocomotive Locomotive { get; protected set; } public EntityLocomotive Locomotive { get; protected set; }
/// <summary>
/// Левая координата отрисовки автомобиля
/// </summary>
protected float _startPosX; protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки автомобиля
/// </summary>
protected float _startPosY; protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
protected int? _pictureWidth = null; protected int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
protected int? _pictureHeight = null; protected int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки автомобиля
/// </summary>
protected readonly int _locomotiveWidth = 160; protected readonly int _locomotiveWidth = 160;
/// <summary>
/// Высота отрисовки автомобиля
/// </summary>
protected readonly int _locomotiveHeight = 90; protected readonly int _locomotiveHeight = 90;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
public DrawningLocomotive(int speed, float weight, Color bodyColor) public DrawningLocomotive(int speed, float weight, Color bodyColor)
{ {
Locomotive = new EntityLocomotive(speed, weight, bodyColor); Locomotive = new EntityLocomotive(speed, weight, bodyColor);
} }
/// <summary>
/// Установка позиции автомобиля
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
// TODO checks if (width <= _locomotiveWidth + x || height <= _locomotiveHeight + y || x < 0 || y < 0)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
_startPosX = x; _startPosX = x + 20;
_startPosY = y; _startPosY = y + 30;
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
if (_startPosX + _locomotiveWidth > _pictureWidth) { _startPosX = 20; }
if (_startPosY - _locomotiveHeight / 2 < 0) { _startPosY = _locomotiveHeight + 10; }
if (_startPosY + _locomotiveHeight > _pictureHeight) { _startPosY -= _locomotiveHeight; }
} }
/// <summary>
/// Изменение направления пермещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction) public void MoveTransport(Direction direction)
{ {
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
@ -131,24 +96,42 @@ namespace ElectricLocomotive
return; return;
} }
Brush brBody = new SolidBrush(Locomotive?.BodyColor ?? Color.Black); Brush brBody = new SolidBrush(Locomotive?.BodyColor ?? Color.Black);
Brush blackturbo = new SolidBrush(Color.Black);
Pen pen = new Pen(Color.Black); Pen pen = new Pen(Color.Black);
//колёса Pen window = new Pen(Color.Blue);
g.FillEllipse(brBody, _startPosX + 10, _startPosY + 50, 30, 30);
g.FillEllipse(brBody, _startPosX + 50, _startPosY + 50, 30, 30);
g.FillEllipse(brBody, _startPosX + 90, _startPosY + 50, 30, 30);
g.FillEllipse(brBody, _startPosX + 130, _startPosY + 50, 30, 30);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 50, 30, 30); g.FillPolygon(brBody, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60) });
g.DrawEllipse(pen, _startPosX + 50, _startPosY + 50, 30, 30); g.FillRectangle(blackturbo, _startPosX + 5, _startPosY + 15, 5, 40);
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 50, 30, 30);
g.DrawEllipse(pen, _startPosX + 130, _startPosY + 50, 30, 30); g.FillEllipse(brBody, _startPosX + 15, _startPosY + 60, 20, 20);
g.FillEllipse(brBody, _startPosX + 45, _startPosY + 60, 20, 20);
g.FillEllipse(brBody, _startPosX + 85, _startPosY + 60, 20, 20);
g.FillEllipse(brBody, _startPosX + 115, _startPosY + 60, 20, 20);
// Окна
g.DrawRectangle(window, _startPosX + 20, _startPosY + 20, 20, 25);
g.DrawRectangle(window, _startPosX + 50, _startPosY + 20, 20, 25);
// Колёса
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 60, 20, 20);
g.DrawEllipse(pen, _startPosX + 45, _startPosY + 60, 20, 20);
g.DrawEllipse(pen, _startPosX + 85, _startPosY + 60, 20, 20);
g.DrawEllipse(pen, _startPosX + 115, _startPosY + 60, 20, 20);
// Дверь
g.DrawRectangle(pen, _startPosX + 85, _startPosY + 15, 20, 40);
// Локомотив
g.DrawPolygon(pen, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60) });
g.FillRectangle(brBody, _startPosX + 10, _startPosY + 20, 150, 30);
g.FillRectangle(brBody, _startPosX + 10, _startPosY - 20, 40, 40);
g.FillRectangle(brBody, _startPosX + 100, _startPosY - 20, 10, 40);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 20, 150, 30);
g.DrawRectangle(pen, _startPosX + 10, _startPosY - 20, 40, 40);
g.DrawRectangle(pen, _startPosX + 100, _startPosY - 20, 10, 40);
} }
/// <summary> /// <summary>
/// Смена границ формы отрисовки /// Смена границ формы отрисовки

View File

@ -35,6 +35,7 @@ namespace ElectricLocomotive
void IDrawningObject.DrawningObject(Graphics g) void IDrawningObject.DrawningObject(Graphics g)
{ {
// TODO // TODO
_locomotive.DrawTransport(g);
} }
} }
} }

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
{
internal class FieldMap : AbstractMap
{
/// <summary>
/// Цвет участка закрытого
/// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Brown);
/// <summary>
/// Цвет участка открытого
/// </summary>
private readonly Brush roadColor = new SolidBrush(Color.Green);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillEllipse(barrierColor, i * (_size_x-1), j * (_size_y-1), 30, 15);
}
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 < 20)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -164,7 +164,9 @@
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] { this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта"}); "Простая карта",
"Поле с грязью",
"Кусты на карте"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(104, 23); this.comboBoxSelectorMap.Size = new System.Drawing.Size(104, 23);

View File

@ -91,6 +91,13 @@ namespace ElectricLocomotive
case "Простая карта": case "Простая карта":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Поле с грязью":
_abstractMap = new FieldMap();
break;
case "Кусты на карте":
_abstractMap = new BushesMap();
break;
} }
} }

View File

@ -11,7 +11,7 @@ namespace ElectricLocomotive
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormLocomotive()); Application.Run(new FormMap());
} }
} }
} }

View File

@ -19,11 +19,11 @@ namespace ElectricLocomotive
protected override void DrawBarrierPart(Graphics g, int i, int j) protected override void DrawBarrierPart(Graphics g, int i, int j)
{ {
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); 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) protected override void DrawRoadPart(Graphics g, int i, int j)
{ {
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y));
} }
protected override void GenerateMap() protected override void GenerateMap()
{ {