Legger til noen tester og gjør noen endringer for enklere testing

Legger til tester til Game i GameTest
Legger til en enum som kan konverteres til/fra teksturer for å fjerne grafikklogikk fra resten av spillet
This commit is contained in:
Kristian Knarvik 2020-01-31 14:29:22 +01:00
parent b3e6521651
commit 646404bc23
5 changed files with 98 additions and 19 deletions

View File

@ -1,8 +1,5 @@
package inf112.skeleton.app; package inf112.skeleton.app;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import java.util.List; 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_WIDTH = TILE_SIZE * TILE_NUMBER;
private final int BOARD_HEIGHT = 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 * Instantiates a new Game object
*/ */
public Game () { 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 @Override

View File

@ -4,6 +4,7 @@ import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
/** /**
@ -14,8 +15,21 @@ public class GameLauncher extends ApplicationAdapter {
private SpriteBatch batch; private SpriteBatch batch;
private IDrawableGame game; private IDrawableGame game;
private Texture robotTexture;
private Texture tileTexture;
private Texture walledTileTexture;
private Texture doublyWalledTileTexture;
private Texture slowTransportBandTexture;
@Override @Override
public void create() { 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(); game = new Game();
camera = new OrthographicCamera(); camera = new OrthographicCamera();
camera.setToOrtho(false, game.getWidth(), game.getHeight()); camera.setToOrtho(false, game.getWidth(), game.getHeight());
@ -33,10 +47,11 @@ public class GameLauncher extends ApplicationAdapter {
batch.begin(); batch.begin();
//Renders all elements the game wants to render //Renders all elements the game wants to render
for (IDrawableObject object : game.objectsToRender()) { 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(), (float)object.getWidth()/2, (float)object.getHeight()/2, object.getWidth(),
object.getHeight(), 1, 1, object.getRotation(), 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()); object.flipY());
} }
batch.end(); batch.end();
@ -46,4 +61,29 @@ public class GameLauncher extends ApplicationAdapter {
public void dispose() { public void dispose() {
batch.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.");
}
}
} }

View File

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

View File

@ -1,7 +1,5 @@
package inf112.skeleton.app; package inf112.skeleton.app;
import com.badlogic.gdx.graphics.Texture;
/** /**
* This interface describes an object drawable using libgdx * This interface describes an object drawable using libgdx
*/ */
@ -11,7 +9,7 @@ public interface IDrawableObject {
* Gets the texture to use for drawing the object * Gets the texture to use for drawing the object
* @return The texture of the object * @return The texture of the object
*/ */
Texture getTexture(); GameTexture getTexture();
/** /**
* Gets the x position the object should be drawn on * Gets the x position the object should be drawn on

View File

@ -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());
}
}