Create GameMap API scaffolding
This commit is contained in:
@@ -12,4 +12,6 @@ public interface Entity {
|
||||
Direction getFaceDirection();
|
||||
|
||||
void setFaceDirection(Direction direction);
|
||||
|
||||
void setSpeed(float speed);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.bartlomiejpluta.base.api.map;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.entity.Movement;
|
||||
|
||||
public interface GameMap {
|
||||
void addEntity(int layerIndex, Entity entity);
|
||||
|
||||
void removeEntity(int layerIndex, Entity entity);
|
||||
|
||||
boolean isMovementPossible(int layerIndex, Movement movement);
|
||||
}
|
||||
@@ -4,7 +4,9 @@ import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.input.Keyboard;
|
||||
|
||||
public interface MapHandler {
|
||||
void init(Context context);
|
||||
void init(Context context, GameMap map);
|
||||
|
||||
void input(Keyboard keyboard);
|
||||
|
||||
void update(Context context, GameMap map, float dt);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.geo.Vector;
|
||||
import com.bartlomiejpluta.base.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.core.util.math.MathUtil;
|
||||
import com.bartlomiejpluta.base.game.entity.config.EntitySpriteConfiguration;
|
||||
import com.bartlomiejpluta.base.game.movement.MovableSprite;
|
||||
import lombok.EqualsAndHashCode;
|
||||
@@ -77,6 +78,11 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
setCoordinates(coordinates.x, coordinates.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpeed(float speed) {
|
||||
setMovementSlowness((int) (1 / MathUtil.clamp(speed, 0.01, 1.0)));
|
||||
}
|
||||
|
||||
public DefaultEntity(Mesh mesh, Material material, EntitySpriteConfiguration configuration) {
|
||||
super(mesh, material);
|
||||
this.defaultSpriteColumn = configuration.getDefaultSpriteColumn();
|
||||
|
||||
@@ -67,6 +67,7 @@ public class DefaultGameLogic implements GameLogic {
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
context.update(dt);
|
||||
fpsMonitor.update(dt);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,14 @@ import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||
import com.bartlomiejpluta.base.core.world.object.Sprite;
|
||||
import com.bartlomiejpluta.base.game.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||
import com.bartlomiejpluta.base.game.map.model.DefaultGameMap;
|
||||
import lombok.NonNull;
|
||||
|
||||
public class ColorLayer extends Sprite implements Layer {
|
||||
private final float mapWidth;
|
||||
private final float mapHeight;
|
||||
|
||||
public ColorLayer(@NonNull MeshManager meshManager, @NonNull GameMap map, float r, float g, float b, float alpha) {
|
||||
public ColorLayer(@NonNull MeshManager meshManager, @NonNull DefaultGameMap map, float r, float g, float b, float alpha) {
|
||||
super(meshManager.createQuad(1, 1, 0, 0), Material.colored(r, g, b, alpha));
|
||||
this.mapWidth = map.getWidth();
|
||||
this.mapHeight = map.getHeight();
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.bartlomiejpluta.base.core.ui.Window;
|
||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||
import com.bartlomiejpluta.base.game.image.model.Image;
|
||||
import com.bartlomiejpluta.base.game.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||
import com.bartlomiejpluta.base.game.map.model.DefaultGameMap;
|
||||
import lombok.NonNull;
|
||||
|
||||
public class ImageLayer implements Layer {
|
||||
@@ -28,7 +28,7 @@ public class ImageLayer implements Layer {
|
||||
|
||||
private boolean parallax;
|
||||
|
||||
public ImageLayer(GameMap map, Image image, float opacity, float x, float y, float scaleX, float scaleY, ImageLayerMode mode, boolean parallax) {
|
||||
public ImageLayer(DefaultGameMap map, Image image, float opacity, float x, float y, float scaleX, float scaleY, ImageLayerMode mode, boolean parallax) {
|
||||
this.mapWidth = map.getWidth();
|
||||
this.mapHeight = map.getHeight();
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ 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.model.DefaultGameMap;
|
||||
import com.bartlomiejpluta.base.game.map.serial.MapDeserializer;
|
||||
import com.bartlomiejpluta.base.game.project.config.ProjectConfiguration;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -17,7 +17,7 @@ import java.util.Map;
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class DefaultMapManager implements MapManager {
|
||||
private final Map<String, GameMap> maps = new HashMap<>();
|
||||
private final Map<String, DefaultGameMap> maps = new HashMap<>();
|
||||
private final Map<String, GameMapAsset> assets = new HashMap<>();
|
||||
private final MapDeserializer mapDeserializer;
|
||||
private final ProjectConfiguration configuration;
|
||||
@@ -29,7 +29,7 @@ public class DefaultMapManager implements MapManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameMap loadObject(String uid) {
|
||||
public DefaultGameMap loadObject(String uid) {
|
||||
var map = maps.get(uid);
|
||||
|
||||
if (map == null) {
|
||||
|
||||
@@ -3,7 +3,7 @@ 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;
|
||||
import com.bartlomiejpluta.base.game.map.model.DefaultGameMap;
|
||||
|
||||
public interface MapManager extends AssetManager<GameMapAsset, GameMap>, Cleanable {
|
||||
public interface MapManager extends AssetManager<GameMapAsset, DefaultGameMap>, Cleanable {
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package com.bartlomiejpluta.base.game.map.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.entity.Movement;
|
||||
import com.bartlomiejpluta.base.api.map.GameMap;
|
||||
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
||||
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||
import com.bartlomiejpluta.base.core.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.core.ui.Window;
|
||||
import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||
import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||
import com.bartlomiejpluta.base.game.entity.model.DefaultEntity;
|
||||
import com.bartlomiejpluta.base.game.image.model.Image;
|
||||
import com.bartlomiejpluta.base.game.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.game.map.layer.color.ColorLayer;
|
||||
@@ -25,7 +28,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class GameMap implements Renderable, Updatable {
|
||||
public class DefaultGameMap implements Renderable, Updatable, GameMap {
|
||||
@Getter
|
||||
private final List<Layer> layers = new ArrayList<>();
|
||||
|
||||
@@ -50,7 +53,7 @@ public class GameMap implements Renderable, Updatable {
|
||||
@Getter
|
||||
private final String handler;
|
||||
|
||||
public GameMap(TileSet tileSet, int rows, int columns, String handler) {
|
||||
public DefaultGameMap(TileSet tileSet, int rows, int columns, String handler) {
|
||||
this.tileSet = tileSet;
|
||||
this.rows = rows;
|
||||
this.columns = columns;
|
||||
@@ -107,48 +110,56 @@ public class GameMap implements Renderable, Updatable {
|
||||
return layers.size() - 1;
|
||||
}
|
||||
|
||||
public GameMap setTile(int layerIndex, int row, int column, int tileId) {
|
||||
public DefaultGameMap setTile(int layerIndex, int row, int column, int tileId) {
|
||||
((TileLayer) layers.get(layerIndex)).setTile(row, column, tileSet.tileById(tileId));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public GameMap setTile(int layerIndex, int row, int column, int tileSetRow, int tileSetColumn) {
|
||||
public DefaultGameMap setTile(int layerIndex, int row, int column, int tileSetRow, int tileSetColumn) {
|
||||
((TileLayer) layers.get(layerIndex)).setTile(row, column, tileSet.tileAt(tileSetRow, tileSetColumn));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public GameMap clearTile(int layerIndex, int row, int column) {
|
||||
public DefaultGameMap clearTile(int layerIndex, int row, int column) {
|
||||
((TileLayer) layers.get(layerIndex)).setTile(row, column, null);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public GameMap addObject(int layerIndex, MovableSprite object) {
|
||||
((ObjectLayer) layers.get(layerIndex)).addObject(object);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public GameMap removeObject(int layerIndex, MovableSprite object) {
|
||||
public DefaultGameMap removeObject(int layerIndex, MovableSprite object) {
|
||||
((ObjectLayer) layers.get(layerIndex)).removeObject(object);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public GameMap setPassageAbility(int layerIndex, int row, int column, PassageAbility passageAbility) {
|
||||
public DefaultGameMap setPassageAbility(int layerIndex, int row, int column, PassageAbility passageAbility) {
|
||||
((ObjectLayer) layers.get(layerIndex)).setPassageAbility(row, column, passageAbility);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public GameMap setColor(int layerIndex, float r, float g, float b, float alpha) {
|
||||
public DefaultGameMap setColor(int layerIndex, float r, float g, float b, float alpha) {
|
||||
((ColorLayer) layers.get(layerIndex)).setColor(r, g, b, alpha);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addEntity(int layerIndex, Entity entity) {
|
||||
var object = (DefaultEntity) entity;
|
||||
object.setStepSize(stepSize.x, stepSize.y);
|
||||
|
||||
((ObjectLayer) layers.get(layerIndex)).addObject(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEntity(int layerIndex, Entity entity) {
|
||||
((ObjectLayer) layers.get(layerIndex)).removeObject((DefaultEntity) entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMovementPossible(int layerIndex, Movement movement) {
|
||||
var target = movement.getTo();
|
||||
|
||||
@@ -1,7 +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;
|
||||
import com.bartlomiejpluta.base.game.map.model.DefaultGameMap;
|
||||
|
||||
public abstract class MapDeserializer extends Deserializer<GameMap> {
|
||||
public abstract class MapDeserializer extends Deserializer<DefaultGameMap> {
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.bartlomiejpluta.base.core.util.mesh.MeshManager;
|
||||
import com.bartlomiejpluta.base.game.image.manager.ImageManager;
|
||||
import com.bartlomiejpluta.base.game.map.layer.image.ImageLayerMode;
|
||||
import com.bartlomiejpluta.base.game.map.layer.object.PassageAbility;
|
||||
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||
import com.bartlomiejpluta.base.game.map.model.DefaultGameMap;
|
||||
import com.bartlomiejpluta.base.game.tileset.manager.TileSetManager;
|
||||
import com.bartlomiejpluta.base.proto.GameMapProto;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -24,17 +24,17 @@ public class ProtobufMapDeserializer extends MapDeserializer {
|
||||
private final ImageManager imageManager;
|
||||
|
||||
@Override
|
||||
protected GameMap parse(InputStream input) throws Exception {
|
||||
protected DefaultGameMap parse(InputStream input) throws Exception {
|
||||
var proto = GameMapProto.GameMap.parseFrom(input);
|
||||
var tileSet = tileSetManager.loadObject(proto.getTileSetUID());
|
||||
var map = new GameMap(tileSet, proto.getRows(), proto.getColumns(), proto.getHandler());
|
||||
var map = new DefaultGameMap(tileSet, proto.getRows(), proto.getColumns(), proto.getHandler());
|
||||
|
||||
proto.getLayersList().forEach(layer -> deserializeLayer(map, layer));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private void deserializeLayer(GameMap map, GameMapProto.Layer proto) {
|
||||
private void deserializeLayer(DefaultGameMap map, GameMapProto.Layer proto) {
|
||||
if (proto.hasTileLayer()) {
|
||||
deserializeTileLayer(map, proto);
|
||||
} else if (proto.hasObjectLayer()) {
|
||||
@@ -48,7 +48,7 @@ public class ProtobufMapDeserializer extends MapDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
private void deserializeTileLayer(GameMap map, GameMapProto.Layer proto) {
|
||||
private void deserializeTileLayer(DefaultGameMap map, GameMapProto.Layer proto) {
|
||||
var layer = map.createTileLayer();
|
||||
var columns = map.getColumns();
|
||||
var tiles = proto.getTileLayer().getTilesList();
|
||||
@@ -64,7 +64,7 @@ public class ProtobufMapDeserializer extends MapDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
private void deserializeObjectLayer(GameMap map, GameMapProto.Layer proto) {
|
||||
private void deserializeObjectLayer(DefaultGameMap map, GameMapProto.Layer proto) {
|
||||
var layer = map.createObjectLayer();
|
||||
var columns = map.getColumns();
|
||||
var passageMap = proto.getObjectLayer().getPassageMapList();
|
||||
@@ -81,7 +81,7 @@ public class ProtobufMapDeserializer extends MapDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
private void deserializeColorLayer(GameMap map, GameMapProto.Layer proto) {
|
||||
private void deserializeColorLayer(DefaultGameMap map, GameMapProto.Layer proto) {
|
||||
var protoColorLayer = proto.getColorLayer();
|
||||
map.createColorLayer(
|
||||
meshManager,
|
||||
@@ -92,7 +92,7 @@ public class ProtobufMapDeserializer extends MapDeserializer {
|
||||
);
|
||||
}
|
||||
|
||||
private void deserializeImageLayer(GameMap map, GameMapProto.Layer proto) {
|
||||
private void deserializeImageLayer(DefaultGameMap map, GameMapProto.Layer proto) {
|
||||
var protoImageLayer = proto.getImageLayer();
|
||||
var image = imageManager.loadObject(protoImageLayer.getImageUID());
|
||||
|
||||
|
||||
@@ -81,6 +81,7 @@ public abstract class MovableSprite extends AnimatedSprite implements Updatable
|
||||
public MovableSprite setStepSize(float x, float y) {
|
||||
coordinateStepSize.x = x;
|
||||
coordinateStepSize.y = y;
|
||||
setCoordinates(coordinates);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import com.bartlomiejpluta.base.game.entity.manager.EntityManager;
|
||||
import com.bartlomiejpluta.base.game.image.manager.ImageManager;
|
||||
import com.bartlomiejpluta.base.game.input.GLFWKeyboard;
|
||||
import com.bartlomiejpluta.base.game.map.manager.MapManager;
|
||||
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||
import com.bartlomiejpluta.base.game.map.model.DefaultGameMap;
|
||||
import com.bartlomiejpluta.base.game.project.loader.ClassLoader;
|
||||
import com.bartlomiejpluta.base.game.tileset.manager.TileSetManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -35,7 +35,7 @@ public class RenderableContext implements Context, Updatable, Renderable {
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
private Keyboard keyboard;
|
||||
private GameMap map;
|
||||
private DefaultGameMap map;
|
||||
private MapHandler mapHandler;
|
||||
|
||||
public void init(Window window) {
|
||||
@@ -50,7 +50,7 @@ public class RenderableContext implements Context, Updatable, Renderable {
|
||||
var handlerClass = classLoader.<MapHandler>loadClass(map.getHandler());
|
||||
mapHandler = handlerClass.getConstructor().newInstance();
|
||||
|
||||
mapHandler.init(this);
|
||||
mapHandler.init(this, map);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,6 +64,10 @@ public class RenderableContext implements Context, Updatable, Renderable {
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
if (mapHandler != null) {
|
||||
mapHandler.update(this, map, dt);
|
||||
}
|
||||
|
||||
if (map != null) {
|
||||
map.update(dt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user