made camera take into account map size

This commit is contained in:
Sem van der Hoeven
2020-05-18 16:04:17 +02:00
parent d70e39e172
commit 3e6bf5223f
2 changed files with 51 additions and 16 deletions

View File

@@ -39,8 +39,6 @@ public class MainGame extends ApplicationAdapter {
screenHeight = Gdx.graphics.getHeight();
frameRate = new FrameRate();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.translate(0, 0);
camera.update();
String[] strings = new String[]{
@@ -62,7 +60,10 @@ public class MainGame extends ApplicationAdapter {
gameInputProcessor = new GameInputProcessor(camera, this);
Gdx.input.setInputProcessor(gameInputProcessor);
mapRenderer = new MapRenderer(map, 32, batch, camera);
camera.translate(screenWidth/2,screenHeight/2);
camera.position.set(screenWidth/2,screenHeight/2,0);
camera.viewportWidth = screenWidth / 2;
camera.viewportHeight = screenHeight / 2;
camera.update();
// playSong();
@@ -130,7 +131,19 @@ public class MainGame extends ApplicationAdapter {
batch.dispose();
}
private void keyDown() {
public float getScreenWidth() {
return screenWidth;
}
public float getScreenHeight() {
return screenHeight;
}
public int getVerticalTileAmount() {
return map.getHeight();
}
public int getHorizontalTileAmount() {
return map.getWidth();
}
}

View File

@@ -3,6 +3,7 @@ package netwerkprog.game.client.map;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.TimeUtils;
import netwerkprog.game.MainGame;
@@ -35,6 +36,14 @@ public class GameInputProcessor implements InputProcessor {
keysList.add(Input.Keys.S);
keysList.add(Input.Keys.D);
camera.zoom = MathUtils.clamp(camera.zoom, 1.5f, 1.8f);
//
// float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
// float effectiveViewportHeight = camera.viewportHeight * camera.zoom;
//
// camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth / 2f, game.getScreenWidth() - effectiveViewportWidth / 2f);
// camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight / 2f, game.getScreenHeight() - effectiveViewportHeight / 2f);
}
public boolean isWPressed() {
@@ -79,6 +88,7 @@ public class GameInputProcessor implements InputProcessor {
@Override
public boolean keyUp(int keycode) {
System.out.println(camera.position.x + " , " + camera.position.y);
if (keysList.contains(keycode)) {
if (keycode == keysList.get(0)) {
this.isWPressed = false;
@@ -96,6 +106,7 @@ public class GameInputProcessor implements InputProcessor {
this.isDPressed = false;
return true;
}
return true;
}
return false;
@@ -128,23 +139,34 @@ public class GameInputProcessor implements InputProcessor {
@Override
public boolean scrolled(int amount) {
float nextZoom = camera.zoom + amount / 5f;
if (nextZoom >= 0.5 && nextZoom <= 2.5) {
camera.zoom += amount / 2f;
return true;
}
return false;
}
public void update() {
long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
lastTimeCounted = TimeUtils.millis();
if (isWPressed()) {
camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0);
}
if (isSPressed()) {
camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0);
}
if (isAPressed()) {
camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0);
}
if (isDPressed()) {
camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0);
}
if (camera.position.x > 5 * game.getHorizontalTileAmount())
if (isAPressed()) {
camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0);
}
if (camera.position.y < 30 * game.getVerticalTileAmount())
if (isWPressed()) {
camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0);
}
if (camera.position.y > 5 * game.getVerticalTileAmount())
if (isSPressed()) {
camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0);
}
if (camera.position.x < game.getScreenWidth() / 2 + 5 * game.getHorizontalTileAmount())
if (isDPressed()) {
camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0);
}
}
}