diff --git a/src/main/java/AbstractMap.java b/src/main/java/AbstractMap.java new file mode 100644 index 0000000..c0af228 --- /dev/null +++ b/src/main/java/AbstractMap.java @@ -0,0 +1,186 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.HashMap; +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) + { + this.width = width; + this.height = height; + this.drawingObject = drawingObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public Image MoveObject(Direction direction) + { + boolean flag = true; + float step = drawingObject.getStep(); + + HashMap hashMap = drawingObject.GetCurrentPosition(); + float left = hashMap.get("Left"); + float right = hashMap.get("Right"); + float top = hashMap.get("Top"); + float bottom = hashMap.get("Bottom"); + + int x1_obj_next = (int)((left - step) / size_x); + int y1_obj_next = (int)((top - step) / size_y); + int x2_obj_next = (int)((right + step) / size_x); + int y2_obj_next = (int)((bottom + step) / size_y); + + int x1_obj_current = (int)(left / size_x); + int y1_obj_current = (int)(top / size_y); + int x2_obj_current = (int)(right / size_x); + int y2_obj_current = (int)(bottom / size_y); + + // Проверка возможности перемещения + switch (direction) + { + case Left: + { + if (x1_obj_next < 0) + flag = false; + else + { + for (int i = x1_obj_next; i <= x1_obj_current; i++) + { + for (int j = y1_obj_current; j <= y2_obj_current; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + case Right: + { + if (x2_obj_next >= map.length) + flag = false; + else + { + for (int i = x2_obj_current; i <= x2_obj_next; i++) + { + for (int j = y1_obj_current; j <= y2_obj_current; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + case Up: + { + if (y1_obj_next < 0) + flag = false; + else + { + for (int i = x1_obj_current; i <= x2_obj_current; i++) + { + for (int j = y1_obj_next; j <= y1_obj_current; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + case Down: + { + if (y2_obj_next >= map.length) + flag = false; + else + { + for (int i = x1_obj_current; i <= x2_obj_current; i++) + { + for (int j = y2_obj_current; j <= y2_obj_next; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + } + + if (flag) + { + 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); + + HashMap hashMap = drawingObject.GetCurrentPosition(); + float left = hashMap.get("Left"); + float right = hashMap.get("Right"); + float top = hashMap.get("Top"); + float bottom = hashMap.get("Bottom"); + + for (int i = (int)(x / size_x); i <= (int) (right / size_x); i++) + { + for (int j = (int)(y / size_y); j <= (int) (bottom / size_y); j++) + { + if (map[i][j] == _barrier) + { + return false; + } + } + } + return true; + } + private Image DrawMapWithObject() + { + BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); + if (drawingObject == null || map == null) + return img; + + Graphics2D gr = img.createGraphics(); + + for (int i = 0; i < map.length; ++i) + { + for (int j = 0; j < map[0].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(Graphics2D g, int i, int j); + protected abstract void DrawBarrierPart(Graphics2D g, int i, int j); +}