From 3adbd8520200168deb4af48454d6e9b52e97fb71 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 20 Feb 2020 14:12:30 +0100 Subject: [PATCH] Forbedrer TextureConverter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Legger til manglende static på metoder Legger til teksturer for flere tiles Forkorter lengden på convertElement metoden drastisk --- .../fiasko/roborally/TextureConverter.java | 165 ++++++------------ 1 file changed, 50 insertions(+), 115 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/TextureConverter.java b/src/main/java/inf112/fiasko/roborally/TextureConverter.java index 5e95cae..2d8e75c 100644 --- a/src/main/java/inf112/fiasko/roborally/TextureConverter.java +++ b/src/main/java/inf112/fiasko/roborally/TextureConverter.java @@ -3,10 +3,14 @@ package inf112.fiasko.roborally; 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.objects.Tile; +/** + * This class can convert an element to an appropriate texture + */ public class TextureConverter { - private Texture textureSheet = new Texture(Gdx.files.internal("assets/tiles.png")); + private static final Texture textureSheet = new Texture(Gdx.files.internal("assets/tiles.png")); private TextureConverter() {} @@ -15,8 +19,8 @@ public class TextureConverter { * @param tile The tile to draw * @return The texture to draw */ - public TextureRegion convertElement(Tile tile) { - final int tileTextureHeight = 300, tileTextureWidth = 300; + public static TextureRegion convertElement(Tile tile) { + Direction direction = tile.getDirection(); switch (tile.getTileType()) { case TILE: return getTextureOnSheet(4, 0); @@ -27,136 +31,67 @@ public class TextureConverter { case COGWHEEL_LEFT: return getTextureOnSheet(4, 6); case TRANSPORT_BAND_SLOW: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(0, 6); - case EAST: - return getTextureOnSheet(3, 6); - case SOUTH: - return getTextureOnSheet(1, 6); - case WEST: - return getTextureOnSheet(2, 6); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 0, 6, 3, 6, 1, 6, 2, 6); case TRANSPORT_BAND_SLOW_RIGHT: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(2, 5); - case EAST: - return getTextureOnSheet(2, 4); - case SOUTH: - return getTextureOnSheet(3, 4); - case WEST: - return getTextureOnSheet(3, 5); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 2, 5, 2, 4, 3, 4, 3, 5); case TRANSPORT_BAND_SLOW_LEFT: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(1, 5); - case EAST: - return getTextureOnSheet(0, 5); - case SOUTH: - return getTextureOnSheet(0, 4); - case WEST: - return getTextureOnSheet(1, 4); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 1, 5, 0, 5, 0, 4, 1, 4); case TRANSPORT_BAND_SLOW_SIDE_ENTRANCES: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(4, 8); - case EAST: - return getTextureOnSheet(0, 5); - case SOUTH: - return new TextureRegion(textureSheet, 0, 4*tileTextureHeight, - tileTextureWidth, tileTextureHeight); - case WEST: - return new TextureRegion(textureSheet, tileTextureWidth, 4*tileTextureHeight, - tileTextureWidth, tileTextureHeight); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 4, 8, 0, 5, 0, 4, 1, 4); case TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(0, 8); - case EAST: - return getTextureOnSheet(1, 8); - case SOUTH: - return getTextureOnSheet(2, 8); - case WEST: - return getTextureOnSheet(3, 8); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 0, 8, 1, 8, 2, 8, 3, 8); case TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(0,7); - case EAST: - return getTextureOnSheet(1, 7); - case SOUTH: - return getTextureOnSheet(2, 7); - case WEST: - return getTextureOnSheet(3, 7); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 0, 7, 1, 7, 2, 7, 3, 7); case TRANSPORT_BAND_FAST: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(4,1); - case EAST: - return getTextureOnSheet(5, 1); - case SOUTH: - return getTextureOnSheet(4, 2); - case WEST: - return getTextureOnSheet(5, 2); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 4, 1, 5, 1, 4, 2, 5, 2); case TRANSPORT_BAND_FAST_RIGHT: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(2,3); - case EAST: - return getTextureOnSheet(2, 2); - case SOUTH: - return getTextureOnSheet(3, 2); - case WEST: - return getTextureOnSheet(3, 3); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 2, 3, 2, 2, 3, 2, 3, 3); case TRANSPORT_BAND_FAST_LEFT: - switch (tile.getDirection()) { - case NORTH: - return getTextureOnSheet(1,3); - case EAST: - return getTextureOnSheet(0, 3); - case SOUTH: - return getTextureOnSheet(3, 2); - case WEST: - return getTextureOnSheet(3, 3); - default: - throw new IllegalArgumentException("Invalid direction for slow transport band encountered"); - } + return getDirectionalTextureRegion(direction, 1, 3, 0, 3, 0, 2, 1, 2); + case TRANSPORT_BAND_FAST_SIDE_ENTRANCES: + return getDirectionalTextureRegion(direction, 3, 10, 0, 10, 1, 10, 2, 10); default: throw new IllegalArgumentException("Invalid or unimplemented tile type encountered"); } } + /** + * Gets a texture region based on the direction of the tile + * @param direction The direction the tile is facing + * @param xNorth The relative x position on the texture sheet if the tile is facing north + * @param yNorth The relative y position on the texture sheet if the tile is facing north + * @param xEast The relative x position on the texture sheet if the tile is facing east + * @param yEast The relative y position on the texture sheet if the tile is facing east + * @param xSouth The relative x position on the texture sheet if the tile is facing south + * @param ySouth The relative y position on the texture sheet if the tile is facing south + * @param xWest The relative x position on the texture sheet if the tile is facing west + * @param yWest The relative y position on the texture sheet if the tile is facing west + * @return The texture region containing the tile's texture + */ + private static TextureRegion getDirectionalTextureRegion(Direction direction, int xNorth, int yNorth, int xEast, + int yEast, int xSouth, int ySouth, int xWest, int yWest) { + String INVALID_DIRECTION_MESSAGE = "Invalid direction for tile encountered"; + switch (direction) { + case NORTH: + return getTextureOnSheet(xNorth, yNorth); + case EAST: + return getTextureOnSheet(xEast, yEast); + case SOUTH: + return getTextureOnSheet(xSouth, ySouth); + case WEST: + return getTextureOnSheet(xWest, yWest); + default: + throw new IllegalArgumentException(INVALID_DIRECTION_MESSAGE); + } + } + /** * Gets a texture on the main tiles texture sheet * @param x The relative x coordinate on the sheet * @param y The relative y coordinate on the sheet * @return The texture region containing the texture */ - private TextureRegion getTextureOnSheet(int x, int y) { + private static TextureRegion getTextureOnSheet(int x, int y) { final int tileTextureHeight = 300, tileTextureWidth = 300; return new TextureRegion(textureSheet, x*tileTextureWidth, y*tileTextureHeight, tileTextureWidth, tileTextureHeight); @@ -170,7 +105,7 @@ public class TextureConverter { * @param tile The tile to check * @return True if rotated versions of the texture exists. False otherwise */ - public boolean hasRotatedTexture(Tile tile) { + public static boolean hasRotatedTexture(Tile tile) { switch (tile.getTileType()) { case TILE: case HOLE: