Search in sources :

Example 1 with EntityScope

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

the class EntitySerializer method serializeEntityFull.

private EntityData.Entity serializeEntityFull(EntityRef entityRef, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    if (!ignoringEntityId) {
        entity.setId(entityRef.getId());
    }
    entity.setAlwaysRelevant(entityRef.isAlwaysRelevant());
    EntityRef owner = entityRef.getOwner();
    if (owner.exists()) {
        entity.setOwner(owner.getId());
    }
    EntityScope scope = entityRef.getScope();
    if (scope != null) {
        switch(scope) {
            case GLOBAL:
                entity.setScope(GLOBAL);
                break;
            case SECTOR:
                entity.setScope(SECTOR);
                break;
            case CHUNK:
                entity.setScope(CHUNK);
                break;
        }
    }
    for (Component component : entityRef.iterateComponents()) {
        if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
            continue;
        }
        EntityData.Component componentData = componentSerializer.serialize(component, fieldCheck);
        if (componentData != null) {
            entity.addComponent(componentData);
        }
    }
    return entity.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope)

Example 2 with EntityScope

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

the class EntitySerializer method serializeEntityDelta.

private EntityData.Entity serializeEntityDelta(EntityRef entityRef, Prefab prefab, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    if (!ignoringEntityId) {
        entity.setId(entityRef.getId());
    }
    entity.setParentPrefab(prefab.getName());
    if (entityRef.isAlwaysRelevant() != prefab.isAlwaysRelevant()) {
        entity.setAlwaysRelevant(entityRef.isAlwaysRelevant());
    }
    EntityRef owner = entityRef.getOwner();
    if (owner.exists()) {
        entity.setOwner(owner.getId());
    }
    EntityScope scope = entityRef.getScope();
    if (scope != null) {
        switch(scope) {
            case GLOBAL:
                entity.setScope(GLOBAL);
                break;
            case SECTOR:
                entity.setScope(SECTOR);
                break;
            case CHUNK:
                entity.setScope(CHUNK);
                break;
        }
    }
    Set<Class<? extends Component>> presentClasses = Sets.newHashSet();
    for (Component component : entityRef.iterateComponents()) {
        if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
            continue;
        }
        presentClasses.add(component.getClass());
        Component prefabComponent = prefab.getComponent(component.getClass());
        EntityData.Component componentData;
        if (prefabComponent == null) {
            componentData = componentSerializer.serialize(component, fieldCheck);
        } else {
            componentData = componentSerializer.serialize(prefabComponent, component, fieldCheck);
        }
        if (componentData != null) {
            entity.addComponent(componentData);
        }
    }
    for (Component prefabComponent : prefab.iterateComponents()) {
        ComponentMetadata<?> metadata = componentLibrary.getMetadata(prefabComponent.getClass());
        if (!presentClasses.contains(prefabComponent.getClass()) && componentSerializeCheck.serialize(metadata)) {
            // TODO: Use component ids here
            entity.addRemovedComponent(metadata.getUri().toString());
        }
    }
    return entity.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope)

Example 3 with EntityScope

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

the class BaseEntityRefTest method testSetScope.

@Test
public void testSetScope() {
    ref = entityManager.create();
    assertEquals(CHUNK, ref.getScope());
    for (EntityScope scope : EntityScope.values()) {
        ref.setScope(scope);
        assertEquals(ref.getScope(), scope);
    }
    // Move into sector scope
    ref.setScope(SECTOR);
    assertEquals(ref.getScope(), SECTOR);
    assertTrue(entityManager.getSectorManager().contains(ref.getId()));
    assertFalse(entityManager.getGlobalPool().contains(ref.getId()));
    // And move back to global scope
    ref.setScope(GLOBAL);
    assertEquals(ref.getScope(), GLOBAL);
    assertTrue(entityManager.getGlobalPool().contains(ref.getId()));
    assertFalse(entityManager.getSectorManager().contains(ref.getId()));
}
Also used : EntityScope(org.terasology.entitySystem.entity.internal.EntityScope) Test(org.junit.Test)

Example 4 with EntityScope

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

the class EntitySerializerTest method testAlwaysRelevantPersisted.

@Test
public void testAlwaysRelevantPersisted() throws Exception {
    EntityRef entity = entityManager.create(prefab);
    boolean defaultSetting = entity.isAlwaysRelevant();
    EntityScope newScope = defaultSetting ? CHUNK : GLOBAL;
    entity.setScope(newScope);
    EntityData.Entity entityData = entitySerializer.serialize(entity);
    long nextId = entityManager.getNextId();
    entityManager.clear();
    entityManager.setNextId(nextId);
    EntityRef newEntity = entitySerializer.deserialize(entityData);
    assertEquals(newScope, newEntity.getScope());
    assertEquals(!defaultSetting, newEntity.isAlwaysRelevant());
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope) Test(org.junit.Test)

Example 5 with EntityScope

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

the class EntitySerializerTest method testScopePersisted.

@Test
public void testScopePersisted() {
    EntityRef entity = entityManager.create(prefab);
    for (EntityScope scope : EntityScope.values()) {
        entity.setScope(scope);
        entity = serializeDeserializeEntity(entity);
        assertEquals(scope, entity.getScope());
    }
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope) Test(org.junit.Test)

Aggregations

EntityScope (org.terasology.entitySystem.entity.internal.EntityScope)5 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 Test (org.junit.Test)3 EntityData (org.terasology.protobuf.EntityData)3 Component (org.terasology.entitySystem.Component)2 EntityInfoComponent (org.terasology.entitySystem.entity.internal.EntityInfoComponent)2