Mis on HUD?

HUD (Head-Up Display) - mängu liides, mis kuvab olulist teavet mängu hetkeolukorra kohta, näiteks tervis, punktid, aeg, tase ja muud andmed. HUD on tavaliselt paigutatud ekraani ülemisse või alumisse ossa ning mängija ei pea selle teabe saamiseks sekkuma.

Näide: Minecraft HUD

HUD

Kuidas luua HUD?

libGDX kasutab HUD-i loomiseks standardseid komponente, nagu Stage, ViewPort, Table, Label ja muud.

  • Stage: Kõigi visuaalsete elementide (näitlejate) konteiner. Stage haldab kuvamist ja sisendi töötlemist.

  • Viewport: Koordinaadistik, mis haldab HUD-i kuvamist ekraanil, arvestades ekraani eraldusvõimet ja mõõtkava. Näiteks võib kasutada FitViewport-i, mis kohandab HUD-i erinevatele ekraanisuurustele.

  • Table: Mugav viis UI komponentide paigutamiseks ridadesse ja veergudesse. Table joondab elemendid automaatselt ja haldab nende paigutust.

  • Label: Teksti kuvamiseks (nt taimeri, skoori). Teksti stiili, fonti ja värvi saab seadistada Label.LabelStyle abil.

Samuti võivad kasutada teisi UI komponente, nagu:

  • ImageButton: Piltidega nuppude loomiseks.

  • TextButton: Tavaliste tekstinuppude loomiseks.

Koodinäitena vaatame HUD-klassi loomist Mario mängust, kuid veidi muudetud kujul.

public class Hud {

    public Stage stage;
    private Viewport viewport;
    private Integer worldTimer;
    private float timeCount;
    private static Integer score;
    private Label countdownLabel;
    private static Label scoreLabel;
    private Label timeLabel;
    private Label levelLabel;
    private Label worldLabel;
    Label gamerLabel;
    private int minutes;
    private float totalTime;
    private int seconds;

    public Hud(SpriteBatch sb){
        // initialize variables
        worldTimer = 999;
        totalTime = 300f;
        timeCount = totalTime;
        minutes = (int) (totalTime / 60);
        seconds = (int) (totalTime % 60);
        score = 0;


        // setup the HUD viewport using a new camera seperate from our gamecam
        // define our stage using that viewport and our games spritebatch
        viewport = new FitViewport(game.vWidth, game.vHeight, new OrthographicCamera());

        stage = new Stage(viewport, sb);

        // define a table used to organize our hud's labels
        Table table = new Table();
        // Top-Align table
        table.top();
        // make the table fill the entire stage
        table.setFillParent(true);

        // define our labels using the String, and a Label style consisting of a font and color
        countdownLabel = new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.GREEN));
        scoreLabel =new Label(String.format("%04d", score), new Label.LabelStyle(new BitmapFont(), Color.GREEN));
        timeLabel = new Label("TIME", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        levelLabel = new Label("1", new Label.LabelStyle(new BitmapFont(), Color.GREEN));
        worldLabel = new Label("LEVEL", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        gamerLabel = new Label("SCORE", new Label.LabelStyle(new BitmapFont(), Color.WHITE));

        // add our labels to our table, padding the top, and giving them all equal width with expandX
        table.add(gamerLabel).expandX().padTop(20);
        table.add(worldLabel).expandX().padTop(20);
        table.add(timeLabel).expandX().padTop(20);
        // add a second row to our table
        table.row();
        table.add(scoreLabel).expandX();
        table.add(levelLabel).expandX();
        table.add(countdownLabel).expandX();

        // add our table to the stage
        stage.addActor(table);
    }
    /**
     * Updates the HUD with the given delta time.
     *
     * @param dt The time elapsed since the last frame.
     */
    public void update(float dt) {
        timeCount -= dt/2;

        if (timeCount <= 0) {
            timeCount = 0;
            minutes = 0;
            seconds = 0;
        } else {
            minutes = (int) (timeCount / 60);
            seconds = (int) (timeCount % 60);
            countdownLabel.setText(String.format("%02d:%02d", minutes, seconds));
        }
    }

    public void increaseScore(int points) {
        score += points;
        scoreLabel.setText(String.format("%04d", score));
    }
}

Kuvamiseks on vaja HUD lisada põhimänguklassi. Näide, kuidas seda teha saab:

public class PlayScreen implements Screen {
    private Hud hud;
    private Game game;

    public PlayScreen(Game game) {
        this.game = game;
        // create our game HUD for scores/timers/level info
        hud = new Hud(game.batch);
    }

    public void update(float dt) {
        hud.update(dt);
    }

    @Override
    public void render(float delta) {
        // separate our update logic from render
        update(delta);
        // clear the game screen with Black
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
        hud.stage.draw();
    }
}

Tulemus

HUD

Lisainfo