Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dermetfan.util.libgdx;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.assets.AssetDescriptor;
- import com.badlogic.gdx.assets.AssetManager;
- import com.badlogic.gdx.files.FileHandle;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.g2d.TextureAtlas;
- /** This class is supposed to be edited by the user.<br/>
- * Pay attention to the comments.
- *
- * <p>
- * To add an asset:
- * <ul>
- * <li>If it should be {@link #queue() queued}, add it to {@link Descriptors}, else to {@link OnDemandFileHandles} for manual use</li>
- * <li>Add a field for direct access</li>
- * <li>Initialize your asset field in {@link #init()}</li>
- * <li>If necessary, dispose your asset field in {@link #dispose()}</li>
- * </ul>
- * </p>
- *
- * <p>
- * To load the {@link #queue() queued} assets:<br/>
- * <code>Assets.queue();<br/>
- * while(!Assets.update()) // or just Assets.manager.finishLoading() followed by Assets.init()<br/>
- * ;<br/>
- * // all assets are loaded and Assets#init() was called</code>
- * </p>
- *
- * <p>
- * To access your assets:<br/>
- * <code>Assets.yourCustomAssetField</code>
- * </p>
- *
- * <p>
- * To access on-demand assets (example):<br/>
- * <code>new Texture(Assets.OnDemandFileHandles.blackImg.fileHandle.path())</code>
- * </p>
- *
- * @author dermetfan */
- public abstract class Assets {
- /** responsible for loading the assets */
- public static final AssetManager manager = new AssetManager();
- /** {@link AssetManager#load(AssetDescriptor) queues} all not loaded {@link Descriptors assets} for loading */
- public static void queue() {
- for(Descriptors desc : Descriptors.values())
- if(!manager.isLoaded(desc.descriptor.fileName))
- manager.load(desc.descriptor.fileName, desc.descriptor.type);
- }
- /** {@link #init() Initializes} when loading is finished. Assets must have been {@link #queue() queued} before.
- * @see AssetManager#update() */
- public static boolean update() {
- if(manager.update()) {
- init();
- return true;
- }
- return false;
- }
- /* DON'T TOUCH ANYTHING ABOVE THIS LINE
- * ADD Descriptors AND OnDemandFileHandles BELOW */
- /** {@link AssetDescriptor AssetDescriptors} of assets that should be {@link Assets#queue() queued} */
- public static enum Descriptors {
- playerAtlas(new AssetDescriptor<TextureAtlas>(Gdx.files.internal("img/player/player.pack"), TextureAtlas.class));
- public final AssetDescriptor<?> descriptor;
- private Descriptors(AssetDescriptor<?> descriptor) {
- this.descriptor = descriptor;
- }
- }
- /** {@link FileHandle FileHandles} of assets that should not be {@link Assets#queue() queued} */
- public static enum OnDemandFileHandles {
- map1(Gdx.files.internal("maps/map.tmx"));
- public final FileHandle fileHandle;
- private OnDemandFileHandles(FileHandle fileHandle) {
- this.fileHandle = fileHandle;
- }
- }
- /* ADD FIELDS FOR DIRECT ACCESS TO YOUR ASSETS AND GENERATED ASSETS */
- public static TextureAtlas playerAtlas;
- public static Animation playerStill, playerLeft, playerRight;
- /* ADD INITIALIZATION OF YOUR FIELDS HERE */
- /** Initializes all fields. {@link #update() Loading} must be finished or some fields will be set to null. */
- public static void init() {
- { // player texture atlas and animations
- playerAtlas = manager.get(Descriptors.playerAtlas.descriptor.fileName);
- playerStill = new Animation(1 / 2f, playerAtlas.findRegions("still"));
- playerLeft = new Animation(1 / 6f, playerAtlas.findRegions("left"));
- playerRight = new Animation(1 / 6f, playerAtlas.findRegions("right"));
- playerStill.setPlayMode(Animation.LOOP);
- playerLeft.setPlayMode(Animation.LOOP);
- playerRight.setPlayMode(Animation.LOOP);
- }
- }
- /* DISPOSE YOUR ASSET FIELDS HERE */
- /** disposes all assets */
- public static void dispose() {
- { // player texture atlas and animations
- playerAtlas.dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement