com.dongbat.invasion.screen.LoadingScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.dongbat.invasion.screen.LoadingScreen.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dongbat.invasion.screen;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.dongbat.invasion.registry.AbilityRegistry;
import com.dongbat.invasion.registry.AtlasRegistry;
import com.dongbat.invasion.registry.BuffRegistry;
import com.dongbat.invasion.registry.BulletRegistry;
import com.dongbat.invasion.registry.EnemyRegistry;
import com.dongbat.invasion.util.AssetUtil;
import com.dongbat.invasion.util.PlayerInputUtil;
import com.dongbat.invasion.util.ScreenUtil;
import com.dongbat.invasion.util.UpgradeUtil;

/**
 *
 * @author Mats Svensson, Vuong
 */
public class LoadingScreen implements Screen {

    private Stage stage;

    private Image logo;
    private Image loadingFrame;
    private Image loadingBarHidden;
    private Image screenBg;
    private Image loadingBg;

    private float startX, endX;
    private float percent;

    private Actor loadingBar;
    private final AssetManager manager;
    private final Game game;

    public LoadingScreen() {
        manager = AssetUtil.getManager();
        this.game = ScreenUtil.getGame();
    }

    @Override
    public void show() {
        // Tell the manager to load assets for the loading screen
        manager.load("data/loading.pack", TextureAtlas.class);
        // Wait until they are finished loading
        manager.finishLoading();
        // Initialize the stage where we will place everything
        stage = new Stage();
        // Get our textureatlas from the manager
        TextureAtlas atlas = manager.get("data/loading.pack", TextureAtlas.class);
        // Grab the regions from the atlas and create some images
        logo = new Image(atlas.findRegion("libgdx-logo"));
        loadingFrame = new Image(atlas.findRegion("loading-frame"));
        loadingBarHidden = new Image(atlas.findRegion("loading-bar-hidden"));
        screenBg = new Image(atlas.findRegion("screen-bg"));
        loadingBg = new Image(atlas.findRegion("loading-frame-bg"));
        // Add the loading bar animation
        Animation anim = new Animation(0.05f, atlas.findRegions("loading-bar-anim"));
        anim.setPlayMode(Animation.PlayMode.LOOP_REVERSED);
        loadingBar = new LoadingBar(anim);
        // Add all the actors to the stage
        stage.addActor(screenBg);
        stage.addActor(loadingBar);
        stage.addActor(loadingBg);
        stage.addActor(loadingBarHidden);
        stage.addActor(loadingFrame);
        stage.addActor(logo);
        // Add everything to be loaded, for instance:
        UpgradeUtil.load();
        AtlasRegistry.load();
        AssetUtil.load();

        AbilityRegistry.load();
        BuffRegistry.load();
        PlayerInputUtil.init();
        BulletRegistry.load();
        EnemyRegistry.load();
    }

    @Override
    public void resize(int width, int height) {
        // Make the background fill the screen
        screenBg.setSize(width, height);
        // Place the logo in the middle of the screen and 100 px up
        logo.setX((width - logo.getWidth()) / 2);
        logo.setY((height - logo.getHeight()) / 2 + 100);
        // Place the loading frame in the middle of the screen
        loadingFrame.setX((stage.getWidth() - loadingFrame.getWidth()) / 2);
        loadingFrame.setY((stage.getHeight() - loadingFrame.getHeight()) / 2);
        // Place the loading bar at the same spot as the frame, adjusted a few px
        loadingBar.setX(loadingFrame.getX() + 15);
        loadingBar.setY(loadingFrame.getY() + 5);
        // Place the image that will hide the bar on top of the bar, adjusted a few px
        loadingBarHidden.setX(loadingBar.getX() + 35);
        loadingBarHidden.setY(loadingBar.getY() - 3);
        // The start position and how far to move the hidden loading bar
        startX = loadingBarHidden.getX();
        endX = 440;
        // The rest of the hidden bar
        loadingBg.setSize(450, 50);
        loadingBg.setX(loadingBarHidden.getX() + 30);
        loadingBg.setY(loadingBarHidden.getY() + 3);
    }

    @Override
    public void render(float delta) {
        // Clear the screen
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        if (ScreenUtil.transitioning) {
            return;
        }
        if (manager.update()) { // Load some, will return true if done loading
            ScreenUtil.switchScreen(this, new GameScreen(UpgradeUtil.getString("l")));
        }
        // Interpolate the percentage to make it more smooth
        percent = Interpolation.linear.apply(percent, manager.getProgress(), 0.1f);
        // Update positions (and size) to match the percentage
        loadingBarHidden.setX(startX + endX * percent);
        loadingBg.setX(loadingBarHidden.getX() + 30);
        loadingBg.setWidth(450 - 450 * percent);
        loadingBg.invalidate();
        // Show the loading screen
        stage.act();
        stage.draw();
    }

    @Override
    public void hide() {
        manager.unload("data/loading.pack");
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}