Skip to content
Snippets Groups Projects
Commit 69a5ea69 authored by Sander Østrem Fagernes's avatar Sander Østrem Fagernes
Browse files

feat: Dummy background, bigger logo

Issue #14
parent 2c1159a0
No related branches found
No related tags found
1 merge request!1314 Main menu screen setup
Pipeline #204243 passed
frontend/assets/ZyWDE4.jpg

952 KiB

package com.game.tankwars.controller;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.game.tankwars.TankWarsGame;
import com.game.tankwars.model.MenuButton;
import com.game.tankwars.view.GameScreen;
public class MainMenuController {
private final TankWarsGame tankWarsGame;
public MainMenuController(final TankWarsGame tankWarsGame) {
this.tankWarsGame = tankWarsGame;
}
public void handleInput(Vector3 touchPos, Array<MenuButton> menuButtons) {
for (MenuButton menuButton : menuButtons) {
if (touchPos.x >= menuButton.getX() && touchPos.x <= menuButton.getX() + menuButton.getWidth() &&
touchPos.y >= menuButton.getY() && touchPos.y <= menuButton.getY() + menuButton.getHeight()) {
switch (menuButton.getId()) {
case 0: tankWarsGame.setScreen(new GameScreen(tankWarsGame)); break;
case 1: System.out.println("Leaderboard button: Not yet functional"); break;
case 2: System.out.println("Settings button: Not yet functional"); break;
}
}
}
}
}
......@@ -7,22 +7,69 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MenuButton {
private final Texture buttonTexture;
private static final Texture buttonTexture = new Texture("menu-button.png");
private final SpriteBatch batch;
private final BitmapFont font;
private final int id;
private final String content;
private final float x, y, width, height;
private final GlyphLayout contentLayout;
public MenuButton() {
this.buttonTexture = new Texture("menu-button.png");
public MenuButton(SpriteBatch batch, BitmapFont font, int id, String content, float x, float y) {
this.batch = batch;
this.font = font;
this.id = id;
this.content = content;
this.x = x - buttonTexture.getWidth() / 2f;
this.y = y - buttonTexture.getHeight() / 2f;
this.width = buttonTexture.getWidth();
this.height = buttonTexture.getHeight();
this.contentLayout = new GlyphLayout();
}
public void draw(SpriteBatch batch, BitmapFont font, String content, float x, float y) {
public void draw() {
if (batch.isDrawing()) {
font.getData().setScale(0.6f);
contentLayout.setText(font, content);
batch.draw(buttonTexture, x - buttonTexture.getWidth() / 2, y - buttonTexture.getHeight() / 2);
font.draw(batch, content, x - contentLayout.width / 2, y + contentLayout.height / 4);
batch.draw(buttonTexture, x, y);
font.draw(batch, content, x + width / 2 - contentLayout.width / 2, y + height / 2 + contentLayout.height / 4);
}
}
public static void dispose() {
buttonTexture.dispose();
}
/**
* Static in order to only load the texture once
*
* @return The button's texture
*/
public static Texture getTexture() {
return buttonTexture;
}
public int getId() {
return id;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}
......@@ -6,10 +6,13 @@ 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.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
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.game.tankwars.TankWarsGame;
import com.game.tankwars.controller.MainMenuController;
import com.game.tankwars.model.MenuButton;
public class MainMenuScreen implements Screen {
......@@ -18,9 +21,13 @@ public class MainMenuScreen implements Screen {
private final SpriteBatch batch;
private final BitmapFont font;
private final Viewport viewport;
private final MainMenuController controller;
private final Texture logo;
private final MenuButton menuButton;
private final Texture background;
private final Array<MenuButton> menuButtons;
private final Vector3 touchPos;
public MainMenuScreen(final TankWarsGame tankWarsGame, final SpriteBatch batch, final BitmapFont font, final OrthographicCamera camera) {
this.tankWarsGame = tankWarsGame;
......@@ -28,9 +35,18 @@ public class MainMenuScreen implements Screen {
this.font = font;
this.viewport = new FitViewport(224, 500, camera);
this.controller = new MainMenuController(tankWarsGame);
this.logo = new Texture("tankwars-logo.png");
this.menuButton = new MenuButton();
// TODO: Create our own pixel art background. ZyWDE4.jpg is a dummy background image
this.background = new Texture("ZyWDE4.jpg");
this.menuButtons = new Array<>();
this.menuButtons.add(new MenuButton(batch, font, 0, "Find Game", 0, -50 + viewport.getWorldHeight() / 6));
this.menuButtons.add(new MenuButton(batch, font, 1, "Leaderboard", 0, -50));
this.menuButtons.add(new MenuButton(batch, font, 2, "Settings", 0, -50 - viewport.getWorldHeight() / 6));
this.touchPos = new Vector3();
}
@Override
......@@ -40,18 +56,35 @@ public class MainMenuScreen implements Screen {
@Override
public void render(float delta) {
ScreenUtils.clear(.1f, .5f, .8f, 1f);
ScreenUtils.clear(0, 0, 0, 1f);
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.begin();
checkInput();
batch.draw(logo, -logo.getWidth() / 2, viewport.getWorldHeight() / 3);
batch.begin();
float backgroundWidth = background.getWidth() * viewport.getWorldHeight() / background.getHeight();
float backgroundHeight = viewport.getWorldHeight();
batch.draw(background, -viewport.getWorldWidth() / 2 - backgroundWidth / 2, -viewport.getWorldHeight() / 2,
backgroundWidth, backgroundHeight);
// TODO: Find a better way to scale the logo. Perhaps using a Pixmap
float logoScaling = 1.3f;
batch.draw(logo, -logo.getWidth() * logoScaling / 2f, viewport.getWorldHeight() / 3,
logo.getWidth() * logoScaling, logo.getHeight() * logoScaling);
for (MenuButton menuButton : menuButtons) {
menuButton.draw();
}
batch.end();
}
menuButton.draw(batch, font, "Find Game", 0, -50 + viewport.getWorldHeight() / 6);
menuButton.draw(batch, font, "Leaderboard", 0, -50);
menuButton.draw(batch, font, "Settings", 0, -50 - viewport.getWorldHeight() / 6);
private void checkInput() {
if (Gdx.input.justTouched()) {
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
viewport.unproject(touchPos);
batch.end();
controller.handleInput(touchPos, menuButtons);
}
}
@Override
......@@ -76,6 +109,6 @@ public class MainMenuScreen implements Screen {
@Override
public void dispose() {
MenuButton.dispose();
}
}
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