diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/AbstractMap.cs b/AirPlaneWithRadar/AirPlaneWithRadar/AbstractMap.cs index 1f32cf1..1e193f7 100644 --- a/AirPlaneWithRadar/AirPlaneWithRadar/AbstractMap.cs +++ b/AirPlaneWithRadar/AirPlaneWithRadar/AbstractMap.cs @@ -32,12 +32,66 @@ namespace AirPlaneWithRadar } public Bitmap MoveObject(Direction direction) { - // TODO проверка, что объект может переместится в требуемом направлении - if (true) + + switch (direction) { - _drawningObject.MoveObject(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() { @@ -48,7 +102,18 @@ namespace AirPlaneWithRadar int x = _random.Next(0, 10); int y = _random.Next(0, 10); _drawningObject.SetObject(x, y, _width, _height); - // TODO првоерка, что объект не "накладывается" на закрытые участки + if (_drawningObject is DrawingObjectPlane) + { + 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() diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/DrawingObject.cs b/AirPlaneWithRadar/AirPlaneWithRadar/DrawingObjectPlane.cs similarity index 84% rename from AirPlaneWithRadar/AirPlaneWithRadar/DrawingObject.cs rename to AirPlaneWithRadar/AirPlaneWithRadar/DrawingObjectPlane.cs index 8fa9368..289af7a 100644 --- a/AirPlaneWithRadar/AirPlaneWithRadar/DrawingObject.cs +++ b/AirPlaneWithRadar/AirPlaneWithRadar/DrawingObjectPlane.cs @@ -6,11 +6,11 @@ using System.Threading.Tasks; namespace AirPlaneWithRadar { - internal class DrawingObject : IDrawingObject + internal class DrawingObjectPlane : IDrawingObject { private DrawingPlain _plain; - public DrawingObject(DrawingPlain plain) + public DrawingObjectPlane(DrawingPlain plain) { _plain = plain; } @@ -19,7 +19,7 @@ namespace AirPlaneWithRadar public void DrawningObject(Graphics g) { - //TODO + _plain?.DrawTransoprt(g); } public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/FormMap.cs b/AirPlaneWithRadar/AirPlaneWithRadar/FormMap.cs index 82e202c..4e94b14 100644 --- a/AirPlaneWithRadar/AirPlaneWithRadar/FormMap.cs +++ b/AirPlaneWithRadar/AirPlaneWithRadar/FormMap.cs @@ -23,7 +23,7 @@ namespace AirPlaneWithRadar toolStripStatusLabelSpeed.Text = $"Скорость: {plain.Plain.Speed}"; toolStripStatusLabelWeight.Text = $"Вес: {plain.Plain.Weight}"; toolStripStatusLabelColor.Text = $"Цвет: {plain.Plain.BodyColor.Name}"; - pictureBoxPlain.Image = _abstractMap.CreateMap(pictureBoxPlain.Width, pictureBoxPlain.Height,new DrawingObject(plain)) ; + pictureBoxPlain.Image = _abstractMap.CreateMap(pictureBoxPlain.Width, pictureBoxPlain.Height,new DrawingObjectPlane(plain)) ; } private void ButtonCreate_Click(object sender, EventArgs e) {