import java.awt.*; import java.awt.image.BufferedImage; import java.util.Random; public abstract class AbstractMap { private IDrawingObject _drawningObject = null; protected int[][] _map = null; protected int _width; protected int _height; protected float[] position; protected float _size_x; protected float _size_y; protected Random _random = new Random(); protected int _water = 0; protected int _barrier = 1; public BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject) { _width = width; _height = height; _drawningObject = drawningObject; GenerateMap(); while (!SetObjectOnMap()) { GenerateMap(); } return DrawMapWithObject(); } public BufferedImage MoveObject(Direction direction) { boolean moveAccept = true; float[] position = _drawningObject.GetCurrentPosition(); int xObjWidth = (int)Math.ceil((position[2] - position[0]) / _size_x); int yObjHeight = (int)Math.ceil((position[3] - position[1]) / _size_y); int vertStep = (int)Math.ceil(_drawningObject.Step() / _size_y); int horizStep = (int)Math.ceil(_drawningObject.Step() / _size_x); int xObjLeftBorder = (int)Math.floor(position[0] / _size_x); int xObjRightBorder = (int)Math.ceil(position[2] / _size_x); int yObjTopBorder = (int)Math.floor(position[1] / _size_y); int yObjBottomBorder = (int)Math.ceil(position[3] / _size_y); switch (direction) { case Up: for (int i = 0; i < vertStep; i++) { if (!moveAccept) { break; } for (int j = 0; j < xObjWidth; j++) { if (yObjTopBorder - i < 0 || xObjLeftBorder + j >= _map[0].length) { break; } if (_map[xObjLeftBorder + j][yObjTopBorder - i] == _barrier) { moveAccept = false; break; } } } break; case Down: for (int i = 0; i < vertStep; i++) { if (!moveAccept) { break; } for (int j = 0; j < xObjWidth; j++) { if (yObjBottomBorder + i >= _map.length || xObjLeftBorder + j >= _map[0].length) { break; } if (_map[xObjLeftBorder + j][yObjBottomBorder + i] == _barrier) { moveAccept = false; break; } } } break; case Left: for (int i = 0; i < yObjHeight; i++) { if (!moveAccept) { break; } for (int j = 0; j < horizStep; j++) { if (yObjTopBorder + i >= _map.length || xObjLeftBorder - j < 0) { break; } if (_map[xObjLeftBorder - j][yObjTopBorder + i] == _barrier) { moveAccept = false; break; } } } break; case Right: for (int i = 0; i < yObjHeight; i++) { if (!moveAccept) { break; } for (int j = 0; j < horizStep; j++) { if (yObjTopBorder + i >= _map.length || xObjRightBorder + j >= _map[0].length) { break; } if (_map[xObjRightBorder + j][yObjTopBorder + i] == _barrier) { moveAccept = false; break; } } } break; } if (moveAccept) { _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); float[] position = _drawningObject.GetCurrentPosition(); int xObjWidth = (int)Math.ceil((position[2] - position[0]) / _size_x); int yObjHeight = (int)Math.ceil((position[3] - position[1]) / _size_y); int xObjLeftBorder = (int)Math.floor(position[0] / _size_x); int yObjTopBorder = (int)Math.floor(position[1] / _size_y); while (y < _height - (position[3] - position[1])) { while (x < _width - (position[2] - position[0])) { if (CheckSpawnArea(xObjWidth, yObjHeight, xObjLeftBorder, yObjTopBorder)) { _drawningObject.SetObject(x, y, _width, _height); return true; } x += (int)_size_x; xObjLeftBorder = (int)(x / _size_x); } x = 0; y += (int)_size_y; yObjTopBorder = (int)(y / _size_y); } return false; } private boolean CheckSpawnArea(int xObjWidth, int yObjHeight, int xObjLeftBorder, int yObjTopBorder) { for (int i = 0; i <= yObjHeight; i++) { for (int j = 0; j <= xObjWidth; j++) { if (yObjTopBorder + i >= _map.length || xObjLeftBorder + j >= _map[0].length || _map[xObjLeftBorder + j][yObjTopBorder + i] == _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(); for (int i = 0; i < _map.length; ++i) { for (int j = 0; j < _map[i].length; ++j) { if (_map[i][j] == _water) { DrawRoadPart(gr, i, j); } else if (_map[i][j] == _barrier) { DrawBarrierPart(gr, i, j); } } } _drawningObject.DrawingObject(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); }