projet-programmation-orient.../core/src/sagittarius/view/GameScreen.java

147 lines
3.8 KiB
Java
Raw Normal View History

package sagittarius.view;
2021-04-05 16:45:57 +00:00
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
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.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
class GameScreen extends ScreenAdapter {
// ---------- ATTRIBUTES ----------
// screen size
static final int WIDTH = 1920;
static final int HEIGHT = 1080;
// drawing stuff
private SpriteBatch batch;
private ShapeRenderer shapeRenderer;
private BitmapFont font;
// camera stuff
static Viewport viewport; // TODO : useless ?
2021-04-06 20:59:10 +00:00
protected static Camera camera;
2021-04-05 16:45:57 +00:00
// TODO: categorize better ?
private HUD hud = new HUD();
// ---------- METHODS ----------
private void clearScreen() {
Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
private void update(float deltaTime) {
SagittariusGame.update(deltaTime);
2021-04-05 16:45:57 +00:00
hud.update();
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
2021-04-05 16:45:57 +00:00
camera.update();
}
@Override
public void render(float deltaTime) {
2021-04-05 16:45:57 +00:00
update(deltaTime);
2021-04-05 16:45:57 +00:00
clearScreen();
// ---------- batch ----------
batch.setProjectionMatrix(camera.projection);
batch.setTransformMatrix(camera.view);
2021-04-05 16:45:57 +00:00
batch.begin();
2021-04-05 18:42:51 +00:00
// planets
for (Planet planet : SagittariusGame.planetList) {
planet.renderDebug(batch, font);
}
2021-04-06 20:59:10 +00:00
// moons
for (Moon moon : SagittariusGame.moonList) {
moon.renderDebug(batch, font);
}
2021-04-05 18:42:51 +00:00
// players
for (Player player : SagittariusGame.playerList) {
player.renderDebug(batch, font);
}
// arrows
for (Arrow arrow : SagittariusGame.arrowList) {
arrow.renderDebug(batch, font);
}
2021-04-05 16:45:57 +00:00
batch.end();
// ---------- shapeRenderer ----------
2021-04-05 16:45:57 +00:00
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
// planets
for (Planet planet : SagittariusGame.planetList) {
planet.render(shapeRenderer);
}
2021-04-05 16:45:57 +00:00
2021-04-05 18:42:51 +00:00
// players
for (Player player : SagittariusGame.playerList) {
player.render(shapeRenderer);
}
// arrows
for (Arrow arrow : SagittariusGame.arrowList) {
arrow.render(shapeRenderer);
}
2021-04-06 20:59:10 +00:00
// moons
for (Moon moon : SagittariusGame.moonList) {
moon.render(shapeRenderer);
}
2021-04-06 10:58:27 +00:00
// float[] vertices = {100, 100, 200, 200, 1000, 500};
// shapeRenderer.polyline(vertices);
2021-04-05 16:45:57 +00:00
shapeRenderer.end();
// HUD
hud.render();
2021-04-05 16:45:57 +00:00
}
@Override
public void show() {
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
camera.update();
viewport = new FitViewport(WIDTH, HEIGHT, camera);
shapeRenderer = new ShapeRenderer();
batch = new SpriteBatch();
font = new BitmapFont();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.update();
batch.setProjectionMatrix(camera.combined);
shapeRenderer.setProjectionMatrix(camera.combined);
hud.resize();
2021-04-05 16:45:57 +00:00
}
@Override
public void dispose() {
batch.dispose();
font.dispose();
shapeRenderer.dispose();
}
}