Sletter ubrukt kode og omstruktuerer klasser

Fjerner AppTest.java
Fjerner HelloWorld.java
Fjerner GameBoard.java
Flytter alle filer fra inf112.skeleton.app til inf112.fiasko.roborally
Flytter IDrawableGame og Game til en egen pakke
Flytter IDrawableObject og DrawableObject til en egen pakke
Flytter GameTexture til en egen pakke
This commit is contained in:
2020-02-04 17:52:17 +01:00
parent d73e027e51
commit 2d40d9fd21
12 changed files with 27 additions and 144 deletions

View File

@ -0,0 +1,98 @@
package inf112.fiasko.roborally;
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;
import inf112.fiasko.roborally.abstractions.GameTexture;
import inf112.fiasko.roborally.game.Game;
import inf112.fiasko.roborally.game.IDrawableGame;
import inf112.fiasko.roborally.objects.IDrawableObject;
/**
* This class renders a game using libgdx
*/
public class GameLauncher extends ApplicationAdapter {
private OrthographicCamera camera;
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());
batch = new SpriteBatch();
}
/**
* Renders all textures necessary to display a game
*/
public void render() {
Gdx.gl.glClearColor(0,0,0.2f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
//Renders all elements the game wants to render
for (IDrawableObject object : game.objectsToRender()) {
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, objectTexture.getWidth(), objectTexture.getHeight(), object.flipX(),
object.flipY());
}
batch.end();
}
@Override
public void dispose() {
robotTexture.dispose();
tileTexture.dispose();
walledTileTexture.dispose();
doublyWalledTileTexture.dispose();
slowTransportBandTexture.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,16 @@
package inf112.fiasko.roborally;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Game Board";
cfg.width = 768;
cfg.height = 769;
new LwjglApplication(new GameLauncher(), cfg);
}
}

View File

@ -0,0 +1,12 @@
package inf112.fiasko.roborally.abstractions;
/**
* This enum represents a drawable texture
*/
public enum GameTexture {
ROBOT,
TILE,
WALLED_TILE,
DOUBLY_WALLED_TILE,
SLOW_TRANSPORT_BAND
}

View File

@ -0,0 +1,50 @@
package inf112.fiasko.roborally.game;
import inf112.fiasko.roborally.abstractions.GameTexture;
import inf112.fiasko.roborally.objects.DrawableObject;
import inf112.fiasko.roborally.objects.IDrawableObject;
import java.util.ArrayList;
import java.util.List;
/**
* This class represent a game which is drawable using libgdx
*/
public class Game implements IDrawableGame {
private final int TILE_SIZE = 64;
private final int TILE_NUMBER = 12;
private final int BOARD_WIDTH = TILE_SIZE * TILE_NUMBER;
private final int BOARD_HEIGHT = TILE_SIZE * TILE_NUMBER;
/**
* Instantiates a new Game object
*/
public Game () {
}
@Override
public int getWidth() {
return BOARD_WIDTH;
}
@Override
public int getHeight() {
return BOARD_HEIGHT;
}
@Override
public List<IDrawableObject> objectsToRender() {
List<IDrawableObject> list = new ArrayList<>();
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
DrawableObject tileObj = new DrawableObject(i * 64, j * 64, GameTexture.TILE);
list.add(tileObj);
}
}
DrawableObject roboObj = new DrawableObject(128,128, GameTexture.ROBOT);
list.add(roboObj);
return list;
}
}

View File

@ -0,0 +1,30 @@
package inf112.fiasko.roborally.game;
import inf112.fiasko.roborally.objects.IDrawableObject;
import java.util.List;
/**
* This interface describes a game drawable using libgdx
*/
public interface IDrawableGame {
/**
* Gets the screen width of the game
* @return A positive integer
*/
int getWidth();
/**
* Gets the screen height of the game
* @return A positive integer
*/
int getHeight();
/**
* Gets a list of objects which are to be rendered
* @return A list of drawable objects in the order they are to be drawn
*/
List<IDrawableObject> objectsToRender();
}

View File

@ -0,0 +1,91 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.abstractions.GameTexture;
/**
* This class represents an object that can be drawn using libgdx
*/
public class DrawableObject implements IDrawableObject {
private int xPos;
private int yPos;
private int width = 64;
private int height = 64;
private int rotation = 0;
private GameTexture texture;
private boolean flipX = false;
private boolean flipY = false;
/**
* Initializes a drawable object
* @param xPos The pixel to start drawing on for the x axis
* @param yPos The pixel to start drawing on for the y axis
* @param texture The texture to use for drawing the element
* @param rotation The amount of degrees to rotate the element counterclockwise
* @param width The width of the element
* @param height The height of the element
* @param flipX Whether to flip/mirror the element over the x axis
* @param flipY Whether to flip/mirror the element over the y axis
*/
public DrawableObject(int xPos, int yPos, GameTexture texture, int rotation, int width, int height, boolean flipX, boolean flipY) {
this.xPos = xPos;
this.yPos = yPos;
this.rotation = rotation;
this.texture = texture;
this.width = width;
this.height = height;
this.flipX = flipX;
this.flipY = flipY;
}
/**
* Initializes a new drawable object
* @param xPos The pixel to start drawing on for the x axis
* @param yPos The pixel to start drawing on for the y axis
* @param texture The texture to use for drawing the element
*/
public DrawableObject(int xPos, int yPos, GameTexture texture) {
this.xPos = xPos;
this.yPos = yPos;
this.texture = texture;
}
@Override
public GameTexture getTexture() {
return texture;
}
@Override
public int getXPosition() {
return xPos;
}
@Override
public int getYPosition() {
return yPos;
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public int getRotation() {
return rotation;
}
@Override
public boolean flipX() {
return flipX;
}
@Override
public boolean flipY() {
return flipY;
}
}

View File

@ -0,0 +1,66 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.abstractions.GameTexture;
/**
* This interface describes an object drawable using libgdx
*/
public interface IDrawableObject {
/**
* Gets the texture to use for drawing the object
* @return The texture of the object
*/
GameTexture getTexture();
/**
* Gets the x position the object should be drawn on
*
* The x position should be in terms of the actual pixel position on the rendered game, not the position according
* to the game tile. E.g. (128,64) not (2,1).
*
* @return An x position libgdx
*/
int getXPosition();
/**
* Gets the y position the object should be drawn on
*
* The y position should be in terms of the actual pixel position on the rendered game, not the position according
* to the game tile. E.g. (128,64) not (2,1).
*
* @return An x position libgdx
*/
int getYPosition();
/**
* Gets the width of the object
* @return A positive integer
*/
int getWidth();
/**
* Gets the height of the object
* @return A positive integer
*/
int getHeight();
/**
* Gets the number of degrees to rotate the texture counterclockwise when rendering
* @return An integer
*/
int getRotation();
/**
* Whether to flip the texture on the x-axis when rendering
* @return True if the texture is to be flipped. False otherwise
*/
boolean flipX();
/**
* Whether to flip the texture on the y-axis when rendering
* @return True if the texture is to be flipped. False otherwise
*/
boolean flipY();
}