Refaktorerer og forenkler Board litt

Lager en hjelpeklasse for Grid
Lager en hjelpeklasser for lasere
Bytter navn på noen metoder i Board
Legger til en interface for å lettere bruke Tile, Wall og Particle om hverandre
This commit is contained in:
2020-04-28 22:54:23 +02:00
parent 1747f1ea21
commit 4aae3663ad
15 changed files with 316 additions and 283 deletions

View File

@@ -0,0 +1,55 @@
package inf112.fiasko.roborally.utility;
import inf112.fiasko.roborally.elementproperties.Position;
import inf112.fiasko.roborally.objects.BoardElement;
import inf112.fiasko.roborally.objects.BoardElementContainer;
import inf112.fiasko.roborally.objects.Grid;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class containing helper methods fro a grid
*/
public class GridUtil {
/**
* Gets all elements in a grid
*
* @param grid The grid to get elements from
* @param <K> The type of the elements int the grid
* @return A list containing all the elements in the grid
*/
public static <K> List<K> getAllElementsFromGrid(Grid<K> grid) {
List<K> elements = new ArrayList<>();
for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int x = 0; x < grid.getWidth(); x++) {
elements.add(grid.getElement(x, y));
}
}
return elements;
}
/**
* Finds all tiles/walls with a certain type
*
* @param type The type of tile/wall to look for
* @param grid The grid to look through
* @param <K> Type of the type to look for
* @param <T> Type of the grid
* @return List of BoardElementContainers
*/
public static <K, T extends BoardElement> List<BoardElementContainer<T>> getMatchingElements(K type, Grid<T> grid) {
List<BoardElementContainer<T>> containerList = new ArrayList<>();
for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int x = 0; x < grid.getWidth(); x++) {
T gridElement = grid.getElement(x, y);
if (gridElement != null && gridElement.getType() == type) {
containerList.add(new BoardElementContainer<>(gridElement, new Position(x, y)));
}
}
}
return containerList;
}
}

View File

@@ -0,0 +1,127 @@
package inf112.fiasko.roborally.utility;
import inf112.fiasko.roborally.elementproperties.Direction;
import inf112.fiasko.roborally.elementproperties.ParticleType;
import inf112.fiasko.roborally.elementproperties.WallType;
import inf112.fiasko.roborally.objects.Particle;
/**
* Helps with displaying laser beams
*/
public class LaserHelper {
/**
* Gets the correct particle type from a laser type
*
* @param laserType The type of laser firing
* @return The particle representing the laser's beam
*/
public static ParticleType getParticleFromLaserType(WallType laserType) {
switch (laserType) {
case WALL_LASER_SINGLE:
return ParticleType.LASER_BEAM_SINGLE;
case WALL_LASER_DOUBLE:
return ParticleType.LASER_BEAM_DOUBLE;
case WALL_LASER_TRIPLE:
return ParticleType.LASER_BEAM_TRIPLE;
default:
throw new IllegalArgumentException("Invalid laser type encountered.");
}
}
/**
* Gets the new particle to use given the laser firing and the existing beam particle
*
* @param laserBeam The laser beam which is fired
* @param existingBeam The laser beam which already exists at a tile
* @return The particle which is a combination of the two
*/
public static Particle getNewLaserBeamParticle(Particle laserBeam, Particle existingBeam) {
ParticleType laserBeamType = laserBeam.getType();
ParticleType existingBeamType = existingBeam.getType();
Direction laserDirection = laserBeam.getDirection();
Direction existingDirection = existingBeam.getDirection();
int forwardBeamsLaser = getNumberOfForwardBeams(laserBeamType);
int crossingBeamsLaser = getNumberOfPerpendicularBeams(laserBeamType);
int forwardBeamsExisting = getNumberOfForwardBeams(existingBeamType);
int crossingBeamsExisting = getNumberOfPerpendicularBeams(existingBeamType);
//Flip number of beams if beams are perpendicular
if (Direction.arePerpendicular(laserDirection, existingDirection)) {
int temp = forwardBeamsExisting;
forwardBeamsExisting = crossingBeamsExisting;
crossingBeamsExisting = temp;
}
//Calculates the new number of forward beams
int forwardBeams = getNumberOfBeams(forwardBeamsLaser, forwardBeamsExisting);
//Calculates the new number of crossing beams
int crossingBeams = getNumberOfBeams(crossingBeamsLaser, crossingBeamsExisting);
//The direction should be the direction with the least amount of beams
Direction newDirection;
if (forwardBeams > crossingBeams) {
newDirection = existingDirection;
} else {
newDirection = laserDirection;
}
//If using the existing direction and the beams are perpendicular, the direction needs to be rotated
if (newDirection.equals(existingDirection) &&
Direction.arePerpendicular(laserDirection, existingDirection)) {
newDirection = Direction.getLeftRotatedDirection(newDirection);
}
//If the particle does not exist, we have to rotate the beam to get the correct one
ParticleType newParticleType = getNewParticleType(forwardBeams, crossingBeams);
if (newParticleType == null) {
newParticleType = getNewParticleType(crossingBeams, forwardBeams);
newDirection = Direction.getLeftRotatedDirection(newDirection);
}
return new Particle(newParticleType, newDirection);
}
/**
* Gets the correct number of beams given existing beams and the beams to add
*
* @param newBeams The beam count of the new beam to add
* @param existingBeams The beam count of the existing beam
* @return The new number/thickness of beams/the beam
*/
private static int getNumberOfBeams(int newBeams, int existingBeams) {
if ((newBeams + existingBeams) != 0 && (newBeams + existingBeams) % 3 == 0) {
return 3;
} else {
return Math.max(newBeams, existingBeams);
}
}
/**
* Gets a new particle type with the given number of beams
*
* @param forwardBeams The number of beams in the direction of the laser
* @param perpendicularBeams The number of beams in the perpendicular direction of the laser
* @return The correct particle type to be displayed
*/
private static ParticleType getNewParticleType(int forwardBeams, int perpendicularBeams) {
return ParticleType.getParticleTypeFromID(10 * forwardBeams + perpendicularBeams);
}
/**
* Gets the number of beams in the forward direction given a particle type
*
* @param particleType The type of particle to check
* @return The number of beams in the forward direction of the laser beam
*/
private static int getNumberOfForwardBeams(ParticleType particleType) {
return particleType.getParticleTypeID() / 10;
}
/**
* Gets the number of beams in the perpendicular direction given a particle type
*
* @param particleType The type of particle to check
* @return The number of beams in the perpendicular direction of the laser beam
*/
private static int getNumberOfPerpendicularBeams(ParticleType particleType) {
return particleType.getParticleTypeID() % 10;
}
}

View File

@@ -124,7 +124,7 @@ public final class TextureConverterUtil {
}
}
Direction direction = tile.getDirection();
TextureConverterContainer converterContainer = tileSheetTileTextureMappings.get(tile.getTileType());
TextureConverterContainer converterContainer = tileSheetTileTextureMappings.get(tile.getType());
if (converterContainer != null) {
return getDirectionalTextureRegion(direction, converterContainer.getXNorth(),
converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(),
@@ -149,7 +149,7 @@ public final class TextureConverterUtil {
}
}
Direction direction = particle.getDirection();
TextureConverterContainer converterContainer = tileSheetParticleTextureMappings.get(particle.getParticleType());
TextureConverterContainer converterContainer = tileSheetParticleTextureMappings.get(particle.getType());
if (converterContainer != null) {
return getDirectionalTextureRegion(direction, converterContainer.getXNorth(),
converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(),
@@ -177,7 +177,7 @@ public final class TextureConverterUtil {
return null;
}
Direction direction = wall.getDirection();
TextureConverterContainer converterContainer = tileSheetWallTextureMappings.get(wall.getWallType());
TextureConverterContainer converterContainer = tileSheetWallTextureMappings.get(wall.getType());
if (converterContainer != null) {
return getDirectionalTextureRegion(direction, converterContainer.getXNorth(),
converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(),
@@ -242,7 +242,7 @@ public final class TextureConverterUtil {
e.printStackTrace();
}
}
return tileSheetTileHasRotatedTextureMappings.get(tile.getTileType());
return tileSheetTileHasRotatedTextureMappings.get(tile.getType());
}
/**
@@ -261,7 +261,7 @@ public final class TextureConverterUtil {
e.printStackTrace();
}
}
return tileSheetWallHasRotatedTextureMappings.get(wall.getWallType());
return tileSheetWallHasRotatedTextureMappings.get(wall.getType());
}
/**
@@ -280,7 +280,7 @@ public final class TextureConverterUtil {
e.printStackTrace();
}
}
return tileSheetParticleHasRotatedTextureMappings.get(particle.getParticleType());
return tileSheetParticleHasRotatedTextureMappings.get(particle.getType());
}
/**