PIbd-21_Eliseev_E.E._Airbus.../Project/src/AbstractMap.java

187 lines
5.6 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 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)
{
float[] cortege = _drawingObject.GetCurrentPosition();
int leftCell = (int)(cortege[0] / _size_x);
int upCell = (int)(cortege[1] / _size_y);
int downCell = (int)(cortege[3] / _size_y);
int rightCell = (int)(cortege[2] / _size_x);
int step = (int)_drawingObject.Step();
boolean canMove = true;
switch (direction)
{
case Left:
for (int i = leftCell - (int)(step / _size_x) - 1 >= 0 ? leftCell - (int)(step / _size_x) - 1 : leftCell; i < leftCell; i++)
{
for (int j = upCell; j < downCell; j++)
{
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
break;
case Up:
for (int i = leftCell; i <= rightCell; i++)
{
for (int j = upCell - (int)(step / _size_x) - 1 >= 0 ? upCell - (int)(step / _size_x) - 1 : downCell - (int)(step / _size_x); j < downCell - (int)(step / _size_x); j++)
{
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
break;
case Down:
for (int i = leftCell; i <= rightCell; i++)
{
for (int j = downCell + (int)(step / _size_x) + 1 <= _map.length - 1 ? downCell + (int)(step / _size_x) + 1 : upCell; j > upCell; j--)
{
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
break;
case Right:
for (int i = rightCell + (int)(step / _size_x) + 1 <= _map.length - 1 ? rightCell + (int)(step / _size_x) + 1 : rightCell; i > rightCell; i--)
{
for (int j = upCell; j < downCell; j++)
{
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
break;
}
if (canMove)
{
_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);
}