use of org.terasology.entitySystem.entity.internal.EntityInfoComponent in project Terasology by MovingBlocks.
the class BaseEntityRefTest method testCreateWithScopeInfoComponent.
@Test
public void testCreateWithScopeInfoComponent() {
EntityInfoComponent info = new EntityInfoComponent();
info.scope = SECTOR;
EntityInfoComponent info2 = new EntityInfoComponent();
info2.scope = SECTOR;
ref = entityManager.create(info);
assertEquals(SECTOR, ref.getScope());
long safeId = ref.getId();
ref.destroy();
ref = entityManager.createEntityWithId(safeId, Lists.newArrayList(info2));
assertNotNull(ref);
assertNotEquals(EntityRef.NULL, ref);
assertEquals(SECTOR, ref.getScope());
}
use of org.terasology.entitySystem.entity.internal.EntityInfoComponent 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.entitySystem.entity.internal.EntityInfoComponent in project Terasology by MovingBlocks.
the class EntitySerializer method deserializeOntoComponents.
/**
* Deserializes the components from an EntityData onto a map of components
*
* @param entityData
* @param componentMap
*/
private void deserializeOntoComponents(EntityData.Entity entityData, Map<Class<? extends Component>, Component> componentMap) {
EntityInfoComponent entityInfo = (EntityInfoComponent) componentMap.get(EntityInfoComponent.class);
if (entityInfo == null) {
entityInfo = new EntityInfoComponent();
componentMap.put(EntityInfoComponent.class, entityInfo);
}
if (entityData.hasOwner()) {
entityInfo.owner = entityManager.getEntity(entityData.getOwner());
}
if (entityData.hasAlwaysRelevant()) {
entityInfo.alwaysRelevant = entityData.getAlwaysRelevant();
}
switch(entityData.getScope()) {
case GLOBAL:
entityInfo.scope = EntityScope.GLOBAL;
break;
case SECTOR:
entityInfo.scope = EntityScope.SECTOR;
break;
case CHUNK:
entityInfo.scope = EntityScope.CHUNK;
break;
}
for (EntityData.Component componentData : entityData.getComponentList()) {
ComponentMetadata<? extends Component> metadata = componentSerializer.getComponentMetadata(componentData);
if (metadata == null || !componentSerializeCheck.serialize(metadata)) {
continue;
}
Component existingComponent = componentMap.get(metadata.getType());
if (existingComponent == null) {
Component newComponent = componentSerializer.deserialize(componentData);
componentMap.put(metadata.getType(), newComponent);
} else {
componentSerializer.deserializeOnto(existingComponent, componentData, FieldSerializeCheck.NullCheck.<Component>newInstance());
}
}
}
use of org.terasology.entitySystem.entity.internal.EntityInfoComponent in project Terasology by MovingBlocks.
the class EntitySerializerTest method testPrefabMaintainedOverSerialization.
@Test
public void testPrefabMaintainedOverSerialization() throws Exception {
EntityRef entity = entityManager.create(prefab);
EntityData.Entity entityData = entitySerializer.serialize(entity);
long nextId = entityManager.getNextId();
entityManager.clear();
entityManager.setNextId(nextId);
EntityRef newEntity = entitySerializer.deserialize(entityData);
assertTrue(newEntity.hasComponent(EntityInfoComponent.class));
EntityInfoComponent comp = newEntity.getComponent(EntityInfoComponent.class);
assertEquals(prefab, comp.parentPrefab);
}
Aggregations