add music support
This commit is contained in:
@@ -1 +1,2 @@
|
||||
https://kenmi-art.itch.io/cute-fantasy-rpg
|
||||
https://yubatake.bandcamp.com/album/jrpg-collection
|
||||
|
||||
BIN
assets/audio/town.ogg
Normal file
BIN
assets/audio/town.ogg
Normal file
Binary file not shown.
@@ -1,5 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="18" height="16" tilewidth="16" tileheight="16" infinite="0" nextlayerid="7" nextobjectid="11">
|
||||
<properties>
|
||||
<property name="music" value="TOWN"/>
|
||||
</properties>
|
||||
<tileset firstgid="1" source="objects.tsx"/>
|
||||
<tileset firstgid="6" source="tileset.tsx"/>
|
||||
<layer id="1" name="ground" width="18" height="16">
|
||||
|
||||
@@ -15,6 +15,7 @@ import com.badlogic.gdx.utils.GdxRuntimeException;
|
||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||
import io.github.com.quillraven.asset.AssetService;
|
||||
import io.github.com.quillraven.audio.AudioService;
|
||||
import io.github.com.quillraven.screen.LoadingScreen;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -27,6 +28,7 @@ public class GdxGame extends Game {
|
||||
|
||||
private Batch batch;
|
||||
private AssetService assetService;
|
||||
private AudioService audioService;
|
||||
private OrthographicCamera camera;
|
||||
private Viewport viewport;
|
||||
private GLProfiler glProfiler;
|
||||
@@ -42,6 +44,7 @@ public class GdxGame extends Game {
|
||||
|
||||
batch = new SpriteBatch();
|
||||
assetService = new AssetService(new InternalFileHandleResolver());
|
||||
audioService = new AudioService(assetService);
|
||||
camera = new OrthographicCamera();
|
||||
viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);
|
||||
|
||||
@@ -117,4 +120,8 @@ public class GdxGame extends Game {
|
||||
public InputMultiplexer getInputMultiplexer() {
|
||||
return inputMultiplexer;
|
||||
}
|
||||
|
||||
public AudioService getAudioService() {
|
||||
return audioService;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ 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.graphics.g2d.TextureAtlas;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||
@@ -36,6 +37,18 @@ public class AssetService implements Disposable {
|
||||
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 boolean update() {
|
||||
return this.assetManager.update();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum MusicAsset {
|
||||
TOWN("town.ogg");
|
||||
|
||||
private final String path;
|
||||
|
||||
MusicAsset(String musicFile) {
|
||||
this.path = "audio/" + musicFile;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package io.github.com.quillraven.audio;
|
||||
|
||||
import com.badlogic.gdx.audio.Music;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import io.github.com.quillraven.asset.AssetService;
|
||||
import io.github.com.quillraven.asset.MusicAsset;
|
||||
|
||||
public class AudioService {
|
||||
|
||||
private final AssetService assetService;
|
||||
private Music currentMusic;
|
||||
private MusicAsset currentMusicAsset;
|
||||
|
||||
public AudioService(AssetService assetService) {
|
||||
this.assetService = assetService;
|
||||
this.currentMusic = null;
|
||||
this.currentMusicAsset = null;
|
||||
}
|
||||
|
||||
public void playMusic(MusicAsset musicAsset) {
|
||||
if (this.currentMusicAsset == musicAsset) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.currentMusic != null) {
|
||||
this.currentMusic.stop();
|
||||
this.assetService.unload(this.currentMusicAsset);
|
||||
}
|
||||
|
||||
this.currentMusic = this.assetService.load(musicAsset);
|
||||
this.currentMusic.setLooping(true);
|
||||
this.currentMusic.play();
|
||||
this.currentMusicAsset = musicAsset;
|
||||
}
|
||||
|
||||
public void setMap(TiledMap tiledMap) {
|
||||
String musicAssetStr = tiledMap.getProperties().get("music", "", String.class);
|
||||
if (musicAssetStr.isBlank()) {
|
||||
return;
|
||||
}
|
||||
|
||||
MusicAsset musicAsset = MusicAsset.valueOf(musicAssetStr);
|
||||
playMusic(musicAsset);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import com.badlogic.gdx.physics.box2d.World;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
import io.github.com.quillraven.GdxGame;
|
||||
import io.github.com.quillraven.asset.MapAsset;
|
||||
import io.github.com.quillraven.audio.AudioService;
|
||||
import io.github.com.quillraven.component.Controller;
|
||||
import io.github.com.quillraven.input.GameControllerState;
|
||||
import io.github.com.quillraven.input.KeyboardController;
|
||||
@@ -35,9 +36,11 @@ public class GameScreen extends ScreenAdapter {
|
||||
private final TiledAshleySpawner tiledAshleySpawner;
|
||||
private final World physicWorld;
|
||||
private final KeyboardController keyboardController;
|
||||
private final AudioService audioService;
|
||||
|
||||
public GameScreen(GdxGame game) {
|
||||
this.game = game;
|
||||
this.audioService = game.getAudioService();
|
||||
this.tiledService = new TiledService(game.getAssetService());
|
||||
this.physicWorld = new World(Vector2.Zero, true);
|
||||
this.physicWorld.setAutoClearForces(false);
|
||||
@@ -66,7 +69,10 @@ public class GameScreen extends ScreenAdapter {
|
||||
Consumer<TiledMap> renderConsumer = this.engine.getSystem(RenderSystem.class)::setMap;
|
||||
Consumer<TiledMap> ashleySpawnerConsumer = this.tiledAshleySpawner::loadMapObjects;
|
||||
Consumer<TiledMap> cameraConsumer = this.engine.getSystem(CameraSystem.class)::setMap;
|
||||
this.tiledService.setMapChangeConsumer(renderConsumer.andThen(ashleySpawnerConsumer).andThen(cameraConsumer));
|
||||
Consumer<TiledMap> audioConsumer = this.audioService::setMap;
|
||||
this.tiledService.setMapChangeConsumer(
|
||||
renderConsumer.andThen(ashleySpawnerConsumer).andThen(cameraConsumer).andThen(audioConsumer)
|
||||
);
|
||||
|
||||
TiledMap startMap = this.tiledService.loadMap(MapAsset.MAIN);
|
||||
this.tiledService.setMap(startMap);
|
||||
|
||||
Reference in New Issue
Block a user