Compare commits

...

15 Commits

20 changed files with 1432 additions and 95 deletions

View File

@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal abstract class AbstractMap
{
private IDrawningObject _drawningObject = 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, IDrawningObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
switch (direction)
{
case Direction.Up:
for (int i = 0; i < _map?.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Left < i * _size_x && _drawningObject.GetCurrentPosition().Top > i * _size_x) || (_drawningObject.GetCurrentPosition().Left < i * (_size_x + 1) && _drawningObject.GetCurrentPosition().Top > i * (_size_x + 1))) && j * (_size_y + 1) < _drawningObject.GetCurrentPosition().Right && _drawningObject.GetCurrentPosition().Right - j * (_size_y + 1) <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
case Direction.Down:
for (int i = 0; i < _map?.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Left < i * _size_x && _drawningObject.GetCurrentPosition().Top > i * _size_x) ||
(_drawningObject.GetCurrentPosition().Left < i * (_size_x + 1) && _drawningObject.GetCurrentPosition().Top > i * (_size_x + 1))) &&
j * _size_y > _drawningObject.GetCurrentPosition().Bottom && j * _size_y - _drawningObject.GetCurrentPosition().Bottom <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
case Direction.Left:
for (int i = 0; i < _map?.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Right < j * _size_y && _drawningObject.GetCurrentPosition().Bottom > j * _size_y) ||
(_drawningObject.GetCurrentPosition().Right < j * (_size_y + 1) && _drawningObject.GetCurrentPosition().Bottom > j * (_size_y + 1))) &&
i * (_size_x + 1) < _drawningObject.GetCurrentPosition().Left && _drawningObject.GetCurrentPosition().Left - i * (_size_x + 1) <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
case Direction.Right:
for (int i = 0; i < _map?.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Right < j * _size_y && _drawningObject.GetCurrentPosition().Bottom > j * _size_y) ||
(_drawningObject.GetCurrentPosition().Right < j * (_size_y + 1) && _drawningObject.GetCurrentPosition().Bottom > j * (_size_y + 1))) &&
i * (_size_x) > _drawningObject.GetCurrentPosition().Top && i * (_size_x) - _drawningObject.GetCurrentPosition().Top <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
}
_drawningObject?.MoveObject(direction);
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
if(_drawningObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 150);
int y = _random.Next(0, 150);
_drawningObject.SetObject(x, y, _width, _height);
if (_drawningObject is DrawningObjectLoco)
{
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && _drawningObject.GetCurrentPosition().Left < i * _size_x && _drawningObject.GetCurrentPosition().Right < j * _size_y && _drawningObject.GetCurrentPosition().Top > i * _size_x && _drawningObject.GetCurrentPosition().Bottom > j * _size_y)
return false;
}
}
}
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if(_drawningObject ==null || bmp == 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)
{
DrawRailPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrielPart(gr, i, j);
}
}
}
_drawningObject.DrawningObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRailPart(Graphics g, int i, int j);
protected abstract void DrawBarrielPart(Graphics g, int i, int j);
}
}

View File

@ -6,8 +6,9 @@ using System.Threading.Tasks;
namespace Locomative
{
internal enum Direction
public enum Direction
{
None = 0,
Up = 1,
Down = 2,
Left = 3,

View File

@ -0,0 +1,91 @@
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
namespace Locomative
{
internal class DrawningFastLoco : DrawningLocomative
{
public DrawningFastLoco(int speed, float weight, Color bodyColor, Color dopColor, bool widow, bool room) :
base(speed, weight, bodyColor, 110, 60)
{
Locomative = new EntityFastLocomative(speed, weight, bodyColor, dopColor, widow, room);
}
public override void DrawTransport(Graphics g)
{
if(Locomative is not EntityFastLocomative fastLocomative)
{
return;
}
//кисти
Pen pen = new(Color.Black);
Pen dopPen = new(fastLocomative.dopColor);
Brush brBody = new SolidBrush(fastLocomative.BodyColor);
Brush brWhite = new SolidBrush(Color.White);
Brush brBlack = new SolidBrush(Color.Black);
Brush brBlue = new SolidBrush(Color.Blue);
Brush Aque = new SolidBrush(Color.Aquamarine);
//крыша
Point point1_1 = new Point(Convert.ToInt32(_startPosX + 30), Convert.ToInt32(_startPosY + 5));
Point point1_2 = new Point(Convert.ToInt32(_startPosX + 32), Convert.ToInt32(_startPosY));
Point point1_3 = new Point(Convert.ToInt32(_startPosX + 60), Convert.ToInt32(_startPosY));
Point point1_4 = new Point(Convert.ToInt32(_startPosX + 62), Convert.ToInt32(_startPosY + 5));
Point[] points1 = { point1_1, point1_2, point1_3, point1_4 };
g.FillPolygon(brWhite, points1);
g.DrawPolygon(pen, points1);
//основная часть
Point point2_1 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 5));
Point point2_2 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 5));
Point point2_3 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 35));
Point point2_4 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 35));
Point[] points2 = { point2_1, point2_2, point2_3, point2_4 };
g.FillPolygon(brWhite, points2);
g.DrawPolygon(pen, points2);
//покраска
Point craska_1 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 30));
Point craska_2 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 5));
Point craska_3 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 5));
Point craska_4 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 35));
Point craska_5 = new Point(Convert.ToInt32(_startPosX + 80), Convert.ToInt32(_startPosY + 35));
Point craska_6 = new Point(Convert.ToInt32(_startPosX + 75), Convert.ToInt32(_startPosY + 7));
Point craska_7 = new Point(Convert.ToInt32(_startPosX + 15), Convert.ToInt32(_startPosY + 7));
Point[] craska = { craska_1, craska_2, craska_3, craska_4, craska_5, craska_6, craska_7 };
g.FillPolygon(brBody, craska);
//нижняя часть
Point point3_1 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 35));
Point point3_2 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 35));
Point point3_3 = new Point(Convert.ToInt32(_startPosX + 95), Convert.ToInt32(_startPosY + 40));
Point point3_4 = new Point(Convert.ToInt32(_startPosX + 5), Convert.ToInt32(_startPosY + 40));
Point[] points3 = { point3_1, point3_2, point3_3, point3_4 };
g.FillPolygon(brBlue, points3);
g.DrawPolygon(pen, points3);
//стекла
Point point4_1 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 10));
Point point4_2 = new Point(Convert.ToInt32(_startPosX + 75), Convert.ToInt32(_startPosY + 10));
Point point4_3 = new Point(Convert.ToInt32(_startPosX + 75), Convert.ToInt32(_startPosY + 23));
Point point4_4 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 23));
Point[] points4 = { point4_1, point4_2, point4_3, point4_4 };
g.FillPolygon(brBlack, points4);
g.FillRectangle(Aque, _startPosX + 5, _startPosY + 11, 15, 11);
g.FillRectangle(Aque, _startPosX + 25, _startPosY + 11, 15, 11);
g.FillRectangle(Aque, _startPosX + 45, _startPosY + 11, 15, 11);
//переднее окно
Point point5_1 = new Point(Convert.ToInt32(_startPosX + 75), Convert.ToInt32(_startPosY + 10));
Point point5_2 = new Point(Convert.ToInt32(_startPosX + 88), Convert.ToInt32(_startPosY + 10));
Point point5_3 = new Point(Convert.ToInt32(_startPosX + 93), Convert.ToInt32(_startPosY + 23));
Point point5_4 = new Point(Convert.ToInt32(_startPosX + 75), Convert.ToInt32(_startPosY + 23));
Point[] poits5 = { point5_1, point5_2, point5_3, point5_4 };
g.FillPolygon(Aque, poits5);////////////////////////////////////
if (fastLocomative.Line)
{
g.DrawLine(dopPen, _startPosX, _startPosY + 25, _startPosX + 87, _startPosY + 25);
g.DrawLine(dopPen, _startPosX + 85, _startPosY + 25, _startPosX + 95, _startPosY + 35);
}
if (fastLocomative.Garmoshka)
{
g.FillRectangle(brBlack, _startPosX + 40, _startPosY + 5, 5, 35);
}
}
}
}

View File

@ -6,43 +6,43 @@ using System.Threading.Tasks;
namespace Locomative
{
internal class DrawningLocomative
public class DrawningLocomative
{
public EntityLocomative Locomative { get; private set; }
private float _startPosX;
private float _startPosY;
public EntityLocomative Locomative { get; protected set; }
protected float _startPosX;
protected float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
protected readonly int _locomativeWidth = 110;
protected readonly int _locomativeHeight = 40;
public void Init(int speed, float weight, Color bodyColor)
protected readonly int _locoWidth = 100;
protected readonly int _locoHeight = 40;
public DrawningLocomative(int speed, float weight, Color bodyColor)
{
Locomative = new EntityLocomative();
Locomative.Init(speed, weight, bodyColor);
Locomative = new EntityLocomative(speed, weight, bodyColor);
}
protected DrawningLocomative(int speed, float weight, Color bodyColor, int locoWidth, int locoHeight)
: this(speed, weight, bodyColor)
{
_locoHeight = locoHeight;
_locoWidth = locoWidth;
}
public void SetPosition(int x, int y, int width, int height)
{
Random rnd1 = new();
if (width < 120 || height < 50) return;
if (x > width - _locomativeWidth)
if (x + _locoWidth > width || y + _locoHeight > height || _locoHeight > height || _locoWidth > width || x < 0 || y < 0)
return;
else
{
x = rnd1.Next(10, width - _locomativeWidth - 1);
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
if( y > height- _locomativeHeight)
{
y = rnd1.Next(10, height - _locomativeHeight - 1);
}
_startPosX = x;
_startPosY = y;
_pictureHeight = height;
_pictureWidth = width;
}
public void MoveTransport(Direction direction)
{
switch (direction)
{
case Direction.Right:
if (_startPosX + _locomativeWidth + Locomative.Step < _pictureWidth)
if (_startPosX + _locoWidth + Locomative.Step < _pictureWidth)
{
_startPosX += Locomative.Step;
}
@ -60,68 +60,102 @@ namespace Locomative
}
break;
case Direction.Down:
if (_startPosY + _locomativeHeight + Locomative.Step < _pictureHeight)
if (_startPosY + _locoHeight + Locomative.Step < _pictureHeight)
{
_startPosY += Locomative.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX <= 0 || _startPosY <= 0 || !_pictureWidth.HasValue || !_pictureHeight.HasValue) return;
//кисти
Pen pen = new(Color.Black);
Brush brRed = new SolidBrush(Color.Red);
Brush brBlue = new SolidBrush(Color.Blue);
Brush brBody = new SolidBrush(Locomative.BodyColor);
Brush brLiteGray = new SolidBrush(Color.LightGray);
Brush brGray = new SolidBrush(Color.Gray);
Brush brDarkGray = new SolidBrush(Color.DarkGray);
Brush brBlack = new SolidBrush(Color.Black);
//Платформа поезда
g.FillRectangle(brBlack, _startPosX, _startPosY, 90, 40);
//котёл
g.FillRectangle(brBlue, _startPosX + 30, _startPosY + 10, 60, 20);
g.DrawLine(pen, _startPosX + 30, _startPosY + 10, _startPosX + 70, _startPosY + 10);
g.DrawLine(pen, _startPosX + 70, _startPosY + 10, _startPosX + 70, _startPosY + 30);
g.DrawLine(pen, _startPosX + 70, _startPosY + 30, _startPosX + 30, _startPosY + 30);
g.DrawLine(pen, _startPosX + 30, _startPosY + 30, _startPosX + 30, _startPosY + 10);
//крыша поезда
g.FillRectangle(brRed, _startPosX - 2, _startPosY - 2, 34, 44);
g.DrawLine(pen, _startPosX - 2, _startPosY - 2, _startPosX + 32, _startPosY - 2);
g.DrawLine(pen, _startPosX + 32, _startPosY - 2, _startPosX + 32, _startPosY + 42);
g.DrawLine(pen, _startPosX + 32, _startPosY + 42, _startPosX - 2, _startPosY + 42);
g.DrawLine(pen, _startPosX - 2, _startPosY + 42, _startPosX - 2, _startPosY - 2);
//трубы
g.FillEllipse(brGray, _startPosX + 36, _startPosY + 16, 8, 8);
g.FillEllipse(brBlack, _startPosX + 35, _startPosY + 15, 10, 10);
g.FillEllipse(brGray, _startPosX + 56, _startPosY + 16, 8, 8);
g.FillEllipse(brBlack, _startPosX + 55, _startPosY + 15, 10, 10);
g.FillEllipse(brGray, _startPosX + 70, _startPosY + 10, 20, 20);
g.FillEllipse(brBlack, _startPosX + 73, _startPosY + 13, 14, 14);
//бампер
Point point1 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY));
Point point2 = new Point(Convert.ToInt32(_startPosX + 110), Convert.ToInt32(_startPosY + 20));
Point point3 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 40));
Point[] points = { point1, point2, point3 };
g.FillPolygon(brGray, points);
Brush brBlue = new SolidBrush(Color.Blue);
Brush Aque = new SolidBrush(Color.Aquamarine);
//крыша
Point point1_1 = new Point(Convert.ToInt32(_startPosX ), Convert.ToInt32(_startPosY + 5));
Point point1_2 = new Point(Convert.ToInt32(_startPosX + 5), Convert.ToInt32(_startPosY));
Point point1_3 = new Point(Convert.ToInt32(_startPosX + 85), Convert.ToInt32(_startPosY));
Point point1_4 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 5));
Point[] points1 = { point1_1, point1_2, point1_3, point1_4 };
g.FillPolygon(brGray, points1);
g.DrawPolygon(pen, points1);
//основная часть
Point point2_1 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 5));
Point point2_2 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 5));
Point point2_3 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 35));
Point point2_4 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 35));
Point[] points2 = { point2_1, point2_2, point2_3, point2_4 };
g.FillPolygon(brLiteGray, points2);
g.DrawPolygon(pen, points2);
//покраска
Point craska_1 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 30));
Point craska_2 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 5));
Point craska_3 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 5));
Point craska_4 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 35));
Point craska_5 = new Point(Convert.ToInt32(_startPosX + 80), Convert.ToInt32(_startPosY + 35));
Point craska_6 = new Point(Convert.ToInt32(_startPosX + 75), Convert.ToInt32(_startPosY + 7));
Point craska_7 = new Point(Convert.ToInt32(_startPosX + 15), Convert.ToInt32(_startPosY + 7));
Point[] craska = { craska_1, craska_2, craska_3, craska_4, craska_5, craska_6, craska_7 };
g.FillPolygon(brBody, craska);
//колёса
int rad = 10;
g.FillEllipse(brBlack, Convert.ToInt32(_startPosX + 10), Convert.ToInt32(_startPosY + 30), rad, rad);
g.FillEllipse(brBlack, Convert.ToInt32(_startPosX + 30), Convert.ToInt32(_startPosY + 30), rad, rad);
g.FillEllipse(brBlack, Convert.ToInt32(_startPosX + 45), Convert.ToInt32(_startPosY + 30), rad, rad);
g.FillEllipse(brBlack, Convert.ToInt32(_startPosX + 60), Convert.ToInt32(_startPosY + 30), rad, rad);
g.FillEllipse(brBlack, Convert.ToInt32(_startPosX + 80), Convert.ToInt32(_startPosY + 30), rad, rad);
//нижняя часть
Point point3_1 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 25));
Point point3_2 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 25));
Point point3_3 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 35));
Point point3_4 = new Point(Convert.ToInt32(_startPosX + 100), Convert.ToInt32(_startPosY + 39));
Point point3_5 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 39));
Point point3_6 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 35));
Point point3_7 = new Point(Convert.ToInt32(_startPosX + 10), Convert.ToInt32(_startPosY + 35));
Point point3_8 = new Point(Convert.ToInt32(_startPosX + 10), Convert.ToInt32(_startPosY + 39));
Point point3_9 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 39));
Point point3_10 = new Point(Convert.ToInt32(_startPosX), Convert.ToInt32(_startPosY + 35));
Point[] points3 = { point3_1, point3_2, point3_3, point3_4, point3_5, point3_6, point3_7, point3_8, point3_9, point3_10 };
g.FillPolygon(brDarkGray, points3);
g.DrawPolygon(pen, points3);
//переднее окно
Point point5_1 = new Point(Convert.ToInt32(_startPosX + 80), Convert.ToInt32(_startPosY + 10));
Point point5_2 = new Point(Convert.ToInt32(_startPosX + 88), Convert.ToInt32(_startPosY + 10));
Point point5_3 = new Point(Convert.ToInt32(_startPosX + 93), Convert.ToInt32(_startPosY + 20));
Point point5_4 = new Point(Convert.ToInt32(_startPosX + 80), Convert.ToInt32(_startPosY + 20));
Point[] poits5 = { point5_1, point5_2, point5_3, point5_4 };
g.FillPolygon(Aque, poits5);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if(_pictureWidth<=_locomativeWidth || _pictureHeight <= _locomativeHeight)
if(_pictureWidth<=_locoWidth || _pictureHeight <= _locoHeight)
{
_pictureHeight = null;
_pictureWidth = null;
return;
}
if (_startPosX + _locomativeWidth > _pictureWidth)
if (_startPosX + _locoWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _locomativeWidth;
_startPosX = _pictureWidth.Value - _locoWidth;
}
if (_startPosY + _locomativeHeight > _pictureHeight)
if (_startPosY + _locoHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _locomativeHeight;
_startPosY = _pictureHeight.Value - _locoHeight;
}
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _locoWidth, _startPosY + _locoHeight);
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class DrawningObjectLoco : IDrawningObject
{
private DrawningLocomative _loco = null;
public DrawningObjectLoco(DrawningLocomative loco)
{
_loco = loco;
}
public float Step => _loco?.Locomative?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _loco?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_loco?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_loco?.SetPosition(x, y, width, height);
}
public void DrawningObject(Graphics g)
{
_loco?.DrawTransport(g);
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class EntityFastLocomative : EntityLocomative
{
public Color dopColor { get; private set; }
public bool Line { get; private set; }
public bool Garmoshka { get; private set; }
public EntityFastLocomative(int speed, float weight, Color bodyColor, Color dopColor, bool line, bool garmoshka) :
base(speed, weight, bodyColor)
{
this.dopColor = dopColor;
Line = line;
Garmoshka = garmoshka;
}
}
}

View File

@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace Locomative
{
internal class EntityLocomative
public class EntityLocomative
{
public int Speed { get; private set; }
public float Weight { get; private set; }
public Color BodyColor { get; private set; }
public float Step => Speed * 100 / Weight;
public void Init(int speed, float weight, Color bodyColor)
public EntityLocomative(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;

View File

@ -0,0 +1,271 @@
namespace Locomative
{
partial class FormMapWithSetLocomatives
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.MapsTextBox = new System.Windows.Forms.MaskedTextBox();
this.buttonMapsAdd = new System.Windows.Forms.Button();
this.ButtonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.ButtonShowOnMap = new System.Windows.Forms.Button();
this.ButtonShowStorage = new System.Windows.Forms.Button();
this.ButtonRemoveCar = new System.Windows.Forms.Button();
this.ButtonAddLoco = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.MapsTextBox);
this.groupBox1.Controls.Add(this.buttonMapsAdd);
this.groupBox1.Controls.Add(this.ButtonDeleteMap);
this.groupBox1.Controls.Add(this.listBoxMaps);
this.groupBox1.Controls.Add(this.maskedTextBox1);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Controls.Add(this.ButtonShowOnMap);
this.groupBox1.Controls.Add(this.ButtonShowStorage);
this.groupBox1.Controls.Add(this.ButtonRemoveCar);
this.groupBox1.Controls.Add(this.ButtonAddLoco);
this.groupBox1.Controls.Add(this.buttonUp);
this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonLeft);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(607, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 539);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// MapsTextBox
//
this.MapsTextBox.Location = new System.Drawing.Point(7, 22);
this.MapsTextBox.Mask = "00";
this.MapsTextBox.Name = "MapsTextBox";
this.MapsTextBox.Size = new System.Drawing.Size(187, 23);
this.MapsTextBox.TabIndex = 19;
//
// buttonMapsAdd
//
this.buttonMapsAdd.Location = new System.Drawing.Point(7, 80);
this.buttonMapsAdd.Name = "buttonMapsAdd";
this.buttonMapsAdd.Size = new System.Drawing.Size(187, 27);
this.buttonMapsAdd.TabIndex = 18;
this.buttonMapsAdd.Text = "Добавить карту";
this.buttonMapsAdd.UseVisualStyleBackColor = true;
this.buttonMapsAdd.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// ButtonDeleteMap
//
this.ButtonDeleteMap.Location = new System.Drawing.Point(7, 213);
this.ButtonDeleteMap.Name = "ButtonDeleteMap";
this.ButtonDeleteMap.Size = new System.Drawing.Size(187, 27);
this.ButtonDeleteMap.TabIndex = 17;
this.ButtonDeleteMap.Text = "Удалить карту";
this.ButtonDeleteMap.UseVisualStyleBackColor = true;
this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(7, 113);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(187, 94);
this.listBoxMaps.TabIndex = 16;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// maskedTextBox1
//
this.maskedTextBox1.Location = new System.Drawing.Point(7, 311);
this.maskedTextBox1.Mask = "00";
this.maskedTextBox1.Name = "maskedTextBox1";
this.maskedTextBox1.Size = new System.Drawing.Size(187, 23);
this.maskedTextBox1.TabIndex = 15;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Пользовательская карта #1",
"Пользовательская карта #2"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(7, 51);
this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(187, 23);
this.comboBoxSelectorMap.TabIndex = 14;
//
// ButtonShowOnMap
//
this.ButtonShowOnMap.Location = new System.Drawing.Point(7, 406);
this.ButtonShowOnMap.Name = "ButtonShowOnMap";
this.ButtonShowOnMap.Size = new System.Drawing.Size(187, 27);
this.ButtonShowOnMap.TabIndex = 13;
this.ButtonShowOnMap.Text = "Посмотреть карту";
this.ButtonShowOnMap.UseVisualStyleBackColor = true;
this.ButtonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// ButtonShowStorage
//
this.ButtonShowStorage.Location = new System.Drawing.Point(7, 373);
this.ButtonShowStorage.Name = "ButtonShowStorage";
this.ButtonShowStorage.Size = new System.Drawing.Size(187, 27);
this.ButtonShowStorage.TabIndex = 12;
this.ButtonShowStorage.Text = "Посмотреть хранилище";
this.ButtonShowStorage.UseVisualStyleBackColor = true;
this.ButtonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// ButtonRemoveCar
//
this.ButtonRemoveCar.Location = new System.Drawing.Point(7, 340);
this.ButtonRemoveCar.Name = "ButtonRemoveCar";
this.ButtonRemoveCar.Size = new System.Drawing.Size(187, 27);
this.ButtonRemoveCar.TabIndex = 11;
this.ButtonRemoveCar.Text = "Удалить поезд";
this.ButtonRemoveCar.UseVisualStyleBackColor = true;
this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveLoco_Click);
//
// ButtonAddLoco
//
this.ButtonAddLoco.Location = new System.Drawing.Point(7, 278);
this.ButtonAddLoco.Name = "ButtonAddLoco";
this.ButtonAddLoco.Size = new System.Drawing.Size(187, 27);
this.ButtonAddLoco.TabIndex = 9;
this.ButtonAddLoco.Text = "Добавить поезд";
this.ButtonAddLoco.UseVisualStyleBackColor = true;
this.ButtonAddLoco.Click += new System.EventHandler(this.ButtonAddLoco_Click);
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Locomative.Properties.Resources.arrow_up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(80, 439);
this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(41, 40);
this.buttonUp.TabIndex = 10;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Locomative.Properties.Resources.arrow_down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(80, 487);
this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(41, 40);
this.buttonDown.TabIndex = 7;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Locomative.Properties.Resources.arrow_right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(127, 487);
this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(41, 40);
this.buttonRight.TabIndex = 9;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Locomative.Properties.Resources.arrow_left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(32, 487);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(41, 40);
this.buttonLeft.TabIndex = 8;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(607, 539);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// FormMapWithSetLocomatives
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(807, 539);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetLocomatives";
this.Text = "Локоматив";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBox1;
private PictureBox pictureBox;
private Button buttonUp;
private Button buttonDown;
private Button buttonRight;
private Button buttonLeft;
private Button ButtonShowOnMap;
private Button ButtonShowStorage;
private Button ButtonRemoveCar;
private Button ButtonAddLoco;
private MaskedTextBox maskedTextBox1;
private ComboBox comboBoxSelectorMap;
private MaskedTextBox MapsTextBox;
private Button buttonMapsAdd;
private Button ButtonDeleteMap;
private ListBox listBoxMaps;
}
}

View File

@ -0,0 +1,177 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.DataFormats;
namespace Locomative
{
public partial class FormMapWithSetLocomatives : Form
{
private MapWithSetLocoGeneric<DrawningObjectLoco, AbstractMap> _mapLocoCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{ "Простая карта", new SimpleMap() },
{ "Пользовательская карта #1", new UserMap_BigBox() },
{ "Пользовательская карта #2", new UserMap_Rally() }
};
private readonly MapsCollection _mapsCollection;
public FormMapWithSetLocomatives()
{
InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >=
listBoxMaps.Items.Count))
{
listBoxMaps.SelectedIndex = 0;
}
else if (listBoxMaps.Items.Count > 0 && index > -1 && index <
listBoxMaps.Items.Count)
{
listBoxMaps.SelectedIndex = index;
}
}
private void ButtonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(MapsTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(MapsTextBox.Text, _mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?",
"Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ??
string.Empty);
ReloadMaps();
}
}
private void ButtonAddLoco_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
LocomativeForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectLoco loco = new(form.SelectedLoco);
if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + loco) >= 0)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image =_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveLoco_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
if (maskedTextBox1.Text == null)
return;
int pos = Convert.ToInt32(maskedTextBox1.Text);
if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - (pos - 1)) != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal interface IDrawningObject
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawningObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
}
}

View File

@ -38,6 +38,8 @@
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.ButtonFastCreate = new System.Windows.Forms.Button();
this.ChangeButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomative)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
@ -45,9 +47,10 @@
// buttonСreate
//
this.buttonСreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonСreate.Location = new System.Drawing.Point(12, 393);
this.buttonСreate.Location = new System.Drawing.Point(14, 453);
this.buttonСreate.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonСreate.Name = "buttonСreate";
this.buttonСreate.Size = new System.Drawing.Size(75, 23);
this.buttonСreate.Size = new System.Drawing.Size(88, 27);
this.buttonСreate.TabIndex = 0;
this.buttonСreate.Text = "Создание";
this.buttonСreate.UseVisualStyleBackColor = true;
@ -58,9 +61,10 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Locomative.Properties.Resources.arrow_down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(711, 381);
this.buttonDown.Location = new System.Drawing.Point(830, 440);
this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(35, 35);
this.buttonDown.Size = new System.Drawing.Size(41, 40);
this.buttonDown.TabIndex = 1;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
@ -69,11 +73,11 @@
//
this.pictureBoxLocomative.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxLocomative.Location = new System.Drawing.Point(0, 0);
this.pictureBoxLocomative.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.pictureBoxLocomative.Name = "pictureBoxLocomative";
this.pictureBoxLocomative.Size = new System.Drawing.Size(800, 428);
this.pictureBoxLocomative.Size = new System.Drawing.Size(933, 497);
this.pictureBoxLocomative.TabIndex = 2;
this.pictureBoxLocomative.TabStop = false;
this.pictureBoxLocomative.Click += new System.EventHandler(this.pictureBoxLocomative_Click);
this.pictureBoxLocomative.Resize += new System.EventHandler(this.PictureBoxResize);
//
// statusStrip1
@ -82,22 +86,23 @@
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 428);
this.statusStrip1.Location = new System.Drawing.Point(0, 497);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 22);
this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 16, 0);
this.statusStrip1.Size = new System.Drawing.Size(933, 22);
this.statusStrip1.TabIndex = 3;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(55, 17);
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17);
this.toolStripStatusLabelSpeed.Text = "Скорость";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(24, 17);
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17);
this.toolStripStatusLabelWeight.Text = "Вес";
//
// toolStripStatusLabelColor
@ -111,9 +116,10 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Locomative.Properties.Resources.arrow_left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(670, 381);
this.buttonLeft.Location = new System.Drawing.Point(782, 440);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(35, 35);
this.buttonLeft.Size = new System.Drawing.Size(41, 40);
this.buttonLeft.TabIndex = 4;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click);
@ -123,9 +129,10 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Locomative.Properties.Resources.arrow_right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(752, 381);
this.buttonRight.Location = new System.Drawing.Point(877, 440);
this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(35, 35);
this.buttonRight.Size = new System.Drawing.Size(41, 40);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
@ -135,18 +142,43 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Locomative.Properties.Resources.arrow_up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(711, 340);
this.buttonUp.Location = new System.Drawing.Point(830, 392);
this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(35, 35);
this.buttonUp.Size = new System.Drawing.Size(41, 40);
this.buttonUp.TabIndex = 6;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
//
// ButtonFastCreate
//
this.ButtonFastCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonFastCreate.Location = new System.Drawing.Point(118, 453);
this.ButtonFastCreate.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.ButtonFastCreate.Name = "ButtonFastCreate";
this.ButtonFastCreate.Size = new System.Drawing.Size(113, 27);
this.ButtonFastCreate.TabIndex = 7;
this.ButtonFastCreate.Text = "Модификация";
this.ButtonFastCreate.UseVisualStyleBackColor = true;
this.ButtonFastCreate.Click += new System.EventHandler(this.ButtonFastCreate_Click);
//
// ChangeButton
//
this.ChangeButton.Location = new System.Drawing.Point(667, 453);
this.ChangeButton.Name = "ChangeButton";
this.ChangeButton.Size = new System.Drawing.Size(75, 27);
this.ChangeButton.TabIndex = 8;
this.ChangeButton.Text = "Выбрать";
this.ChangeButton.UseVisualStyleBackColor = true;
this.ChangeButton.Click += new System.EventHandler(this.ChangeButton_Click);
//
// LocomativeForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.ClientSize = new System.Drawing.Size(933, 519);
this.Controls.Add(this.ChangeButton);
this.Controls.Add(this.ButtonFastCreate);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonСreate);
this.Controls.Add(this.buttonRight);
@ -154,9 +186,9 @@
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.pictureBoxLocomative);
this.Controls.Add(this.statusStrip1);
this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.Name = "LocomativeForm";
this.Text = "Локоматив";
this.Load += new System.EventHandler(this.LocomativeForm_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomative)).EndInit();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
@ -177,5 +209,7 @@
private Button buttonLeft;
private Button buttonRight;
private Button buttonUp;
private Button ButtonFastCreate;
private Button ChangeButton;
}
}

View File

@ -3,6 +3,7 @@ namespace Locomative
public partial class LocomativeForm : Form
{
private DrawningLocomative _loco;
public DrawningLocomative SelectedLoco { get; private set; }
public LocomativeForm()
{
InitializeComponent();
@ -14,20 +15,26 @@ namespace Locomative
_loco?.DrawTransport(gr);
pictureBoxLocomative.Image = bmp;
}
private void LocomativeForm_Load(object sender, EventArgs e)
public void SetData()
{
Random rnd = new();
_loco.SetPosition(rnd.Next(10, 300), rnd.Next(10, 300), pictureBoxLocomative.Width, pictureBoxLocomative.Height);
toolStripStatusLabelColor.Text = $"Öâåò:{_loco.Locomative?.BodyColor.Name}";
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü:{_loco.Locomative?.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ:{_loco.Locomative?.Weight}";
}
private void buttonÑreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_loco = new DrawningLocomative();
_loco.Init(rnd.Next(1, 200), rnd.Next(10, 300), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_loco.SetPosition(rnd.Next(10, 300), rnd.Next(10, 300), pictureBoxLocomative.Width, pictureBoxLocomative.Height);
toolStripStatusLabelColor.Text = $"Öâåò:{_loco.Locomative?.BodyColor.Name}";
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü:{_loco.Locomative?.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ:{_loco.Locomative?.Weight}";
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_loco = new DrawningLocomative(rnd.Next(20, 70), rnd.Next(10, 300), color);
SetData();
Draw();
}
@ -57,15 +64,36 @@ namespace Locomative
Draw();
}
private void pictureBoxLocomative_Click(object sender, EventArgs e)
{
}
private void PictureBoxResize(object sender, EventArgs e)
{
_loco?.ChangeBorders(pictureBoxLocomative.Width, pictureBoxLocomative.Height);
Draw();
}
private void ButtonFastCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialogDop = new();
if (dialogDop.ShowDialog() == DialogResult.OK)
{
dopColor = dialogDop.Color;
}
_loco = new DrawningFastLoco(rnd.Next(20, 70), rnd.Next(100, 300), color, dopColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData();
Draw();
}
private void ChangeButton_Click(object sender, EventArgs e)
{
SelectedLoco = _loco;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class MapWithSetLocoGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;
private readonly int _placeSizeHeight = 90;
private readonly SetLocoGeneric<T> _setLoco;
private readonly U _map;
public MapWithSetLocoGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setLoco = new SetLocoGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static int operator +(MapWithSetLocoGeneric<T, U> map, T plain)
{
return map._setLoco.Insert(plain);
}
public static T operator -(MapWithSetLocoGeneric<T, U> map, int position)
{
return map._setLoco.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawLocomatives(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
foreach (var loco in _setLoco.GetLoco())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, loco);
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
private void Shaking()
{
int j = _setLoco.Count - 1;
for(int i = 0; i < _setLoco.Count; i++)
{
if (_setLoco[j] == null)
{
for(; j>1; j--)
{
var car = _setLoco[j];
if (car != null)
{
_setLoco.Insert(car, i);
_setLoco.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawLocomatives(Graphics g)
{
int CountWidth = _pictureWidth / _placeSizeWidth;
int x = 10;
int y = _pictureHeight - _placeSizeHeight;
int k = 0;
foreach (var loco in _setLoco.GetLoco())
{
if ((k + 1) % CountWidth != 0 || k == 0)
{
loco?.SetObject(x, y, _pictureWidth, _pictureHeight);
loco?.DrawningObject(g);
x += _placeSizeWidth;
}
else
{
loco?.SetObject(x, y, _pictureWidth, _pictureHeight);
loco?.DrawningObject(g);
x = 10;
y -= _placeSizeHeight;
}
k++;
}
}
}
}

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetLocoGeneric<DrawningObjectLoco,AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string,MapWithSetLocoGeneric<DrawningObjectLoco, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name))
return;
Keys.Add(name);
_mapStorages.Add(name, new MapWithSetLocoGeneric<DrawningObjectLoco, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
public void DelMap(string name)
{
if (!_mapStorages.ContainsKey(name))
return;
Keys.Remove(name);
_mapStorages.Remove(name);
}
public MapWithSetLocoGeneric<DrawningObjectLoco, AbstractMap> this[string ind]
{
get
{
if (!_mapStorages.ContainsKey(ind))
return null;
else
return _mapStorages[ind];
}
}
}
}

View File

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

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class SetLocoGeneric<T>
where T : class
{
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetLocoGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
public int Insert(T plain)
{
Insert(plain, 0);
return _places.Count;
}
public int Insert(T loco, int position)
{
if (position < 0 || _places.Count < position || position > _maxCount || _places.Count == _maxCount)
return -1;
else if (loco == null)
return -1;
_places.Insert(position, loco);
return position;
}
public T Remove(int position)
{
T mid;
if (position < 0 || _places.Count < position || position > _maxCount)
return null;
else if (_places[position] == null)
return null;
else
{
mid = _places[position];
_places.RemoveAt(position);
}
return mid;
}
public T this[int position]
{
get
{
if (position < 0 || _places.Count < position || position > _maxCount)
return null;
else if (_places[position] == null)
return null;
else
return _places[position];
}
set
{
if (position < 0 || _places.Count < position || position > _maxCount)
return;
else if (_places.Count == _maxCount)
return;
else
_places[position] = value;
}
}
public IEnumerable<T> GetLoco()
{
foreach (var loco in _places)
{
if (loco != null)
yield return loco;
else
yield break;
}
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class SimpleMap : AbstractMap
{
Brush barrielColor = new SolidBrush(Color.Black);
Brush railColor = new SolidBrush(Color.Gray);
protected override void DrawBarrielPart(Graphics g, int i, int j)
{
g.FillRectangle(barrielColor, i * _size_x, j*_size_y, i*(_size_x+1), j*(_size_y+1));
}
protected override void DrawRailPart(Graphics g, int i, int j)
{
g.FillRectangle(railColor, 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 < 30)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class UserMap_BigBox : AbstractMap
{
Brush barrierColor = new SolidBrush(Color.DarkGray);
Brush roadColor = new SolidBrush(Color.Aqua);
protected override void DrawBarrielPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRailPart(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 < 30)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class UserMap_Rally : AbstractMap
{
Brush barrielColor = new SolidBrush(Color.Red);
Brush railColor = new SolidBrush(Color.Gray);
protected override void DrawBarrielPart(Graphics g, int i, int j)
{
g.FillRectangle(barrielColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRailPart(Graphics g, int i, int j)
{
g.FillRectangle(railColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
int sizeKub = 3;
_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;
}
}
for (int i = 0; i < _map.GetLength(0); ++i)
{
_map[i, 1] = _barrier;
_map[i, 99] = _barrier;
_map[1, i] = _barrier;
_map[99, i] = _barrier;
}
while (counter < 10)
{
Random rand = new Random();
int i = rand.Next(10, 80);
int j = rand.Next(10, 80);
if (i > _map.GetLength(0) - sizeKub || j > _map.GetLength(0) - sizeKub)
continue;
for (int iKub = i; iKub < i + sizeKub; iKub++)
{
for (int jKub = j; jKub < j + sizeKub; jKub++)
{
_map[iKub, jKub] = _barrier;
}
}
counter++;
}
}
}
}