use of org.terasology.gestalt.entitysystem.component.Component 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).getId().toString());
}
}
}
return prefabData.build();
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class EntitySerializer method createInitialComponents.
/**
* Creates the components for the entity being deserialized based on its prefab (if any)
*
* @param entityData
* @return The mapping of components
*/
private Map<Class<? extends Component>, Component> createInitialComponents(EntityData.Entity entityData) {
Set<ComponentMetadata<?>> removedComponents = Sets.newHashSet();
for (String removedComp : entityData.getRemovedComponentList()) {
ComponentMetadata<?> removedMetadata = componentLibrary.resolve(removedComp);
if (removedMetadata != null) {
removedComponents.add(removedMetadata);
}
}
Map<Class<? extends Component>, Component> componentMap = Maps.newHashMap();
if (entityData.hasParentPrefab() && !entityData.getParentPrefab().isEmpty() && prefabManager.exists(entityData.getParentPrefab())) {
Prefab prefab = prefabManager.getPrefab(entityData.getParentPrefab());
for (Component component : prefab.iterateComponents()) {
ComponentMetadata<?> metadata = componentLibrary.getMetadata(component);
if (!removedComponents.contains(metadata)) {
componentMap.put(component.getClass(), componentLibrary.copy(component));
}
}
componentMap.put(EntityInfoComponent.class, new EntityInfoComponent(prefab, true, prefab.isAlwaysRelevant()));
}
return componentMap;
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method cleanUpTemporaryEntity.
private void cleanUpTemporaryEntity(EntityRef entity) {
Prefab prefab = entity.getParentPrefab();
for (Component comp : entity.iterateComponents()) {
// TODO: should this also check for components listed in `RetainComponentsComponent`?
if (!COMMON_BLOCK_COMPONENTS.contains(comp.getClass()) && (prefab == null || !prefab.hasComponent(comp.getClass()))) {
entity.removeComponent(comp.getClass());
}
}
entity.removeComponent(NetworkComponent.class);
if (prefab != null) {
for (Component comp : prefab.iterateComponents()) {
Component currentComp = entity.getComponent(comp.getClass());
if (currentComp == null) {
entity.addComponent(entityManager.getComponentLibrary().copy(comp));
} else {
ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
boolean changed = false;
for (FieldMetadata field : metadata.getFields()) {
Object expected = field.getValue(comp);
if (!Objects.equal(expected, field.getValue(currentComp))) {
field.setValue(currentComp, expected);
changed = true;
}
}
if (changed) {
entity.saveComponent(currentComp);
}
}
}
}
entityManager.destroyEntityWithoutEvents(entity);
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method updateBlockEntityComponents.
/**
* Transforms a block entity with the change of block type. This is driven from the delta between the old and new
* block type prefabs, but takes into account changes made to the block entity.
* Components contained in `blockEntity` that
* <ul>
* <li>are not "common block components" (e.g. `NetworkComponent`)</li>
* <li>don't have `reatinUnalteredOnBlockChange` metadata</li>
* <li>are not listed in the block prefab</li>
* <li>are not listed in the set of components to be retained</li>
* </ul>
* will be removed.
*
* @param blockEntity The entity to update
* @param oldType The previous type of the block
* @param type The new type of the block
* @param retainComponents List of components to be retained
*/
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
Optional<Prefab> oldPrefab = oldType.getPrefab();
EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
oldEntityBuilder.addComponent(new BlockComponent(oldType, blockComponent.getPosition()));
BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
blockEntity.send(oldEntityEvent);
for (Component comp : oldEntityEvent.getResultComponents()) {
oldEntityBuilder.addComponent(comp);
}
Optional<Prefab> newPrefab = type.getPrefab();
EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
newEntityBuilder.addComponent(new BlockComponent(type, blockComponent.getPosition()));
BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
blockEntity.send(newEntityEvent);
for (Component comp : newEntityEvent.getResultComponents()) {
newEntityBuilder.addComponent(comp);
}
for (Component component : blockEntity.iterateComponents()) {
if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass()) && !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange() && !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
blockEntity.removeComponent(component.getClass());
}
}
BlockComponent newBlockComponent = new BlockComponent(type, blockComponent.getPosition());
blockEntity.saveComponent(newBlockComponent);
for (Component comp : newEntityBuilder.iterateComponents()) {
copyIntoPrefab(blockEntity, comp, retainComponents);
}
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class BehaviorEditorScreen method onEntitySelected.
private void onEntitySelected(Interpreter value, PropertyProvider provider) {
if (selectedInterpreter != null) {
selectedInterpreter.setCallback(null);
}
selectedInterpreter = value;
if (selectedInterpreter != null) {
EntityRef entity = value.actor().getEntity();
onTreeSelected(selectedInterpreter.getTree());
entityProperties.clear();
for (Component component : entity.iterateComponents()) {
String name = component.getClass().getSimpleName().replace("Component", "");
List<Property<?, ?>> componentProperties = provider.createProperties(component);
entityProperties.addProperties(name, componentProperties);
}
selectedInterpreter.setCallback(behaviorEditor);
}
}
Aggregations