189 lines
6.0 KiB
Java
189 lines
6.0 KiB
Java
|
import java.awt.*;
|
||
|
import java.util.Random;
|
||
|
|
||
|
public abstract class AbstractMap
|
||
|
{
|
||
|
private IDrawingObject _drawningObject = null;
|
||
|
protected int[][] _map = null;
|
||
|
protected int _width;
|
||
|
protected int _height;
|
||
|
protected float _size_x;
|
||
|
protected float _size_y;
|
||
|
protected Random _random = new Random();
|
||
|
protected int _freeRoad = 0;
|
||
|
protected int _barrier = 1;
|
||
|
public void CreateMap(int width, int height, IDrawingObject drawningObject)
|
||
|
{
|
||
|
_width = width;
|
||
|
_height = height;
|
||
|
_drawningObject = drawningObject;
|
||
|
GenerateMap();
|
||
|
while (!SetObjectOnMap())
|
||
|
{
|
||
|
GenerateMap();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private Point checkBarrier(Point leftTop, Point rightBottom)
|
||
|
{
|
||
|
return checkBarrier(leftTop, rightBottom, false, false, true, false);
|
||
|
}
|
||
|
private Point checkBarrier(Point leftTop, Point rightBottom, boolean minLeft, boolean maxLeft, boolean isTop, boolean isBottom)
|
||
|
{
|
||
|
Point res = new Point(-1, -1);
|
||
|
|
||
|
for (int i = (int)(leftTop.y / _size_y); i <= (int)(rightBottom.y / _size_y) && i < _map.length; ++i)
|
||
|
{
|
||
|
for (int j = (int)(leftTop.x / _size_x); j <= (int)(rightBottom.x / _size_x) && j < _map[0].length; ++j)
|
||
|
{
|
||
|
if (j < 0) j = 0;
|
||
|
if (i < 0) i = 0;
|
||
|
if (_map[i][j] != _barrier) continue;
|
||
|
|
||
|
if (res.y == -1) res = new Point(j, i);
|
||
|
if (minLeft && res.x > j) res = new Point(j, i);
|
||
|
if (maxLeft && res.x < j) res = new Point(j, i);
|
||
|
if(isBottom) res = new Point(j, i);
|
||
|
if (isTop) return new Point(j, i);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return res;
|
||
|
}
|
||
|
public void MoveObject(Direction direction)
|
||
|
{
|
||
|
// TODO проверка, что объект может переместится в требуемом
|
||
|
|
||
|
Point leftTop = _drawningObject.GetLeftTop();
|
||
|
Point rightBottom = _drawningObject.GetRightBottom();
|
||
|
|
||
|
float drawningWidth = rightBottom.x - leftTop.x;
|
||
|
float drawningHeight = rightBottom.y - leftTop.y;
|
||
|
|
||
|
boolean minLeft = false;
|
||
|
boolean maxLeft = false;
|
||
|
boolean isTop = false;
|
||
|
boolean isBottom = false;
|
||
|
|
||
|
if (direction == Direction.Left)
|
||
|
{
|
||
|
leftTop.x -= _drawningObject.getStep();
|
||
|
maxLeft = true;
|
||
|
}
|
||
|
if (direction == Direction.Right)
|
||
|
{
|
||
|
leftTop.x += _drawningObject.getStep();
|
||
|
minLeft = true;
|
||
|
}
|
||
|
if (direction == Direction.Up) {
|
||
|
leftTop.y -= _drawningObject.getStep();
|
||
|
isTop = true;
|
||
|
}
|
||
|
if (direction == Direction.Down)
|
||
|
{
|
||
|
leftTop.y += _drawningObject.getStep();
|
||
|
isBottom = true;
|
||
|
}
|
||
|
|
||
|
rightBottom.x = leftTop.x + (int)drawningWidth;
|
||
|
rightBottom.y = leftTop.y + (int)drawningHeight;
|
||
|
|
||
|
Point currentBarrier = checkBarrier(leftTop, rightBottom, minLeft, maxLeft, isTop, isBottom);
|
||
|
|
||
|
if (currentBarrier.x == -1)
|
||
|
{
|
||
|
_drawningObject.MoveObject(direction);
|
||
|
}
|
||
|
|
||
|
else if (direction == Direction.Left)
|
||
|
leftTop.x = (int)((currentBarrier.x + 1) * _size_x) + 1;
|
||
|
|
||
|
else if (direction == Direction.Right)
|
||
|
leftTop.x = (int)(currentBarrier.x * _size_x) - (int)drawningWidth - 1;
|
||
|
|
||
|
else if (direction == Direction.Up)
|
||
|
leftTop.y = (int)((currentBarrier.y + 1) * _size_y) + 1;
|
||
|
|
||
|
else if (direction == Direction.Down)
|
||
|
leftTop.y = (int)(currentBarrier.y * _size_y) - (int)drawningHeight - 1;
|
||
|
|
||
|
if (currentBarrier.y != -1)
|
||
|
_drawningObject.SetObject(leftTop.x, leftTop.y, _width, _height);
|
||
|
|
||
|
}
|
||
|
private boolean SetObjectOnMap()
|
||
|
{
|
||
|
if (_drawningObject == null || _map == null)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
int x = _random.nextInt(0, 10);
|
||
|
int y = _random.nextInt(0, 10);
|
||
|
_drawningObject.SetObject(x, y, _width, _height);
|
||
|
// TODO првоерка, что объект не "накладывается" на закрытые участки
|
||
|
|
||
|
Point leftTop = _drawningObject.GetLeftTop();
|
||
|
Point rightBottom = _drawningObject.GetRightBottom();
|
||
|
|
||
|
float drawningWidth = rightBottom.x - leftTop.x;
|
||
|
float drawningHeight = rightBottom.y - leftTop.y;
|
||
|
|
||
|
Point currentBarrier = checkBarrier(leftTop, rightBottom);
|
||
|
int minRowIndex = _map.length;
|
||
|
|
||
|
while(currentBarrier.y != -1)
|
||
|
{
|
||
|
minRowIndex = currentBarrier.y < minRowIndex ? currentBarrier.y : minRowIndex;
|
||
|
|
||
|
leftTop.x = (int)((currentBarrier.x + 1) * _size_x) + 1;
|
||
|
rightBottom.x = leftTop.x + (int)drawningWidth;
|
||
|
|
||
|
if(rightBottom.x > _width)
|
||
|
{
|
||
|
leftTop.y = (int)((minRowIndex + 1) * _size_y) + 1;
|
||
|
rightBottom.y = leftTop.y + (int)drawningHeight;
|
||
|
|
||
|
leftTop.x = 0;
|
||
|
rightBottom.x = (int)drawningWidth;
|
||
|
|
||
|
minRowIndex = _map.length;
|
||
|
}
|
||
|
|
||
|
if (rightBottom.y > _height) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
currentBarrier = checkBarrier(leftTop, rightBottom);
|
||
|
}
|
||
|
|
||
|
_drawningObject.SetObject((int)leftTop.x, (int)leftTop.y, _width, _height);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
public void DrawMapWithObject(Graphics2D gr)
|
||
|
{
|
||
|
if (_drawningObject == null || _map == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
for (int i = 0; i < _map.length; ++i)
|
||
|
{
|
||
|
for (int j = 0; j < _map[0].length; ++j)
|
||
|
{
|
||
|
if (_map[i][j] == _freeRoad)
|
||
|
{
|
||
|
DrawRoadPart(gr, i, j);
|
||
|
}
|
||
|
else if (_map[i][j] == _barrier)
|
||
|
{
|
||
|
DrawBarrierPart(gr, i, j);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
_drawningObject.DrawningObject(gr);
|
||
|
}
|
||
|
protected abstract void GenerateMap();
|
||
|
protected abstract void DrawRoadPart(Graphics2D g, int i, int j);
|
||
|
protected abstract void DrawBarrierPart(Graphics2D g, int i, int j);
|
||
|
}
|