Создание методов проверки возможности передвижения.
This commit is contained in:
parent
88b6b488ba
commit
479df94730
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using Microsoft.VisualBasic.Logging;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -33,17 +34,60 @@ namespace Airbus
|
|||||||
return DrawMapWithObject();
|
return DrawMapWithObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//проверка на "накладывание"
|
||||||
|
protected bool CheckPosition(float Left, float Right, float Top, float Bottom)
|
||||||
|
{
|
||||||
|
int x_start = (int)(Left / _size_x);
|
||||||
|
int y_start = (int)(Right / _size_y);
|
||||||
|
int x_end = (int)(Top / _size_x);
|
||||||
|
int y_end = (int)(Bottom / _size_y);
|
||||||
|
|
||||||
|
for (int i = x_start; i <= x_end; i++)
|
||||||
|
{
|
||||||
|
for (int j = y_start; j <= y_end; j++)
|
||||||
|
{
|
||||||
|
if (_map[i, j] == _barrier)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public Bitmap MoveObject(Direction direction)
|
public Bitmap MoveObject(Direction direction)
|
||||||
{
|
{
|
||||||
//Сделать проверку на то, что объект может поместиться в выбранном направлении
|
_drawningObject.MoveObject(direction);
|
||||||
if (true)
|
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
|
||||||
|
|
||||||
|
if (CheckPosition(Left, Right, Top, Bottom))
|
||||||
{
|
{
|
||||||
_drawningObject.MoveObject(direction);
|
_drawningObject.MoveObject(MoveObjectBack(direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
return DrawMapWithObject();
|
return DrawMapWithObject();
|
||||||
}
|
}
|
||||||
|
private Direction MoveObjectBack(Direction direction)
|
||||||
|
{
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
case Direction.Up:
|
||||||
|
return Direction.Down;
|
||||||
|
break;
|
||||||
|
case Direction.Down:
|
||||||
|
return Direction.Up;
|
||||||
|
break;
|
||||||
|
case Direction.Left:
|
||||||
|
return Direction.Right;
|
||||||
|
break;
|
||||||
|
case Direction.Right:
|
||||||
|
return Direction.Left;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Direction.None;
|
||||||
|
}
|
||||||
private bool SetObjectOnMap()
|
private bool SetObjectOnMap()
|
||||||
{
|
{
|
||||||
if (_drawningObject == null || _map == null)
|
if (_drawningObject == null || _map == null)
|
||||||
@ -54,8 +98,40 @@ namespace Airbus
|
|||||||
int x = _random.Next(0, 10);
|
int x = _random.Next(0, 10);
|
||||||
int y = _random.Next(0, 10);
|
int y = _random.Next(0, 10);
|
||||||
_drawningObject.SetObject(x, y, _width, _height);
|
_drawningObject.SetObject(x, y, _width, _height);
|
||||||
//Сделать проверку на то, то объект не "накладывается" на закрытые участки
|
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
|
||||||
return true;
|
|
||||||
|
if (!CheckPosition(Left, Right, Top, Bottom))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
float x_start = Left;
|
||||||
|
float y_start = Right;
|
||||||
|
float x_length = Top - Left;
|
||||||
|
float y_length = Bottom - Right;
|
||||||
|
while (CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length))
|
||||||
|
{
|
||||||
|
bool check;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
check = CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length);
|
||||||
|
|
||||||
|
if (!check)
|
||||||
|
{
|
||||||
|
_drawningObject.SetObject((int)x_start, (int)y_start, _width, _height);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
x_start += _size_x;
|
||||||
|
}
|
||||||
|
} while (check);
|
||||||
|
}
|
||||||
|
|
||||||
|
x_start = x;
|
||||||
|
y_start += _size_y;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bitmap DrawMapWithObject()
|
private Bitmap DrawMapWithObject()
|
||||||
|
Loading…
Reference in New Issue
Block a user