diff --git a/AirBomber/AirBomber/AbstractMap.cs b/AirBomber/AirBomber/AbstractMap.cs index e1b34b4..2754461 100644 --- a/AirBomber/AirBomber/AbstractMap.cs +++ b/AirBomber/AirBomber/AbstractMap.cs @@ -32,12 +32,69 @@ namespace AirBomber return DrawMapWithObject(); } + private bool CanMove(Direction direction, (float Left, float Right, float Top, float Bottom) position) + { + if (direction == Direction.Left) + { + for (int i = (int)((position.Left - _drawningObject.Step) / _size_y); i <= (int)(position.Left / _size_y); ++i) + { + for (int j = (int)(position.Top / _size_x); j <= (int)(position.Bottom / _size_x); ++j) + { + if (i >= 0 && _map[i,j] == _barrier) + { + return false; + } + } + } + } + else if (direction == Direction.Right) + { + for (int i = (int)(position.Right / _size_y); i <= (int)((position.Right + _drawningObject.Step) / _size_y); ++i) + { + for (int j = (int)(position.Top / _size_x); j <= (int)(position.Bottom / _size_x); ++j) + { + if (i <= _map.GetLength(0) && _map[i, j] == _barrier) + { + return false; + } + } + } + } + else if (direction == Direction.Up) + { + for (int i = (int)(position.Left / _size_y); i <= (int)(position.Right / _size_y); ++i) + { + for (int j = (int)((position.Top - _drawningObject.Step) / _size_x); j <= (int)(position.Top / _size_x); ++j) + { + if (j >= 0 && _map[i, j] == _barrier) + { + return false; + } + } + } + } + else if (direction == Direction.Down) + { + for (int i = (int)(position.Left / _size_y); i <= (int)(position.Right / _size_y); ++i) + { + for (int j = (int)(position.Bottom / _size_x); j <= (int)((position.Bottom + _drawningObject.Step) / _size_x); ++j) + { + if (j <= _map.GetLength(1) && _map[i, j] == _barrier) + { + return false; + } + } + } + } + return true; + } + public Bitmap MoveObject(Direction direction) - { - //TODO - if (true) + { + if (CanMove(direction, _drawningObject.GetCurrentPosition())) { _drawningObject.MoveObject(direction); + } return DrawMapWithObject(); } diff --git a/AirBomber/AirBomber/DrawningBomber.cs b/AirBomber/AirBomber/DrawningBomber.cs index d9f1ee5..6b0e810 100644 --- a/AirBomber/AirBomber/DrawningBomber.cs +++ b/AirBomber/AirBomber/DrawningBomber.cs @@ -133,7 +133,7 @@ namespace AirBomber public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { - return (_startPosX, _startPosY, _startPosX + _airBomberWidth, _airBomberHeight); + return (_startPosX, _startPosX + _airBomberWidth, _startPosY, _startPosY + _airBomberHeight); } } } diff --git a/AirBomber/AirBomber/SimpleMap.cs b/AirBomber/AirBomber/SimpleMap.cs index b4eb779..347fcdb 100644 --- a/AirBomber/AirBomber/SimpleMap.cs +++ b/AirBomber/AirBomber/SimpleMap.cs @@ -13,12 +13,12 @@ namespace AirBomber protected override void DrawBarrierPart(Graphics g, int i, int j) { - g.FillRectangle(barrierColor, i * _size_x, j * _size_y, 20, 20); + g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y); } protected override void DrawRoadPart(Graphics g, int i, int j) { - g.FillRectangle(roadColor, i * _size_x, j * _size_y, 20, 20); + g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _size_y); } protected override void GenerateMap()