From 354542414a5e20cff304ce332f05643c7b5b84c6 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 23 Feb 2020 23:18:04 +0100 Subject: [PATCH] =?UTF-8?q?Fullf=C3=B8rer=20forel=C3=B8big=20arbeid=20p?= =?UTF-8?q?=C3=A5=20TextureConverterUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utility/TextureConverterUtil.java | 74 +++++++++++++++++-- .../resources/texture_sheet_tile_mapping.txt | 11 ++- .../resources/texture_sheet_wall_mapping.txt | 4 + 3 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/texture_sheet_wall_mapping.txt diff --git a/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java b/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java index d3328d6..4270184 100644 --- a/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java +++ b/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java @@ -4,11 +4,14 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureRegion; import inf112.fiasko.roborally.element_properties.Direction; +import inf112.fiasko.roborally.element_properties.RobotID; import inf112.fiasko.roborally.element_properties.TileType; +import inf112.fiasko.roborally.element_properties.WallType; +import inf112.fiasko.roborally.objects.Robot; import inf112.fiasko.roborally.objects.Tile; +import inf112.fiasko.roborally.objects.Wall; import java.io.BufferedReader; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -20,8 +23,10 @@ import java.util.Map; */ public final class TextureConverterUtil { private static final Texture textureSheet = new Texture(Gdx.files.internal("assets/tiles.png")); + private static final Texture robotTexture = new Texture(Gdx.files.internal("assets/Robot.png")); private static Map tileSheetTileTextureMappings; private static Map tileSheetTileHasRotatedTextureMappings; + private static Map tileSheetWallTextureMappings; private TextureConverterUtil() {} @@ -49,6 +54,37 @@ public final class TextureConverterUtil { throw new IllegalArgumentException("Invalid or unimplemented tile type encountered"); } + /** + * Gets the texture representing the tile + * @param wall The wall to draw + * @return The texture to draw + */ + public static TextureRegion convertElement(Wall wall) { + if (tileSheetWallTextureMappings == null) { + try { + loadWallMappings(); + } catch (IOException e) { + e.printStackTrace(); + } + } + Direction direction = wall.getDirection(); + TextureConverterContainer converterContainer = tileSheetWallTextureMappings.get(wall.getWallType()); + if (converterContainer != null) { + return getDirectionalTextureRegion(direction, converterContainer.getXNorth(), + converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(), + converterContainer.getXSouth(), converterContainer.getYSouth(), converterContainer.getXWest(), + converterContainer.getYWest()); + } + throw new IllegalArgumentException("Invalid or unimplemented tile type encountered"); + } + + public static TextureRegion convertElement(Robot robot) { + if (robot.getRobotId() == RobotID.ROBOT_1) { + return new TextureRegion(robotTexture, 0, 0, 64, 64); + } + throw new IllegalArgumentException("Robot has no drawable texture."); + } + /** * Checks whether a tile has textures for different rotations * @@ -79,11 +115,7 @@ public final class TextureConverterUtil { private static synchronized void loadTileMappings() throws IOException { tileSheetTileTextureMappings = new HashMap<>(); tileSheetTileHasRotatedTextureMappings = new HashMap<>(); - ClassLoader classloader = Thread.currentThread().getContextClassLoader(); - InputStream fileStream = classloader.getResourceAsStream("texture_sheet_tile_mapping.txt"); - if (fileStream == null) { - throw new FileNotFoundException("Unable to load texture sheet mapping file."); - } + InputStream fileStream = ResourceUtil.getResourceAsInputStream("texture_sheet_tile_mapping.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(fileStream)); String line; while ((line = reader.readLine()) != null) { @@ -94,6 +126,24 @@ public final class TextureConverterUtil { } } + /** + * Loads mappings between a wall and texture + * + * @throws IOException If the mapping file can't be properly read + */ + private static synchronized void loadWallMappings() throws IOException { + tileSheetWallTextureMappings = new HashMap<>(); + InputStream fileStream = ResourceUtil.getResourceAsInputStream("texture_sheet_wall_mapping.txt"); + BufferedReader reader = new BufferedReader(new InputStreamReader(fileStream)); + String line; + while ((line = reader.readLine()) != null) { + String[] parameters = line.split(" "); + WallType type = WallType.valueOf(parameters[0]); + storeTextMappingInMap(parameters, type, tileSheetWallTextureMappings, + null); + } + } + /** * Reads one line of texture mapping and puts it into the correct maps * @param parameters The parameters describing the texture mapping of the element @@ -111,7 +161,9 @@ public final class TextureConverterUtil { if (parameters.length == 3) { container = new TextureConverterContainer(xNorth, yNorth, xNorth, yNorth, xNorth, yNorth, xNorth, yNorth); - hasRotatedTextureMapping.put(mapKey, false); + if (hasRotatedTextureMapping != null) { + hasRotatedTextureMapping.put(mapKey, false); + } } else { int xEast = Integer.parseInt(parameters[3]); int yEast = Integer.parseInt(parameters[4]); @@ -121,7 +173,9 @@ public final class TextureConverterUtil { int yWest = Integer.parseInt(parameters[8]); container = new TextureConverterContainer(xNorth, yNorth, xEast, yEast, xSouth, ySouth, xWest, yWest); - hasRotatedTextureMapping.put(mapKey, true); + if (hasRotatedTextureMapping != null) { + hasRotatedTextureMapping.put(mapKey, true); + } } textureMapping.put(mapKey, container); } @@ -144,12 +198,16 @@ public final class TextureConverterUtil { String INVALID_DIRECTION_MESSAGE = "Invalid direction for tile encountered"; switch (direction) { case NORTH: + case NORTH_EAST: return getTextureOnSheet(xNorth, yNorth); case EAST: + case SOUTH_EAST: return getTextureOnSheet(xEast, yEast); case SOUTH: + case SOUTH_WEST: return getTextureOnSheet(xSouth, ySouth); case WEST: + case NORTH_WEST: return getTextureOnSheet(xWest, yWest); default: throw new IllegalArgumentException(INVALID_DIRECTION_MESSAGE); diff --git a/src/main/resources/texture_sheet_tile_mapping.txt b/src/main/resources/texture_sheet_tile_mapping.txt index 3e0e171..c9cf235 100644 --- a/src/main/resources/texture_sheet_tile_mapping.txt +++ b/src/main/resources/texture_sheet_tile_mapping.txt @@ -11,4 +11,13 @@ TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT 0 7 1 7 2 7 3 7 TRANSPORT_BAND_FAST 4 1 5 1 4 2 5 2 TRANSPORT_BAND_FAST_RIGHT 2 3 2 2 3 2 3 3 TRANSPORT_BAND_FAST_LEFT 1 3 0 3 0 2 1 2 -TRANSPORT_BAND_FAST_SIDE_ENTRANCES 3 10 0 10 1 10 2 10 \ No newline at end of file +TRANSPORT_BAND_FAST_SIDE_ENTRANCES 3 10 0 10 1 10 2 10 +TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT 4 9 5 9 5 10 4 10 +TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT 0 9 1 9 2 9 3 9 +FLAG_1 6 6 +FLAG_2 6 7 +FLAG_3 6 8 +FLAG_4 6 9 +WRENCH 6 1 +WRENCH_AND_HAMMER 6 0 +DEATH_TILE 3 11 \ No newline at end of file diff --git a/src/main/resources/texture_sheet_wall_mapping.txt b/src/main/resources/texture_sheet_wall_mapping.txt new file mode 100644 index 0000000..646c71d --- /dev/null +++ b/src/main/resources/texture_sheet_wall_mapping.txt @@ -0,0 +1,4 @@ +WALL_NORMAL 6 3 6 2 4 3 5 3 +WALL_CORNER 7 0 7 1 7 3 7 2 +WALL_LASER_SINGLE 4 5 5 5 4 4 5 4 +WALL_LASER_DOUBLE 5 11 6 11 6 10 5 11 \ No newline at end of file