Сделана проверка при движении объекта и его создании.

This commit is contained in:
Danil Malin 2022-09-25 14:09:15 +04:00
parent d0c4dedf44
commit cf067f83de
2 changed files with 74 additions and 9 deletions

View File

@ -32,20 +32,80 @@ namespace WarmlyLocomotive
public Bitmap MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом направлении
if (true)
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
for (int i = 0; i < _map.GetLength(0); ++i)
{
_drawningObject.MoveObject(direction);
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i,j] == _barrier)
{
switch (direction)
{
case Direction.Up:
if(_size_y * (j+1) >= topY - _drawningObject.Step && _size_y * (j+1) < topY && _size_x * (i + 1) > leftX
&& _size_x * (i + 1) <= rightX)
{
return DrawMapWithObject();
}
break;
case Direction.Down:
if (_size_y * j <= bottomY + _drawningObject.Step && _size_y * j > bottomY && _size_x * (i+1) > leftX
&& _size_x * (i+1) <= rightX)
{
return DrawMapWithObject();
}
break;
case Direction.Left:
if (_size_x * (i+1) >= leftX - _drawningObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
case Direction.Right:
if (_size_x * i <= rightX + _drawningObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
}
}
}
}
_drawningObject.MoveObject(direction);
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
(float currentX, float currentY, float locomotiveWidth, float locomotiveHeight) = _drawningObject.GetCurrentPosition();
if (_drawningObject == null || _map == null)
{
return false;
}
if(currentX != 0)
{
locomotiveWidth -= currentX;
}
if (currentY != 0)
{
locomotiveHeight -= currentY;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i,j] == _barrier)
{
if(x + locomotiveWidth >= _size_x * i && x <= _size_x * i && y + locomotiveHeight > _size_y * j && y <= _size_y * j)
{
return false;
}
}
}
}
_drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
return true;

View File

@ -127,13 +127,13 @@ namespace WarmlyLocomotive
Pen pen = new(Color.Black);
Brush br = new SolidBrush(Locomotive?.BodyColor ?? Color.Black);
//кузов
PointF[] polygon_points = new PointF[4];
polygon_points[0] = new PointF(_startPosX + 10, _startPosY);
polygon_points[1] = new PointF(_startPosX + 190, _startPosY);
polygon_points[2] = new PointF(_startPosX + 210, _startPosY + 40);
polygon_points[3] = new PointF(_startPosX + 10, _startPosY + 40);
g.FillPolygon(br, polygon_points);
g.DrawPolygon(pen, polygon_points);
PointF[] polygonPoints = new PointF[4];
polygonPoints[0] = new PointF(_startPosX + 10, _startPosY);
polygonPoints[1] = new PointF(_startPosX + 190, _startPosY);
polygonPoints[2] = new PointF(_startPosX + 210, _startPosY + 40);
polygonPoints[3] = new PointF(_startPosX + 10, _startPosY + 40);
g.FillPolygon(br, polygonPoints);
g.DrawPolygon(pen, polygonPoints);
g.FillRectangle(br, _startPosX + 10, _startPosY + 40, 200, 40);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 40, 200, 40);
//колеса
@ -142,6 +142,11 @@ namespace WarmlyLocomotive
g.FillEllipse(brGray, _startPosX + 70, _startPosY + 80, 30, 25);
g.FillEllipse(brGray, _startPosX + 130, _startPosY + 80, 30, 25);
g.FillEllipse(brGray, _startPosX + 170, _startPosY + 80, 30, 25);
Pen dopPen = new(Color.Black, 2);
g.DrawEllipse(dopPen, _startPosX + 30, _startPosY + 80, 30, 25);
g.DrawEllipse(dopPen, _startPosX + 70, _startPosY + 80, 30, 25);
g.DrawEllipse(dopPen, _startPosX + 130, _startPosY + 80, 30, 25);
g.DrawEllipse(dopPen, _startPosX + 170, _startPosY + 80, 30, 25);
//стекла и дверь
Brush brBrown = new SolidBrush(Color.Brown);
g.FillRectangle(brBrown, _startPosX + 100, _startPosY + 20, 25, 45);