Create working deserializers in :game module
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -136,3 +136,6 @@ gradle-app.setting
|
|||||||
*.png
|
*.png
|
||||||
|
|
||||||
# End of https://www.toptal.com/developers/gitignore/api/java,gradle,intellij
|
# End of https://www.toptal.com/developers/gitignore/api/java,gradle,intellij
|
||||||
|
|
||||||
|
# BASE sample data
|
||||||
|
**/resources/project/
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.common.asset;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public abstract class Asset {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
protected final String uid;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
protected final String source;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.common.serial;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.error.AppException;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public abstract class Deserializer<T> {
|
||||||
|
protected abstract T parse(InputStream input) throws Exception;
|
||||||
|
|
||||||
|
public T deserialize(InputStream input) {
|
||||||
|
try {
|
||||||
|
return parse(input);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new AppException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,15 +3,17 @@ package com.bartlomiejpluta.base.game.logic;
|
|||||||
import com.bartlomiejpluta.base.core.error.AppException;
|
import com.bartlomiejpluta.base.core.error.AppException;
|
||||||
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
|
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
|
||||||
import com.bartlomiejpluta.base.core.gl.render.Renderer;
|
import com.bartlomiejpluta.base.core.gl.render.Renderer;
|
||||||
import com.bartlomiejpluta.base.core.image.ImageManager;
|
|
||||||
import com.bartlomiejpluta.base.core.logic.GameLogic;
|
import com.bartlomiejpluta.base.core.logic.GameLogic;
|
||||||
import com.bartlomiejpluta.base.core.profiling.fps.FPSMonitor;
|
import com.bartlomiejpluta.base.core.profiling.fps.FPSMonitor;
|
||||||
import com.bartlomiejpluta.base.core.profiling.time.TimeProfilerService;
|
import com.bartlomiejpluta.base.core.profiling.time.TimeProfilerService;
|
||||||
import com.bartlomiejpluta.base.core.ui.Window;
|
import com.bartlomiejpluta.base.core.ui.Window;
|
||||||
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.core.world.map.GameMap;
|
import com.bartlomiejpluta.base.game.image.manager.ImageManager;
|
||||||
import com.bartlomiejpluta.base.core.world.tileset.manager.TileSetManager;
|
import com.bartlomiejpluta.base.game.map.manager.MapManager;
|
||||||
|
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||||
|
import com.bartlomiejpluta.base.game.project.loader.ProjectLoader;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.manager.TileSetManager;
|
||||||
import com.bartlomiejpluta.base.game.world.entity.manager.EntityManager;
|
import com.bartlomiejpluta.base.game.world.entity.manager.EntityManager;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -30,6 +32,8 @@ public class DefaultGameLogic implements GameLogic {
|
|||||||
private final ImageManager imageManager;
|
private final ImageManager imageManager;
|
||||||
private final TimeProfilerService profiler;
|
private final TimeProfilerService profiler;
|
||||||
private final FPSMonitor fpsMonitor;
|
private final FPSMonitor fpsMonitor;
|
||||||
|
private final MapManager mapManager;
|
||||||
|
private final ProjectLoader projectLoader;
|
||||||
|
|
||||||
private final Camera camera = new Camera();
|
private final Camera camera = new Camera();
|
||||||
|
|
||||||
@@ -40,6 +44,10 @@ public class DefaultGameLogic implements GameLogic {
|
|||||||
log.info("Initializing game logic");
|
log.info("Initializing game logic");
|
||||||
renderer.init();
|
renderer.init();
|
||||||
|
|
||||||
|
// Loading is not available right now, because no project is located in the resources
|
||||||
|
// projectLoader.loadProject();
|
||||||
|
// map = mapManager.getTheStartMapFromSomewhere() ...
|
||||||
|
|
||||||
throw new AppException("TODO: Everything seems to be working fine. The game engine logic is not implemented yet though...");
|
throw new AppException("TODO: Everything seems to be working fine. The game engine logic is not implemented yet though...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.map.asset;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.game.common.asset.Asset;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
public class GameMapAsset extends Asset {
|
||||||
|
public GameMapAsset(@NonNull String uid, @NonNull String source) {
|
||||||
|
super(uid, source);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.layer.base;
|
package com.bartlomiejpluta.base.game.map.layer.base;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
||||||
import com.bartlomiejpluta.base.core.logic.Updatable;
|
import com.bartlomiejpluta.base.core.logic.Updatable;
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.layer.image;
|
package com.bartlomiejpluta.base.game.map.layer.image;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.shader.constant.UniformName;
|
import com.bartlomiejpluta.base.core.gl.shader.constant.UniformName;
|
||||||
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||||
import com.bartlomiejpluta.base.game.image.model.Image;
|
|
||||||
import com.bartlomiejpluta.base.core.ui.Window;
|
import com.bartlomiejpluta.base.core.ui.Window;
|
||||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.base.Layer;
|
import com.bartlomiejpluta.base.game.image.model.Image;
|
||||||
import com.bartlomiejpluta.base.game.world.map.GameMap;
|
import com.bartlomiejpluta.base.game.map.layer.base.Layer;
|
||||||
|
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||||
|
|
||||||
public class ImageLayer implements Layer {
|
public class ImageLayer implements Layer {
|
||||||
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.layer.object;
|
package com.bartlomiejpluta.base.game.map.layer.object;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||||
import com.bartlomiejpluta.base.core.ui.Window;
|
import com.bartlomiejpluta.base.core.ui.Window;
|
||||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.base.Layer;
|
import com.bartlomiejpluta.base.game.map.layer.base.Layer;
|
||||||
import com.bartlomiejpluta.base.game.world.movement.Direction;
|
import com.bartlomiejpluta.base.game.world.movement.Direction;
|
||||||
import com.bartlomiejpluta.base.game.world.movement.MovableSprite;
|
import com.bartlomiejpluta.base.game.world.movement.MovableSprite;
|
||||||
import org.joml.Vector2i;
|
import org.joml.Vector2i;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.layer.object;
|
package com.bartlomiejpluta.base.game.map.layer.object;
|
||||||
|
|
||||||
public enum PassageAbility {
|
public enum PassageAbility {
|
||||||
BLOCK,
|
BLOCK,
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.layer.tile;
|
package com.bartlomiejpluta.base.game.map.layer.tile;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||||
import com.bartlomiejpluta.base.core.ui.Window;
|
import com.bartlomiejpluta.base.core.ui.Window;
|
||||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.base.Layer;
|
import com.bartlomiejpluta.base.game.map.layer.base.Layer;
|
||||||
import com.bartlomiejpluta.base.game.world.tileset.model.Tile;
|
import com.bartlomiejpluta.base.game.tileset.model.Tile;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.map.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.error.AppException;
|
||||||
|
import com.bartlomiejpluta.base.game.map.asset.GameMapAsset;
|
||||||
|
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||||
|
import com.bartlomiejpluta.base.game.map.serial.MapDeserializer;
|
||||||
|
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 DefaultMapManager implements MapManager {
|
||||||
|
private final Map<String, GameMap> maps = new HashMap<>();
|
||||||
|
private final Map<String, GameMapAsset> assets = new HashMap<>();
|
||||||
|
private final MapDeserializer mapDeserializer;
|
||||||
|
private final ProjectConfiguration configuration;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerAsset(GameMapAsset asset) {
|
||||||
|
log.info("Registering [{}] map asset under UID: [{}]", asset.getSource(), asset.getUid());
|
||||||
|
assets.put(asset.getUid(), asset);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GameMap loadMap(String uid) {
|
||||||
|
var map = maps.get(uid);
|
||||||
|
|
||||||
|
if (map == null) {
|
||||||
|
var asset = assets.get(uid);
|
||||||
|
|
||||||
|
if (asset == null) {
|
||||||
|
throw new AppException("The map asset with UID: [%s] does not exist", uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
var source = configuration.projectFile("maps", asset.getSource());
|
||||||
|
map = mapDeserializer.deserialize(DefaultMapManager.class.getResourceAsStream(source));
|
||||||
|
log.info("Loading map from assets to cache under the key: [{}]", uid);
|
||||||
|
maps.put(uid, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanUp() {
|
||||||
|
log.info("There is nothing to clean up here");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.map.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.gc.Cleanable;
|
||||||
|
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);
|
||||||
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.map;
|
package com.bartlomiejpluta.base.game.map.model;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
||||||
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||||
import com.bartlomiejpluta.base.game.image.model.Image;
|
|
||||||
import com.bartlomiejpluta.base.core.logic.Updatable;
|
import com.bartlomiejpluta.base.core.logic.Updatable;
|
||||||
import com.bartlomiejpluta.base.core.ui.Window;
|
import com.bartlomiejpluta.base.core.ui.Window;
|
||||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.base.Layer;
|
import com.bartlomiejpluta.base.game.image.model.Image;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.image.ImageLayer;
|
import com.bartlomiejpluta.base.game.map.layer.base.Layer;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.game.map.layer.image.ImageLayer;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.object.PassageAbility;
|
import com.bartlomiejpluta.base.game.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.game.world.layer.tile.TileLayer;
|
import com.bartlomiejpluta.base.game.map.layer.object.PassageAbility;
|
||||||
|
import com.bartlomiejpluta.base.game.map.layer.tile.TileLayer;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.model.TileSet;
|
||||||
import com.bartlomiejpluta.base.game.world.movement.MovableSprite;
|
import com.bartlomiejpluta.base.game.world.movement.MovableSprite;
|
||||||
import com.bartlomiejpluta.base.game.world.movement.Movement;
|
import com.bartlomiejpluta.base.game.world.movement.Movement;
|
||||||
import com.bartlomiejpluta.base.game.world.tileset.model.TileSet;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import org.joml.Vector2f;
|
import org.joml.Vector2f;
|
||||||
@@ -23,6 +23,7 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class GameMap implements Renderable, Updatable {
|
public class GameMap implements Renderable, Updatable {
|
||||||
|
@Getter
|
||||||
private final List<Layer> layers = new ArrayList<>();
|
private final List<Layer> layers = new ArrayList<>();
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@@ -62,7 +63,7 @@ public class GameMap implements Renderable, Updatable {
|
|||||||
return new Vector2f(columns * stepSize.x, rows * stepSize.y);
|
return new Vector2f(columns * stepSize.x, rows * stepSize.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameMap createObjectLayer() {
|
public int createObjectLayer() {
|
||||||
var passageMap = new PassageAbility[rows][columns];
|
var passageMap = new PassageAbility[rows][columns];
|
||||||
for (int i = 0; i < rows; ++i) {
|
for (int i = 0; i < rows; ++i) {
|
||||||
Arrays.fill(passageMap[i], 0, columns, PassageAbility.ALLOW);
|
Arrays.fill(passageMap[i], 0, columns, PassageAbility.ALLOW);
|
||||||
@@ -70,19 +71,19 @@ public class GameMap implements Renderable, Updatable {
|
|||||||
|
|
||||||
layers.add(new ObjectLayer(new ArrayList<>(), passageMap));
|
layers.add(new ObjectLayer(new ArrayList<>(), passageMap));
|
||||||
|
|
||||||
return this;
|
return layers.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameMap createTileLayer() {
|
public int createTileLayer() {
|
||||||
layers.add(new TileLayer(rows, columns));
|
layers.add(new TileLayer(rows, columns));
|
||||||
|
|
||||||
return this;
|
return layers.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameMap createImageLayer(Image image, ImageLayer.Mode imageDisplayMode) {
|
public int createImageLayer(Image image, ImageLayer.Mode imageDisplayMode) {
|
||||||
layers.add(new ImageLayer(this, image, imageDisplayMode));
|
layers.add(new ImageLayer(this, image, imageDisplayMode));
|
||||||
|
|
||||||
return this;
|
return layers.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameMap addObject(int layerIndex, MovableSprite object) {
|
public GameMap addObject(int layerIndex, MovableSprite object) {
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.map.serial;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.game.common.serial.Deserializer;
|
||||||
|
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||||
|
|
||||||
|
public abstract class MapDeserializer extends Deserializer<GameMap> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.map.serial;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.error.AppException;
|
||||||
|
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.manager.TileSetManager;
|
||||||
|
import com.bartlomiejpluta.base.proto.GameMapProto;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
|
public class ProtobufMapDeserializer extends MapDeserializer {
|
||||||
|
private final TileSetManager tileSetManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected GameMap parse(InputStream input) throws Exception {
|
||||||
|
var proto = GameMapProto.GameMap.parseFrom(input);
|
||||||
|
var tileSet = tileSetManager.loadTileSet(proto.getTileSetUID());
|
||||||
|
var map = new GameMap(tileSet, proto.getRows(), proto.getColumns());
|
||||||
|
|
||||||
|
proto.getLayersList().forEach(layer -> deserializeLayer(map, layer));
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deserializeLayer(GameMap map, GameMapProto.Layer proto) {
|
||||||
|
if (proto.hasTileLayer()) {
|
||||||
|
deserializeTileLayer(map, proto);
|
||||||
|
} else {
|
||||||
|
throw new AppException("Not supported layer type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deserializeTileLayer(GameMap map, GameMapProto.Layer proto) {
|
||||||
|
var layer = map.createTileLayer();
|
||||||
|
var columns = map.getColumns();
|
||||||
|
var tiles = proto.getTileLayer().getTilesList();
|
||||||
|
|
||||||
|
for (var i = 0; i < tiles.size(); ++i) {
|
||||||
|
var tile = tiles.get(i);
|
||||||
|
|
||||||
|
if (tile == 0) {
|
||||||
|
map.clearTile(layer, i / columns, i % columns);
|
||||||
|
} else {
|
||||||
|
map.setTile(layer, i / columns, i % columns, tile - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.project.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "app.project")
|
||||||
|
public class ProjectConfiguration {
|
||||||
|
private String mainFile;
|
||||||
|
private String resourcePath;
|
||||||
|
|
||||||
|
|
||||||
|
public String projectFile(String... path) {
|
||||||
|
return Path.of(resourcePath, path).toString().replaceAll("\\\\", "/");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.project.loader;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.game.map.manager.MapManager;
|
||||||
|
import com.bartlomiejpluta.base.game.project.config.ProjectConfiguration;
|
||||||
|
import com.bartlomiejpluta.base.game.project.serial.ProjectDeserializer;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.manager.TileSetManager;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
|
public class DefaultProjectLoader implements ProjectLoader {
|
||||||
|
private final ProjectDeserializer projectDeserializer;
|
||||||
|
private final TileSetManager tileSetManager;
|
||||||
|
private final MapManager mapManager;
|
||||||
|
private final ProjectConfiguration configuration;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadProject() {
|
||||||
|
var resource = DefaultProjectLoader.class.getResourceAsStream(configuration.projectFile(configuration.getMainFile()));
|
||||||
|
var project = projectDeserializer.deserialize(resource);
|
||||||
|
project.getTileSetAssets().forEach(tileSetManager::registerAsset);
|
||||||
|
project.getMapAssets().forEach(mapManager::registerAsset);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.project.loader;
|
||||||
|
|
||||||
|
public interface ProjectLoader {
|
||||||
|
void loadProject();
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.project.model;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.game.map.asset.GameMapAsset;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.asset.TileSetAsset;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class Project {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final List<TileSetAsset> tileSetAssets;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final List<GameMapAsset> mapAssets;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.project.serial;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.game.common.serial.Deserializer;
|
||||||
|
import com.bartlomiejpluta.base.game.project.model.Project;
|
||||||
|
|
||||||
|
public abstract class ProjectDeserializer extends Deserializer<Project> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.project.serial;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.game.map.asset.GameMapAsset;
|
||||||
|
import com.bartlomiejpluta.base.game.project.model.Project;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.asset.TileSetAsset;
|
||||||
|
import com.bartlomiejpluta.base.proto.ProjectProto;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ProtobufProjectDeserializer extends ProjectDeserializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Project parse(InputStream input) throws Exception {
|
||||||
|
var proto = ProjectProto.Project.parseFrom(input);
|
||||||
|
var name = proto.getName();
|
||||||
|
var tileSetAssets = proto.getTileSetsList().stream().map(this::parseTileSetAsset).collect(toList());
|
||||||
|
var mapAssets = proto.getMapsList().stream().map(this::parseGameMapAsset).collect(toList());
|
||||||
|
return new Project(name, tileSetAssets, mapAssets);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TileSetAsset parseTileSetAsset(ProjectProto.TileSetAsset proto) {
|
||||||
|
return new TileSetAsset(proto.getUid(), proto.getSource(), proto.getRows(), proto.getColumns());
|
||||||
|
}
|
||||||
|
|
||||||
|
private GameMapAsset parseGameMapAsset(ProjectProto.GameMapAsset proto) {
|
||||||
|
return new GameMapAsset(proto.getUid(), proto.getSource());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.tileset.asset;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.game.common.asset.Asset;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class TileSetAsset extends Asset {
|
||||||
|
private final int rows;
|
||||||
|
private final int columns;
|
||||||
|
|
||||||
|
public TileSetAsset(@NonNull String uid, @NonNull String source, int rows, int columns) {
|
||||||
|
super(uid, source);
|
||||||
|
this.rows = rows;
|
||||||
|
this.columns = columns;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.tileset.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.error.AppException;
|
||||||
|
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
|
||||||
|
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||||
|
import com.bartlomiejpluta.base.game.project.config.ProjectConfiguration;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.asset.TileSetAsset;
|
||||||
|
import com.bartlomiejpluta.base.game.tileset.model.TileSet;
|
||||||
|
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 DefaultTileSetManager implements TileSetManager {
|
||||||
|
private final TextureManager textureManager;
|
||||||
|
private final MeshManager meshManager;
|
||||||
|
private final Map<String, TileSet> tileSets = new HashMap<>();
|
||||||
|
private final Map<String, TileSetAsset> assets = new HashMap<>();
|
||||||
|
private final ProjectConfiguration configuration;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerAsset(TileSetAsset asset) {
|
||||||
|
log.info("Registering [{}] tileset asset under UID: [{}]", asset.getSource(), asset.getUid());
|
||||||
|
assets.put(asset.getUid(), asset);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TileSet loadTileSet(String uid) {
|
||||||
|
var tileset = tileSets.get(uid);
|
||||||
|
|
||||||
|
if (tileset == null) {
|
||||||
|
var asset = assets.get(uid);
|
||||||
|
|
||||||
|
if (asset == null) {
|
||||||
|
throw new AppException("The tile set asset with UID: [%s] does not exist", uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
var source = configuration.projectFile("tilesets", asset.getSource());
|
||||||
|
var texture = textureManager.loadTexture(source, asset.getRows(), asset.getColumns());
|
||||||
|
var size = texture.getSpriteSize();
|
||||||
|
var mesh = meshManager.createQuad(size.x, size.y, 0, 0);
|
||||||
|
tileset = new TileSet(texture, mesh);
|
||||||
|
log.info("Loading tileset from assets to cache under the key: [{}]", uid);
|
||||||
|
tileSets.put(uid, tileset);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tileset;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanUp() {
|
||||||
|
log.info("There is nothing to clean up here");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.bartlomiejpluta.base.game.tileset.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.core.gc.Cleanable;
|
||||||
|
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);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.tileset.model;
|
package com.bartlomiejpluta.base.game.tileset.model;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||||
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.tileset.model;
|
package com.bartlomiejpluta.base.game.tileset.model;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.core.gl.object.texture.Texture;
|
import com.bartlomiejpluta.base.core.gl.object.texture.Texture;
|
||||||
@@ -3,7 +3,7 @@ package com.bartlomiejpluta.base.game.world.entity.manager;
|
|||||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||||
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
||||||
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||||
import com.bartlomiejpluta.base.game.world.map.GameMap;
|
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||||
import com.bartlomiejpluta.base.game.world.entity.config.EntitySpriteConfiguration;
|
import com.bartlomiejpluta.base.game.world.entity.config.EntitySpriteConfiguration;
|
||||||
import com.bartlomiejpluta.base.game.world.entity.model.Entity;
|
import com.bartlomiejpluta.base.game.world.entity.model.Entity;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.bartlomiejpluta.base.game.world.entity.manager;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gc.Cleanable;
|
import com.bartlomiejpluta.base.core.gc.Cleanable;
|
||||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||||
import com.bartlomiejpluta.base.game.world.map.GameMap;
|
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||||
import com.bartlomiejpluta.base.game.world.entity.model.Entity;
|
import com.bartlomiejpluta.base.game.world.entity.model.Entity;
|
||||||
|
|
||||||
public interface EntityManager extends Cleanable {
|
public interface EntityManager extends Cleanable {
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.tileset.manager;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
|
|
||||||
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
|
||||||
import com.bartlomiejpluta.base.game.world.tileset.model.TileSet;
|
|
||||||
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;
|
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
||||||
public class DefaultTileSetManager implements TileSetManager {
|
|
||||||
private final TextureManager textureManager;
|
|
||||||
private final MeshManager meshManager;
|
|
||||||
private final Map<String, TileSet> tileSets = new HashMap<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TileSet createTileSet(String tileSetFileName, int rows, int columns) {
|
|
||||||
var key = format("%dx%d__%s", rows, columns, tileSetFileName);
|
|
||||||
var tileset = tileSets.get(key);
|
|
||||||
|
|
||||||
if (tileset == null) {
|
|
||||||
var texture = textureManager.loadTexture(tileSetFileName, rows, columns);
|
|
||||||
var size = texture.getSpriteSize();
|
|
||||||
var mesh = meshManager.createQuad(size.x, size.y, 0, 0);
|
|
||||||
tileset = new TileSet(texture, mesh);
|
|
||||||
log.info("Loading [{}] tileset to cache under the key: [{}]", tileSetFileName, key);
|
|
||||||
tileSets.put(key, tileset);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tileset;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void cleanUp() {
|
|
||||||
log.info("There is nothing to clean up here");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.bartlomiejpluta.base.game.world.tileset.manager;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.core.gc.Cleanable;
|
|
||||||
import com.bartlomiejpluta.base.game.world.tileset.model.TileSet;
|
|
||||||
|
|
||||||
public interface TileSetManager extends Cleanable {
|
|
||||||
TileSet createTileSet(String tileSetFileName, int rows, int columns);
|
|
||||||
}
|
|
||||||
@@ -7,6 +7,10 @@ app:
|
|||||||
core:
|
core:
|
||||||
targetUps: 50 # Updates per second
|
targetUps: 50 # Updates per second
|
||||||
|
|
||||||
|
project:
|
||||||
|
main-file: project.bep
|
||||||
|
resource-path: /project
|
||||||
|
|
||||||
sprite:
|
sprite:
|
||||||
entity:
|
entity:
|
||||||
dimension:
|
dimension:
|
||||||
|
|||||||
Reference in New Issue
Block a user