MenuScreen.java

package com.vikingz.unitycoon.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.vikingz.unitycoon.menus.AchievementsMenu;
import com.vikingz.unitycoon.menus.TutorialMenu;
import com.vikingz.unitycoon.menus.UsernameMenu;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

/**
 * This class represents the main menu of the game.
 *
 * The main menu is where the user begins from. This menu contains multiple buttons that allow the user 
 * to begin the game.
 *
 * Inherits Screen, SuperScreen
 * 
 * This class has been refactored slightly to make the code more readable and improve UI.
 */
public class MenuScreen extends SuperScreen implements Screen {

    /**
     * Creates a new menu screen
     */
    public MenuScreen() {
        Gdx.input.setInputProcessor(stage);

        // Create buttons
        TextButton playButton = new TextButton("Play", skin);
        TextButton howToPlayButton = new TextButton("How To Play", skin);
        TextButton achievementsButton = new TextButton("Achievements",skin);
        TextButton settingsButton = new TextButton("Settings", skin);
        TextButton quitButton = new TextButton("Quit", skin);

        AchievementsMenu achievementsMenu = new AchievementsMenu(skin);
        
        TutorialMenu tutorialMenu = new TutorialMenu(skin);
        tutorialMenu.setPosition((stage.getWidth() - tutorialMenu.getWidth()) / 2, (stage.getHeight() - tutorialMenu.getHeight()) / 2);
        tutorialMenu.setupButton(skin);

        // Add listeners to buttons
        playButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                ScreenMultiplexer.switchScreens(ScreenMultiplexer.Screens.MAPSELECTION);
            };
        });

        achievementsButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                achievementsMenu.setPosition((stage.getWidth() - achievementsMenu.getWidth()) / 2, (stage.getHeight() - achievementsMenu.getHeight()) / 2);
                achievementsMenu.update();
                stage.addActor(achievementsMenu);
                };
         });
      
        howToPlayButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                stage.addActor(tutorialMenu);
            };
        });

        settingsButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                ScreenMultiplexer.switchScreens(ScreenMultiplexer.Screens.SETTINGS);
            };
        });

        quitButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.exit(); // Quit the application
            };
        });

        // Create a table for layout
        Table table = new Table();
        table.setFillParent(true);  // Center table on stage
        table.center();

        Image texture = new Image(new Texture(Gdx.files.internal("gameLogo.png")));
        table.add(texture).pad(50);
        table.row();

        // Add buttons to table with 2 rows of 2 buttons and quit at the bottom
        Table buttonRows = new Table();
        buttonRows.add(playButton).width(425).pad(10);
        buttonRows.add(howToPlayButton).width(425).pad(10);
        buttonRows.row();
        buttonRows.add(achievementsButton).width(425).pad(10);
        buttonRows.add(settingsButton).width(425).pad(10);
        
        table.add(buttonRows).pad(10).row();
        table.add(quitButton).width(425).pad(10);

        // Add the table to the stage
        stage.addActor(table);

        // Opens a username screen if it hasn't already been entered
        if (UsernameMenu.getUsername() == "") {
            UsernameMenu usernamePopUp = new UsernameMenu(skin);
            usernamePopUp.setPosition((stage.getWidth() - usernamePopUp.getWidth()) / 2, (stage.getHeight() - usernamePopUp.getHeight()) / 2);
            usernamePopUp.setupButton();
            stage.addActor(usernamePopUp);
        }
    }

    @Override
    public void show() {}

    @Override
    public void render(float delta) {
        // Clear the screen
        Gdx.gl.glClearColor(25/255f, 25/255f, 25/255f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the screen

        // Draw the stage
        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        // Update the stage's viewport when the screen size changes
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void pause() { }

    @Override
    public void resume() { }

    @Override
    public void hide() {}

    /**
     * Disposes MenuScreen for garbage collection.
     */
    @Override
    public void dispose() {
        // Dispose of assets when this screen is no longer used
        stage.dispose();
        skin.dispose();
    }
}