refactor asset management
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
import com.badlogic.gdx.assets.AssetDescriptor;
|
||||
|
||||
public interface Asset<T> {
|
||||
AssetDescriptor<T> getDescriptor();
|
||||
}
|
||||
@@ -3,9 +3,6 @@ package io.github.com.quillraven.asset;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
|
||||
import com.badlogic.gdx.audio.Music;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
|
||||
@@ -21,52 +18,23 @@ public class AssetService implements Disposable {
|
||||
assetManager.setLoader(Skin.class, new FreeTypistSkinLoader(assetManager.getFileHandleResolver()));
|
||||
}
|
||||
|
||||
public TiledMap load(MapAsset mapAsset) {
|
||||
String path = mapAsset.getPath();
|
||||
this.assetManager.load(path, TiledMap.class);
|
||||
public <T> T load(Asset<T> asset) {
|
||||
this.assetManager.load(asset.getDescriptor());
|
||||
this.assetManager.finishLoading();
|
||||
return this.assetManager.get(path, TiledMap.class);
|
||||
return this.assetManager.get(asset.getDescriptor());
|
||||
}
|
||||
|
||||
public void unload(MapAsset mapAsset) {
|
||||
this.assetManager.unload(mapAsset.getPath());
|
||||
public <T> void unload(Asset<T> asset) {
|
||||
this.assetManager.unload(asset.getDescriptor().fileName);
|
||||
this.assetManager.finishLoading();
|
||||
}
|
||||
|
||||
public void queue(AtlasAsset atlasAsset) {
|
||||
this.assetManager.load(atlasAsset.getPath(), TextureAtlas.class);
|
||||
public <T> void queue(Asset<T> asset) {
|
||||
this.assetManager.load(asset.getDescriptor());
|
||||
}
|
||||
|
||||
public TextureAtlas get(AtlasAsset atlasAsset) {
|
||||
return this.assetManager.get(atlasAsset.getPath(), TextureAtlas.class);
|
||||
}
|
||||
|
||||
public Music load(MusicAsset musicAsset) {
|
||||
String path = musicAsset.getPath();
|
||||
this.assetManager.load(path, Music.class);
|
||||
this.assetManager.finishLoading();
|
||||
return this.assetManager.get(path, Music.class);
|
||||
}
|
||||
|
||||
public void unload(MusicAsset musicAsset) {
|
||||
this.assetManager.unload(musicAsset.getPath());
|
||||
this.assetManager.finishLoading();
|
||||
}
|
||||
|
||||
public void queue(SkinAsset skinAsset) {
|
||||
this.assetManager.load(skinAsset.getPath(), Skin.class);
|
||||
}
|
||||
|
||||
public Skin get(SkinAsset skinAsset) {
|
||||
return this.assetManager.get(skinAsset.getPath(), Skin.class);
|
||||
}
|
||||
|
||||
public void queue(SoundAsset soundAsset) {
|
||||
this.assetManager.load(soundAsset.getPath(), Sound.class);
|
||||
}
|
||||
|
||||
public Sound get(SoundAsset soundAsset) {
|
||||
return this.assetManager.get(soundAsset.getPath(), Sound.class);
|
||||
public <T> T get(Asset<T> asset) {
|
||||
return this.assetManager.get(asset.getDescriptor());
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum AtlasAsset {
|
||||
import com.badlogic.gdx.assets.AssetDescriptor;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
|
||||
public enum AtlasAsset implements Asset<TextureAtlas> {
|
||||
OBJECTS("objects.atlas");
|
||||
|
||||
private final String path;
|
||||
private final AssetDescriptor<TextureAtlas> descriptor;
|
||||
|
||||
AtlasAsset(String atlasName) {
|
||||
this.path = "graphics/" + atlasName;
|
||||
this.descriptor = new AssetDescriptor<>("graphics/" + atlasName, TextureAtlas.class);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
@Override
|
||||
public AssetDescriptor<TextureAtlas> getDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum MapAsset {
|
||||
import com.badlogic.gdx.assets.AssetDescriptor;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
|
||||
public enum MapAsset implements Asset<TiledMap> {
|
||||
MAIN("mainmap.tmx");
|
||||
|
||||
private final String path;
|
||||
private final AssetDescriptor<TiledMap> descriptor;
|
||||
|
||||
MapAsset(String mapName) {
|
||||
this.path = "maps/" + mapName;
|
||||
this.descriptor = new AssetDescriptor<>("maps/" + mapName, TiledMap.class);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
@Override
|
||||
public AssetDescriptor<TiledMap> getDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum MusicAsset {
|
||||
import com.badlogic.gdx.assets.AssetDescriptor;
|
||||
import com.badlogic.gdx.audio.Music;
|
||||
|
||||
public enum MusicAsset implements Asset<Music> {
|
||||
TOWN("town.ogg"),
|
||||
MENU("menu.ogg");
|
||||
|
||||
private final String path;
|
||||
private final AssetDescriptor<Music> descriptor;
|
||||
|
||||
MusicAsset(String musicFile) {
|
||||
this.path = "audio/" + musicFile;
|
||||
this.descriptor = new AssetDescriptor<>("audio/" + musicFile, Music.class);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
@Override
|
||||
public AssetDescriptor<Music> getDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum SkinAsset {
|
||||
import com.badlogic.gdx.assets.AssetDescriptor;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
|
||||
|
||||
public enum SkinAsset implements Asset<Skin> {
|
||||
DEFAULT("skin.json");
|
||||
|
||||
private final String path;
|
||||
private final AssetDescriptor<Skin> descriptor;
|
||||
|
||||
SkinAsset(String skinJsonFile) {
|
||||
this.path = "ui/" + skinJsonFile;
|
||||
this.descriptor = new AssetDescriptor<>("ui/" + skinJsonFile, Skin.class);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
@Override
|
||||
public AssetDescriptor<Skin> getDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum SoundAsset {
|
||||
import com.badlogic.gdx.assets.AssetDescriptor;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
|
||||
public enum SoundAsset implements Asset<Sound> {
|
||||
SWORD_HIT("sword_hit.wav"),
|
||||
LIFE_REG("life_reg.wav"),
|
||||
TRAP("trap.wav"),
|
||||
SWING("swing.wav"),
|
||||
;
|
||||
|
||||
private final String path;
|
||||
private final AssetDescriptor<Sound> descriptor;
|
||||
|
||||
SoundAsset(String musicFile) {
|
||||
this.path = "audio/" + musicFile;
|
||||
this.descriptor = new AssetDescriptor<>("audio/" + musicFile, Sound.class);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
@Override
|
||||
public AssetDescriptor<Sound> getDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user