Skip to content
Snippets Groups Projects
Commit 6a38f776 authored by Magnus Segtnan Skjølberg's avatar Magnus Segtnan Skjølberg
Browse files

(#18): add camera for hud and world + scaling of camera

parent e1ef339e
No related branches found
No related tags found
3 merge requests!51Resolve "Adjust cannon angle from touch input",!35Draft: Resolve "Adjust cannon angle from touch input",!32Combine hud with gameplay
......@@ -6,19 +6,13 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.game.tankwars.TankWarsGame;
import com.game.tankwars.controller.GameController;
......@@ -29,8 +23,7 @@ import com.game.tankwars.model.Terrain;
public class GameScreen implements Screen {
final TankWarsGame tankWarsGame;
int VIEWPORT_WIDTH;
int VIEWPORT_HEIGHT;
int horizontalScaling;
int verticalScaling;
SpriteBatch batch;
......@@ -40,7 +33,8 @@ public class GameScreen implements Screen {
Box2dWorld model;
World world;
Terrain terrain;
OrthographicCamera cam;
OrthographicCamera worldCam;
OrthographicCamera hudCam;
Box2DDebugRenderer debugRenderer;
Bullet bullet;
GameController controller;
......@@ -49,30 +43,34 @@ public class GameScreen implements Screen {
public GameScreen(final TankWarsGame tankWarsGame){
this.tankWarsGame = tankWarsGame;
VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight();
VIEWPORT_WIDTH = tankWarsGame.getViewportWidth();
batch = new SpriteBatch();
shapeRender = new ShapeRenderer();
model = new Box2dWorld();
world = Box2dWorld.getWorld();
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
cam.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0);
cam.update();
worldCam = new OrthographicCamera(scale(TankWarsGame.GAMEPORT_WIDTH), scale(TankWarsGame.GAMEPORT_HEIGHT));
worldCam.position.set(scale(TankWarsGame.GAMEPORT_WIDTH)/2, scale(TankWarsGame.GAMEPORT_HEIGHT)/2, 0);
hudCam = new OrthographicCamera(TankWarsGame.GAMEPORT_WIDTH, TankWarsGame.GAMEPORT_HEIGHT);
hudCam.position.set(TankWarsGame.GAMEPORT_WIDTH/2, TankWarsGame.GAMEPORT_HEIGHT/2, 0);
worldCam.update();
hudCam.update();
debugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true);
terrain = new Terrain();
int initPos = 50;
tank = new Tank(initPos, new Texture("tank-khaki.png"), new Texture("cannon.png"),terrain, tankWarsGame);
horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
/*horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;*/
hud = new GameHud(new FitViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, cam), batch);
hud = new GameHud(new FitViewport(TankWarsGame.GAMEPORT_WIDTH, TankWarsGame.GAMEPORT_HEIGHT, hudCam), batch);
Gdx.input.setInputProcessor(hud.getStage());
controller = new GameController(tankWarsGame, hud, tank);
Gdx.input.setInputProcessor(hud.getStage());
controller.handleHudEvents();
}
@Override
......@@ -80,8 +78,8 @@ public class GameScreen implements Screen {
model.logicStep(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(0, 0, 100, 100);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, cam.combined);
shapeRender.setProjectionMatrix(cam.combined);
debugRenderer.render(world, worldCam.combined);
shapeRender.setProjectionMatrix(worldCam.combined);
controller.checkKeyInput();
......@@ -93,7 +91,7 @@ public class GameScreen implements Screen {
Sprite s = (Sprite) b.getUserData();
if (s != null) {
s.setPosition(b.getPosition().x * (float) horizontalScaling - s.getWidth() / 2, (b.getPosition().y + 0.25f) * (float) verticalScaling);
s.setPosition(b.getPosition().x * (float) TankWarsGame.SCALE - s.getWidth() / 2, (b.getPosition().y + 0.25f) * (float) TankWarsGame.SCALE);
if (s.equals(tank.getChassisSprite())) {
s.setRotation(tank.getAngle());
}
......@@ -102,8 +100,7 @@ public class GameScreen implements Screen {
s.setRotation(tank.getCannonAngle());
}
}
hud.getStage().draw();
}
shapeRender.begin(ShapeRenderer.ShapeType.Filled);
terrain.draw(shapeRender);
......@@ -113,12 +110,16 @@ public class GameScreen implements Screen {
tank.getChassisSprite().draw(batch);
tank.getCannonSprite().draw(batch);
batch.end();
batch.setProjectionMatrix(hud.getStage().getCamera().combined);
hud.getStage().draw();
}
@Override
public void show() {
}
@Override
public void resize(int width, int height) {
......@@ -147,4 +148,8 @@ public class GameScreen implements Screen {
batch.dispose();
shapeRender.dispose();
}
private float scale(float value) {
return value / TankWarsGame.SCALE;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment