diff --git a/Airbus/Airbus/AbstractMap.cs b/Airbus/Airbus/AbstractMap.cs index 60899c4..b21999b 100644 --- a/Airbus/Airbus/AbstractMap.cs +++ b/Airbus/Airbus/AbstractMap.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.VisualBasic.Logging; using System.Collections.Generic; using System.Linq; using System.Text; @@ -33,17 +34,60 @@ namespace Airbus return DrawMapWithObject(); } + //проверка на "накладывание" + protected bool CheckPosition(float Left, float Right, float Top, float Bottom) + { + int x_start = (int)(Left / _size_x); + int y_start = (int)(Right / _size_y); + int x_end = (int)(Top / _size_x); + int y_end = (int)(Bottom / _size_y); + + for (int i = x_start; i <= x_end; i++) + { + for (int j = y_start; j <= y_end; j++) + { + if (_map[i, j] == _barrier) + { + return true; + } + } + } + + return false; + } + public Bitmap MoveObject(Direction direction) { - //Сделать проверку на то, что объект может поместиться в выбранном направлении - if (true) + _drawningObject.MoveObject(direction); + (float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition(); + + if (CheckPosition(Left, Right, Top, Bottom)) { - _drawningObject.MoveObject(direction); + _drawningObject.MoveObject(MoveObjectBack(direction)); } return DrawMapWithObject(); } + private Direction MoveObjectBack(Direction direction) + { + switch (direction) + { + case Direction.Up: + return Direction.Down; + break; + case Direction.Down: + return Direction.Up; + break; + case Direction.Left: + return Direction.Right; + break; + case Direction.Right: + return Direction.Left; + break; + } + return Direction.None; + } private bool SetObjectOnMap() { if (_drawningObject == null || _map == null) @@ -54,8 +98,40 @@ namespace Airbus int x = _random.Next(0, 10); int y = _random.Next(0, 10); _drawningObject.SetObject(x, y, _width, _height); - //Сделать проверку на то, то объект не "накладывается" на закрытые участки - return true; + (float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition(); + + if (!CheckPosition(Left, Right, Top, Bottom)) + { + return true; + } + + float x_start = Left; + float y_start = Right; + float x_length = Top - Left; + float y_length = Bottom - Right; + while (CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length)) + { + bool check; + + do + { + check = CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length); + + if (!check) + { + _drawningObject.SetObject((int)x_start, (int)y_start, _width, _height); + return true; + } + else + { + x_start += _size_x; + } + } while (check); + } + + x_start = x; + y_start += _size_y; + return false; } private Bitmap DrawMapWithObject()