Search in sources :

Example 1 with EntityInfoComponent

use of org.terasology.engine.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.engine.entitySystem.entity.EntityRef) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent) Test(org.junit.jupiter.api.Test)

Example 2 with EntityInfoComponent

use of org.terasology.engine.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.engine.entitySystem.metadata.ComponentMetadata) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.gestalt.entitysystem.component.Component) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)

Example 3 with EntityInfoComponent

use of org.terasology.engine.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.engine.entitySystem.entity.internal.EntityInfoComponent) Test(org.junit.jupiter.api.Test)

Example 4 with EntityInfoComponent

use of org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent in project Terasology by MovingBlocks.

the class EntityBuilder method addPrefab.

/**
 * Adds all of the components from a prefab to this builder
 *
 * @param prefab the prefab to add
 * @return whether the prefab was successfully added
 */
public void addPrefab(Prefab prefab) {
    if (prefab != null) {
        for (Component component : prefab.iterateComponents()) {
            Component componentCopy = entityManager.getComponentLibrary().copy(component);
            addComponent(verifyNotNull(componentCopy, "Component %s not registered (in prefab %s)", component, prefab));
        }
        addComponent(new EntityInfoComponent(prefab, prefab.isPersisted(), prefab.isAlwaysRelevant()));
    } else {
        addComponent(new EntityInfoComponent());
    }
}
Also used : EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.gestalt.entitysystem.component.Component) OnAddedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)

Example 5 with EntityInfoComponent

use of org.terasology.engine.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.engine.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.gestalt.entitysystem.component.Component) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)

Aggregations

EntityInfoComponent (org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)5 Component (org.terasology.gestalt.entitysystem.component.Component)3 Test (org.junit.jupiter.api.Test)2 EntityData (org.terasology.protobuf.EntityData)2 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)1 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1 OnAddedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent)1 ComponentMetadata (org.terasology.engine.entitySystem.metadata.ComponentMetadata)1 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)1