using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bus { 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 Random(); protected readonly int _freeRoad = 0; protected readonly int _barrier = 1; public Bitmap CreateMap(int width, int height, IDrawingObject drawningObject) { _width = width; _height = height; _drawingObject = drawningObject; GenerateMap(); while (!SetObjectOnMap()) { GenerateMap(); } return DrawMapWithObject(); } public Bitmap MoveObject(Direction direction) { // proverka _drawingObject.MoveObject(direction); bool collision = CheckCollision(); if (collision) { switch (direction) { case Direction.Left: _drawingObject.MoveObject(Direction.Right); break; case Direction.Right: _drawingObject.MoveObject(Direction.Left); break; case Direction.Up: _drawingObject.MoveObject(Direction.Down); break; case Direction.Down: _drawingObject.MoveObject(Direction.Up); break; } } return DrawMapWithObject(); } private bool SetObjectOnMap() { if (_drawingObject == null || _map == null) { return false; } int x = _random.Next(0, 10); int y = _random.Next(0, 10); _drawingObject.SetObject(x, y, _width, _height); //////////////////////////////// proverka 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; } private bool CheckCollision() { var pos = _drawingObject.GetCurrentPosition(); int startX = (int)((pos.Left) / _size_x); int endX = (int)((pos.Right) / _size_x); int startY = (int)((pos.Top) / _size_y); int endY = (int)((pos.Bottom) / _size_y); if (startX < 0 || startY < 0 || endX > _map.GetLength(1) || endY > _map.GetLength(0)) { return false; } for (int y = startY; y < endY; y++) { for (int x = startX; x < endX; x++) { if (_map[x, y] == _barrier) { return true; } } } return false; } protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); } }