Create EntitySetAsset | create common AssetManager<A, T> interface

This commit is contained in:
2021-03-02 09:02:01 +01:00
parent ee3ce79e9f
commit 814ec50267
15 changed files with 103 additions and 20 deletions

View File

@@ -0,0 +1,7 @@
package com.bartlomiejpluta.base.game.common.manager;
public interface AssetManager<A, T> {
void registerAsset(A asset);
T loadObject(String uid);
}

View File

@@ -0,0 +1,17 @@
package com.bartlomiejpluta.base.game.entity.asset;
import com.bartlomiejpluta.base.game.common.asset.Asset;
import lombok.Getter;
import lombok.NonNull;
@Getter
public class EntitySetAsset extends Asset {
private final int rows;
private final int columns;
public EntitySetAsset(@NonNull String uid, @NonNull String source, int rows, int columns) {
super(uid, source);
this.rows = rows;
this.columns = columns;
}
}

View File

@@ -0,0 +1,43 @@
package com.bartlomiejpluta.base.game.entity.manager;
import com.bartlomiejpluta.base.core.error.AppException;
import com.bartlomiejpluta.base.core.gl.object.material.Material;
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
import com.bartlomiejpluta.base.game.entity.asset.EntitySetAsset;
import com.bartlomiejpluta.base.game.project.config.ProjectConfiguration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DefaultEntitySetManager implements EntitySetManager {
private final TextureManager textureManager;
private final Map<String, EntitySetAsset> assets = new HashMap<>();
private final ProjectConfiguration configuration;
@Override
public void registerAsset(EntitySetAsset asset) {
log.info("Registering [{}] entity set asset under UID: [{}]", asset.getSource(), asset.getUid());
assets.put(asset.getUid(), asset);
}
@Override
public Material loadObject(String uid) {
var asset = assets.get(uid);
if (asset == null) {
throw new AppException("The entity set asset with UID: [%s] does not exist", uid);
}
var source = configuration.projectFile("entsets", asset.getSource());
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
return Material.textured(texture);
}
}

View File

@@ -0,0 +1,8 @@
package com.bartlomiejpluta.base.game.entity.manager;
import com.bartlomiejpluta.base.core.gl.object.material.Material;
import com.bartlomiejpluta.base.game.common.manager.AssetManager;
import com.bartlomiejpluta.base.game.entity.asset.EntitySetAsset;
public interface EntitySetManager extends AssetManager<EntitySetAsset, Material> {
}

View File

@@ -33,7 +33,7 @@ public class DefaultImageManager implements ImageManager {
}
@Override
public Image loadImage(String uid) {
public Image loadObject(String uid) {
var asset = assets.get(uid);
if (asset == null) {

View File

@@ -1,11 +1,9 @@
package com.bartlomiejpluta.base.game.image.manager;
import com.bartlomiejpluta.base.core.gc.Cleanable;
import com.bartlomiejpluta.base.game.common.manager.AssetManager;
import com.bartlomiejpluta.base.game.image.asset.ImageAsset;
import com.bartlomiejpluta.base.game.image.model.Image;
public interface ImageManager extends Cleanable {
void registerAsset(ImageAsset asset);
Image loadImage(String uid);
public interface ImageManager extends AssetManager<ImageAsset, Image>, Cleanable {
}

View File

@@ -29,7 +29,7 @@ public class DefaultMapManager implements MapManager {
}
@Override
public GameMap loadMap(String uid) {
public GameMap loadObject(String uid) {
var map = maps.get(uid);
if (map == null) {

View File

@@ -1,11 +1,9 @@
package com.bartlomiejpluta.base.game.map.manager;
import com.bartlomiejpluta.base.core.gc.Cleanable;
import com.bartlomiejpluta.base.game.common.manager.AssetManager;
import com.bartlomiejpluta.base.game.map.asset.GameMapAsset;
import com.bartlomiejpluta.base.game.map.model.GameMap;
public interface MapManager extends Cleanable {
void registerAsset(GameMapAsset asset);
GameMap loadMap(String uid);
public interface MapManager extends AssetManager<GameMapAsset, GameMap>, Cleanable {
}

View File

@@ -26,7 +26,7 @@ public class ProtobufMapDeserializer extends MapDeserializer {
@Override
protected GameMap parse(InputStream input) throws Exception {
var proto = GameMapProto.GameMap.parseFrom(input);
var tileSet = tileSetManager.loadTileSet(proto.getTileSetUID());
var tileSet = tileSetManager.loadObject(proto.getTileSetUID());
var map = new GameMap(tileSet, proto.getRows(), proto.getColumns(), proto.getHandler());
proto.getLayersList().forEach(layer -> deserializeLayer(map, layer));
@@ -94,7 +94,7 @@ public class ProtobufMapDeserializer extends MapDeserializer {
private void deserializeImageLayer(GameMap map, GameMapProto.Layer proto) {
var protoImageLayer = proto.getImageLayer();
var image = imageManager.loadImage(protoImageLayer.getImageUID());
var image = imageManager.loadObject(protoImageLayer.getImageUID());
var mode = switch (proto.getImageLayer().getMode()) {
case NORMAL -> ImageLayerMode.NORMAL;

View File

@@ -1,5 +1,6 @@
package com.bartlomiejpluta.base.game.project.loader;
import com.bartlomiejpluta.base.game.entity.manager.EntitySetManager;
import com.bartlomiejpluta.base.game.image.manager.ImageManager;
import com.bartlomiejpluta.base.game.map.manager.MapManager;
import com.bartlomiejpluta.base.game.project.config.ProjectConfiguration;
@@ -18,6 +19,7 @@ public class DefaultProjectLoader implements ProjectLoader {
private final TileSetManager tileSetManager;
private final MapManager mapManager;
private final ImageManager imageManager;
private final EntitySetManager entitySetManager;
@Override
public Project loadProject() {
@@ -26,6 +28,7 @@ public class DefaultProjectLoader implements ProjectLoader {
project.getTileSetAssets().forEach(tileSetManager::registerAsset);
project.getMapAssets().forEach(mapManager::registerAsset);
project.getImageAssets().forEach(imageManager::registerAsset);
project.getEntitySetAssets().forEach(entitySetManager::registerAsset);
return project;
}

View File

@@ -1,5 +1,6 @@
package com.bartlomiejpluta.base.game.project.model;
import com.bartlomiejpluta.base.game.entity.asset.EntitySetAsset;
import com.bartlomiejpluta.base.game.image.asset.ImageAsset;
import com.bartlomiejpluta.base.game.map.asset.GameMapAsset;
import com.bartlomiejpluta.base.game.tileset.asset.TileSetAsset;
@@ -27,4 +28,7 @@ public class Project {
@NonNull
private final List<ImageAsset> imageAssets;
@NonNull
private final List<EntitySetAsset> entitySetAssets;
}

View File

@@ -44,7 +44,7 @@ public class RenderableContext implements Context, Updatable, Renderable {
@SneakyThrows
@Override
public void openMap(String mapUid) {
map = mapManager.loadMap(mapUid);
map = mapManager.loadObject(mapUid);
var handlerClass = classLoader.<MapHandler>loadClass(map.getHandler());
mapHandler = handlerClass.getConstructor().newInstance();

View File

@@ -1,5 +1,6 @@
package com.bartlomiejpluta.base.game.project.serial;
import com.bartlomiejpluta.base.game.entity.asset.EntitySetAsset;
import com.bartlomiejpluta.base.game.image.asset.ImageAsset;
import com.bartlomiejpluta.base.game.map.asset.GameMapAsset;
import com.bartlomiejpluta.base.game.project.model.Project;
@@ -22,7 +23,9 @@ public class ProtobufProjectDeserializer extends ProjectDeserializer {
var tileSetAssets = proto.getTileSetsList().stream().map(this::parseTileSetAsset).collect(toList());
var mapAssets = proto.getMapsList().stream().map(this::parseGameMapAsset).collect(toList());
var imageAssets = proto.getImagesList().stream().map(this::parseImageAsset).collect(toList());
return new Project(name, runner, tileSetAssets, mapAssets, imageAssets);
var entitySetAssets = proto.getEntitySetsList().stream().map(this::parseEntitySetAsset).collect(toList());
return new Project(name, runner, tileSetAssets, mapAssets, imageAssets, entitySetAssets);
}
private TileSetAsset parseTileSetAsset(ProjectProto.TileSetAsset proto) {
@@ -36,4 +39,8 @@ public class ProtobufProjectDeserializer extends ProjectDeserializer {
private ImageAsset parseImageAsset(ProjectProto.ImageAsset proto) {
return new ImageAsset(proto.getUid(), proto.getSource());
}
private EntitySetAsset parseEntitySetAsset(ProjectProto.EntitySetAsset proto) {
return new EntitySetAsset(proto.getUid(), proto.getSource(), proto.getRows(), proto.getColumns());
}
}

View File

@@ -26,12 +26,12 @@ public class DefaultTileSetManager implements TileSetManager {
@Override
public void registerAsset(TileSetAsset asset) {
log.info("Registering [{}] tileset asset under UID: [{}]", asset.getSource(), asset.getUid());
log.info("Registering [{}] tile set asset under UID: [{}]", asset.getSource(), asset.getUid());
assets.put(asset.getUid(), asset);
}
@Override
public TileSet loadTileSet(String uid) {
public TileSet loadObject(String uid) {
var tileset = tileSets.get(uid);
if (tileset == null) {

View File

@@ -1,11 +1,9 @@
package com.bartlomiejpluta.base.game.tileset.manager;
import com.bartlomiejpluta.base.core.gc.Cleanable;
import com.bartlomiejpluta.base.game.common.manager.AssetManager;
import com.bartlomiejpluta.base.game.tileset.asset.TileSetAsset;
import com.bartlomiejpluta.base.game.tileset.model.TileSet;
public interface TileSetManager extends Cleanable {
void registerAsset(TileSetAsset asset);
TileSet loadTileSet(String uid);
public interface TileSetManager extends AssetManager<TileSetAsset, TileSet>, Cleanable {
}