Create MapHandler API scaffolding
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.map;
|
||||
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
|
||||
public interface MapHandler {
|
||||
void init(Context context);
|
||||
}
|
||||
@@ -56,6 +56,7 @@ class MainController : Controller() {
|
||||
val map = GameMap(tileSet).apply {
|
||||
rows = vm.rows
|
||||
columns = vm.columns
|
||||
handler = vm.handler
|
||||
}
|
||||
projectContext.importMap(vm.name, map)
|
||||
openItems[scope] = map
|
||||
|
||||
@@ -2,13 +2,11 @@ package com.bartlomiejpluta.base.editor.map.model.map
|
||||
|
||||
import com.bartlomiejpluta.base.editor.map.model.layer.Layer
|
||||
import com.bartlomiejpluta.base.editor.tileset.model.TileSet
|
||||
import javafx.beans.property.ReadOnlyStringWrapper
|
||||
import javafx.beans.property.SimpleDoubleProperty
|
||||
import javafx.beans.property.SimpleIntegerProperty
|
||||
import javafx.beans.property.SimpleStringProperty
|
||||
import javafx.collections.FXCollections
|
||||
import tornadofx.getValue
|
||||
import tornadofx.observableListOf
|
||||
import tornadofx.setValue
|
||||
|
||||
|
||||
@@ -35,6 +33,9 @@ class GameMap(val tileSet: TileSet) {
|
||||
var height by heightProperty
|
||||
private set
|
||||
|
||||
val handlerProperty = SimpleStringProperty()
|
||||
var handler by handlerProperty
|
||||
|
||||
init {
|
||||
rowsProperty.addListener { _, _, newValue ->
|
||||
val newRows = newValue.toInt()
|
||||
|
||||
@@ -19,4 +19,7 @@ class GameMapBuilder {
|
||||
|
||||
val columnsProperty = SimpleIntegerProperty(20)
|
||||
var columns by columnsProperty
|
||||
|
||||
val handlerProperty = SimpleStringProperty()
|
||||
var handler by handlerProperty
|
||||
}
|
||||
@@ -25,6 +25,7 @@ class ProtobufMapDeserializer : MapDeserializer {
|
||||
map.uid = proto.uid
|
||||
map.rows = proto.rows
|
||||
map.columns = proto.columns
|
||||
map.handler = proto.handler
|
||||
|
||||
proto.layersList.forEach {
|
||||
map.layers.add(deserializeLayer(map.rows, map.columns, tileSet, it))
|
||||
|
||||
@@ -17,6 +17,7 @@ class ProtobufMapSerializer : MapSerializer {
|
||||
protoMap.rows = item.rows
|
||||
protoMap.columns = item.columns
|
||||
protoMap.tileSetUID = item.tileSet.uid
|
||||
protoMap.handler = item.handler
|
||||
|
||||
item.layers.forEach { layer -> protoMap.addLayers(serializeLayer(layer)) }
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.base.editor.map.view.editor
|
||||
|
||||
import com.bartlomiejpluta.base.editor.common.parameter.model.IntegerParameter
|
||||
import com.bartlomiejpluta.base.editor.common.parameter.model.JavaClassParameter
|
||||
import com.bartlomiejpluta.base.editor.common.parameter.view.ParametersTableFragment
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
|
||||
import tornadofx.View
|
||||
@@ -12,6 +13,7 @@ class MapParameters : View() {
|
||||
private val parameters = observableListOf(
|
||||
IntegerParameter("rows", mapVM.rows, 1, 100).apply { bindBidirectional(mapVM.item.rowsProperty) },
|
||||
IntegerParameter("columns", mapVM.columns, 1, 100).apply { bindBidirectional(mapVM.item.columnsProperty) },
|
||||
JavaClassParameter("handler", mapVM.handler).apply { bindBidirectional(mapVM.item.handlerProperty) }
|
||||
)
|
||||
|
||||
override val root = find<ParametersTableFragment>(ParametersTableFragment::parameters to parameters).root
|
||||
|
||||
@@ -7,7 +7,12 @@ import tornadofx.*
|
||||
class MapCreationBasicDataView : View("Basic Data") {
|
||||
private val mapBuilderVM = find<GameMapBuilderVM>()
|
||||
|
||||
override val complete = mapBuilderVM.valid(mapBuilderVM.nameProperty, mapBuilderVM.rowsProperty, mapBuilderVM.columnsProperty)
|
||||
override val complete = mapBuilderVM.valid(
|
||||
mapBuilderVM.nameProperty,
|
||||
mapBuilderVM.rowsProperty,
|
||||
mapBuilderVM.columnsProperty,
|
||||
mapBuilderVM.handlerProperty
|
||||
)
|
||||
|
||||
override val root = form {
|
||||
fieldset("Map Settings") {
|
||||
@@ -47,6 +52,13 @@ class MapCreationBasicDataView : View("Basic Data") {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
field("Map Handler class") {
|
||||
textfield(mapBuilderVM.handlerProperty) {
|
||||
required()
|
||||
trimWhitespace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,7 @@ class GameMapBuilderVM : ItemViewModel<GameMapBuilder>(GameMapBuilder()) {
|
||||
|
||||
val columnsProperty = bind(GameMapBuilder::columnsProperty, autocommit = true)
|
||||
var columns by columnsProperty
|
||||
|
||||
val handlerProperty = bind(GameMapBuilder::handlerProperty, autocommit = true)
|
||||
var handler by handlerProperty
|
||||
}
|
||||
@@ -30,6 +30,9 @@ class GameMapVM(map: GameMap) : ItemViewModel<GameMap>(map) {
|
||||
|
||||
val heightProperty = bind(GameMap::heightProperty)
|
||||
val height by heightProperty
|
||||
|
||||
val handlerProperty = bind(GameMap::handlerProperty)
|
||||
var handler by handlerProperty
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ class DefaultProjectContext : ProjectContext {
|
||||
it.maps += asset
|
||||
|
||||
save()
|
||||
javaClassService.createClassFile(map.handler, it.codeFSNode, "map_handler.ftl")
|
||||
File(it.mapsDirectory, asset.source).outputStream().use { fos -> mapSerializer.serialize(map, fos) }
|
||||
}
|
||||
}
|
||||
|
||||
12
editor/src/main/resources/java_templates/map_handler.ftl
Normal file
12
editor/src/main/resources/java_templates/map_handler.ftl
Normal file
@@ -0,0 +1,12 @@
|
||||
package ${package};
|
||||
|
||||
import com.bartlomiejpluta.base.api.map.MapHandler;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
|
||||
public class ${className} implements MapHandler {
|
||||
|
||||
@Override
|
||||
public void init(Context context) {
|
||||
throw new RuntimeException("Not implemented yet");
|
||||
}
|
||||
}
|
||||
@@ -47,13 +47,17 @@ public class GameMap implements Renderable, Updatable {
|
||||
@Getter
|
||||
private final Vector2f stepSize;
|
||||
|
||||
public GameMap(TileSet tileSet, int rows, int columns) {
|
||||
@Getter
|
||||
private final String handler;
|
||||
|
||||
public GameMap(TileSet tileSet, int rows, int columns, String handler) {
|
||||
this.tileSet = tileSet;
|
||||
this.rows = rows;
|
||||
this.columns = columns;
|
||||
this.stepSize = new Vector2f(tileSet.getTileSet().getSpriteSize());
|
||||
this.width = columns * stepSize.x;
|
||||
this.height = rows * stepSize.y;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,7 +27,7 @@ public class ProtobufMapDeserializer extends MapDeserializer {
|
||||
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());
|
||||
var map = new GameMap(tileSet, proto.getRows(), proto.getColumns(), proto.getHandler());
|
||||
|
||||
proto.getLayersList().forEach(layer -> deserializeLayer(map, layer));
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.base.game.project.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.map.MapHandler;
|
||||
import com.bartlomiejpluta.base.core.gl.object.texture.TextureManager;
|
||||
import com.bartlomiejpluta.base.core.gl.render.Renderable;
|
||||
import com.bartlomiejpluta.base.core.gl.shader.manager.ShaderManager;
|
||||
@@ -11,9 +12,11 @@ import com.bartlomiejpluta.base.core.world.camera.Camera;
|
||||
import com.bartlomiejpluta.base.game.image.manager.ImageManager;
|
||||
import com.bartlomiejpluta.base.game.map.manager.MapManager;
|
||||
import com.bartlomiejpluta.base.game.map.model.GameMap;
|
||||
import com.bartlomiejpluta.base.game.project.loader.ClassLoader;
|
||||
import com.bartlomiejpluta.base.game.tileset.manager.TileSetManager;
|
||||
import com.bartlomiejpluta.base.game.world.entity.manager.EntityManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -26,12 +29,19 @@ public class RenderableContext implements Context, Updatable, Renderable {
|
||||
private final EntityManager entityManager;
|
||||
private final ImageManager imageManager;
|
||||
private final MapManager mapManager;
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
private GameMap map;
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void openMap(String mapUid) {
|
||||
map = mapManager.loadMap(mapUid);
|
||||
|
||||
var handlerClass = classLoader.<MapHandler>loadClass(map.getHandler());
|
||||
var handler = handlerClass.getConstructor().newInstance();
|
||||
|
||||
handler.init(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,6 +9,7 @@ message GameMap {
|
||||
required uint32 columns = 3;
|
||||
required string tileSetUID = 4;
|
||||
repeated Layer layers = 5;
|
||||
required string handler = 6;
|
||||
}
|
||||
|
||||
message Layer {
|
||||
|
||||
Reference in New Issue
Block a user