com.badlogicgames.superjumper.mainmenu.HighScoresScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.badlogicgames.superjumper.mainmenu.HighScoresScreen.java

Source

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogicgames.superjumper.mainmenu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogicgames.superjumper.Assets;
import com.badlogicgames.superjumper.Settings;
import com.badlogicgames.superjumper.SuperJumper;
import com.badlogicgames.superjumper.mainmenu.MainMenuScreen;

public class HighScoresScreen extends ScreenAdapter {
    SuperJumper game;
    OrthographicCamera guiCam;
    Rectangle backBounds;
    Vector3 touchPoint;
    String[] highScores;
    float xOffset = 0;

    public HighScoresScreen(SuperJumper game) {
        this.game = game;

        guiCam = new OrthographicCamera(320, 480);
        guiCam.position.set(320 / 2, 480 / 2, 0);
        backBounds = new Rectangle(0, 0, 64, 64);
        touchPoint = new Vector3();
        highScores = new String[5];
        for (int i = 0; i < 5; i++) {
            highScores[i] = i + 1 + ". " + Settings.highScores[i];
            xOffset = Math.max(Assets.font.getBounds(highScores[i]).width, xOffset);
        }
        xOffset = 160 - xOffset / 2 + Assets.font.getSpaceWidth() / 2;
    }

    public void update() {
        if (Gdx.input.justTouched()) {
            guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

            if (backBounds.contains(touchPoint.x, touchPoint.y)) {
                Assets.playSound(Assets.clickSound);
                game.setScreen(game.mainMenuScreen);
                this.dispose();
                return;
            }
        }
    }

    public void draw() {
        GL20 gl = Gdx.gl;
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        guiCam.update();

        game.batch.setProjectionMatrix(guiCam.combined);
        game.batch.disableBlending();
        game.batch.begin();
        game.batch.draw(Assets.backgroundRegion, 0, 0, 320, 480);
        game.batch.end();

        game.batch.enableBlending();
        game.batch.begin();
        game.batch.draw(Assets.highScoresRegion, 10, 360 - 16, 300, 33);

        float y = 230;
        for (int i = 4; i >= 0; i--) {
            Assets.font.draw(game.batch, highScores[i], xOffset, y);
            y += Assets.font.getLineHeight();
        }

        game.batch.draw(Assets.arrow, 0, 0, 64, 64);
        game.batch.end();
    }

    @Override
    public void render(float delta) {
        update();
        draw();
    }
}