diff --git a/Locomotive/Locomotive/AbstractMap.cs b/Locomotive/Locomotive/AbstractMap.cs new file mode 100644 index 0000000..f7aa68c --- /dev/null +++ b/Locomotive/Locomotive/AbstractMap.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Locomotive +{ + 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(); // abstract void + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public Bitmap MoveObject(Direction direction) + { + // added logic must be checked! + bool isFree = true; + + int startPosX = (int)(_drawningObject.GetCurrentPosition().Left / _size_x); + int startPosY = (int)(_drawningObject.GetCurrentPosition().Top / _size_y); + + int objectWidth = (int)(_drawningObject.GetCurrentPosition().Right / _size_x); + int objectHeight = (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y); + + switch (direction) + { + case Direction.Right: + for (int i = objectWidth; i <= objectWidth + (int)(_drawningObject.Step / _size_x); i++) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Direction.Left: + for (int i = startPosX; i >= (int)(_drawningObject.Step / _size_x); i--) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Direction.Up: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = startPosY; j >= (int)(_drawningObject.Step / _size_y); j--) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Direction.Down: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = objectHeight; j <= objectHeight + (int)(_drawningObject.Step / _size_y); j++) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + } + if (isFree) + { + _drawningObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + + private bool SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.Next(0, 10); + int y = _random.Next(0, 10); + _drawningObject.SetObject(x, y, _width, _height); + + // added logic must be checked! + + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + if (i * _size_x >= x && j * _size_y >= y && + i * _size_x <= x + _drawningObject.GetCurrentPosition().Right && + j * _size_y <= j + _drawningObject.GetCurrentPosition().Bottom) + { + if (_map[i,j] == _barrier) + { + return false; + } + } + } + } + + return true; + } + + private Bitmap DrawMapWithObject() + { + Bitmap bmp = new(_width, _height); + if (_drawningObject == 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); + } + } + } + _drawningObject.DrawningObject(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); + + } +} diff --git a/Locomotive/Locomotive/DrawningLocomotive.cs b/Locomotive/Locomotive/DrawningLocomotive.cs index d89816b..35c7c93 100644 --- a/Locomotive/Locomotive/DrawningLocomotive.cs +++ b/Locomotive/Locomotive/DrawningLocomotive.cs @@ -145,7 +145,7 @@ namespace Locomotive // Получение текущей позиции объекта public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { - return (_startPosX, _startPosY, _startPosX + _locomotiveWidth, _startPosY + _locomotiveHeight); + return (_startPosX, _startPosX + _locomotiveWidth, _startPosY, _startPosY + _locomotiveHeight); } } } diff --git a/Locomotive/Locomotive/SimpleMap.cs b/Locomotive/Locomotive/SimpleMap.cs new file mode 100644 index 0000000..c45dca8 --- /dev/null +++ b/Locomotive/Locomotive/SimpleMap.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Locomotive +{ + 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 + + 1), 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 < 50) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + + } +}