116 lines
3.8 KiB
Java
116 lines
3.8 KiB
Java
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
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();
|
|
}
|
|
|
|
private boolean CheckBarriers(float topOffset, float rightOffset, float leftOffset, float bottomOffset)
|
|
{
|
|
float[] arrayPossition = _drawningObject.GetCurrentPosition();
|
|
int top = (int)((arrayPossition[1] + topOffset) / _size_y);
|
|
int right = (int)((arrayPossition[2] + rightOffset) / _size_x);
|
|
int left = (int)((arrayPossition[0] + leftOffset) / _size_x);
|
|
int bottom = (int)((arrayPossition[3] + bottomOffset) / _size_y);
|
|
if (top < 0 || left < 0 || right >= _map[0].length || bottom >= _map.length) return false;
|
|
for (int i = top; i <= bottom; i++)
|
|
{
|
|
for (int j = left; j <= right; j++)
|
|
{
|
|
if (_map[j][i] == _barrier) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public BufferedImage MoveObject(Direction direction)
|
|
{
|
|
if (_drawningObject == null) return DrawMapWithObject();
|
|
boolean isTrue = true;
|
|
switch (direction)
|
|
{
|
|
case Left:
|
|
if (!CheckBarriers(0, -1 * _drawningObject.Step(), -1 * _drawningObject.Step(), 0)) isTrue = false;
|
|
break;
|
|
case Right:
|
|
if (!CheckBarriers(0, _drawningObject.Step(), _drawningObject.Step(), 0)) isTrue = false;
|
|
break;
|
|
case Up:
|
|
if (!CheckBarriers(-1 * _drawningObject.Step(), 0, 0, -1 * _drawningObject.Step())) isTrue = false;
|
|
break;
|
|
case Down:
|
|
if (!CheckBarriers(_drawningObject.Step(), 0, 0, _drawningObject.Step())) isTrue = false;
|
|
break;
|
|
}
|
|
if (isTrue)
|
|
{
|
|
_drawningObject.MoveObject(direction);
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
|
|
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);
|
|
if (!CheckBarriers(0, 0, 0, 0)) 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();
|
|
for (int i = 0; i < _map.length; ++i)
|
|
{
|
|
for (int j = 0; j < _map[0].length; ++j)
|
|
{
|
|
if (_map[i][j] == _freeRoad)
|
|
{
|
|
DrawRoadPart((Graphics2D) gr, i, j);
|
|
}
|
|
else if (_map[i][j] == _barrier)
|
|
{
|
|
DrawBarrierPart((Graphics2D) gr, i, j);
|
|
}
|
|
}
|
|
}
|
|
_drawningObject.DrawningObject(gr);
|
|
return bmp;
|
|
}
|
|
|
|
protected abstract void GenerateMap();
|
|
protected abstract void DrawRoadPart(Graphics2D g, int i, int j);
|
|
protected abstract void DrawBarrierPart(Graphics2D g, int i, int j);
|
|
}
|