This commit is contained in:
MaxKarme 2022-10-19 08:11:37 +03:00
parent 2fc394b825
commit 1260b0dc88

View File

@ -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;