204 lines
6.3 KiB
Java
204 lines
6.3 KiB
Java
package Classes.Maps;
|
|
|
|
import Classes.Direction;
|
|
import Classes.IDrawingObject;
|
|
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.util.Random;
|
|
|
|
public abstract class AbstractMap
|
|
{
|
|
private IDrawingObject _drawingObject = 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 Image CreateMap(int width, int height, IDrawingObject drawingObject)
|
|
{
|
|
_width = width;
|
|
_height = height;
|
|
_drawingObject = drawingObject;
|
|
GenerateMap();
|
|
|
|
while (!SetObjectOnMap())
|
|
{
|
|
GenerateMap();
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
|
|
public Image MoveObject(Direction direction)
|
|
{
|
|
int CollisionFlag = 0;
|
|
|
|
Dimension objSize1 = new Dimension(100,100);
|
|
|
|
//COLLISION CHECK
|
|
if (direction == Direction.Up)
|
|
{
|
|
for (int i = 0; i < _map.length; i++)
|
|
{
|
|
for (int j = 0; j < _map[i].length; j++)
|
|
{
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
Point LeftTop = new Point((int)_drawingObject.getCurrentPosition().Left(), (int)(_drawingObject.getCurrentPosition().Top() - _drawingObject.getStep()));
|
|
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
|
|
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
|
|
|
|
if (objectRect.intersects(rectBarrier))
|
|
{
|
|
CollisionFlag = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
else if (direction == Direction.Down)
|
|
{
|
|
for (int i = 0; i < _map.length; i++)
|
|
{
|
|
for (int j = 0; j < _map[i].length; j++)
|
|
{
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
Point LeftTop = new Point((int)_drawingObject.getCurrentPosition().Left(), (int)(_drawingObject.getCurrentPosition().Top() + _drawingObject.getStep()));
|
|
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
|
|
|
|
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y),(int) _size_x,(int) _size_y);
|
|
|
|
if (objectRect.intersects(rectBarrier))
|
|
{
|
|
CollisionFlag = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (direction == Direction.Left)
|
|
{
|
|
for (int i = 0; i < _map.length; i++)
|
|
{
|
|
for (int j = 0; j < _map[i].length; j++)
|
|
{
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
Point LeftTop = new Point((int)(_drawingObject.getCurrentPosition().Left() - _drawingObject.getStep()), (int)_drawingObject.getCurrentPosition().Top());
|
|
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
|
|
|
|
|
|
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
|
|
|
|
if (objectRect.intersects(rectBarrier))
|
|
{
|
|
CollisionFlag = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (direction == Direction.Right)
|
|
{
|
|
for (int i = 0; i < _map.length; i++)
|
|
{
|
|
for (int j = 0; j < _map[i].length; j++)
|
|
{
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
Point LeftTop = new Point((int)(_drawingObject.getCurrentPosition().Left() + _drawingObject.getStep()), (int)_drawingObject.getCurrentPosition().Top());
|
|
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
|
|
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
|
|
|
|
if (objectRect.intersects(rectBarrier))
|
|
{
|
|
CollisionFlag = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (CollisionFlag != 1)
|
|
{
|
|
_drawingObject.MoveObject(direction);
|
|
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
|
|
private boolean SetObjectOnMap()
|
|
{
|
|
if (_drawingObject == null || _map == null)
|
|
{
|
|
return false;
|
|
|
|
}
|
|
|
|
int x = _random.nextInt(0,10);
|
|
int y = _random.nextInt(0,10);
|
|
_drawingObject.SetObject(x, y, _width, _height);
|
|
|
|
Rectangle rectObject = new Rectangle(x, y, 100, 100);
|
|
|
|
for (int i = 0; i < _map.length; i++)
|
|
{
|
|
for (int j = 0; j < _map[i].length; j++)
|
|
{
|
|
if (_map[i][j] == _barrier)
|
|
{
|
|
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
|
|
if (rectObject.intersects(rectBarrier))
|
|
{
|
|
return false;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private Image DrawMapWithObject()
|
|
{
|
|
Image img = new BufferedImage(_width, _height,BufferedImage.TYPE_INT_ARGB);
|
|
if (_drawingObject == null || _map == null)
|
|
{
|
|
return img;
|
|
}
|
|
Graphics gr = (Graphics2D) img.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.DrawingObject(gr);
|
|
return img;
|
|
}
|
|
|
|
protected abstract void GenerateMap();
|
|
|
|
protected abstract void DrawRoadPart(Graphics g, int i, int j);
|
|
|
|
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
|
|
|
}
|