@PrefabData("prefab/player.json")
public class PlayerPrefab extends Prefab {
private ComponentMapper<Position> positionMapper;
public PlayerPrefab(World world) {
super(world, new FallbackFileResolver());
}
public SaveFileFormat createPlayer(float x, float y) {
SaveFileFormat l = create();
Entity player = l.get("player");
positionMapper.get(player).xy.set(x, y);
return l;
}
}
One Prefab creates 1 type of prefab specialization (uses same source json)
custom PrefabReader<JsonValue> implementations can be used for more dynamic/polymorphic prefabs
@PrefabData(path) is passed to the FileHandleResolver
default implementations cache the JsonValue, to avoid re-parsing the json tree
alternatively, if you need more control: implement PrefabReader<JsonValue> (say because you want to different behavior on desktop vs mobile, e.g. non-caching vs caching)
super.create() is wrapped in a prefab-specific method, aka createPlayer(x, y)
this is the primary reason for the existence of @PrefabData; we need a straightforward approach for finding prefabs+json, and converting .json to .class.
it probably won't work with any esoteric custom PrefabReader<JsonValue> impl
mininmum viable world configuration:
world = new World(new WorldConfiguration()
.setSystem(GroupManager.class) // not strictly necessary
.setSystem(TagManager.class) // not strictly necessary
.setSystem(WorldSerializationManager.class));
JsonArtemisSerializer backend = new JsonArtemisSerializer(world);
backend.prettyPrint(true);
world.getSystem(WorldSerializationManager.class).setSerializer(backend);
saving entities to json
The (/some) tests use:
public static String save(IntBag entities,
WorldSerializationManager manger)
throws Exception {
SaveFileFormat save = new SaveFileFormat(entities);
ByteArrayOutputStream baos = new ByteArrayOutputStream(256);
manger.save(baos, save);
return baos.toString(StandardCharsets.UTF_8.name());
}
Prefab
creates 1 type of prefab specialization (uses same source json)PrefabReader<JsonValue>
implementations can be used for more dynamic/polymorphic prefabs@PrefabData(path)
is passed to theFileHandleResolver
PrefabReader<JsonValue>
(say because you want to different behavior on desktop vs mobile, e.g. non-caching vs caching)super.create()
is wrapped in a prefab-specific method, akacreatePlayer(x, y)
@PrefabData
; we need a straightforward approach for finding prefabs+json, and converting.json
to.class
.PrefabReader<JsonValue>
implmininmum viable world configuration:
saving entities to json
The (/some) tests use: