mmb.engine.worlds.DataLayers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multimachinebuilder Show documentation
Show all versions of multimachinebuilder Show documentation
Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world.
THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<
The newest version!
/**
*
*/
package mmb.engine.worlds;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Set;
import java.util.function.Function;
import com.fasterxml.jackson.databind.JsonNode;
import mmb.NN;
import mmb.engine.GameEvents;
import mmb.engine.debug.Debugger;
import mmb.engine.worlds.universe.Universe;
import mmb.engine.worlds.world.DataLayer;
import mmb.engine.worlds.world.World;
import monniasza.collects.datalayer.IndexedDatalayerMap;
/**
* World and universe data layer utilities
* @author oskar
*/
public class DataLayers {
private DataLayers() {}
private static final Debugger debug = new Debugger("DATA LAYERS");
//Init
private static boolean inited = false;
/** Initializes data layer */
public static void init() {
if(inited) return;
inited = true;
//init logic
}
//Map data layers
@SuppressWarnings("null")
@NN private static final Set>> layersWorld0 =
Collections.newSetFromMap(new IdentityHashMap<>());
/** An unmodifiable set of world data layers */
@NN public static final Set>> layersWorld =
Collections.unmodifiableSet(layersWorld0);
/**
* Sets up a world data layer for given JSON node
* @param type of the data layer
* @param nodeName name of JSON node used
* @param databinder the indexed data layer map
*/
public static > void registerWorldDataLayerUsingNode
(String nodeName, IndexedDatalayerMap<@NN World, T> databinder){
debug.printl("New world data layer: "+nodeName);
boolean add = layersWorld0.add(databinder);
if(!add) throw new IllegalStateException("Data layer "+nodeName+" already exists");
GameEvents.onWorldLoad.addListener(tuple -> {
debug.printl("Loading world data layer: "+nodeName);
JsonNode node = tuple._2.get(nodeName);
T datalayer = databinder.get(tuple._1);
if(datalayer == null) throw new InternalError("Data layer found must not be null for a valid world");
if(node != null) datalayer.load(node);
datalayer.startup();
});
GameEvents.onWorldSave.addListener(tuple -> {
debug.printl("Saving world data layer: "+nodeName);
T datalayer = databinder.get(tuple._1);
if(datalayer == null) throw new InternalError("Data layer found must not be null for a valid world");
datalayer.shutdown();
JsonNode save = datalayer.save();
tuple._2.set(nodeName, save);
});
}
/**
* Creates a world data layer for given JSON node
* @param type of the data layer
* @param nodeName name of JSON node used
* @param populator populator for data layer
* @return a new data layer
*/
@NN public static > IndexedDatalayerMap<@NN World, T> createWorldDataLayerUsingNode
(String nodeName, Function<@NN World, T> populator){
IndexedDatalayerMap<@NN World, T> datalayer = new IndexedDatalayerMap<>(World.allocator, populator);
registerWorldDataLayerUsingNode(nodeName, datalayer);
return datalayer;
}
//Universe data layers
@SuppressWarnings("null")
@NN private static final Set>> layersUniverse0 =
Collections.newSetFromMap(new IdentityHashMap<>());
/** An unmodifiable set of universe data layers */
@NN public static final Set>> layersUniverse =
Collections.unmodifiableSet(layersUniverse0);
/**
* Sets up a world data layer for given JSON node
* @param type of the data layer
* @param nodeName name of JSON node used
* @param databinder the indexed data layer map
*/
public static > void registerUniverseDataLayerUsingNode
(String nodeName, IndexedDatalayerMap databinder){
boolean add = layersUniverse0.add(databinder);
if(!add) throw new IllegalStateException("Data layer "+nodeName+" already exists");
debug.printl("New universe data layer: "+nodeName);
GameEvents.onUniverseLoad.addListener(tuple -> {
JsonNode node = tuple._2.get(nodeName);
T datalayer = databinder.get(tuple._1);
if(datalayer == null) throw new InternalError("Data layer found must not be null for a valid universe");
if(node != null) datalayer.load(node);
datalayer.startup();
});
GameEvents.onUniverseSave.addListener(tuple -> {
T datalayer = databinder.get(tuple._1);
if(datalayer == null) throw new InternalError("Data layer found must not be null for a valid universe");
datalayer.shutdown();
JsonNode save = datalayer.save();
tuple._2.set(nodeName, save);
});
}
}