167 lines
5.2 KiB
Java
167 lines
5.2 KiB
Java
|
import java.awt.*;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import java.util.Random;
|
||
|
|
||
|
public abstract class AbstractMap
|
||
|
{
|
||
|
private IDrawningObject _drawingObject = null;
|
||
|
protected int[][] _map = null;
|
||
|
protected int _width;
|
||
|
protected int _height;
|
||
|
protected float _size_x;
|
||
|
protected float _size_y;
|
||
|
protected float[] position;
|
||
|
protected Random _random = new Random();
|
||
|
protected int _freeRoad = 0;
|
||
|
protected int _barrier = 1;
|
||
|
|
||
|
public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject)
|
||
|
{
|
||
|
_width = width;
|
||
|
_height = height;
|
||
|
_drawingObject = drawningObject;
|
||
|
|
||
|
GenerateMap();
|
||
|
while (!SetObjectOnMap())
|
||
|
{
|
||
|
GenerateMap();
|
||
|
}
|
||
|
|
||
|
return DrawMapWithObject();
|
||
|
}
|
||
|
|
||
|
//проверка на "накладывание"
|
||
|
protected boolean 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 BufferedImage MoveObject(Direction direction)
|
||
|
{
|
||
|
position = _drawingObject.GetCurrentPosition();
|
||
|
|
||
|
for (int i = 0; i < _map.length; ++i)
|
||
|
{
|
||
|
for (int j = 0; j < _map[i].length; ++j)
|
||
|
{
|
||
|
if (_map[i][j] == _barrier)
|
||
|
{
|
||
|
switch (direction)
|
||
|
{
|
||
|
case Up:
|
||
|
if(_size_y * (j+1) >= position[0] - _drawingObject.Step() && _size_y * (j+1) < position[0] && _size_x * (i + 1) > position[3]
|
||
|
&& _size_x * (i + 1) <= position[1])
|
||
|
{
|
||
|
return DrawMapWithObject();
|
||
|
}
|
||
|
break;
|
||
|
case Down:
|
||
|
if (_size_y * j <= position[2] + _drawingObject.Step() && _size_y * j > position[2] && _size_x * (i+1) > position[3]
|
||
|
&& _size_x * (i+1) <= position[1])
|
||
|
{
|
||
|
return DrawMapWithObject();
|
||
|
}
|
||
|
break;
|
||
|
case Left:
|
||
|
if (_size_x * (i+1) >= position[3] - _drawingObject.Step() && _size_x * (i + 1) < position[3] && _size_y * (j + 1) < position[2]
|
||
|
&& _size_y * (j + 1) >= position[0])
|
||
|
{
|
||
|
return DrawMapWithObject();
|
||
|
}
|
||
|
break;
|
||
|
case Right:
|
||
|
if (_size_x * i <= position[1] + _drawingObject.Step() && _size_x * i > position[3] && _size_y * (j + 1) < position[2]
|
||
|
&& _size_y * (j + 1) >= position[0])
|
||
|
{
|
||
|
return DrawMapWithObject();
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_drawingObject.MoveObject(direction);
|
||
|
return DrawMapWithObject();
|
||
|
}
|
||
|
|
||
|
private boolean SetObjectOnMap()
|
||
|
{
|
||
|
if(_drawingObject == null || _map == null)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
int x = _random.nextInt(10);
|
||
|
int y = _random.nextInt(10);
|
||
|
_drawingObject.SetObject(x, y, _width, _height);
|
||
|
|
||
|
for (int i = 0; i < _map.length; ++i)
|
||
|
{
|
||
|
for(int j = 0; j < _map[0].length; ++j)
|
||
|
{
|
||
|
if(i * _size_x >= x && j * _size_y >= y &&
|
||
|
i * _size_x <= x + _drawingObject.GetCurrentPosition()[1] &&
|
||
|
j * _size_y <= j + _drawingObject.GetCurrentPosition()[2])
|
||
|
{
|
||
|
if (_map[i][j] == _barrier)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
private BufferedImage DrawMapWithObject()
|
||
|
{
|
||
|
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
|
||
|
|
||
|
if(_drawingObject == null || _map == null)
|
||
|
{
|
||
|
return bmp;
|
||
|
}
|
||
|
|
||
|
Graphics gr = bmp.getGraphics();
|
||
|
|
||
|
for(int i = 0; i < _map.length; ++i)
|
||
|
{
|
||
|
for(int j = 0; j < _map[i].length; ++j)
|
||
|
{
|
||
|
if (_map[i][j] == _freeRoad)
|
||
|
{
|
||
|
DrawRoadPart(gr, i, j);
|
||
|
}
|
||
|
else if (_map[i][j] == _barrier)
|
||
|
{
|
||
|
DrawBarrierPart(gr, i, j);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_drawingObject.DrawningObject(gr);
|
||
|
return bmp;
|
||
|
}
|
||
|
|
||
|
protected abstract void GenerateMap();
|
||
|
protected abstract void DrawRoadPart(Graphics g, int i, int j);
|
||
|
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
||
|
}
|