de.cubicvoxel.openspacebox.OpenSpaceBox.java Source code

Java tutorial

Introduction

Here is the source code for de.cubicvoxel.openspacebox.OpenSpaceBox.java

Source

/*
 * This file is part of OpenSpaceBox.
 * Copyright (C) 2018 by Maik Becker <hi@maik.codes>
 *
 * OpenSpaceBox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenSpaceBox is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenSpaceBox.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.cubicvoxel.openspacebox;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import de.cubicvoxel.openspacebox.assetmanagement.LoadingScreen;
import de.cubicvoxel.openspacebox.assetmanagement.OsbAssetManager;
import de.cubicvoxel.openspacebox.assetmanagement.asset.*;
import de.cubicvoxel.openspacebox.assetmanagement.util.DefinitionFinder;
import de.cubicvoxel.openspacebox.context.OsbContext;
import lombok.Getter;

import java.util.Locale;

/**
 * Main
 *
 * @author Maik Becker {@literal <hi@maik.codes>}
 */
public class OpenSpaceBox extends Game {

    @Getter
    private static OpenSpaceBox instance;

    @Getter
    private SpriteBatch objectBatch;
    private OsbAssetManager assetManager;
    private OsbContext context;

    public OpenSpaceBox(OsbContext context) {
        instance = this;
        this.context = context;
    }

    @Override
    public void create() {
        objectBatch = new SpriteBatch();
        assetManager = new OsbAssetManager();
        ResourceBundles.initialize(Locale.getDefault());

        setScreen(new LoadingScreen(assetManager, this::onLoadingFinished, FontAsset.valuesAsAssets(),
                TypeDefinitionsAsset.valuesAsAssets(), UniverseAsset.valuesAsAssets(),
                SectorTemplateAsset.valuesAsAssets(), TextureAtlasAsset.valuesAsAssets(),
                TextureAsset.valuesAsAssets()));
    }

    private void onLoadingFinished() {
        assetManager.finishLoading();
        setScreen(context.getPostLoadingScreen());
    }

    @Override
    public void pause() {
        super.pause();
    }

    @Override
    public void resume() {
        super.resume();
    }

    @Override
    public void dispose() {
        super.dispose();
        getScreen().dispose();
        objectBatch.dispose();
        assetManager.dispose();
    }

    public synchronized static <T> T getAsset(ManagedAsset<T> asset) {
        return getInstance().assetManager.get(asset.getAssetDescriptor());
    }

    public static Sprite createSprite(ManagedAtlas atlas) {
        return getAsset(atlas.getAtlas()).createSprite(atlas.getName());
    }

    public static DefinitionFinder createDefinitionFinder() {
        return new DefinitionFinder(getInstance().assetManager);
    }

}