use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class PrefabSerializer method serialize.
/**
* @param prefab
* @return The serialized prefab
*/
public EntityData.Prefab serialize(Prefab prefab) {
EntityData.Prefab.Builder prefabData = EntityData.Prefab.newBuilder();
prefabData.setName(prefab.getName());
if (prefab.getParent() != null) {
prefabData.setParentName(prefab.getParent().getName());
}
prefabData.setAlwaysRelevant(prefab.isAlwaysRelevant());
prefabData.setPersisted(prefab.isPersisted());
// Delta off the parent
for (Component component : prefab.iterateComponents()) {
if (prefab.getParent() != null && prefab.getParent().hasComponent(component.getClass())) {
EntityData.Component serializedComponent = componentSerializer.serialize(prefab.getParent().getComponent(component.getClass()), component);
if (serializedComponent != null) {
prefabData.addComponent(serializedComponent);
}
} else {
prefabData.addComponent(componentSerializer.serialize(component));
}
}
if (prefab.getParent() != null) {
for (Component parentComp : prefab.getParent().iterateComponents()) {
if (!prefab.hasComponent(parentComp.getClass())) {
prefabData.addRemovedComponent(componentLibrary.getMetadata(parentComp).getUri().toString());
}
}
}
return prefabData.build();
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class WorldSerializerImpl method serializeWorld.
@Override
public EntityData.GlobalStore serializeWorld(boolean verbose) {
final EntityData.GlobalStore.Builder world = EntityData.GlobalStore.newBuilder();
if (!verbose) {
writeComponentTypeTable(world);
}
for (Prefab prefab : prefabManager.listPrefabs()) {
world.addPrefab(prefabSerializer.serialize(prefab));
}
for (EntityRef entity : entityManager.getAllEntities()) {
if (verbose || entity.isPersistent()) {
world.addEntity(entitySerializer.serialize(entity));
}
}
writeIdInfo(world);
entitySerializer.removeComponentIdMapping();
prefabSerializer.removeComponentIdMapping();
return world.build();
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class CharacterSystem method getInstigatorName.
/**
* Extracts the name from an entity.
* If the entity is a character, then the display name from the {@link ClientComponent#clientInfo} is used.
* Otherwise the entity itself is checked for a {@link DisplayNameComponent}.
* In the last case, the prefab name of the entity is used, e.g. "engine:player" will be parsed to "Player".
* @param instigator The entity for which an instigator name is needed.
* @return The instigator name.
*/
public String getInstigatorName(EntityRef instigator) {
if (instigator.hasComponent(CharacterComponent.class)) {
EntityRef instigatorClient = instigator.getComponent(CharacterComponent.class).controller;
EntityRef instigatorClientInfo = instigatorClient.getComponent(ClientComponent.class).clientInfo;
DisplayNameComponent displayNameComponent = instigatorClientInfo.getComponent(DisplayNameComponent.class);
return displayNameComponent.name;
} else if (instigator.getParentPrefab() != null) {
// A DisplayName can be specified in the entity prefab
// Otherwise, the game will attempt to generate one from the name of that prefab
Prefab parentPrefab = instigator.getParentPrefab();
if (parentPrefab.hasComponent(DisplayNameComponent.class)) {
DisplayNameComponent displayNameComponent = parentPrefab.getComponent(DisplayNameComponent.class);
return displayNameComponent.name;
} else {
String instigatorName = parentPrefab.getName();
// getParentPrefab.getName() returns a ResourceUrn String such as "engine:player"
// The following calls change the damage type to be more readable
// For instance, "engine:player" becomes "Player"
instigatorName = instigatorName.replaceAll(".*:(.*)", "$1");
instigatorName = Character.toUpperCase(instigatorName.charAt(0)) + instigatorName.substring(1);
return instigatorName;
}
} else {
return null;
}
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class VisualCharacterSystem method onCreateDefaultVisualCharacter.
@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
public void onCreateDefaultVisualCharacter(CreateVisualCharacterEvent event, EntityRef characterEntity) {
Prefab prefab = assetManager.getAsset("engine:defaultVisualCharacter", Prefab.class).get();
EntityBuilder entityBuilder = event.getVisualCharacterBuilder();
entityBuilder.addPrefab(prefab);
event.consume();
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class ModuleConfigSystem method preBegin.
@Override
public void preBegin() {
for (Prefab prefab : prefabManager.listPrefabs(ModuleConfigComponent.class)) {
ModuleConfigComponent moduleConfig = prefab.getComponent(ModuleConfigComponent.class);
String moduleName = moduleConfig.moduleName;
Map<String, String> properties;
if (propertiesPerModule.containsKey(moduleName)) {
logger.error("Encountered more than one Module Config for module - " + moduleName + ", this is not recommended, as the property values visible are not going to be well defined.");
properties = propertiesPerModule.get(moduleName);
} else {
properties = new HashMap<>();
propertiesPerModule.put(moduleName, properties);
}
properties.putAll(moduleConfig.properties);
}
}
Aggregations