add ashley tiled spawner (wip)
This commit is contained in:
@@ -1,28 +1,40 @@
|
|||||||
package io.github.com.quillraven.screen;
|
package io.github.com.quillraven.screen;
|
||||||
|
|
||||||
import com.badlogic.ashley.core.Engine;
|
import com.badlogic.ashley.core.Engine;
|
||||||
|
import com.badlogic.ashley.core.EntitySystem;
|
||||||
import com.badlogic.gdx.ScreenAdapter;
|
import com.badlogic.gdx.ScreenAdapter;
|
||||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||||
|
import com.badlogic.gdx.utils.Disposable;
|
||||||
import io.github.com.quillraven.GdxGame;
|
import io.github.com.quillraven.GdxGame;
|
||||||
import io.github.com.quillraven.asset.MapAsset;
|
import io.github.com.quillraven.asset.MapAsset;
|
||||||
import io.github.com.quillraven.system.RenderSystem;
|
import io.github.com.quillraven.system.RenderSystem;
|
||||||
import io.github.com.quillraven.system.TiledServiceTestSystem;
|
import io.github.com.quillraven.system.TiledServiceTestSystem;
|
||||||
|
import io.github.com.quillraven.tiled.TiledAshleySpawner;
|
||||||
import io.github.com.quillraven.tiled.TiledService;
|
import io.github.com.quillraven.tiled.TiledService;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class GameScreen extends ScreenAdapter {
|
public class GameScreen extends ScreenAdapter {
|
||||||
private final TiledService tiledService;
|
private final TiledService tiledService;
|
||||||
private final Engine engine;
|
private final Engine engine;
|
||||||
|
private final TiledAshleySpawner tiledAshleySpawner;
|
||||||
|
|
||||||
public GameScreen(GdxGame game) {
|
public GameScreen(GdxGame game) {
|
||||||
this.tiledService = new TiledService(game.getAssetService());
|
this.tiledService = new TiledService(game.getAssetService());
|
||||||
this.engine = new Engine();
|
this.engine = new Engine();
|
||||||
|
this.tiledAshleySpawner = new TiledAshleySpawner(this.engine);
|
||||||
|
|
||||||
|
// add ECS systems
|
||||||
this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera()));
|
this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera()));
|
||||||
this.engine.addSystem(new TiledServiceTestSystem(this.tiledService));
|
this.engine.addSystem(new TiledServiceTestSystem(this.tiledService));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void show() {
|
public void show() {
|
||||||
this.tiledService.setMapChangeConsumer(this.engine.getSystem(RenderSystem.class)::setMap);
|
Consumer<TiledMap> renderConsumer = this.engine.getSystem(RenderSystem.class)::setMap;
|
||||||
|
Consumer<TiledMap> ashleySpawnerConsumer = this.tiledAshleySpawner::loadMapObjects;
|
||||||
|
this.tiledService.setMapChangeConsumer(renderConsumer.andThen(ashleySpawnerConsumer));
|
||||||
|
|
||||||
TiledMap startMap = this.tiledService.loadMap(MapAsset.MAIN);
|
TiledMap startMap = this.tiledService.loadMap(MapAsset.MAIN);
|
||||||
this.tiledService.setMap(startMap);
|
this.tiledService.setMap(startMap);
|
||||||
}
|
}
|
||||||
@@ -32,4 +44,14 @@ public class GameScreen extends ScreenAdapter {
|
|||||||
delta = Math.min(1 / 30f, delta);
|
delta = Math.min(1 / 30f, delta);
|
||||||
engine.update(delta);
|
engine.update(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose() {
|
||||||
|
super.dispose();
|
||||||
|
for (EntitySystem system : this.engine.getSystems()) {
|
||||||
|
if (system instanceof Disposable disposable) {
|
||||||
|
disposable.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package io.github.com.quillraven.tiled;
|
||||||
|
|
||||||
|
import com.badlogic.ashley.core.Engine;
|
||||||
|
import com.badlogic.ashley.core.Entity;
|
||||||
|
import com.badlogic.gdx.graphics.Color;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||||
|
import com.badlogic.gdx.maps.MapLayer;
|
||||||
|
import com.badlogic.gdx.maps.MapObject;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
|
||||||
|
import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject;
|
||||||
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.badlogic.gdx.utils.GdxRuntimeException;
|
||||||
|
import io.github.com.quillraven.GdxGame;
|
||||||
|
import io.github.com.quillraven.component.Graphic;
|
||||||
|
import io.github.com.quillraven.component.Transform;
|
||||||
|
|
||||||
|
public class TiledAshleySpawner {
|
||||||
|
private final Engine engine;
|
||||||
|
|
||||||
|
public TiledAshleySpawner(Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadMapObjects(TiledMap tiledMap) {
|
||||||
|
for (MapLayer layer : tiledMap.getLayers()) {
|
||||||
|
if (layer instanceof TiledMapTileLayer tileLayer) {
|
||||||
|
loadTileLayer(tileLayer);
|
||||||
|
} else if ("objects".equals(layer.getName())) {
|
||||||
|
loadObjectLayer(layer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadTileLayer(TiledMapTileLayer tileLayer) {
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadObjectLayer(MapLayer objectLayer) {
|
||||||
|
for (MapObject mapObject : objectLayer.getObjects()) {
|
||||||
|
if (mapObject instanceof TiledMapTileMapObject tileMapObject) {
|
||||||
|
spawnEntityOf(tileMapObject);
|
||||||
|
} else {
|
||||||
|
throw new GdxRuntimeException("Unsupported map object: " + mapObject.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void spawnEntityOf(TiledMapTileMapObject tileMapObject) {
|
||||||
|
Entity entity = this.engine.createEntity();
|
||||||
|
|
||||||
|
addEntityTransform(tileMapObject, entity);
|
||||||
|
entity.add(new Graphic(tileMapObject.getTile().getTextureRegion(), Color.WHITE.cpy()));
|
||||||
|
|
||||||
|
this.engine.addEntity(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addEntityTransform(TiledMapTileMapObject tileMapObject, Entity entity) {
|
||||||
|
Vector2 position = new Vector2(tileMapObject.getX(), tileMapObject.getY());
|
||||||
|
TextureRegion textureRegion = tileMapObject.getTile().getTextureRegion();
|
||||||
|
Vector2 size = new Vector2(textureRegion.getRegionWidth(), textureRegion.getRegionHeight());
|
||||||
|
position.scl(GdxGame.UNIT_SCALE);
|
||||||
|
size.scl(tileMapObject.getScaleX(), tileMapObject.getScaleY());
|
||||||
|
size.scl(GdxGame.UNIT_SCALE);
|
||||||
|
|
||||||
|
entity.add(new Transform(position, 0, size));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user