159 lines
5.1 KiB
Java
159 lines
5.1 KiB
Java
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.BufferedReader;
|
|
import java.util.Random;
|
|
|
|
public abstract class AbstractMap {
|
|
private IDrawningObject _drawningObject = null;
|
|
protected int[][] _map = null;
|
|
protected int _width;
|
|
protected int _height;
|
|
protected float _size_x;
|
|
protected float _size_y;
|
|
protected final Random _random = new Random();
|
|
protected final int _freeRoad = 0;
|
|
protected final int _barrier = 1;
|
|
public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject){
|
|
_width = width;
|
|
_height = height;
|
|
_drawningObject = drawningObject;
|
|
GenerateMap();
|
|
while (!SetObjectOnMap())
|
|
{
|
|
GenerateMap();
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
public BufferedImage MoveObject(Direction direction)
|
|
{
|
|
int _objWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x);
|
|
int _startX = (int)(_drawningObject.GetCurrentPosition()[0] / _size_x);
|
|
int _startY = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y);
|
|
int _objHeight = (int)(_drawningObject.GetCurrentPosition()[3] / _size_y);
|
|
|
|
boolean isMove = true;
|
|
switch (direction)
|
|
{
|
|
case LEFT:
|
|
for (int i = _startX; i >= Math.abs(_startX - (int)(_drawningObject.getStep() / _size_x)); i--)
|
|
{
|
|
for (int j = _startY; j <= _objHeight && j<_map.length; j++)
|
|
{
|
|
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
isMove = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case RIGHT:
|
|
for (int i = _objWidth; i <=_objWidth + (int)(_drawningObject.getStep() / _size_x); i++)
|
|
{
|
|
for (int j = _startY; j <= _objHeight && j<_map.length; j++)
|
|
{
|
|
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
isMove = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case DOWN:
|
|
for (int i = _startX; i <= _objWidth && i < _map[0].length; i++)
|
|
{
|
|
for (int j = _objHeight; j <= _objHeight + (int)(_drawningObject.getStep() / _size_y) && j<_map.length; j++)
|
|
{
|
|
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
isMove = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
case UP:
|
|
for (int i = _startX; i <= _objWidth && i < _map[0].length; i++)
|
|
{
|
|
for (int j = _startY; j >= Math.abs(_startY - (int)(_drawningObject.getStep() / _size_y)) ; j--)
|
|
{
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
isMove = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (isMove)
|
|
{
|
|
_drawningObject.MoveObject(direction);
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
private boolean SetObjectOnMap()
|
|
{
|
|
if (_drawningObject == null || _map == null)
|
|
{
|
|
return false;
|
|
}
|
|
int x = _random.nextInt(10, 15);
|
|
int y = _random.nextInt(10, 15);
|
|
_drawningObject.SetObject(x, y, _width, _height);
|
|
// TODO првоерка, что объект не "накладывается" на закрытые участки
|
|
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 + _drawningObject.GetCurrentPosition()[1] &&
|
|
j * _size_y <= y + _drawningObject.GetCurrentPosition()[3] && _map[i][j] == _barrier)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
private BufferedImage DrawMapWithObject()
|
|
{
|
|
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
|
|
if (_drawningObject == null || _map == null)
|
|
{
|
|
return bmp;
|
|
}
|
|
|
|
Graphics gr = bmp.getGraphics();
|
|
gr.setColor(DrawRoadPart());
|
|
gr.fillRect(0,0,_width, _height);
|
|
|
|
for (int i = 0; i < _map.length; i++)
|
|
{
|
|
for (int j = 0; j < _map[0].length; j++)
|
|
{
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
DrawBarrierPart(gr, i, j);
|
|
}
|
|
}
|
|
}
|
|
|
|
_drawningObject.DrawningObject(gr);
|
|
return bmp;
|
|
}
|
|
protected abstract void GenerateMap();
|
|
protected abstract Color DrawRoadPart();
|
|
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
|
|
|
}
|