Legger til nye visningsfunksjoner

This commit is contained in:
Kristian Knarvik 2020-02-26 19:46:54 +01:00
parent 2524b8d585
commit 6a544086e1

View File

@ -3,11 +3,14 @@ package inf112.fiasko.roborally;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
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.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import inf112.fiasko.roborally.game.Game; import inf112.fiasko.roborally.game.Game;
import inf112.fiasko.roborally.game.IDrawableGame; import inf112.fiasko.roborally.game.IDrawableGame;
import inf112.fiasko.roborally.objects.IDrawableObject; import inf112.fiasko.roborally.objects.IDrawableObject;
@ -18,7 +21,7 @@ import java.util.List;
/** /**
* This class renders a game using libgdx * This class renders a game using libgdx
*/ */
public class GameLauncher extends ApplicationAdapter { public class GameLauncher extends ApplicationAdapter implements InputProcessor {
private OrthographicCamera camera; private OrthographicCamera camera;
private SpriteBatch batch; private SpriteBatch batch;
private IDrawableGame game; private IDrawableGame game;
@ -30,6 +33,9 @@ public class GameLauncher extends ApplicationAdapter {
private float cameraZoom = 1; private float cameraZoom = 1;
private int cameraX = 0; private int cameraX = 0;
private int cameraY = 0; private int cameraY = 0;
private Vector2 lastTouch;
private final int viewPortWidth = 12 * tileDimensions;
private final int viewPortHeight = 12 * tileDimensions;
@Override @Override
public void create() { public void create() {
@ -39,11 +45,13 @@ public class GameLauncher extends ApplicationAdapter {
game = new Game(); game = new Game();
camera = new OrthographicCamera(); camera = new OrthographicCamera();
camera.setToOrtho(false, game.getWidth() * tileDimensions, camera.setToOrtho(false, viewPortWidth, viewPortHeight);
game.getHeight() * tileDimensions); camera.position.set(viewPortWidth/2f, viewPortHeight/2f, 0);
batch = new SpriteBatch(); batch = new SpriteBatch();
/*MyTextInputListener listener = new MyTextInputListener(); /*MyTextInputListener listener = new MyTextInputListener();
Gdx.input.getTextInput(listener, "Input name", "", "Name");*/ Gdx.input.getTextInput(listener, "Input name", "", "Name");*/
Gdx.input.setInputProcessor(this);
lastTouch = new Vector2();
} }
/** /**
@ -56,6 +64,8 @@ public class GameLauncher extends ApplicationAdapter {
batch.setProjectionMatrix(camera.combined); batch.setProjectionMatrix(camera.combined);
batch.begin(); batch.begin();
camera.translate(cameraX, cameraY); camera.translate(cameraX, cameraY);
cameraX = 0;
cameraY = 0;
camera.zoom = cameraZoom; camera.zoom = cameraZoom;
//Draws all elements the game wants to draw //Draws all elements the game wants to draw
List<IDrawableObject> elementsToDraw = IOUtil.getDrawableObjectsFromGame(game, tileDimensions, tileDimensions); List<IDrawableObject> elementsToDraw = IOUtil.getDrawableObjectsFromGame(game, tileDimensions, tileDimensions);
@ -69,15 +79,6 @@ public class GameLauncher extends ApplicationAdapter {
object.flipX(), object.flipY()); object.flipX(), object.flipY());
} }
batch.end(); batch.end();
if (Gdx.input.isKeyJustPressed(Input.Keys.CONTROL_LEFT) && Gdx.input.isKeyJustPressed(Input.Keys.PLUS) && cameraZoom > 0) {
cameraZoom -= 0.1;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.CONTROL_LEFT) && Gdx.input.isKeyJustPressed(Input.Keys.MINUS) && cameraZoom < 2) {
cameraZoom += 0.1;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)) {
cameraX += 1;
}
} }
@Override @Override
@ -87,6 +88,107 @@ public class GameLauncher extends ApplicationAdapter {
batch.dispose(); batch.dispose();
} }
@Override
public boolean keyDown(int keycode) {
return false;
}
@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;
}
case Input.Keys.MINUS:
if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
cameraZoom += 0.1;
return true;
}
}
return false;
}
@Override
public boolean keyTyped(char character) {
if (character == 'r') {
//camera.rotate(-90);
camera.rotateAround(
new Vector3(viewPortWidth/2f, viewPortHeight/2f, 0),
new Vector3(0, 0, 1), 90);
return true;
} else if (character == 'q') {
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;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
lastTouch = new Vector2(screenX, screenY);
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
lastTouch = new Vector2(screenX, screenY);
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector2 newTouch = new Vector2(screenX, screenY);
Vector2 diff = newTouch.cpy().sub(lastTouch);
lastTouch = newTouch;
int[] change = translateToDirection(diff.x, diff.y);
cameraX = change[0];
cameraY = change[1];
return true;
}
/**
* Translates x and y coordinates according to the camera's direction
* @param x The x coordinate to translate
* @param y The y coordinate to translate
* @return A list containing the translated coordinates of x and y
*/
private int[] translateToDirection(float x, float y) {
int outX = 0;
int outY = 0;
if (Math.round(camera.up.x) == 0 && Math.round(camera.up.y) == 1) {
outX = (int)-x;
outY = (int)y;
} else if (Math.round(camera.up.x) == 0 && Math.round(camera.up.y) == -1) {
outX = (int)x;
outY = (int)-y;
} else if (Math.round(camera.up.x) == -1 && Math.round(camera.up.y) == 0) {
outX = (int)-y;
outY = (int)-x;
} else if (Math.round(camera.up.x) == 1 && Math.round(camera.up.y) == 0) {
outX = (int)y;
outY = (int)x;
}
return new int[]{outX, outY};
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
if (amount < 0 && cameraZoom > 0 || amount > 0 && cameraZoom < 2) {
cameraZoom += amount / 10.0;
}
return true;
}
/*public static class MyTextInputListener implements Input.TextInputListener { /*public static class MyTextInputListener implements Input.TextInputListener {
@Override @Override
public void input (String text) { public void input (String text) {