Dolgov D.A. Lab Work 2 #2

Closed
devil_1nc wants to merge 10 commits from LabWork02 into LabWork01
Showing only changes of commit 397a890de9 - Show all commits

View File

@ -18,35 +18,6 @@ namespace ProjectPlane
protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1;
public bool IsMoveable(float x, float y)
{
var getPos = _drawingObject.GetCurrentPosition();
int Right = Convert.ToInt32((getPos.Right + x) / _size_x) > 0
? Convert.ToInt32((getPos.Right + x) / _size_x) : 0;
int Left = Convert.ToInt32((getPos.Left + x) / _size_x) > 0
? Convert.ToInt32((getPos.Left + x) / _size_x) : 0;
int Top = Convert.ToInt32((getPos.Top + y) / _size_y) > 0
? Convert.ToInt32((getPos.Top + y) / _size_y) : 0;
int Bottom = Convert.ToInt32((getPos.Bottom + y) / _size_y) > 0
? Convert.ToInt32((getPos.Bottom + y) / _size_y) : 0;
for (int i = Left; i <= Right; i++)
{
for (int j = Top; j <= Bottom; j++)
{
if (_map[i, j] == _barrier) return false;
}
}
return true;
}
public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
@ -65,16 +36,59 @@ namespace ProjectPlane
switch (direction)
{
case Direction.Right:
isMoveable = IsMoveable(_drawingObject.Step, 0);
for (int i = _objWidth; i <= _objWidth + Convert.ToInt32(_drawingObject.Step / _size_x); i++)
{
for (int j = _startY; j <= _objHeight; j++)
{
if (_map[i, j] == _barrier)
{
isMoveable = false;
break;
}
}
}
break;
case Direction.Left:
isMoveable = IsMoveable(-_drawingObject.Step, 0);
for (int i = _startX; i >= Convert.ToInt32(_drawingObject.Step / _size_x); i--)
{
for (int j = _startY; j <= _objHeight; j++)
{
if (_map[i, j] == _barrier)
{
isMoveable = false;
break;
}
}
}
break;
case Direction.Up:
isMoveable = IsMoveable(0, -_drawingObject.Step);
for (int i = _startX; i <= _objWidth; i++)
{
for (int j = _startY; j >= Convert.ToInt32(_drawingObject.Step / _size_y); j--)
{
if (_map[i, j] == _barrier)
{
isMoveable = false;
break;
}
}
}
break;
case Direction.Down:
isMoveable = IsMoveable(0, _drawingObject.Step);
for (int i = _startX; i <= _objWidth; i++)
{
for (int j = _objHeight; j <= _objHeight + Convert.ToInt32(_drawingObject.Step / _size_y); j++)
{
if (_map[i, j] == _barrier)
{
isMoveable = false;
break;
}
}
}
break;
}
@ -93,17 +107,21 @@ namespace ProjectPlane
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
while (!IsMoveable(0, 0))
{
x += 5;
if (x < _width)
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
x = 0;
y += 5;
if (i * _size_x >= x && j * _size_y >= y &&
i * _size_x <= x + _drawingObject.GetCurrentPosition().Right &&
j * _size_y <= j + _drawingObject.GetCurrentPosition().Bottom)
{
if (_map[i, j] == _barrier)
{
return false;
}
}
}
_drawingObject.SetObject(x, y, _width, _height);
}
return true;