Search in sources :

Example 1 with EntityInfoComponent

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());
}
Also used : EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Test(org.junit.Test)

Example 2 with EntityInfoComponent

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;
}
Also used : ComponentMetadata(org.terasology.entitySystem.metadata.ComponentMetadata) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent)

Example 3 with EntityInfoComponent

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());
        }
    }
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent)

Example 4 with EntityInfoComponent

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);
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Test(org.junit.Test)

Aggregations

EntityInfoComponent (org.terasology.entitySystem.entity.internal.EntityInfoComponent)4 Test (org.junit.Test)2 Component (org.terasology.entitySystem.Component)2 EntityData (org.terasology.protobuf.EntityData)2 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 ComponentMetadata (org.terasology.entitySystem.metadata.ComponentMetadata)1 Prefab (org.terasology.entitySystem.prefab.Prefab)1