diff --git a/src/main/java/inf112/skeleton/app/Game.java b/src/main/java/inf112/skeleton/app/Game.java index 0181e9d..3bbbe51 100644 --- a/src/main/java/inf112/skeleton/app/Game.java +++ b/src/main/java/inf112/skeleton/app/Game.java @@ -1,8 +1,5 @@ package inf112.skeleton.app; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Texture; - import java.util.List; /** @@ -14,22 +11,13 @@ public class Game implements IDrawableGame { private final int BOARD_WIDTH = TILE_SIZE * TILE_NUMBER; private final int BOARD_HEIGHT = TILE_SIZE * TILE_NUMBER; - private Texture robotTexture; - private Texture tileTexture; - private Texture walledTileTexture; - private Texture doublyWalledTileTexture; - private Texture slowTransportBandTexture; + /** * Instantiates a new Game object */ public Game () { - //Loads some textures - robotTexture = new Texture(Gdx.files.internal("assets/Robot.png")); - tileTexture = new Texture(Gdx.files.internal("assets/Tile.png")); - walledTileTexture = new Texture(Gdx.files.internal("assets/WalledTile.png")); - doublyWalledTileTexture = new Texture(Gdx.files.internal("assets/DoublyWalledTile.png")); - slowTransportBandTexture = new Texture(Gdx.files.internal("assets/TransportBandSlow.png")); + } @Override diff --git a/src/main/java/inf112/skeleton/app/GameLauncher.java b/src/main/java/inf112/skeleton/app/GameLauncher.java index 04f575c..422cfca 100644 --- a/src/main/java/inf112/skeleton/app/GameLauncher.java +++ b/src/main/java/inf112/skeleton/app/GameLauncher.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; /** @@ -14,8 +15,21 @@ public class GameLauncher extends ApplicationAdapter { private SpriteBatch batch; private IDrawableGame game; + private Texture robotTexture; + private Texture tileTexture; + private Texture walledTileTexture; + private Texture doublyWalledTileTexture; + private Texture slowTransportBandTexture; + @Override public void create() { + //Loads some textures + robotTexture = new Texture(Gdx.files.internal("assets/Robot.png")); + tileTexture = new Texture(Gdx.files.internal("assets/Tile.png")); + walledTileTexture = new Texture(Gdx.files.internal("assets/WalledTile.png")); + doublyWalledTileTexture = new Texture(Gdx.files.internal("assets/DoublyWalledTile.png")); + slowTransportBandTexture = new Texture(Gdx.files.internal("assets/TransportBandSlow.png")); + game = new Game(); camera = new OrthographicCamera(); camera.setToOrtho(false, game.getWidth(), game.getHeight()); @@ -33,10 +47,11 @@ public class GameLauncher extends ApplicationAdapter { batch.begin(); //Renders all elements the game wants to render for (IDrawableObject object : game.objectsToRender()) { - batch.draw(object.getTexture(), object.getXPosition(), object.getYPosition(), + Texture objectTexture = gameTextureToTexture(object.getTexture()); + batch.draw(objectTexture, object.getXPosition(), object.getYPosition(), (float)object.getWidth()/2, (float)object.getHeight()/2, object.getWidth(), object.getHeight(), 1, 1, object.getRotation(), - 0, 0, object.getTexture().getWidth(), object.getTexture().getHeight(), object.flipX(), + 0, 0, objectTexture.getWidth(), objectTexture.getHeight(), object.flipX(), object.flipY()); } batch.end(); @@ -46,4 +61,29 @@ public class GameLauncher extends ApplicationAdapter { public void dispose() { batch.dispose(); } + + /** + * Turns a GameTexture element into a Texture element + * + * This is necessary to keep all libgdx logic in this class only. Otherwise, testing would be painful. + * + * @param gameTexture A GameTexture enum + * @return A Gdx Texture + */ + private Texture gameTextureToTexture(GameTexture gameTexture) { + switch (gameTexture) { + case ROBOT: + return robotTexture; + case TILE: + return tileTexture; + case WALLED_TILE: + return walledTileTexture; + case DOUBLY_WALLED_TILE: + return doublyWalledTileTexture; + case SLOW_TRANSPORT_BAND: + return slowTransportBandTexture; + default: + throw new IllegalArgumentException("Non existing texture encountered."); + } + } } \ No newline at end of file diff --git a/src/main/java/inf112/skeleton/app/GameTexture.java b/src/main/java/inf112/skeleton/app/GameTexture.java new file mode 100644 index 0000000..05cc5bd --- /dev/null +++ b/src/main/java/inf112/skeleton/app/GameTexture.java @@ -0,0 +1,12 @@ +package inf112.skeleton.app; + +/** + * This enum represents a drawable texture + */ +public enum GameTexture { + ROBOT, + TILE, + WALLED_TILE, + DOUBLY_WALLED_TILE, + SLOW_TRANSPORT_BAND +} diff --git a/src/main/java/inf112/skeleton/app/IDrawableObject.java b/src/main/java/inf112/skeleton/app/IDrawableObject.java index ac4dce7..7359ebc 100644 --- a/src/main/java/inf112/skeleton/app/IDrawableObject.java +++ b/src/main/java/inf112/skeleton/app/IDrawableObject.java @@ -1,7 +1,5 @@ package inf112.skeleton.app; -import com.badlogic.gdx.graphics.Texture; - /** * This interface describes an object drawable using libgdx */ @@ -11,7 +9,7 @@ public interface IDrawableObject { * Gets the texture to use for drawing the object * @return The texture of the object */ - Texture getTexture(); + GameTexture getTexture(); /** * Gets the x position the object should be drawn on diff --git a/src/test/java/inf112/skeleton/app/GameTest.java b/src/test/java/inf112/skeleton/app/GameTest.java new file mode 100644 index 0000000..237fc8d --- /dev/null +++ b/src/test/java/inf112/skeleton/app/GameTest.java @@ -0,0 +1,41 @@ +package inf112.skeleton.app; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +public class GameTest { + IDrawableGame game; + + @Before + public void setUp() { + game = new Game(); + } + + @Test + public void gameWidthPositiveTest() { + assertTrue(game.getWidth() > 0); + } + + @Test + public void gameWidthMaximumFullHDTest() { + assertTrue(game.getWidth() <= 1920); + } + + @Test + public void gameHeightPositiveTest() { + assertTrue(game.getWidth() > 0); + } + + @Test + public void gameHeightMaximumFullHDTest() { + assertTrue(game.getWidth() <= 1080); + } + + @Test + public void getObjectsToRenderTest() { + assertFalse(game.objectsToRender().isEmpty()); + } +}