Fullfører foreløbig arbeid på TextureConverterUtil

This commit is contained in:
Kristian Knarvik 2020-02-23 23:18:04 +01:00
parent e0e676e0fb
commit 354542414a
3 changed files with 80 additions and 9 deletions

View File

@ -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<TileType, TextureConverterContainer> tileSheetTileTextureMappings;
private static Map<TileType, Boolean> tileSheetTileHasRotatedTextureMappings;
private static Map<WallType, TextureConverterContainer> 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);

View File

@ -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
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

View File

@ -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