Изменение проверок в методах абстрактого класса.

This commit is contained in:
Anastasia 2022-10-31 19:04:08 +04:00
parent 4b474bc9fb
commit 118cc6316c
4 changed files with 50 additions and 85 deletions

View File

@ -31,67 +31,62 @@ namespace AirplaneWithRadar
}
public Bitmap MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом направлении
if (direction == Direction.Right)
if (_drawingObject == null) return DrawMapWithObject();
bool canMoveObject = true;
switch (direction)
{
for (int i = (int)(_drawingObject.GetCurrentPosition().Right / _size_x + 1); i < _drawingObject.GetCurrentPosition().Right + _drawingObject.Step / _size_x + 1; i++)
{
for (int j = (int)(_drawingObject.GetCurrentPosition().Top / _size_y + 1 ); j < _drawingObject.GetCurrentPosition().Bottom / _size_y + 1; j++)
case Direction.Left:
if (!CheckBarriers(-1 * _drawingObject.Step, -1 * _drawingObject.Step, 0, 0))
{
if (_map[i, j] == _barrier)
{
break;
}
canMoveObject = false;
}
}
}
if (direction == Direction.Up)
{
for (int i = (int)(_drawingObject.GetCurrentPosition().Top - _drawingObject.Step / _size_y); i < _drawingObject.GetCurrentPosition().Top / _size_y; i++)
{
for (int j = (int)(_drawingObject.GetCurrentPosition().Left / _size_x); j < _drawingObject.GetCurrentPosition().Right / _size_x; j++)
break;
case Direction.Right:
if (!CheckBarriers(_drawingObject.Step, _drawingObject.Step, 0, 0))
{
if (_map[i, j] == _barrier)
{
break;
}
canMoveObject = false;
}
}
}
if (direction == Direction.Left)
{
for (int i = (int)(_drawingObject.GetCurrentPosition().Left - _drawingObject.Step / _size_x); i < _drawingObject.GetCurrentPosition().Left / _size_x; i++)
{
for (int j = (int)(_drawingObject.GetCurrentPosition().Top / _size_y); j < _drawingObject.GetCurrentPosition().Bottom / _size_y; j++)
break;
case Direction.Up:
if (!CheckBarriers(0, 0, -1 * _drawingObject.Step, -1 * _drawingObject.Step))
{
if (_map[i, j] == _barrier)
{
break;
}
canMoveObject = false;
}
}
}
if (direction == Direction.Down)
{
for (int i = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y); i < _drawingObject.GetCurrentPosition().Bottom + _drawingObject.Step / _size_y; i++)
{
for (int j = (int)(_drawingObject.GetCurrentPosition().Left / _size_x); j < _drawingObject.GetCurrentPosition().Right / _size_x; j++)
break;
case Direction.Down:
if (!CheckBarriers(0, 0, _drawingObject.Step, _drawingObject.Step))
{
if (_map[i, j] == _barrier)
{
break;
}
canMoveObject = false;
}
}
break;
}
if (true)
if (canMoveObject)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private bool CheckBarriers(float leftMove, float rightMove, float topMove, float bottomMove)
{
int left = Convert.ToInt32((_drawingObject.GetCurrentPosition().Left + leftMove) / _size_x);
int right = Convert.ToInt32((_drawingObject.GetCurrentPosition().Right + rightMove) / _size_x);
int top = Convert.ToInt32((_drawingObject.GetCurrentPosition().Top + topMove) / _size_y);
int bottom = Convert.ToInt32((_drawingObject.GetCurrentPosition().Bottom + bottomMove) / _size_y);
if (top < 0 || left < 0 || right >= _map.GetLength(1) || bottom >= _map.GetLength(0))
{
return false;
}
for (int i = top; i <= bottom; i++)
{
for (int j = left; j <= right; j++)
{
if (_map[j, i] == 1) return false;
}
}
return true;
}
private bool SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
@ -101,18 +96,13 @@ namespace AirplaneWithRadar
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
for (int i = (int)(_drawingObject.GetCurrentPosition().Left/_size_x); i < _drawingObject.GetCurrentPosition().Right/_size_x; i++)
{
for (int j = (int)(_drawingObject.GetCurrentPosition().Top/_size_y); j < _drawingObject.GetCurrentPosition().Bottom/_size_y; j++)
{
if (_map[i,j] == _barrier)
{
return false;
}
}
}
// TODO проверка, что объект не "накладывается" на закрытые участки
if (!CheckBarriers(0, 0, 0, 0))
{
return false;
}
return true;
}
private Bitmap DrawMapWithObject()
{

View File

@ -38,7 +38,7 @@ namespace AirplaneWithRadar
/// <summary>
/// Высота отрисовки автомобиля
/// </summary>
private readonly int _airplaneHeight = 107;
private readonly int _airplaneHeight = 108;
/// <summary>
/// Инициализация свойств
/// </summary>

View File

@ -17,7 +17,7 @@ namespace AirplaneWithRadar
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="radar">Признак наличия обвеса</param>
/// <param name="extraFuelTank">Признак наличия антикрыла</param>
public DrawingAirplaneWithRadar(int speed, float weight, Color bodyColor, Color dopColor, bool radar, bool extraFuelTank) : base(speed, weight, bodyColor, 303, 102)
public DrawingAirplaneWithRadar(int speed, float weight, Color bodyColor, Color dopColor, bool radar, bool extraFuelTank) : base(speed, weight, bodyColor, 304, 109)
{
Airplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, dopColor, radar, extraFuelTank);
}

View File

@ -42,7 +42,7 @@ namespace AirplaneWithRadar
}
int counter = 0;
int counter;
for (int j = 0; j < _map.GetLength(1); j++)
{
@ -57,33 +57,8 @@ namespace AirplaneWithRadar
counter++;
}
}
j++;
j += 3;
}
//for (int i = 0; i < _map.GetLength(0); ++i)
//{
// int startX = _random.Next(0, 90);
// int x = _random.Next(0, 100 - startX - 10);
// counter = x;
// while (counter > 0)
// {
// for (int j = 0; j < _map.GetLength(1); ++j)
// {
// _map[i, j] = _barrier;
// counter--;
// }
// }
//}
//while (counter < 50)
//{
// int x = _random.Next(0, 100);
// int y = _random.Next(0, 100);
// if (_map[x, y] == _freeRoad)
// {
// _map[x, y] = _barrier;
// counter++;
// }
//}
}
}