diff --git a/src/main/java/inf112/fiasko/roborally/utility/IOUtil.java b/src/main/java/inf112/fiasko/roborally/utility/IOUtil.java index f200c7a..25cea6a 100644 --- a/src/main/java/inf112/fiasko/roborally/utility/IOUtil.java +++ b/src/main/java/inf112/fiasko/roborally/utility/IOUtil.java @@ -6,6 +6,7 @@ import inf112.fiasko.roborally.element_properties.Position; import inf112.fiasko.roborally.objects.IDrawableGame; import inf112.fiasko.roborally.objects.DrawableObject; import inf112.fiasko.roborally.objects.IDrawableObject; +import inf112.fiasko.roborally.objects.Particle; import inf112.fiasko.roborally.objects.Robot; import inf112.fiasko.roborally.objects.Tile; import inf112.fiasko.roborally.objects.Wall; @@ -27,11 +28,13 @@ public final class IOUtil { List drawableObjects = new ArrayList<>(); List tilesToDraw = game.getTilesToDraw(); List wallsToDraw = game.getWallsToDraw(); + List particlesToDraw = game.getParticlesToDraw(); List robotsToDraw = game.getRobotsToDraw(); int gameWidth = game.getWidth(); int gameHeight = game.getHeight(); drawableObjects.addAll(getDrawableObjectsFromElementList(tilesToDraw, gameWidth, tileWidth, tileHeight)); drawableObjects.addAll(getDrawableObjectsFromElementList(wallsToDraw, gameWidth, tileWidth, tileHeight)); + drawableObjects.addAll(getDrawableObjectsFromElementList(particlesToDraw, gameWidth, tileWidth, tileHeight)); drawableObjects.addAll(getDrawableRobots(robotsToDraw, gameHeight, tileWidth, tileHeight)); return drawableObjects; } @@ -86,6 +89,9 @@ public final class IOUtil { } else if (currentElement.getClass().isAssignableFrom(Wall.class)) { Wall wall = (Wall) currentElement; region = TextureConverterUtil.convertElement(wall); + } else if (currentElement.getClass().isAssignableFrom(Particle.class)) { + Particle particle = (Particle) currentElement; + region = TextureConverterUtil.convertElement(particle); } else { throw new IllegalArgumentException("Unknown element type passed to function."); } @@ -118,6 +124,10 @@ public final class IOUtil { Wall wall = (Wall) element; hasRotatedTexture = true; direction = wall.getDirection(); + } else if (element.getClass().isAssignableFrom(Particle.class)) { + Particle particle = (Particle) element; + hasRotatedTexture = true; + direction = particle.getDirection(); } else { throw new IllegalArgumentException("Unknown element type passed to function."); } diff --git a/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java b/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java index 98d0ddb..6113f52 100644 --- a/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java +++ b/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java @@ -5,9 +5,11 @@ import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.utils.Disposable; import inf112.fiasko.roborally.element_properties.Direction; +import inf112.fiasko.roborally.element_properties.ParticleType; 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.Particle; import inf112.fiasko.roborally.objects.Robot; import inf112.fiasko.roborally.objects.Tile; import inf112.fiasko.roborally.objects.Wall; @@ -29,6 +31,8 @@ public final class TextureConverterUtil { private static final Texture robotsTexture = new Texture(Gdx.files.internal("assets/robots.png")); private static Map tileSheetTileTextureMappings; private static Map tileSheetTileHasRotatedTextureMappings; + private static Map tileSheetParticleTextureMappings; + private static Map tileSheetParticleHasRotatedTextureMappings; private static Map tileSheetWallTextureMappings; private TextureConverterUtil() {} @@ -68,6 +72,30 @@ public final class TextureConverterUtil { throw new IllegalArgumentException("Invalid or unimplemented tile type encountered"); } + /** + * Gets the texture representing the particle + * @param particle The particle to draw + * @return The texture to draw + */ + public static TextureRegion convertElement(Particle particle) { + if (tileSheetParticleTextureMappings == null) { + try { + loadParticleMappings(); + } catch (IOException e) { + e.printStackTrace(); + } + } + Direction direction = particle.getDirection(); + TextureConverterContainer converterContainer = tileSheetParticleTextureMappings.get(particle.getParticleType()); + 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 particle type encountered"); + } + /** * Gets the texture representing the tile * @param wall The wall to draw @@ -95,8 +123,12 @@ public final class TextureConverterUtil { throw new IllegalArgumentException("Invalid or unimplemented tile type encountered"); } + /** + * Gets the texture representing the robot + * @param robot The robot to draw + * @return The texture to draw + */ public static TextureRegion convertElement(Robot robot) { - if (robot.getRobotId() == RobotID.ROBOT_1) { return new TextureRegion(robotsTexture, 0, 0, 64, 64); } else if (robot.getRobotId() == RobotID.ROBOT_2) { @@ -136,6 +168,25 @@ public final class TextureConverterUtil { return tileSheetTileHasRotatedTextureMappings.get(tile.getTileType()); } + /** + * Checks whether a particle has textures for different rotations + * + * For a particle without a rotated texture, the texture needs to be rotated when rendering. + * + * @param particle The particle to check + * @return True if rotated versions of the texture exists. False otherwise + */ + public static boolean hasRotatedTexture(Particle particle) { + if (tileSheetTileTextureMappings == null) { + try { + loadParticleMappings(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return tileSheetParticleHasRotatedTextureMappings.get(particle.getParticleType()); + } + /** * Loads mappings between a tile and texture * @@ -158,6 +209,28 @@ public final class TextureConverterUtil { } } + /** + * Loads mappings between a particle and a texture + * + * Loads both information about mapping from a particle to a texture converter container and information about + * mapping from a particle to whether the particle has a rotated version of each texture + * + * @throws IOException If the mapping file can't be properly read + */ + private static synchronized void loadParticleMappings() throws IOException { + tileSheetParticleTextureMappings = new HashMap<>(); + tileSheetParticleHasRotatedTextureMappings = new HashMap<>(); + InputStream fileStream = ResourceUtil.getResourceAsInputStream("texture_sheet_particle_mapping.txt"); + BufferedReader reader = new BufferedReader(new InputStreamReader(fileStream)); + String line; + while ((line = reader.readLine()) != null) { + String[] parameters = line.split(" "); + ParticleType type = ParticleType.valueOf(parameters[0]); + storeTextMappingInMap(parameters, type, tileSheetParticleTextureMappings, + tileSheetParticleHasRotatedTextureMappings); + } + } + /** * Loads mappings between a wall and texture *