Diverse forbedringer til GameLauncher

Fjerner funksjon som ikke lenger trengs
Flytter en del kode ut til egne metoder
Legger til en viewport som hindrer at spillet blir strukket når skjermen endrer seg
Legger til anti aliasing
Scrolling zoomer lengre ut
Scrolling zoomer dobbelt så fort
This commit is contained in:
Kristian Knarvik 2020-02-28 23:43:38 +01:00
parent 9a5a465f11
commit 983c27f745
2 changed files with 44 additions and 56 deletions

View File

@ -6,11 +6,12 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
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 com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.game.Game;
import inf112.fiasko.roborally.game.IDrawableGame;
import inf112.fiasko.roborally.objects.IDrawableObject;
@ -27,9 +28,6 @@ public class GameLauncher extends ApplicationAdapter implements InputProcessor {
private IDrawableGame game;
private IDrawableGame debugGame;
private Texture robotTexture;
private Texture textureSheet;
private final int tileDimensions = 64;
private float cameraZoom = 1;
private int cameraX = 0;
@ -37,39 +35,47 @@ public class GameLauncher extends ApplicationAdapter implements InputProcessor {
private Vector2 lastTouch;
private final int viewPortWidth = 12 * tileDimensions;
private final int viewPortHeight = 12 * tileDimensions;
private Viewport viewport;
@Override
public void create() {
//Loads some textures
robotTexture = new Texture(Gdx.files.internal("assets/Robot.png"));
textureSheet = new Texture(Gdx.files.internal("assets/tiles.png"));
debugGame = new Game(true);
game = new Game(false);
camera = new OrthographicCamera();
camera.setToOrtho(false, viewPortWidth, viewPortHeight);
camera.position.set(viewPortWidth/2f, viewPortHeight/2f, 0);
batch = new SpriteBatch();
/*MyTextInputListener listener = new MyTextInputListener();
Gdx.input.getTextInput(listener, "Input name", "", "Name");*/
viewport = new ExtendViewport(viewPortWidth, viewPortHeight, camera);
Gdx.input.setInputProcessor(this);
batch = new SpriteBatch();
lastTouch = new Vector2();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
/**
* 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();
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT |
(Gdx.graphics.getBufferFormat().coverageSampling?GL20.GL_COVERAGE_BUFFER_BIT_NV:0));
updateCamera();
batch.setProjectionMatrix(camera.combined);
batch.begin();
camera.translate(cameraX, cameraY);
cameraX = 0;
cameraY = 0;
camera.zoom = cameraZoom;
//Draws all elements the game wants to draw
drawBoard(batch);
batch.end();
}
/**
* Renders all drawable objects on the board
* @param batch The sprite batch to use for drawing
*/
private void drawBoard(SpriteBatch batch) {
List<IDrawableObject> elementsToDraw = IOUtil.getDrawableObjectsFromGame(game, tileDimensions, tileDimensions);
for (IDrawableObject object : elementsToDraw) {
TextureRegion objectTextureRegion = object.getTexture();
@ -80,13 +86,21 @@ public class GameLauncher extends ApplicationAdapter implements InputProcessor {
objectTextureRegion.getRegionWidth(), objectTextureRegion.getRegionHeight(),
object.flipX(), object.flipY());
}
batch.end();
}
/**
* Updates the camera according to any user input
*/
private void updateCamera() {
camera.translate(cameraX, cameraY);
cameraX = 0;
cameraY = 0;
camera.zoom = cameraZoom;
camera.update();
}
@Override
public void dispose() {
robotTexture.dispose();
textureSheet.dispose();
batch.dispose();
}
@ -97,26 +111,11 @@ public class GameLauncher extends ApplicationAdapter implements InputProcessor {
@Override
public boolean keyUp(int keycode) {
switch (keycode) {
case Input.Keys.PLUS:
if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
cameraZoom -= 0.1;
return true;
}
break;
case Input.Keys.MINUS:
if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
cameraZoom += 0.1;
return true;
}
break;
case Input.Keys.HOME:
IDrawableGame temp = game;
this.game = debugGame;
this.debugGame = temp;
return true;
default:
return false;
if (keycode == Input.Keys.HOME) {
IDrawableGame temp = game;
this.game = debugGame;
this.debugGame = temp;
return true;
}
return false;
}
@ -133,7 +132,6 @@ public class GameLauncher extends ApplicationAdapter implements InputProcessor {
camera.up.x = 0;
camera.up.y = 1;
cameraZoom = 1;
System.out.print((game.getHeight() * tileDimensions) - viewPortHeight/2f);
camera.position.set(viewPortWidth/2f, viewPortHeight/2f, 0);
}
return false;
@ -196,20 +194,9 @@ public class GameLauncher extends ApplicationAdapter implements InputProcessor {
@Override
public boolean scrolled(int amount) {
if (amount < 0 && cameraZoom > 0 || amount > 0 && cameraZoom < 2) {
cameraZoom += amount / 10.0;
if (amount < 0 && cameraZoom > 0.3 || amount > 0 && cameraZoom < 3) {
cameraZoom += 0.2 * amount;
}
return true;
}
/*public static class MyTextInputListener implements Input.TextInputListener {
@Override
public void input (String text) {
System.out.println(text);
}
@Override
public void canceled () {
}
}*/
}

View File

@ -10,6 +10,7 @@ public class Main {
cfg.title = "Game Board";
cfg.width = 900;
cfg.height = 900;
cfg.samples = 3;
new LwjglApplication(new GameLauncher(), cfg);
}