From 1260b0dc88cf0fd53f7d6bf74cb5ec34f67eb3fc Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Wed, 19 Oct 2022 08:11:37 +0300 Subject: [PATCH] fix bug --- AirFighter/AirFighter/AbstractMap.cs | 48 +++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/AirFighter/AirFighter/AbstractMap.cs b/AirFighter/AirFighter/AbstractMap.cs index 35e2ad6..0dd466d 100644 --- a/AirFighter/AirFighter/AbstractMap.cs +++ b/AirFighter/AirFighter/AbstractMap.cs @@ -32,17 +32,29 @@ namespace AirFighter private (int mapI, int mapJ) checkBarrier((float Left, float Right, float Top, float Bottom) rect) { + return checkBarrier(rect, false, false, true, false); + } + private (int mapI, int mapJ) checkBarrier((float Left, float Right, float Top, float Bottom) rect, bool minLeft, bool maxLeft, bool isTop, bool isBottom) + { + (int mapI, int mapJ) res = (-1, -1); + for (int i = (int)(rect.Top / _size_y); i <= (int)(rect.Bottom / _size_y) && i < _map.GetLength(0); ++i) { for (int j = (int)(rect.Left / _size_x); j <= (int)(rect.Right / _size_x) && j < _map.GetLength(1); ++j) { if (j < 0) j = 0; if (i < 0) i = 0; - if (_map[i, j] == _barrier) return (i, j); + if (_map[i, j] != _barrier) continue; + + if (res.mapI == -1) res = (i, j); + if (minLeft && res.mapJ > j) res = (i, j); + if (maxLeft && res.mapJ < j) res = (i, j); + if(isBottom) res = (i, j); + if (isTop) return (i, j); } } - return (-1, -1); + return res; } public Bitmap MoveObject(Direction direction) { @@ -53,17 +65,37 @@ namespace AirFighter float drawningWidth = position.Right - position.Left; float drawningHeight = position.Bottom - position.Top; - if (direction == Direction.Left) position.Left -= _drawningObject.Step; - if (direction == Direction.Right) position.Left += _drawningObject.Step; - if (direction == Direction.Up) position.Top -= _drawningObject.Step; - if (direction == Direction.Down) position.Top += _drawningObject.Step; + bool minLeft = false; + bool maxLeft = false; + bool isTop = false; + bool isBottom = false; + + if (direction == Direction.Left) + { + position.Left -= _drawningObject.Step; + maxLeft = true; + } + if (direction == Direction.Right) + { + position.Left += _drawningObject.Step; + minLeft = true; + } + if (direction == Direction.Up) { + position.Top -= _drawningObject.Step; + isTop = true; + } + if (direction == Direction.Down) + { + position.Top += _drawningObject.Step; + isBottom = true; + } position.Right = position.Left + drawningWidth; position.Bottom = position.Top + drawningHeight; - var currentBarrier = checkBarrier(position); + var currentBarrier = checkBarrier(position, minLeft, maxLeft, isTop, isBottom); if (currentBarrier.mapI == -1) { @@ -114,7 +146,7 @@ namespace AirFighter if(position.Right > _width) { - position.Top = (minRowIndex + 1) * _size_y; + position.Top = (minRowIndex + 1) * _size_y + 1; position.Bottom = position.Top + drawningHeight; position.Left = 0;