From 9d01e9ae664e7cf1b8ebcdd91ae13b61cb6197fb Mon Sep 17 00:00:00 2001 From: the Date: Wed, 5 Oct 2022 11:05:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D1=82=20=D0=BA=D0=BE=D0=BB=D0=BB=D0=B8=D0=B7=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/AbstractMap.cs | 63 +++++++++++++++++++++++++-- AirBomber/AirBomber/DrawningBomber.cs | 2 +- AirBomber/AirBomber/SimpleMap.cs | 4 +- 3 files changed, 63 insertions(+), 6 deletions(-) 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()