From e720b435c51ad1a6f7e706d9899a7cd3cae6dc94 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sat, 22 Oct 2022 22:53:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B0=D0=B1=D1=81=D1=82=D1=80=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/AbstractMap.java | 186 +++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 src/main/java/AbstractMap.java 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); +}