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