refactor rendersystem and introduce new TiledService and AssetService
This commit is contained in:
@@ -10,16 +10,27 @@
|
||||
"fileStates": {
|
||||
"mainmap.tmx": {
|
||||
"scale": 1.74265625,
|
||||
"selectedLayer": 3,
|
||||
"selectedLayer": 4,
|
||||
"viewCenter": {
|
||||
"x": 191.94835470277056,
|
||||
"y": 356.35255088317047
|
||||
"x": 152.9274634627455,
|
||||
"y": 285.1968080337129
|
||||
}
|
||||
},
|
||||
"objects.tsx": {
|
||||
"dynamicWrapping": true
|
||||
"dynamicWrapping": true,
|
||||
"scaleInDock": 1,
|
||||
"scaleInEditor": 1
|
||||
},
|
||||
"secondmap.tmx": {
|
||||
"scale": 1.6328125,
|
||||
"selectedLayer": 4,
|
||||
"viewCenter": {
|
||||
"x": 288.1531100478469,
|
||||
"y": 256
|
||||
}
|
||||
},
|
||||
"tileset.tsx": {
|
||||
"dynamicWrapping": false,
|
||||
"scaleInDock": 1,
|
||||
"scaleInEditor": 1
|
||||
}
|
||||
@@ -32,11 +43,13 @@
|
||||
"map.width": 20,
|
||||
"openFiles": [
|
||||
"mainmap.tmx",
|
||||
"tileset.tsx"
|
||||
"tileset.tsx",
|
||||
"objects.tsx"
|
||||
],
|
||||
"project": "mystic.tiled-project",
|
||||
"property.type": "float",
|
||||
"recentFiles": [
|
||||
"objects.tsx",
|
||||
"tileset.tsx",
|
||||
"mainmap.tmx"
|
||||
],
|
||||
|
||||
28
assets/maps/secondmap.tmx
Normal file
28
assets/maps/secondmap.tmx
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<map version="1.10" tiledversion="1.11.1-99-gec89c545" orientation="orthogonal" renderorder="right-down" width="18" height="16" tilewidth="32" tileheight="32" infinite="0" nextlayerid="6" nextobjectid="3">
|
||||
<tileset firstgid="1" source="tileset.tsx"/>
|
||||
<tileset firstgid="99" source="objects.tsx"/>
|
||||
<layer id="1" name="ground" width="18" height="16">
|
||||
<data encoding="base64" compression="zlib">
|
||||
eJx9k1EOgDAMQnuC0fufVj9c0jyhH426RmiBdVXpq/NWf89b7DXe3XfjHwHvGL5e+hfD9RR4Jyfx545acLjf1INcqr8W3CdhzRkFbrc3+elb0oN7J4+pjfNl027z12XM4au8rvOcZw6bnnBWzpw0Tr47zxKu67m7xDwkz1MuqWnCp5bprqQMu5mYGZfl2X8AE4YQ2w==
|
||||
</data>
|
||||
</layer>
|
||||
<layer id="3" name="water" width="18" height="16">
|
||||
<data encoding="base64" compression="zlib">
|
||||
eJxjYBgFo2AUjIKBAQAEgAAB
|
||||
</data>
|
||||
</layer>
|
||||
<layer id="4" name="water2" width="18" height="16">
|
||||
<data encoding="base64" compression="zlib">
|
||||
eJxjYBgFo2AUjIKBAQAEgAAB
|
||||
</data>
|
||||
</layer>
|
||||
<layer id="2" name="props" width="18" height="16">
|
||||
<data encoding="base64" compression="zlib">
|
||||
eJxjYBgFo2AUjIKBAQAEgAAB
|
||||
</data>
|
||||
</layer>
|
||||
<objectgroup id="5" name="objects">
|
||||
<object id="2" gid="99" x="67.2153" y="411.713" width="64" height="64"/>
|
||||
</objectgroup>
|
||||
</map>
|
||||
@@ -1,40 +0,0 @@
|
||||
package io.github.com.quillraven;
|
||||
|
||||
import com.badlogic.ashley.core.Engine;
|
||||
import com.badlogic.gdx.ScreenAdapter;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||
import io.github.com.quillraven.system.RenderSystem;
|
||||
|
||||
public class GameScreen extends ScreenAdapter {
|
||||
private final GdxGame game;
|
||||
|
||||
private final SpriteBatch batch;
|
||||
private final AssetManager assetManager;
|
||||
private final Viewport viewport;
|
||||
private final OrthographicCamera camera;
|
||||
|
||||
private Engine engine;
|
||||
|
||||
public GameScreen(GdxGame game) {
|
||||
this.game = game;
|
||||
this.batch = game.getBatch();
|
||||
this.assetManager = game.getAssetManager();
|
||||
this.viewport = game.getViewport();
|
||||
this.camera = game.getCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
engine = new Engine();
|
||||
engine.addSystem(new RenderSystem(batch, viewport, camera, assetManager));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
delta = Math.min(1 / 30f, delta);
|
||||
engine.update(delta);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,29 @@
|
||||
package io.github.com.quillraven;
|
||||
|
||||
import com.badlogic.gdx.Application;
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
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.screen.GameScreen;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class GdxGame extends Game {
|
||||
public static final float WORLD_HEIGHT = 11f;
|
||||
public static final float WORLD_WIDTH = 6f;
|
||||
public static final float UNIT_SCALE = 1f / 32f;
|
||||
|
||||
private SpriteBatch batch;
|
||||
private AssetManager assetManager;
|
||||
private Batch batch;
|
||||
private AssetService assetService;
|
||||
private OrthographicCamera camera;
|
||||
private Viewport viewport;
|
||||
|
||||
@@ -26,8 +31,10 @@ public class GdxGame extends Game {
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
Gdx.app.setLogLevel(Application.LOG_DEBUG);
|
||||
|
||||
batch = new SpriteBatch();
|
||||
assetManager = new AssetManager();
|
||||
assetService = new AssetService(new InternalFileHandleResolver());
|
||||
camera = new OrthographicCamera();
|
||||
viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);
|
||||
|
||||
@@ -73,15 +80,16 @@ public class GdxGame extends Game {
|
||||
screenCache.clear();
|
||||
|
||||
batch.dispose();
|
||||
assetManager.dispose();
|
||||
assetService.debugDiagnostics();
|
||||
assetService.dispose();
|
||||
}
|
||||
|
||||
public SpriteBatch getBatch() {
|
||||
public Batch getBatch() {
|
||||
return batch;
|
||||
}
|
||||
|
||||
public AssetManager getAssetManager() {
|
||||
return assetManager;
|
||||
public AssetService getAssetService() {
|
||||
return assetService;
|
||||
}
|
||||
|
||||
public OrthographicCamera getCamera() {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
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.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
|
||||
public class AssetService implements Disposable {
|
||||
private final AssetManager assetManager;
|
||||
|
||||
public AssetService(FileHandleResolver fileHandleResolver) {
|
||||
this.assetManager = new AssetManager(fileHandleResolver);
|
||||
this.assetManager.setLoader(TiledMap.class, new TmxMapLoader());
|
||||
}
|
||||
|
||||
public TiledMap load(MapAsset mapAsset) {
|
||||
this.assetManager.load(mapAsset.getPath(), TiledMap.class);
|
||||
this.assetManager.finishLoading();
|
||||
return this.assetManager.get(mapAsset.getPath(), TiledMap.class);
|
||||
}
|
||||
|
||||
public void unload(MapAsset mapAsset) {
|
||||
this.assetManager.unload(mapAsset.getPath());
|
||||
this.assetManager.finishLoading();
|
||||
}
|
||||
|
||||
public void debugDiagnostics() {
|
||||
Gdx.app.debug("AssetService", this.assetManager.getDiagnostics());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
this.assetManager.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.github.com.quillraven.asset;
|
||||
|
||||
public enum MapAsset {
|
||||
MAIN("mainmap.tmx"),
|
||||
SECOND("secondmap.tmx");
|
||||
|
||||
private final String path;
|
||||
|
||||
MapAsset(String mapName) {
|
||||
this.path = "maps/" + mapName;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.github.com.quillraven.screen;
|
||||
|
||||
import com.badlogic.ashley.core.Engine;
|
||||
import com.badlogic.gdx.ScreenAdapter;
|
||||
import io.github.com.quillraven.GdxGame;
|
||||
import io.github.com.quillraven.asset.MapAsset;
|
||||
import io.github.com.quillraven.system.RenderSystem;
|
||||
import io.github.com.quillraven.system.TiledServiceTestSystem;
|
||||
import io.github.com.quillraven.tiled.TiledService;
|
||||
|
||||
public class GameScreen extends ScreenAdapter {
|
||||
private final TiledService tiledService;
|
||||
private final Engine engine;
|
||||
|
||||
public GameScreen(GdxGame game) {
|
||||
this.tiledService = new TiledService(game.getAssetService());
|
||||
this.engine = new Engine();
|
||||
this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera()));
|
||||
this.engine.addSystem(new TiledServiceTestSystem(this.tiledService));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
this.tiledService.setMapChangeConsumer(this.engine.getSystem(RenderSystem.class)::onMapChange);
|
||||
var startMap = this.tiledService.loadMap(MapAsset.MAIN);
|
||||
this.tiledService.setMap(startMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
delta = Math.min(1 / 30f, delta);
|
||||
engine.update(delta);
|
||||
}
|
||||
}
|
||||
@@ -3,34 +3,22 @@ package io.github.com.quillraven.system;
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.SortedIteratingSystem;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
|
||||
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||
import io.github.com.quillraven.GdxGame;
|
||||
import io.github.com.quillraven.component.Graphic;
|
||||
import io.github.com.quillraven.component.Transform;
|
||||
|
||||
/**
|
||||
* System responsible for rendering entities with Transform and Graphic components.
|
||||
* Also handles rendering the tiled map.
|
||||
*/
|
||||
public class RenderSystem extends SortedIteratingSystem implements Disposable {
|
||||
private static final String MAP_PATH = "maps/mainmap.tmx";
|
||||
|
||||
private final SpriteBatch batch;
|
||||
public class RenderSystem extends SortedIteratingSystem {
|
||||
private final Batch batch;
|
||||
private final OrthographicCamera camera;
|
||||
private final Viewport viewport;
|
||||
private final AssetManager assetManager;
|
||||
private final OrthogonalTiledMapRenderer mapRenderer;
|
||||
|
||||
private TiledMapRenderer mapRenderer;
|
||||
private TiledMap map;
|
||||
|
||||
public RenderSystem(SpriteBatch batch, Viewport viewport, OrthographicCamera camera, AssetManager assetManager) {
|
||||
public RenderSystem(Batch batch, Viewport viewport, OrthographicCamera camera) {
|
||||
super(
|
||||
Family.all(Transform.class, Graphic.class).get(),
|
||||
(entity1, entity2) -> {
|
||||
@@ -43,20 +31,7 @@ public class RenderSystem extends SortedIteratingSystem implements Disposable {
|
||||
this.batch = batch;
|
||||
this.viewport = viewport;
|
||||
this.camera = camera;
|
||||
this.assetManager = assetManager;
|
||||
|
||||
loadMap();
|
||||
}
|
||||
|
||||
private void loadMap() {
|
||||
if (!assetManager.isLoaded(MAP_PATH)) {
|
||||
assetManager.setLoader(TiledMap.class, new TmxMapLoader());
|
||||
assetManager.load(MAP_PATH, TiledMap.class);
|
||||
assetManager.finishLoading(); // Block until loaded
|
||||
}
|
||||
|
||||
map = assetManager.get(MAP_PATH, TiledMap.class);
|
||||
mapRenderer = new OrthogonalTiledMapRenderer(map, 1f / 32f, batch);
|
||||
this.mapRenderer = new OrthogonalTiledMapRenderer(null, GdxGame.UNIT_SCALE, batch);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,10 +64,7 @@ public class RenderSystem extends SortedIteratingSystem implements Disposable {
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
if (map != null) {
|
||||
map.dispose();
|
||||
}
|
||||
public void onMapChange(TiledMap tiledMap) {
|
||||
this.mapRenderer.setMap(tiledMap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.github.com.quillraven.system;
|
||||
|
||||
import com.badlogic.ashley.core.EntitySystem;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import io.github.com.quillraven.asset.MapAsset;
|
||||
import io.github.com.quillraven.tiled.TiledService;
|
||||
|
||||
public class TiledServiceTestSystem extends EntitySystem {
|
||||
private final TiledService tiledService;
|
||||
|
||||
public TiledServiceTestSystem(TiledService tiledService) {
|
||||
super();
|
||||
this.tiledService = tiledService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float deltaTime) {
|
||||
if (Gdx.input.isKeyJustPressed(Input.Keys.X)) {
|
||||
Gdx.app.debug("TiledServiceTestSystem", "Setting map to MAIN");
|
||||
var tiledMap = tiledService.loadMap(MapAsset.MAIN);
|
||||
tiledService.setMap(tiledMap);
|
||||
} else if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
|
||||
Gdx.app.debug("TiledServiceTestSystem", "Setting map to SECOND");
|
||||
var tiledMap = tiledService.loadMap(MapAsset.SECOND);
|
||||
tiledService.setMap(tiledMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.github.com.quillraven.tiled;
|
||||
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import io.github.com.quillraven.asset.AssetService;
|
||||
import io.github.com.quillraven.asset.MapAsset;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class TiledService {
|
||||
private final AssetService assetService;
|
||||
private Consumer<TiledMap> mapChangeConsumer;
|
||||
private TiledMap currentMap;
|
||||
|
||||
public TiledService(AssetService assetService) {
|
||||
this.assetService = assetService;
|
||||
this.mapChangeConsumer = null;
|
||||
this.currentMap = null;
|
||||
}
|
||||
|
||||
public TiledMap loadMap(MapAsset mapAsset) {
|
||||
var tiledMap = this.assetService.load(mapAsset);
|
||||
tiledMap.getProperties().put("mapAsset", mapAsset);
|
||||
return tiledMap;
|
||||
}
|
||||
|
||||
public void setMap(TiledMap tiledMap) {
|
||||
if (this.currentMap != null) {
|
||||
this.assetService.unload(this.currentMap.getProperties().get("mapAsset", MapAsset.class));
|
||||
}
|
||||
|
||||
this.currentMap = tiledMap;
|
||||
if (this.mapChangeConsumer != null) {
|
||||
this.mapChangeConsumer.accept(tiledMap);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMapChangeConsumer(Consumer<TiledMap> mapChangeConsumer) {
|
||||
this.mapChangeConsumer = mapChangeConsumer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user